我尝试过多次重启 视觉工作室编译它和一切,但错误似乎没有消失。但它是在我创建临时着色器和程序时开始的,如果有帮助的话。
这是代码
#include <iostream>
#include <string>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
std::string fatalError(std::string error)
{
std::cout << error << std::endl;
return error;
}
int main()
{
int screenWidth = 1024;
int screenHeight = 726;
if (!glfwInit())
{
fatalError("GLFW failed to be initialized");
glfwTerminate();
return -1;
}
GLFWwindow *window;
window = glfwCreateWindow(screenWidth, screenHeight, "Graphics", NULL, NULL);
if (window == nullptr)
{
fatalError("Window failed to be created");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
fatalError("GLEW failed to be initialized");
glfwTerminate();
return -1;
}
GLfloat vertexData[] =
{
0.0f, 1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
-1.0f, -1.0f, 0.0f
};
GLuint vboID = 0;
glGenBuffers(1, &vboID);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
GLuint vaoID = 0;
glGenVertexArrays(1, &vaoID);
glBindVertexArray(vaoID);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_ARRAY_BUFFER, GL_FALSE, 0, NULL);
const GLchar* vertexShader[] =
{
"#version 410 \n"
"in vec3 vertexPos \n"
"void main() \n"
"{ \n"
"gl_Position = vec4(vertexPos, 1); \n"
"} \n"
};
const GLchar* fragmentShader[] =
{
"#version 410 \n"
"out vec4 vertexColor \n"
"void main() \n"
"{ \n"
"vertexColor = vec4(0.0, 1.0, 0.0, 1.0); \n"
"} \n"
};
GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertShader, 1, vertexShader, NULL);
glCompileShader(vertShader);
GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragShader, 1, fragmentShader, NULL);
glCompileShader(fragShader);
GLuint program = glCreateProgram();
glAttachShader(program, vertShader);
glAttachShader(program, fragShader);
glLinkProgram(program);
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
答案 0 :(得分:3)
你的程序有三个我可以找到的问题(在我得到一个绿色三角形之前,无论如何):
将来处理GL时你应该尝试使用glGetError进行一些错误检查,对于着色器glGetShaderInfoLog(那里是here的教程)。对于您的代码,glGetError在glVertexAttribePointer之后返回GL_INVALID_ENUM,这是一个很好的提示您的arugments不正确。 glDetShaderInfoLog(加上其他支持代码)打印出着色器无法编译的地方。
正如其他人提到的那样,您也希望熟悉一个调试器(尽管在这种情况下它不会有太多帮助)。对于像这样的GL特定问题,还有一些很好的调试器(我在AMD卡上有GPU PerfStudio的良好体验)。