我想使用一些方便的方法来生成用于对象的顶点和颜色数组。根据我在生成数组时所见,这是我目前使用的一个示例:
GLfloat * CDMeshVertexesCreateRectangle(CGFloat height, CGFloat width) {
// Requires the rendering method GL_TRIANGLE_FAN
GLfloat *squareVertexes = (GLfloat *) malloc(8 * sizeof(GLfloat));
squareVertexes[0] = -(width / 2);
squareVertexes[1] = -(height / 2);
squareVertexes[2] = -(width / 2);
squareVertexes[3] = (height / 2);
squareVertexes[4] = (width / 2);
squareVertexes[5] = (height / 2);
squareVertexes[6] = (width / 2);
squareVertexes[7] = -(height / 2);
return squareVertexes;
}
但是当我在这样的事情上使用它时:
GLuint memoryPointer = 0;
GLuint colourMemoryPointer = 0;
GLfloat *vertexes = CDMeshVertexesCreateRectangle(200, 200);
GLfloat *colors = CDMeshColorsCreateGrey(1.0, 4);
// Allocate the buffer
glGenBuffers(1, &memoryPointer);
// Bind the buffer object (tell OpenGL what to use)
glBindBuffer(GL_ARRAY_BUFFER, memoryPointer);
// Allocate space for the VBO
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexes), vertexes, GL_STATIC_DRAW);
// Allocate the buffer
glGenBuffers(1, &colourMemoryPointer);
// Bind the buffer object (tell OpenGL what to use)
glBindBuffer(GL_ARRAY_BUFFER, colourMemoryPointer);
// Allocate space for the VBO
glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
glEnableClientState(GL_VERTEX_ARRAY); // Activate vertex coordinates array
glEnableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, memoryPointer);
glVertexPointer(2, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, colourMemoryPointer);
glColorPointer(4, GL_FLOAT, 0, 0);
//render
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glDisableClientState(GL_VERTEX_ARRAY); // Deactivate vertex coordinates array
glDisableClientState(GL_COLOR_ARRAY);
free(vertexes);
free(colors);
渲染不成立,渲染过程中会出现随机问题,例如闪烁、颜色失真等。在使用正常定义的数组(删除生成的顶点及其相关代码)时使用相同的代码进行初始化和渲染时,不会出现问题。
GLfloat Square[8] = {
-100, -100,
-100, 100,
100, 100,
100, -100
};
有谁知道我要去哪里错?