简体   繁体   中英

How to draw a 2D Image using OpenGL

I've started learning OpenGL a few days ago, and I'm mainly focusing OpenGL on 2D game development...

I've learned the basics of OpenGL - Creating a window, handling keyboard input.

What I did not find in tutorials (and did not found a clear solution in the net), is how to draw 2D image (such as a "player" image in a casual 2D game) using OpenGL.

I've learned XNA before, and remembered there was a structure called 2DTexture, though I did not find any in OpenGL..

I'm not looking toward 3D currently in OpenGL...

Edit:

if it makes it anyway easier, I can have the image's RGB in an array (array sized [WIDTH][HEIGHT], every cell contains the Pixel's R, G, B

First of all, OpenGL is a much lower level API than XNA and it is not primarily focused on 2D rendering.

In older versions of OpenGL, the function glDrawPixels() could serve the purpose of old school bit blitting on the screen, but is was ridiculously inefficient.

The efficient way of rendering 2D images, like sprites for a 2D game, on modern OpenGL is by mean of drawing a flat quadrilateral with an orthographic projection camera and applying a texture on top of it.

I can't remember any tutorial right now that covers 2D rendering specifically, but I think it would be best for you to first get a good grasp on texture mapping and the basics of polygon rendering. For this, ogldev is a good starting point; opengl-tutorial.org is also a great source.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM