glTexCoordPointer with two or more texutures

Started by
4 comments, last by cloudending 9 years, 6 months ago

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);

Advertisement

What is the reason for three textures and not an atlas?

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.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

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.

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.

This topic is closed to new replies.

Advertisement