Jump to content
  • Advertisement
Sign in to follow this  
cloudending

OpenGL glTexCoordPointer with two or more texutures

This topic is 1419 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

I have three textures(three .jpg image).In general.I render a triangle with the below code

glBindTexture(GL_TEXTURE_2D, texName[texFileNameIdx]); //texFileNameIdx is the indice of different texture
glBegin(GL_TRIANGLES);
glTexCoord2f(...);
glVertex3f(...);
glTexCoord2f(...);
glVertex3f(...);
glTexCoord2f(...);
glVertex3f(...);
glEnd();

before rendering a triangle,I bind texture with glBindTexture first because different triangles maybe rendering with different texture.

but it will result in low efficiency.So I leanrn to use vertex array.Now I have question.how to render a model with vertex array if I have two or more

texture image.

I init the buffer first just as the Opengl programming guide book did.But dont know how to init the buffer correspond to texture and how to draw it.

	glGenBuffers(3, this->buffers);
	glBindBuffer(GL_ARRAY_BUFFER, this->buffers[VERTEXBUFFER]);

	glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*3*objInfo->points3d.size(), this->vertexs, GL_STATIC_DRAW);
	glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
	glEnableClientState(GL_VERTEX_ARRAY);
	
	glBindBuffer(GL_ARRAY_BUFFER, this->buffers[NORMALBUFFER]);
	glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*3*objInfo->points3d.size(), this->normals, GL_STATIC_DRAW);
	glNormalPointer(GL_FLOAT, 0, BUFFER_OFFSET(0));
	glEnableClientState(GL_NORMAL_ARRAY);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->buffers[INDICEBUFFER]);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint)*3*objInfo->faces.size(), this->indices, GL_STATIC_DRAW);

Edited by cloudending

Share this post


Link to post
Share on other sites
Advertisement

Are the texture coordinates the same for each?  If so, you don't need to do anything special - just make a glTexCoordPointer call, then glBindTexture the texture you wish to draw with, as before.

Share this post


Link to post
Share on other sites

Are the texture coordinates the same for each?  If so, you don't need to do anything special - just make a glTexCoordPointer call, then glBindTexture the texture you wish to draw with, as before.

thank you.I know how to do.but there is a problem. the texture have gap if i render with the below code:

glDrawElements(GL_TRIANGLES, 3*objInfo->faces.size(), GL_UNSIGNED_INT, BUFFER_OFFSET(0));

image-1A51_542A6262.jpg

and if i render with the following code 

unsigned int texFileNameIdx = objInfo->tex_id[faceIdx]; //find which texture to use
glBindTexture(GL_TEXTURE_2D, texName[texFileNameIdx]);
glBegin(GL_TRIANGLES);
glNormal3d(normalPerFace[faceIdx][0], normalPerFace[faceIdx][1], normalPerFace[faceIdx][2]);
for (int i = 0; i < 3; i++)
{
int pointIdx = objInfo->faces[faceIdx][i]; //find point
int texPointIdx = objInfo->tex_faces[faceIdx][i]; //find texture point (u,v)

glTexCoord2f(objInfo->tex_points[texPointIdx][0], objInfo->tex_points[texPointIdx][1]);
glVertex3f(objInfo->points3d[pointIdx][0], objInfo->points3d[pointIdx][1], objInfo->points3d[pointIdx][2]);
}
glEnd();

it work fine. the result is below:

image-B682_542A63BB.jpg

I have no idea why.

 
Edited by cloudending

Share this post


Link to post
Share on other sites

I know why.because in the gap. point's texture coordinate may has more than one texture coordinate.I make a mistake that one point has only one texture coordinate. 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!