Sign in to follow this  
Mohanddo

Can't render 2 textures

Recommended Posts

Mohanddo    109
I have been trying to render two different objects with 2 different textures. I'm using GLSL and VBOs. Everything works fine except the first texture is not displaying for some reason. I am totally stumped, ive looked at my code many times and I cant figure it out so i would appreciate any help.

I load my textures from an SDL surface like this:
[code]

glGenTextures(1, &m_textureID);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_textureID);

SDL_Surface* tmpsurface;

tmpsurface = IMG_Load("test.jpg");

GLenum bpp = GL_RGB;

if(tmpsurface->format->BytesPerPixel == 4)
{
bpp = GL_RGBA;
}

glTexImage2D(GL_TEXTURE_2D, 0, bpp, tmpsurface->w, tmpsurface->h, 0, bpp, GL_UNSIGNED_BYTE, tmpsurface->pixels);

glGenTextures(1, &m_textureID2);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_textureID2);

SDL_Surface* tmpsurface2;

tmpsurface2 = IMG_Load("okay.jpg");

bpp = GL_RGB;

if(tmpsurface2->format->BytesPerPixel == 4)
{
bpp = GL_RGBA;
}

glTexImage2D(GL_TEXTURE_2D, 0, bpp, tmpsurface2->w, tmpsurface2->h, 0, bpp, GL_UNSIGNED_BYTE, tmpsurface2->pixels);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

SDL_FreeSurface(tmpsurface);
SDL_FreeSurface(tmpsurface2);
[/code]


Then I render the scene like this:

[code]

float modelviewMatrix[16];
float projectionMatrix[16];

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);

glPushMatrix();
glLoadIdentity();

glBindBuffer(GL_ARRAY_BUFFER, m_bufferID);

glBindTexture(GL_TEXTURE_2D, m_textureID);

glVertexAttribPointer((GLuint)2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(28));
glVertexAttribPointer((GLuint)1, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(12));
glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(0));

glGetFloatv(GL_MODELVIEW_MATRIX, modelviewMatrix);
glGetFloatv(GL_PROJECTION_MATRIX, projectionMatrix);

m_GLSLProgram->SendUniform("modelview_matrix", modelviewMatrix);
m_GLSLProgram->SendUniform("projection_matrix", projectionMatrix);
m_GLSLProgram->SendUniform("texture0", 0);

glDrawArrays(GL_TRIANGLE_FAN, 0, 6);

glBindBuffer(GL_ARRAY_BUFFER, m_bufferID2);

glBindTexture(GL_TEXTURE_2D, m_textureID2);

glVertexAttribPointer((GLuint)2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(28));
glVertexAttribPointer((GLuint)1, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(12));
glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(0));

glDrawArrays(GL_TRIANGLE_STRIP, 0, 5);

glPopMatrix();
[/code]


What i get as a result is the first draw call being rendered with a 'plain black' texture. And the second call being rendered correctly.

The weird thing is that if I switch the loading order of the 2 textures, then the first draw call is textured and the second with a black texture.

Does anyone have any idea of why this might be happening? Thanks

Share this post


Link to post
Share on other sites
Mohanddo    109
[quote name='EvilTesla-RG' timestamp='1310241596' post='4833149']
I havn't done alot with VBO's (working on fixing that), but shouldn't you be sending the uniforms before drawing your first texture?
[/quote]

Thanks for your reply.
Correct me if im wrong on this but i am not rendering anything before sending my uniforms, just making opengl ready for the render phase( Just binding vertex attrib to my buffer data - vertex co-ordinates, color, and uv co-ordinates and binding the texture ID) I believe the rendering starts from the DrawArray call. Just to make sure i tried sending uniforms before anything else and i still had the same result.

Share this post


Link to post
Share on other sites
Mohanddo    109
I have fixed the problem now. The texture parameters are per texture and should be specified after binding every texture when loading them. For some reason i thought they followed the state system like how you would use glEnable().

Share this post


Link to post
Share on other sites

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