gpt4 book ai didi

c++ - 白屏(OpenGL)

转载 作者:行者123 更新时间:2023-12-03 07:15:39 26 4
gpt4 key购买 nike

我是OpenGL新手,我想用我的着色器在屏幕上绘制一个简单的三角形。我设置了缓冲区并编写了一个着色器,但是它似乎不起作用。它显示白色屏幕,我尝试使用glGenBuffer更改glCreateBuffer并将屏幕切换为黑屏。我想在一个名为createAndCompileShader(...)的函数中创建和编译着色器,缓冲区是在main函数中定义的。

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <string>
#include <iostream>

static unsigned int createAndCompileShader(const char* vertexShader, const char* fragmentShader) {

unsigned int fragmentShaderr = glCreateShader(GL_FRAGMENT_SHADER);
unsigned int vertexShaderr = glCreateShader(GL_VERTEX_SHADER);

glShaderSource(vertexShaderr, 1, &vertexShader, NULL);
glShaderSource(fragmentShaderr, 1, &fragmentShader, NULL);

glCompileShader(vertexShaderr);
glCompileShader(fragmentShaderr);

unsigned int program = glCreateProgram();

glAttachShader(program, vertexShaderr);
glAttachShader(program, fragmentShaderr);
glLinkProgram(program);


return program;
}


int main(void)
{
GLFWwindow* window;

/* Initialize the library */
if (!glfwInit())
return -1;

/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}

/* Make the window's context current */
glfwMakeContextCurrent(window);

float vertexPositions[6] = {
-0.5, -0.5,
0.0, 0.5,
0.5, -0.5
};


const char* vertexShader = "#version 330 core\n"
"layout(location = 0) in vec4 position;\n"
"void main()\n"
"{\n"
"gl_Position = position;\n"
"}\n";

const char* fragementShader = "#version 330 core\n"
"layout(location = 0) in vec4 position;\n"
"void main()\n"
"{\n"
"color = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\n";

unsigned int program = createAndCompileShader(vertexShader, fragementShader);

glUseProgram(program);

unsigned int buffer;

glGenBuffers(1, &buffer);
//glCreateBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), vertexPositions, GL_STATIC_DRAW);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float)*2 , 0);


//std::string vertexShader;
//std::string fragmentShader;

/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);


glDrawArrays(GL_TRIANGLES, 0 ,3);

/*glBegin(GL_TRIANGLES);
glVertex2f(-0.5, -0.5);
glVertex2f( 0.0, 0.5);
glVertex2f( 0.5, -0.5);
glEnd();*/

/* Swap front and back buffers */
glfwSwapBuffers(window);

/* Poll for and process events */
glfwPollEvents();
}

glDeleteProgram(program);

glfwTerminate();
return 0;
}


最佳答案

GLSL 3.30中未提供片段着色器输入布局位置限定符。您必须切换到GLSL 4.10或删除片段着色器中的布局位置:layout(location = 0) in vec4 position;

in vec4 position;
此外,您错过了片段着色器输出 color的规范。
#version 330 core
in vec4 position;
out vec4 color;
void main()
{
color = vec4(1.0, 0.0, 0.0, 1.0);
}
我建议使用 Raw string liters:
const char* fragementShader = R"(#version 330 core
in vec4 position;
out vec4 color;
void main()
{
color = vec4(1.0, 0.0, 0.0, 1.0);
})";
检查着色器编译是否成功以及程序对象是否成功链接。
如果可以成功通过着色器 glGetShaderiv 和参数 GL_COMPILE_STATUS检查着色器的编译。
如果程序链接成功,可以通过 glGetProgramiv 和参数 GL_LINK_STATUS进行检查。
(请参阅 this问题的答案。)

如果使用核心配置文件OpenGL上下文,则默认的 Vertex Array Object无效。创建一个命名的VAO:
unsigned int VAO;
glCreateVertexArrays(1, &VAO);
glBindVertexArray(VAO);

unsigned int buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), vertexPositions, GL_STATIC_DRAW);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float)*2 , 0);

GLEW必须由 glewInit()初始化。参见 Initializing GLEW:
glfwMakeContextCurrent(window);
glewInit();

关于c++ - 白屏(OpenGL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64514393/

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