我目前正在尝试制作一个非常简单的蛇游戏,但在创建蛇广场时遇到了一些问题。现在我正在通过创建 2 个三角形来创建正方形。我已经创建了蛇应该像这样移动的网格:
方块的颜色一开始实际上是黄色的。然后我尝试用红色创建蛇。但是我所有的方块都变红了。
我像这样创建蛇:
void drawSnake()
{
mat4 modelView;
modelView = Translate(1,0,0);
glUniformMatrix4fv(modelViewUniform, 1, GL_TRUE, modelView);
GLuint indices2[3] = {0,1,2}; //, 0, 5, 1, 0};
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, indices2);
modelView = Translate(1, 0, 0);
glUniformMatrix4fv(modelViewUniform, 1, GL_TRUE, modelView);
GLuint indices6[3] = {4,3,2}; //, 0, 5, 1, 0};
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, indices6);
//vec2 blockCoordinate = vec2(-t + 1, t - 1);
//blockCol[i] = blockCoordinate;
}
这里定义的颜色:
void loadGeometry() {
vec3 color(1.0f, 1.0f, 0.0f);
Vertex rectangleData[rectangleSize] = {
{ vec2( -1.0, -1.0 ), color },
{ vec2( 1.0, -1.0 ), color },
{ vec2( 1.0, 1.0 ), color },
{ vec2( -1.0, 1.0 ), color },
{ vec2(-1.0, -1.0 ), color }
};
shapeVertexArrayBuffer = loadBufferData(rectangleData, rectangleSize);
vec3 color1(1.0f, 0.0f, 0.0f);
Vertex rectangleData1[rectangleSize] = {
{ vec2( -1.0, -1.0 ), color1 },
{ vec2( 1.0, -1.0 ), color1 },
{ vec2( 1.0, 1.0 ), color1 },
{ vec2( -1.0, 1.0 ), color1 },
{ vec2(-1.0, -1.0 ), color1 }
};
shapeSnakeArrayBuffer = loadBufferData(rectangleData1, rectangleSize);
}
所以问题是为什么当我显然用另一种颜色加载缓冲区数据时我所有的方块都变成了红色?
我在 display() 中像这样绘制它们:
void display() {
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
mat4 projection = Ortho2D(-15.0f, 15.0f, -15.0f, 15.0f);
glUniformMatrix4fv(projectionUniform, 1, GL_TRUE, projection);
glBindVertexArray(shapeVertexArrayBuffer);
glBindVertexArray(shapeSnakeArrayBuffer);
drawBlock();
drawSnake();
glutSwapBuffers();
}