使用VBO / IBO(IPhone)在OpenGL ES 2.0中渲染纹理

时间:2011-12-26 02:19:21

标签: iphone textures opengl-es-2.0

我正在尝试使用顶点缓冲区对象和索引缓冲区对象渲染带纹理的简单四边形。我还没有看到任何有关如何使用glDrawElements进行IBO的解决方案。在任何情况下,所有出现的情况都是仅使用纹理图像的左上角像素来纹理四边形。

这是初始化将要呈现的实体的方式:

void Entity::init(){

// create triangle verticies
numVerticies = 20;
verticies = new GLfloat[numVerticies];

// 3 verts, 2 tex coords
verticies[0] = -1.0f;  verticies[1] = -1.0f;  verticies[2] = 0.0f;  verticies[3] = 0.0f;  verticies[4] = 0.0f;
verticies[5] = 1.0f;   verticies[6] = 1.0f;   verticies[7] = 0.0f;  verticies[8] = 1.0f;   verticies[9] = 1.0f;
verticies[10] = -1.0f; verticies[11] = 1.0f;  verticies[12] = 0.0f; verticies[13] = 0.0f; verticies[14] = 1.0f;
verticies[15] = 1.0f;  verticies[16] = -1.0f; verticies[17] = 0.0f; verticies[18] = 1.0f;  verticies[19] = 0.0f;

// create face indicies
numIndicies = 6;
indicies = new GLushort[numIndicies];
indicies[0] = 0; indicies[1] = 1; indicies[2] = 2;
indicies[3] = 0; indicies[4] = 3; indicies[5] = 1;

// generate buffers for geometry
glGenBuffers(2, geometryBuffer);

// buffer the data to the vertex buffer
glBindBuffer(GL_ARRAY_BUFFER, geometryBuffer[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(verticies) * numVerticies, verticies, GL_STATIC_DRAW);

// buffer the data to the index buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, geometryBuffer[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indicies) * numIndicies, indicies, GL_STATIC_DRAW);

rotation = 0.0f;

// load a texure object
std::string path = [[[NSBundle mainBundle] bundlePath] UTF8String];
path += "/test.png";
NSString *fileName = [NSString stringWithUTF8String:path.c_str()];

NSError *error;
GLKTextureInfo *texture = [GLKTextureLoader textureWithContentsOfFile:fileName options:nil error:&error];
if(error || texture == nil){
    NSLog(@"Error loading texture %@\n", error);
}

// get the texture id
tex = [texture name];
}

这就是我渲染对象的方式(我应该提一下,我在调用这个方法之前调用glUseProgram):

void Entity::render(GLint uniformPos, GLint uniformTex, GLKMatrix4 projection){

GLKMatrix4 modelViewProjection = GLKMatrix4Multiply(projection, modelView);

// bind and enable enable verticies
glBindBuffer(GL_ARRAY_BUFFER, geometryBuffer[0]);  
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), BUFFER_OFFSET(0));  

// enable tex coords
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), BUFFER_OFFSET(12));

// bind the index buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, geometryBuffer[1]);

// bind the texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);

// render with uniforms
glUniform1i(uniformTex, 0);
glUniformMatrix4fv(uniformPos, 1, 0, modelViewProjection.m);
glDrawElements(GL_TRIANGLES, numIndicies, GL_UNSIGNED_SHORT, 0);

// disable attributes and buffers
glDisableVertexAttribArray(GLKVertexAttribPosition);
glDisableVertexAttribArray(GLKVertexAttribTexCoord0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}

所以我想出来了。

此:

// enable tex coords
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), BUFFER_OFFSET(12));

需要这样:

GLint texCoordLoc = glGetAttributeLocation(program, texCoordAttributeName);
glEnableVertexAttribArray(texCoordLoc);
glVertexAttribPointer(texCoordLoc, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), BUFFER_OFFSET(12));

0 个答案:

没有答案
相关问题