N
N
Nikita Kargin2020-06-05 13:01:42
OpenGL
Nikita Kargin, 2020-06-05 13:01:42

Why are colors not taken into account in OpenGL?

I set the vertex shader:

const char* vsSource = "#version 450\n"
    "layout (location = 0) in vec3 vertexCords;\n"
    "layout (location = 1) in vec3 color;\n"
    "out vec3 Color;\n"
    "void main(){\n"
    "gl_Position = vec4(vertexCords,1.0f) ;\n"
    "Color = color;"
    "}";

Fragment:
const char* fsSource = "#version 450\n"
    "in vec3 Color;\n"
    "out vec4 out_fragColor;\n"
    "void main(){\n"
    "out_fragColor = Color;"
    "}";

I create and link shader program:
unsigned int vShader = glCreateShader(GL_VERTEX_SHADER);
  glShaderSource(vShader,1, &vsSource,NULL);
  glCompileShader(vShader);
  unsigned int fShader = glCreateShader(GL_FRAGMENT_SHADER);
  glShaderSource(fShader, 1, &fsSource, NULL);
  glCompileShader(fShader);
  shaderProgram = glCreateProgram();
  glAttachShader(shaderProgram, vShader); 
  glAttachShader(shaderProgram, fShader); 
  glLinkProgram(shaderProgram);

I set an array of vertices/colors:
float vertices[] = {
    //координаты      //цвет
  -0.8f, -0.8f, 0.0f,  1.0f, 0.0f, 0.0f,
  0.8f, -0.8f, 0.0f,   0.0f, 1.0f, 0.0f,
  0.0f, 0.8f, 0.0f,     0.0f, 0.0f, 1.0f };

I create buffers:
glGenBuffers(1, &VBO);
  glGenVertexArrays(1, &VAO);
  glBindVertexArray(VAO);
  glBindBuffer(GL_ARRAY_BUFFER, VBO);
  glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  glEnableVertexAttribArray(0);
  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), nullptr);
  glEnableVertexAttribArray(1);
  glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), reinterpret_cast<void*>
    (sizeof(float) * 3));
  glBindBuffer(GL_ARRAY_BUFFER, 0);
  glBindVertexArray(0);

Here is the render function:
void renderScene() {

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glUseProgram(shaderProgram);
  glBindVertexArray(VAO);
  glDrawArrays(GL_TRIANGLES,0,3);

  glutSwapBuffers();
}

My array of vertices/colors has 3 points, one is red, the second is blue, the third is green. Only the output triangle is white for some reason. Where did I screw up?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question