PDA

View Full Version : Textures Missing



kotoko
05-13-2009, 03:40 PM
Hi!

I'm trying to add texture to a project I'm working on. But somehow no textures appear.
The project is really big so I'm just gonna post the parts I think are important to the textures. Please ask if you need more.



int main(int argc, char* argv[]){
glutDisplayFunc(HandleDisplayCallback);
world = new World();
glutMainLoop();
delete world;
return 0;
}

World::World()
{
cars[0] = new Car("model.3ds");
terrain = new Terrain("Terrain.bmp");
}

void World::Draw()
{
HandleKeyboard();
terrain->Draw();
cars[0]->Draw();
}

Terrain::Terrain(const char* terrainFileName_)
{
sprintf(terrainFileName, "%s", terrainFileName_);
LoadTextures();
InitializeTerrain();
}


void Terrain::LoadTextures()
{
glGenTextures(1, &t_relva);
perror("glGenTextures");
glBindTexture(GL_TEXTURE_2D, t_relva);
perror("glBindTexture");
sprintf(tPath, "%sgreen.bmp", TEX_PATH);
imag.LoadBmpFile(tPath);
//imag.LoadBmpFile(TEX_PATH+"green.bmp");
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
perror("glTexEnvf");
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
perror("glTexParameteri");
glTexImage2D(GL_TEXTURE_2D, 0, 3,
imag.GetNumCols(),
imag.GetNumRows(), 0, GL_RGB, GL_UNSIGNED_BYTE,
imag.ImageData());
perror("glTexImage2D");
}

void Terrain::Draw(){
// set the current texture to the land texture
glBindTexture(GL_TEXTURE_2D, t_relva);
//printf("Draw\n");
// we are going to loop through all of our terrain's data points,
// but we only want to draw one triangle strip for each set along the x-axis.
for (int z = 0; z < MAP_Z-1; z++)
{
glBegin(GL_TRIANGLE_STRIP);
for (int x = 0; x < MAP_X-1; x++)
{
// for each vertex, we calculate the grayscale shade color,
// we set the texture coordinate, and we draw the vertex.
/*
the vertices are drawn in this order:

0 ---> 1
/
/
|/
2 ---> 3
*/

///printf("triangle strip: %d, %d, %d\n", terrain[x][z][0], terrain[x][z][1], terrain[x][z][2]);
// draw vertex 0
glColor3f(terrain[x][z][1]/255.0f, terrain[x][z][1]/255.0f, terrain[x][z][1]/255.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(terrain[x][z][0], terrain[x][z][1], terrain[x][z][2]);

// draw vertex 1
glTexCoord2f(1.0f, 0.0f);
glColor3f(terrain[x+1][z][1]/255.0f, terrain[x+1][z][1]/255.0f, terrain[x+1][z][1]/255.0f);
glVertex3f(terrain[x+1][z][0], terrain[x+1][z][1], terrain[x+1][z][2]);

// draw vertex 2
glTexCoord2f(0.0f, 1.0f);
glColor3f(terrain[x][z+1][1]/255.0f, terrain[x][z+1][1]/255.0f, terrain[x][z+1][1]/255.0f);
glVertex3f(terrain[x][z+1][0], terrain[x][z+1][1], terrain[x][z+1][2]);

// draw vertex 3
glColor3f(terrain[x+1][z+1][1]/255.0f, terrain[x+1][z+1][1]/255.0f, terrain[x+1][z+1][1]/255.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(terrain[x+1][z+1][0], terrain[x+1][z+1][1], terrain[x+1][z+1][2]);
}
glEnd();
}
// enable blending
glEnable(GL_BLEND);

}


I want to leave here a disclaimer, I did not do everything by myself but I understand all that is here.

ZbuffeR
05-13-2009, 03:47 PM
The glEnable(GL_TEXTURE_2D); is missing from this snippet.

kotoko
05-13-2009, 03:59 PM
Thank you, thank you, thank you, thank you, thank you, thank you, thank you . . .

I put glEnable(GL_TEXTURE_2D); in the LoadTextures method and in the Terrain::Draw method. So now it's like




void Terrain::LoadTextures()
{
errno = 0;
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &amp;t_relva);
perror("glGenTextures");
glBindTexture(GL_TEXTURE_2D, t_relva);
perror("glBindTexture");
sprintf(tPath, "%sgreen.bmp", TEX_PATH);
imag.LoadBmpFile(tPath);
//imag.LoadBmpFile(TEX_PATH+"green.bmp");
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
perror("glTexEnvf");
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
perror("glTexParameteri");
glTexImage2D(GL_TEXTURE_2D, 0, 3,
imag.GetNumCols(),
imag.GetNumRows(), 0, GL_RGB, GL_UNSIGNED_BYTE,
imag.ImageData());
perror("glTexImage2D");
glDisable(GL_TEXTURE_2D);
}

void Terrain::Draw(){
glEnable(GL_TEXTURE_2D);
// set the current texture to the land texture
glBindTexture(GL_TEXTURE_2D, t_relva);
//printf("Draw\n");
// we are going to loop through all of our terrain's data points,
// but we only want to draw one triangle strip for each set along the x-axis.
for (int z = 0; z < MAP_Z-1; z++)
{
glBegin(GL_TRIANGLE_STRIP);
for (int x = 0; x < MAP_X-1; x++)
{
// for each vertex, we calculate the grayscale shade color,
// we set the texture coordinate, and we draw the vertex.
/*
the vertices are drawn in this order:

0 ---> 1
/
/
|/
2 ---> 3
*/

///printf("triangle strip: %d, %d, %d\n", terrain[x][z][0], terrain[x][z][1], terrain[x][z][2]);
// draw vertex 0
glColor3f(terrain[x][z][1]/255.0f, terrain[x][z][1]/255.0f, terrain[x][z][1]/255.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(terrain[x][z][0], terrain[x][z][1], terrain[x][z][2]);

// draw vertex 1
glTexCoord2f(1.0f, 0.0f);
glColor3f(terrain[x+1][z][1]/255.0f, terrain[x+1][z][1]/255.0f, terrain[x+1][z][1]/255.0f);
glVertex3f(terrain[x+1][z][0], terrain[x+1][z][1], terrain[x+1][z][2]);

// draw vertex 2
glTexCoord2f(0.0f, 1.0f);
glColor3f(terrain[x][z+1][1]/255.0f, terrain[x][z+1][1]/255.0f, terrain[x][z+1][1]/255.0f);
glVertex3f(terrain[x][z+1][0], terrain[x][z+1][1], terrain[x][z+1][2]);

// draw vertex 3
glColor3f(terrain[x+1][z+1][1]/255.0f, terrain[x+1][z+1][1]/255.0f, terrain[x+1][z+1][1]/255.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(terrain[x+1][z+1][0], terrain[x+1][z+1][1], terrain[x+1][z+1][2]);
}
glEnd();
}
glDisable(GL_TEXTURE_2D);
// enable blending
glEnable(GL_BLEND);

}


Is this correct?
It works but am I putting too many times or maybe in strange places?

nachovall
05-14-2009, 05:41 AM
Just enabling once is enough (Obviously if you don't call function glDisable with GL_TEXTURE_2D as param).

Remember OpenGL is a machine state, so if you change the state to enable GL_TEXTURE_2D the state won't change until you change it.

ZbuffeR
05-14-2009, 06:51 AM
And it is not needed in LoadTextures.
Only needed before actually drawing stuff.