Answer the question
In order to leave comments, you need to log in
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;"
"}";
const char* fsSource = "#version 450\n"
"in vec3 Color;\n"
"out vec4 out_fragColor;\n"
"void main(){\n"
"out_fragColor = Color;"
"}";
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);
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 };
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);
void renderScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES,0,3);
glutSwapBuffers();
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question