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);
glLoadIdentity();
glOrtho(0,screenW,0,screenH,-1,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(px, py, 0);
glBindTexture(GL_TEXTURE_2D, texture1);
glBegin(GL_QUADS);
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 -

OpenGL Image Loading and Display Demo (http://www.mfwweb.com/OpenGL/Loading_Textures/)

The demo produces the scene below. Loading the image is the hard part. Displaying it requires two OpenGL calls: 1) glRasterPos to position the image, and 2) glDrawPixels to draw it. The demo loads 3 images - an earth map, a fractal image, and the OpenGL logo. The first 2 are displayed as textures. The OGL logo is displayed as an unscaled image.

http://www.mfwweb.com/OpenGL/Loading_Textures/MaxH_Textures.jpg