Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Programming Talk / C++ / C# - Load / Draw Image With OpenGL

Author
Message
The Lone Programmer
22
Years of Service
User Offline
Joined: 29th Jan 2003
Location: California, USA
Posted: 12th Sep 2007 18:55
When I googled looking for this answer I got everything, but the answer.

I was wondering if someone here knew the command(s) necessary in getting images drawn to the screen.

Please respond if you know.

Thanks,
The Lone Programmer

"Is The Juice Worth The Squeeze"
-The Girl Next Door
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 12th Sep 2007 20:06
I suspect that you meant the title to be 'Load and draw image with OpenGL'. Well, the bad news is that OpenGL contains no functions for loading images - you have to do it yourself.

As for displaying them, google for 'draw image with opengl' and quite a few sites pop up. Take a look at [url=nehe.gamedev.net]NeHe[/url] in particular - the God of OpenGL tutorial sites.

Utility plugins collection and
http://www.matrix1.demon.co.uk for older plug-ins and example code
ionstream
20
Years of Service
User Offline
Joined: 4th Jul 2004
Location: Overweb
Posted: 13th Sep 2007 01:13
For drawing the image you can either use DrawPixels, which takes a buffer of pixels and plunks them verbatim on the screen, or load the image into a texture and then draw it onto a plane. The texture option is much more flexible geometrically, because you can scale/rotate the plane around as much as you want, or set the vertex colors to affect the overall color. The downside is that it takes texture memory, which you only have so much of. But depending on what you want to do you should check out your options.


For loading, it really depends. You can use Windows GDI I believe, or an external image library like FreeImage or DevIL, or if its a simple image format (or you're a math freak) you can write it yourself. As long as your method allows you to obtain a buffer of the uncompressed pixel data of the image, and the image follows a standard color protocol (RGB, RGBA), it'll work with OpenGL.

That's not as bad as you think you said.
Kentaree
22
Years of Service
User Offline
Joined: 5th Oct 2002
Location: Clonmel, Ireland
Posted: 13th Sep 2007 01:38
I'd advise you use another library such as SDL. It can load images, and it works together quite well with OpenGL.

Chalkey
17
Years of Service
User Offline
Joined: 25th May 2007
Location:
Posted: 21st Sep 2007 15:10
Somebody asked a similar question a while back... Check my response
http://forum.thegamecreators.com/?m=forum_view&t=107733&b=20 it shows how to load a TGA file, it will be a starting point for you. Now you just need to google how to apply a texture to a poly. I'd try and dig up the code but I cant remember where ive stuck that little project. Sorry.

Chalk
The Lone Programmer
22
Years of Service
User Offline
Joined: 29th Jan 2003
Location: California, USA
Posted: 10th Oct 2007 03:36
I just made this code, but I don't know if it is sufficient in getting the job done in OpenGL. Here is my LoadImage command for C++:


If that is in fact loading correctly, how do I texture it to a 2d box?

I googled all over the place and can't seem to be able to figure out how to get this to work.

The Lone Programmer

"Is The Juice Worth The Squeeze"
-The Girl Next Door
ionstream
20
Years of Service
User Offline
Joined: 4th Jul 2004
Location: Overweb
Posted: 10th Oct 2007 05:55
Unless the file is raw image data, that function isn't going to work, as it's just putting the contents of the file into a buffer.

For a tutorial on texturing in OpenGL, check this out:

http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=06

The key functions here are glGenTextures(), which allocates a texture number for later use, glBindTexture(), which specifies the current texture for use, and glTexImage2D(), which actually turns the pixel data into a texture on the video card memory.

That's not as bad as you think you said.
The Lone Programmer
22
Years of Service
User Offline
Joined: 29th Jan 2003
Location: California, USA
Posted: 10th Oct 2007 18:48
I tried to get it working, but I am still having troubles.

When I load my image I store it in as texture data like the tutorial says. When I go to texture it onto a box, the colors are all out of proportion.

The picture almost looks tiled and inverse negative in color.

Any reason why this would happen. I even tried it on the image provided in the tutorial and still the same thing.

The Lone Programmer

"Is The Juice Worth The Squeeze"
-The Girl Next Door

Login to post a reply

Server time is: 2025-05-21 05:10:11
Your offset time is: 2025-05-21 05:10:11