Sign in to follow this  

[OpenGL][DevIL] Problem loading multiple textures.

This topic is 2661 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm using C# with DevIL, and thus OpenGL, to try and load 2 textures and display them on screen. (I have been reading and copying the code from the book "C# Game Programming for Serious Game Creation.")

Unfortunately, when I try to display the images, both textures are the same. They are both the last image to be loaded. I have checked everywhere online, including this forum, and have found some problems that sound similar, if not the same, as mine, but their solutions fail to fix my problem.

The loading code (copied and pasted directly from the book):

public void LoadTexture(string textureId, string path)
{
int devilId = 0;
Il.ilGenImages(1, out devilId);
Il.ilBindImage(devilId); // set as the active texture.
if (!Il.ilLoadImage(path))
{
System.Diagnostics.Debug.Assert(false,
"Could not open file, [" + path + "].");
}
// The files we'll be using need to be flipped before passing to OpenGL
Ilu.iluFlipImage();
int width = Il.ilGetInteger(Il.IL_IMAGE_WIDTH);
int height = Il.ilGetInteger(Il.IL_IMAGE_HEIGHT);
int openGLId = Ilut.ilutGLBindTexImage();
System.Diagnostics.Debug.Assert(openGLId != 0);
Il.ilDeleteImages(1, ref devilId);
_textureDatabase.Add(textureId, new Texture(openGLId, width,
height));
}





The rendering code is:

public void drawSprite(Sprite sprite)
{
Gl.glBegin(Gl.GL_TRIANGLES);
{
for (int i = 0; i < Sprite.VertexAmount; i++)
{
Gl.glBindTexture(Gl.GL_TEXTURE_2D, sprite.texture.ID);
DrawImmediateModeVertex(
sprite.vertexPositions[i],
sprite.vertexColors[i],
sprite.vertexUVs[i]);
}
Gl.glEnd();
}
}





Some notes:
- The "sprite" class holds a variable "ID", which is used by glBindTexture to choose the texture specific to the sprite. I have investigated and found that sprite #1 has an ID of "1" and sprite #2 has "2", so as far as I know, that isn't the problem.

- Rendering only 1 of the images does not fix the problem. If the second image is loaded, the first image seems to be replaced.

- As commenting out the second image load function call seems to allow the first image to be rendered, and the load function itself relies on no calls to functions I have written (only to the DevIL libraries and a Dictionary<String, Texture> object), I assume the problem lies within the loadTexture function.

- When I call the drawSprite function for both sprites, 2 images are displayed on screen. The size, shape and color of each picture is what they have been set to (One is squashed, the other is red), but the texture of each is the same. The ID for the textures being called are correct (to my knowledge): 1 and 2.

My question, if it is not obvious, is why is my program failing to display BOTH images, rather than just the second image? Thanks in advance.

[Edited by - ApochPiQ on September 1, 2010 4:46:19 PM]

Share this post


Link to post
Share on other sites
It is illegal to call any OpenGL call other than glVertex/glNormal/glTexCoord/glColour/glVertexArttrib/glFogCoord between a begin and end block.....


public void drawSprite(Sprite sprite)
{
{
for (int i = 0; i < Sprite.VertexAmount; i++)
{
Gl.glBindTexture(Gl.GL_TEXTURE_2D, sprite.texture.ID);
Gl.glBegin(Gl.GL_TRIANGLES);
DrawImmediateModeVertex(
sprite.vertexPositions[i],
sprite.vertexColors[i],
sprite.vertexUVs[i]);
Gl.glEnd();
}
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by RobTheBloke
It is illegal to call any OpenGL call other than glVertex/glNormal/glTexCoord/glColour/glVertexArttrib/glFogCoord between a begin and end block.....


Oops, I should have included this:

The rendering class uses a function called DrawImmediateModeVertex which looks like this:

public void DrawImmediateModeVertex(Sprite.Vector position, Sprite.Color color,
Sprite.Point uvs)
{
Gl.glColor4f(color.Red, color.Green, color.Blue, color.Alpha);
Gl.glTexCoord2f(uvs.X, uvs.Y);
Gl.glVertex3d(position.X, position.Y, position.Z);
}



Sorry for missing that out before. As you can see, it does accord to your statement. Thanks for the advice though.

Share this post


Link to post
Share on other sites
Quote:
Original post by RobTheBloke
Quote:
Original post by grimfate
Sorry for missing that out before. As you can see, it does accord to your statement. Thanks for the advice though.


I was referring to the glBindTexture call.....


Oh, right. Thanks. I really should pay more attention and read more carefully.

AWESOME it works =) thanks, RobTheBloke, your a godsend!

I can't believe the book got this wrong. Very disappointing.

Share this post


Link to post
Share on other sites

This topic is 2661 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this