尝试在 openGL 中创建 USB 记忆棒时,我总是遇到错误
我无法编译代码。代码的唯一错误对应于第 87-100 行,其余部分看起来还不错。
#include <iostream> // cout, cerr
#include <cstdlib> // EXIT_FAILURE
#include <GL/glew.h> // GLEW library
#include <GLFW/glfw3.h> // GLFW library
const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 600;
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"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);\n"
"}\n\0";
unsigned int CompileShader(unsigned int type, const char* source)
{
unsigned int shader = glCreateShader(type);
glShaderSource(shader, 1, &source, NULL);
glCompileShader(shader);
int success;
char infoLog[512];
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(shader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::COMPILATION_FAILED\n" << infoLog << std::endl;
}
return shader;
}
unsigned int CreateShader()
{
unsigned int vertexShader = CompileShader(GL_VERTEX_SHADER, vertexShaderSource);
unsigned int fragmentShader = CompileShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
unsigned int shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
int success;
char infoLog[512];
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::LINKING_FAILED\n" << infoLog << std::endl;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
return shaderProgram;
}
float vertices[] = {
// first cube
-0.5f, -0.5f, -0.5f, // bottom-left-back
0.5f, -0.5f, -0.5f, // bottom-right-back
0.5f, 0.5f, -0.5f, // top-right-back
0.5f, 0.5f, -0.5f, // top-right-back
-0.5f, 0.5f, -0.5f, // top-left-back
-0.5f, -0.5f, -0.5f, // bottom-left-back
-0.5f, -0.5f, 0.5f, // bottom-right-back
0.5f, -0.5f, 0.5f, // bottom-right-front
0.5f, 0.5f, 0.5f, // top-right-front
-0.5f, 0.5f, 0.5f, // top-left-front
-0.5f, 0.5f, 0.5f, // top-left-front
-0.5f, -0.5f, 0.5f, // bottom-left-front
0.5f, -0.5f, 0.5f // bottom-right-front
};
unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "USB Drive", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
std::cout << "Failed to initialize GLEW" << std::endl;
return -1;
}
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
unsigned int shaderProgram = CreateShader();
while (!glfwWindowShouldClose(window))
{
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteProgram(shaderProgram);
glfwTerminate();
return 0;
}
我一直在谷歌搜索以查看问题所在,但我对 opengl 很陌生,真的不知道该怎么做
Severity Code Description Project File Line Suppression State
Error C2373 '__glewBindBuffer': redefinition; different type modifiers OpenGLSample C:\Users\Ismael\Documents\CS330\OpenGLSample\OpenGLSample\Source.cpp 93
Error C2373 '__glewBindBuffer': redefinition; different type modifiers OpenGLSample C:\Users\Ismael\Documents\CS330\OpenGLSample\OpenGLSample\Source.cpp 99
Error C2373 '__glewBindVertexArray': redefinition; different type modifiers OpenGLSample C:\Users\Ismael\Documents\CS330\OpenGLSample\OpenGLSample\Source.cpp 91
Error C2373 '__glewBindVertexArray': redefinition; different type modifiers OpenGLSample C:\Users\Ismael\Documents\CS330\OpenGLSample\OpenGLSample\Source.cpp 100
Error C2065 '__glewBindVertexArray': undeclared identifier OpenGLSample C:\Users\Ismael\Documents\CS330\OpenGLSample\OpenGLSample\Source.cpp 135
Error C2373 '__glewBufferData': redefinition; different type modifiers OpenGLSample C:\Users\Ismael\Documents\CS330\OpenGLSample\OpenGLSample\Source.cpp 94
Error C2373 '__glewEnableVertexAttribArray': redefinition; different type modifiers OpenGLSample C:\Users\Ismael\Documents\CS330\OpenGLSample\OpenGLSample\Source.cpp 97
Error C2373 '__glewGenBuffers': redefinition; different type modifiers OpenGLSample C:\Users\Ismael\Documents\CS330\OpenGLSample\OpenGLSample\Source.cpp 89
Error C2373 '__glewGenVertexArrays': redefinition; different type modifiers OpenGLSample C:\Users\Ismael\Documents\CS330\OpenGLSample\OpenGLSample\Source.cpp 88
Error C2373 '__glewVertexAttribPointer': redefinition; different type modifiers OpenGLSample C:\Users\Ismael\Documents\CS330\OpenGLSample\OpenGLSample\Source.cpp 96
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int OpenGLSample C:\Users\Ismael\Documents\CS330\OpenGLSample\OpenGLSample\Source.cpp 88
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int OpenGLSample C:\Users\Ismael\Documents\CS330\OpenGLSample\OpenGLSample\Source.cpp 89
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int OpenGLSample C:\Users\Ismael\Documents\CS330\OpenGLSample\OpenGLSample\Source.cpp 91
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int OpenGLSample C:\Users\Ismael\Documents\CS330\OpenGLSample\OpenGLSample\Source.cpp 93
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int OpenGLSample C:\Users\Ismael\Documents\CS330\OpenGLSample\OpenGLSample\Source.cpp 94
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int OpenGLSample C:\Users\Ismael\Documents\CS330\OpenGLSample\OpenGLSample\Source.cpp 96
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int OpenGLSample C:\Users\Ismael\Documents\CS330\OpenGLSample\OpenGLSample\Source.cpp 97
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int OpenGLSample C:\Users\Ismael\Documents\CS330\OpenGLSample\OpenGLSample\Source.cpp 99
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int OpenGLSample C:\Users\Ismael\Documents\CS330\OpenGLSample\OpenGLSample\Source.cpp 100
Error (active) E0077 this declaration has no storage class or type specifier OpenGLSample C:\Users\Ismael\Documents\CS330\OpenGLSample\OpenGLSample\Source.cpp 88
Error (active) E0077 this declaration has no storage class or type specifier OpenGLSample C:\Users\Ismael\Documents\CS330\OpenGLSample\OpenGLSample\Source.cpp 89
Error (active) E0077 this declaration has no storage class or type specifier OpenGLSample C:\Users\Ismael\Documents\CS330\OpenGLSample\OpenGLSample\Source.cpp 91
Error (active) E0077 this declaration has no storage class or type specifier OpenGLSample C:\Users\Ismael\Documents\CS330\OpenGLSample\OpenGLSample\Source.cpp 93
Error (active) E0077 this declaration has no storage class or type specifier OpenGLSample C:\Users\Ismael\Documents\CS330\OpenGLSample\OpenGLSample\Source.cpp 94
Error (active) E0077 this declaration has no storage class or type specifier OpenGLSample C:\Users\Ismael\Documents\CS330\OpenGLSample\OpenGLSample\Source.cpp 96
Error (active) E0077 this declaration has no storage class or type specifier OpenGLSample C:\Users\Ismael\Documents\CS330\OpenGLSample\OpenGLSample\Source.cpp 97
Error (active) E0077 this declaration has no storage class or type specifier OpenGLSample C:\Users\Ismael\Documents\CS330\OpenGLSample\OpenGLSample\Source.cpp 99
Error (active) E0077 this declaration has no storage class or type specifier OpenGLSample C:\Users\Ismael\Documents\CS330\OpenGLSample\OpenGLSample\Source.cpp 100