PDA

View Full Version : Load and display an Image on a specific place

Stanbr
12-28-2009, 03:15 AM
Can anyone provides me an example on how to load an image/texture and display it on the screen with its original image size (no scaling)on a specific position of the screen?

I developed a 2D game some time ago using C++/SDL now I want to port it to OpenGL. What I need is a function like: drawimage(texture, positionX, positionY).

I'm an openGL beginner and I looked at many examples but I was not able to create such a simple function (yet).

ColacX
12-28-2009, 04:50 AM
hi there im pretty fresh at opengl aswell but look at
http://nehe.gamedev.net/
for some nice executable tutorials

the code you are looking for is going to look something like this

glMatrixMode(GL_PROJECTION);
glOrtho(0,screenW,0,screenH,-1,1);
glMatrixMode(GL_MODELVIEW);
glTranslatef(px, py, 0);
glBindTexture(GL_TEXTURE_2D, texture1);
glTexCoord2f(offsetX, offsetY); glVertex2f(0.0f,+0.0f);
glTexCoord2f(offsetX, offsetY + -1.0f/16); glVertex2f(0.0f,-16.0f);
glTexCoord2f(offsetX+1.0f/16, offsetY + -1.0f/16); glVertex2f(16.0f,-16.0f);
glTexCoord2f(offsetX+1.0f/16, offsetY); glVertex2f(16.0f,+0.0f);
glEnd();

this is some code taken from my "game-console" the thought is that by making the projection completly flat and reseting the modelview so that z doesn't matter and by knowing how big the picture is you can display the image without scaling it. in my case every character was 16x16 pixels big.
the code above is just something close, you'll have to experiment on your own to make it perfect.

Stanbr
12-28-2009, 08:17 AM
Thanks a lot. I'll play with your example :)
I must read more about opengl, some things are not clear to me yet, but looks very very nice (and fast). A little complicated in the beginning but very powerful.

thanks!

MaxH
12-28-2009, 10:47 AM
Last August I posted an OpenGL/GLUT/Windows demo that loads image files and displays them as textures AND as an image (with no scaling). The code is self-contained meaning that you don't have to link with (and learn) an image loading libary. There is also a Linux version of the demo provided by Marshats at the same URL which uses an image handling library called DevIL (looks pretty useful). The URL is -