Reputation: 1647
I'm currently trying to make a very simple snake game and have some problem creating the snake square. Right now I am creating the squares by creating 2 triangles. I have created the grid where the snake should be moving like this:
The color of the squares were actually yellow in the beginning. Then I tried to create the snake with the color red. But all my squares turned red.
I create the snake like this:
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;
}
The colors defined here:
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);
}
So the question is why all my squares turned red when I obviously load the buffer data with another color?
I draw them like this in the 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();
}
Upvotes: 1
Views: 650
Reputation: 474376
glBindVertexArray(shapeVertexArrayBuffer);
glBindVertexArray(shapeSnakeArrayBuffer);
drawBlock();
drawSnake();
You seem to have some ordering problems here. Presumably the shapeVertexArrayBuffer
(note: there's no such thing as a "vertex array buffer". Those are supposed to be vertex array objects; if loadBufferData
doesn't return a VAO, then you have more problems) is meant for drawing blocks, and the shapeSnakeArrayBuffer
is meant for drawing the snake.
You can only have one VAO active at a time. glBindVertexArray
sets the given vertex array object to be what is used for all rendering commands after it. Since the last one you used was shapeSnakeArrayBuffer
, that will be the VAO used for all rendering commands in drawBlock
just as well as for drawSnake
.
Upvotes: 1
Reputation: 101
Shouldn't you do:
glBindVertexArray(shapeVertexArrayBuffer);
drawBlock();
glBindVertexArray(shapeSnakeArrayBuffer);
drawSnake();
Upvotes: 1