Note that there are some explanatory texts on larger screens.

plurals
  1. POOpenGL 3.2 Color GL_TRIANGLES
    text
    copied!<p>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: <img src="https://i.stack.imgur.com/oFVxm.png" alt="enter image description here"></p> <p>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.</p> <p>I create the snake like this:</p> <pre><code>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; } </code></pre> <p>The colors defined here:</p> <pre><code>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); } </code></pre> <p>So the question is why all my squares turned red when I obviously load the buffer data with another color?</p> <p>I draw them like this in the display():</p> <pre><code>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(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload