我正在尝试使用Visual C ++ 2008(Windows 7)中的GLFW设置OpenGL项目。我已将GLFW include和library目录添加到项目的include和library路径中,并且我已将以下内容添加到其他依赖项中:
GLFW.lib glu32.lib opengl32.lib
这就是我需要做的一切,不是吗?但是当我尝试编译任何OpenGl程序时(除了一个只使用glClear清除屏幕的非常简单的程序)我得到一堆未声明的标识符和标识符未找到错误。知道为什么会这样吗? 这是完整的错误列表:
1>Compiling...
1>main.cpp
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(9) : error C2146: syntax error : missing ';' before identifier 'VBO'
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(9) : error C2377: 'GLuint' : redefinition; typedef cannot be overloaded with any other symbol
1> c:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(52) : see declaration of 'GLuint'
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(9) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(43) : error C3861: 'glEnableVertexAttribArray': identifier not found
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(45) : error C2065: 'GL_ARRAY_BUFFER' : undeclared identifier
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(45) : error C3861: 'glBindBuffer': identifier not found
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(47) : error C3861: 'glVertexAttribPointer': identifier not found
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(55) : error C3861: 'glDisableVertexAttribArray': identifier not found
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(78) : error C3861: 'glGenBuffers': identifier not found
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(80) : error C2065: 'GL_ARRAY_BUFFER' : undeclared identifier
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(80) : error C3861: 'glBindBuffer': identifier not found
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(82) : error C2065: 'GL_ARRAY_BUFFER' : undeclared identifier
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(82) : error C2065: 'GL_STATIC_DRAW' : undeclared identifier
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(82) : error C3861: 'glBufferData': identifier not found
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(87) : error C2146: syntax error : missing ')' before identifier 'ShaderProgram'
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(87) : error C2182: 'AddShader' : illegal use of type 'void'
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(87) : error C2440: 'initializing' : cannot convert from 'Vector3f' to 'int'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(87) : error C2059: syntax error : ')'
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(89) : error C2143: syntax error : missing ';' before '{'
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(89) : error C2447: '{' : missing function header (old-style formal list?)
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(144) : error C2146: syntax error : missing ';' before identifier 'ShaderProgram'
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(144) : error C2065: 'ShaderProgram' : undeclared identifier
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(144) : error C3861: 'glCreateProgram': identifier not found
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(148) : error C2065: 'ShaderProgram' : undeclared identifier
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(158) : error C2065: 'ShaderProgram' : undeclared identifier
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(158) : error C2065: 'GL_VERTEX_SHADER' : undeclared identifier
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(160) : error C2065: 'ShaderProgram' : undeclared identifier
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(160) : error C2065: 'GL_FRAGMENT_SHADER' : undeclared identifier
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(166) : error C2065: 'GLchar' : undeclared identifier
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(166) : error C2146: syntax error : missing ';' before identifier 'ErrorLog'
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(166) : error C2065: 'ErrorLog' : undeclared identifier
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(166) : error C2059: syntax error : '{'
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(166) : error C2143: syntax error : missing ';' before '{'
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(166) : error C2143: syntax error : missing ';' before '}'
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(170) : error C2065: 'ShaderProgram' : undeclared identifier
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(170) : error C3861: 'glLinkProgram': identifier not found
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(172) : error C2065: 'ShaderProgram' : undeclared identifier
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(172) : error C2065: 'GL_LINK_STATUS' : undeclared identifier
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(172) : error C3861: 'glGetProgramiv': identifier not found
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(176) : error C2065: 'ShaderProgram' : undeclared identifier
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(176) : error C2065: 'ErrorLog' : undeclared identifier
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(176) : error C2070: ''unknown-type'': illegal sizeof operand
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(176) : error C2065: 'ErrorLog' : undeclared identifier
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(176) : error C3861: 'glGetProgramInfoLog': identifier not found
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(178) : error C2065: 'ErrorLog' : undeclared identifier
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(186) : error C2065: 'ShaderProgram' : undeclared identifier
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(186) : error C3861: 'glValidateProgram': identifier not found
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(188) : error C2065: 'ShaderProgram' : undeclared identifier
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(188) : error C2065: 'GL_VALIDATE_STATUS' : undeclared identifier
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(188) : error C3861: 'glGetProgramiv': identifier not found
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(192) : error C2065: 'ShaderProgram' : undeclared identifier
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(192) : error C2065: 'ErrorLog' : undeclared identifier
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(192) : error C2070: ''unknown-type'': illegal sizeof operand
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(192) : error C2065: 'ErrorLog' : undeclared identifier
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(192) : error C3861: 'glGetProgramInfoLog': identifier not found
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(194) : error C2065: 'ErrorLog' : undeclared identifier
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(202) : error C2065: 'ShaderProgram' : undeclared identifier
1>c:\glfw-2.7.3\opengl ikasi\main.cpp(202) : error C3861: 'glUseProgram': identifier not found
编辑:这是整个计划
#include <stdio.h>
#include <string.h>
#include <GL/glfw.h>
#include "math_3d.h"
GLuint VBO;
static const char* pVS = " \n\
#version 150 \n\
\n\
layout (location = 0) in vec3 Position; \n\
\n\
void main() \n\
{ \n\
gl_Position = vec4(0.5 * Position.x, 0.5 * Position.y, Position.z, 1.0); \n\
}";
static const char* pFS = " \n\
#version 150 \n\
\n\
out vec4 FragColor; \n\
\n\
void main() \n\
{ \n\
FragColor = vec4(1.0, 0.0, 0.0, 1.0); \n\
}";
static void RenderSceneCB()
{
glClear(GL_COLOR_BUFFER_BIT);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glfwSwapBuffers();
}
static void CreateVertexBuffer()
{
Vector3f Vertices[3];
Vertices[0] = Vector3f(-1.0f, -1.0f, 0.0f);
Vertices[1] = Vector3f(1.0f, -1.0f, 0.0f);
Vertices[2] = Vector3f(0.0f, 1.0f, 0.0f);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
}
static void AddShader(GLuint ShaderProgram, const char* pShaderText, GLenum ShaderType)
{
GLuint ShaderObj = glCreateShader(ShaderType);
if (ShaderObj == 0) {
fprintf(stderr, "Error creating shader type %d\n", ShaderType);
return ;
}
const GLchar* p[1];
p[0] = pShaderText;
GLint Lengths[1];
Lengths[0]= strlen(pShaderText);
glShaderSource(ShaderObj, 1, p, Lengths);
glCompileShader(ShaderObj);
GLint success;
glGetShaderiv(ShaderObj, GL_COMPILE_STATUS, &success);
if (!success) {
GLchar InfoLog[1024];
glGetShaderInfoLog(ShaderObj, 1024, NULL, InfoLog);
fprintf(stderr, "Error compiling shader type %d: '%s'\n", ShaderType, InfoLog);
return;
}
glAttachShader(ShaderProgram, ShaderObj);
}
static void CompileShaders()
{
GLuint ShaderProgram = glCreateProgram();
if (ShaderProgram == 0) {
fprintf(stderr, "Error creating shader program\n");
return;
}
AddShader(ShaderProgram, pVS, GL_VERTEX_SHADER);
AddShader(ShaderProgram, pFS, GL_FRAGMENT_SHADER);
GLint Success = 0;
GLchar ErrorLog[1024] = { 0 };
glLinkProgram(ShaderProgram);
glGetProgramiv(ShaderProgram, GL_LINK_STATUS, &Success);
if (Success == 0) {
glGetProgramInfoLog(ShaderProgram, sizeof(ErrorLog), NULL, ErrorLog);
fprintf(stderr, "Error linking shader program: '%s'\n", ErrorLog);
return;
}
glValidateProgram(ShaderProgram);
glGetProgramiv(ShaderProgram, GL_VALIDATE_STATUS, &Success);
if (!Success) {
glGetProgramInfoLog(ShaderProgram, sizeof(ErrorLog), NULL, ErrorLog);
fprintf(stderr, "Invalid shader program: '%s'\n", ErrorLog);
return;
}
glUseProgram(ShaderProgram);
}
int main(int argc, char** argv)
{
glfwInit();
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2);
glfwOpenWindow(600, 600, 0, 0, 0, 0, 0, 0, GLFW_WINDOW);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
CreateVertexBuffer();
CompileShaders();
while (1)
{
RenderSceneCB();
}
return 0;
}
编辑2:这里是math_3d.h标题
#ifndef MATH_3D_H
#define MATH_3D_H
struct Vector3f
{
float x;
float y;
float z;
Vector3f()
{
}
Vector3f(float _x, float _y, float _z)
{
x = _x;
y = _y;
z = _z;
}
};
#endif
答案 0 :(得分:7)
问题在the OpenGL Wiki's Getting Started page上清楚地阐明了。您的问题是您没有加载OpenGL函数的机制,这是使用OpenGL的必要步骤。你是strongly encouraged to use a library to do that。