Displaying image with OpenGL and DevIL in C?
how to display image with OpenGL and DevIL? Or is this not possible, and I must use libs like libPNG instead?
This code doesn't do anything. Not surprisingly..
and then I tried using textures...
This code manages to turn every shape's colour in the screen to the most predominant colour in the image. I was trying to do it with textures.
Code:
ilutGLLoadImage("/Users/eric/GLExperiment/Images/fighter.png");
glDrawPixels(64,64,GL_RGB,GL_UNSIGNED_BYTE,ilGetData);This code doesn't do anything. Not surprisingly..
and then I tried using textures...
Code:
ilutGLLoadImage("/Users/eric/GLExperiment/Images/fighter.png");
ilGenImages (1, &ImgId);
ilBindImage (ImgId);
glEnable (GL_TEXTURE_2D);
glBindTexture (GL_TEXTURE_2D, TexID);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, Width, Height, 0, GL_RGB, GL_UNSIGNED_BYTE, ilGetData);
glBegin (GL_QUADS);
glTexCoord2f (X1, Y1);// glVertex3i (0, 0, 0);
glVertex2f(X1, Y1);
glTexCoord2f (X1+Width,Y1);// glVertex3i (Width, 0, 0);
glVertex2f(X1+Width, Y1);
glTexCoord2f (X1+Width,Y1+Height);// glVertex3i (Width, Height, 0);
glVertex2f(X1+Width, Y1+Height);
glTexCoord2f (X1, Y1+Height);// glVertex3i (0, Height, 0);
glVertex2f(X1,Y1+Height);
glEnd();This code manages to turn every shape's colour in the screen to the most predominant colour in the image. I was trying to do it with textures.
The texture coordinates should go from 0 to 1. So where you have glTexCoord2f(X1, Y1), you should have glTexCoord2f(0, 0). Also, in place of glVertex2f(X1 + Width, Y1 + Height), you should have glVertex2f(1, 1). I'm sure you can figure out the rest.
and I'm sure ilGetData is a function, not a global variable!
akb825 Wrote:The texture coordinates should go from 0 to 1. So where you have glTexCoord2f(X1, Y1), you should have glTexCoord2f(0, 0). Also, in place of glVertex2f(X1 + Width, Y1 + Height), you should have glVertex2f(1, 1). I'm sure you can figure out the rest.
I did that thing OneSadCookie told me in his tutorial. -> make the coordinate system measure interms of pixels instead of the default system. I still use the coordinates you told me
OneSadCookie Wrote:and I'm sure ilGetData is a function, not a global variable!
Its a function, I can use it like this, right? It gets the memory address of the image I loaded, and returns it as a pointer.
Function calls in C always have parentheses.
OneSadCookie Wrote:Function calls in C always have parentheses.
>.> Why didn't it give me an error?
In C, you can pass any kind of pointer as a void*. You were passing a function pointer.
I put the parenthesis in, now all the rectangles and lines on my app flash green blue and red, make my head dizzy.. >< But I still don't see any image! Note that I am able to get data about the image, eg. its width and height, so I am sure I have it loaded into the app.
Code:
'see other posts. deleted for space.
You still haven't changed the texture coordinates. Having the coordinate system match up pixel by pixel works with the vertex coordinates, but not the texture coordinates. Like I said earlier, they go from 0 to 1: always. (unless you are using GL_TEXTURE_RECTANGLE_EXT/ARB instead of GL_TEXTURE_2D, but that's the only exception) Also, you can't just say "TexID = 2". You have to call glGenTextures first.
akb825 Wrote:Also, you can't just say "TexID = 2". You have to call glGenTextures first.
Unfortunately, you're wrong about that. You *can* choose your own texture IDs, and OpenGL just has to deal. This is something that they will fix for OpenGL LM / OpenGL 3.0. You're right, though, that you *should* call GenTextures.
leRiCi, isn't there a DevIL function that does the TexImage2D for you?
There is, but the example containing the command won't compile. Anyways... I realised I posted the wrong version of the code I had. Here, I called GenTextures and didn't declare my TexID and changed the texture coordinates and used the GL command for DevIL. Now it doesn't do anything at all. ><
Code:
// GL texture ID
GLuint TexID;
// IL image ID
ILuint ImgId;
ILboolean success;
ilGenImages (1, &ImgId);
ilBindImage (ImgId);
success = ilLoadImage("/Users/eric/GLExperiment/Images/fighter.png");
if (success)
{
success= ilConvertImage(IL_RGB,IL_UNSIGNED_BYTE);
if (!success)
{
}
glGenTextures(1, &TexID);
glBindTexture(GL_TEXTURE_2D, TexID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); /* We will use linear
interpolation for magnification filter */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); /* We will use linear
interpolation for minifying filter */
glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH),
ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE,
ilGetData()); /* Texture specification */
}
else
{
}
int Width = ilGetInteger (IL_IMAGE_WIDTH);
int Height = ilGetInteger (IL_IMAGE_HEIGHT);
glBegin(GL_QUADS);
glTexCoord2i(0, 0); glVertex2i(X1, Y1);
glTexCoord2i(0, 1); glVertex2i(X1, Y1+Height);
glTexCoord2i(1, 1); glVertex2i(X1+Width, Y1+Height);
glTexCoord2i(1, 0); glVertex2i(X1+Width, Y1);
glEnd();
What's the value of X1 and Y1? Are you trying to give the value of the window's x and y? If so, don't. Assuming you correctly set up the orthographic matrix (for example, with glOrtho(0, width, 0, height, 1, -1)) and viewport (for example, glViewport(0, 0, width, height)), the viewable coordinates should be 0-width for the x and 0-height for the y.
I want to put my head in the toilet and flush it away.... I called this function before glClear.... *sigh* It probably had always been working, and I was just wasting my time. Sorry guys. but thanks.
leRiCl Wrote:I want to put my head in the toilet and flush it away.... I called this function before glClear.... *sigh* It probably had always been working, and I was just wasting my time. Sorry guys. but thanks.
No sweat. I did the same thing yesterday. I was pulling my hair out trying to figure out why my screen was blank.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Another beginner's question about displaying an animated object | superlemonster | 2 | 4,139 |
Jan 28, 2011 08:03 AM Last Post: OptimisticMonkey |
|
| OpenGL Image Textures | mikey | 52 | 20,244 |
Jun 30, 2009 10:42 AM Last Post: AnotherJake |
|
| Simple OpenGL 2d test - nothing displaying | SaxMan | 8 | 5,469 |
Jan 19, 2009 12:04 AM Last Post: TythosEternal |
|
| How do I build DevIL? | GolfHacker | 4 | 4,231 |
Feb 25, 2007 08:00 AM Last Post: GolfHacker |
|
| drawing or displaying vertex normal | shru_ani | 3 | 3,026 |
Oct 29, 2006 06:04 AM Last Post: shru_ani |
|

