gpt4 book ai didi

c++ - 不同的片段着色器颜色

转载 作者:行者123 更新时间:2023-12-02 10:27:06 27 4
gpt4 key购买 nike

我想使用着色器绘制不同颜色的不同图形。
我尝试使用统一变量并使用网格的顶点属性来分离顶点缓冲区对象,但是当我运行程序时,只使用了第二种颜色,而第一个图根本没有显示。我不确定这是否与我绑定(bind)着色器的方式或使用统一变量的方式有关。提前致谢。
这是代码:

#include <GL/glew.h>
#include <GL/glut.h>
#include <stdio.h>

const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";

const char* fragmentShaderSource = "#version 330 core\n"
"in vec4 color;\n"
"out vec4 FragColor;\n"
"uniform vec3 figColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(figColor, 0.0f);\n"
"}\n\0";

// Set of vertices that draw the different figures:
float vertices1[] = {
-0.8f, 0.6f, 0.0f,
-0.8f, 0.4f, 0.0f,
-0.83f, 0.44f, 0.0f,
-0.87f, 0.51f, 0.0f,
-0.9f, 0.57f, 0.0f,
-0.93f, 0.63f, 0.0f,
-0.95f, 0.69f, 0.0f,
-0.97f, 0.75f, 0.0f,
-0.98f, 0.8f, 0.0f,
-0.91f, 0.8f, 0.0f,
-0.85f, 0.79f, 0.0f,
-0.8f, 0.77f, 0.0f,
};

float vertices2[] = {
-0.8f, -0.15f, 0.0f,
-1.0f, 0.0f, 0.0f,
-0.95f, 0.0f, 0.0f,
-0.86f, -0.01f, 0.0f,
-0.81f, -0.02f, 0.0f,
-0.75f, -0.03f, 0.0f,
};

GLuint vertexShader;
GLuint fragmentShader;
GLuint shaderProgram;

void display(void) {
// Background color.
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
GLint uniform = glGetUniformLocation(shaderProgram, "figColor");
glUniform3f(uniform, 1.0f, 0.0f, 0.0f);
glDrawArrays(GL_TRIANGLE_FAN, 0, 12);
glUniform3f(uniform, 0.0f, 0.749f, 1.0f);
glDrawArrays(GL_TRIANGLE_FAN, 0, 6);
glUseProgram(0);

glFlush();
}

// Main.
int main(int argc, char** argv) {
glutInit(&argc, argv);
// Color mode.
glutInitDisplayMode(GLUT_RGBA);
// Window size.
glutInitWindowSize(500, 500);
// Title.
glutCreateWindow("Tarea 3: Figura con curvas");
GLenum err = glewInit();
if (err != GLEW_OK) {
printf("glewInit failed: %s", glewGetErrorString(err));
exit(1);
}

// Compile vertex shader.
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);

// Check for vertex shader compilation errors.
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
printf("ERROR::SHADER::VERTEX::COMPILATION_FAILED\n");
}

// Compile fragment shader.
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);

// Check for fragment shader compile errors.
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
printf("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n");
}

// Link and delete shaders.
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);

//Separating the Vertex Buffer Objects.
GLuint VBOs[2];
glBindBuffer(GL_ARRAY_BUFFER, VBOs[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices1), vertices1, GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, VBOs[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices2), vertices2, GL_STATIC_DRAW);

// Specify a VertexArrayObject for each mesh.
GLuint VAOs[2];
glGenVertexArrays(2, VAOs);

glBindVertexArray(VAOs[0]);
glBindBuffer(GL_ARRAY_BUFFER, VBOs[0]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);

glBindVertexArray(VAOs[1]);
glBindBuffer(GL_ARRAY_BUFFER, VBOs[1]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);

// Bind the VAO before the draw call.
glBindVertexArray(VAOs[0]);
glDrawArrays(GL_TRIANGLE_FAN, 0, 12);

glBindVertexArray(VAOs[1]);
glDrawArrays(GL_TRIANGLE_FAN, 0, 24);

// Displaying the figures.
glutDisplayFunc(display);
glutMainLoop();
}

最佳答案

您错过了生成顶点缓冲区对象名称:

//Separating the Vertex Buffer Objects.
GLuint VBOs[2];
glGenBuffers(2, VBOs); <----
在主循环中绘制网格之前,您必须绑定(bind)顶点数组:
GLuint VAOs[2];

void display(void) {
// Background color.
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
GLint uniform = glGetUniformLocation(shaderProgram, "figColor");

glUniform3f(uniform, 1.0f, 0.0f, 0.0f);
glBindVertexArray(VAOs[0]); // <---
glDrawArrays(GL_TRIANGLE_FAN, 0, 12);

glUniform3f(uniform, 0.0f, 0.749f, 1.0f);
glBindVertexArray(VAOs[1]); // <----
glDrawArrays(GL_TRIANGLE_FAN, 0, 6);
glUseProgram(0);

glFlush();
}
int main(int argc, char** argv) {
// [...]

// Specify a VertexArrayObject for each mesh.
// GLuint VAOs[2]; <---- DELETE
glGenVertexArrays(2, VAOs);

// [...]

// DELETE
/*
// Bind the VAO before the draw call.
glBindVertexArray(VAOs[0]);
glDrawArrays(GL_TRIANGLE_FAN, 0, 12);

glBindVertexArray(VAOs[1]);
glDrawArrays(GL_TRIANGLE_FAN, 0, 24);
*/

// [...]

关于c++ - 不同的片段着色器颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63909963/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com