2012-08-14 59 views
8

Tôi đang cố gắng thiết lập dự án OpenGL bằng GLFW trong Visual C++ 2008 (Windows 7). Tôi đã thêm GLFW bao gồm và thư mục thư viện để các dự án bao gồm đường dẫn và thư viện, và tôi đã đặt sau đây trong phụ thuộc bổ sung:Rất nhiều số nhận dạng không khai báo có liên quan đến OpenGL sử dụng GLFW

GLFW.lib glu32.lib opengl32.lib 

Đó là tất cả những gì tôi cần phải làm, không phải là nó? Nhưng khi tôi cố gắng biên dịch bất kỳ chương trình OpenGl nào (ngoại trừ một chương trình cực kỳ đơn giản chỉ xóa màn hình bằng cách sử dụng glClear), tôi nhận được một loạt các số nhận dạng không khai báo và số nhận dạng không tìm thấy erros. Bất kỳ ý tưởng nào tại sao điều này có thể xảy ra? Dưới đây là danh sách lỗi hoàn chỉnh:

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 

EDIT: đây là toàn bộ chương trình

#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; 

} 

EDIT 2: Và đây header 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

Dòng đầu tiên là lỗi cú pháp có thể làm hỏng các câu lệnh 'include' sau nó. Khắc phục điều đó trước và xem điều gì xảy ra. Nó cũng sẽ là tốt nếu bạn bao gồm ít nhất là phần đầu tiên của chương trình của bạn có chứa những dòng này. – HonkyTonk

+0

Cảm ơn bạn! lỗi đầu tiên tương ứng với dòng này: 'GLuint VBO;', vì vậy tôi nghĩ rằng nó không thực sự là lỗi sintax, nhưng lỗi gây ra bởi vì nó không biết GLuint là gì. Tôi sẽ cập nhật câu hỏi với phần đầu tiên của chương trình. – XaitormanX

+0

Điều gì ẩn bên trong 'math_3d.h'? Có lẽ một cái gì đó trong đó là không đúng định dạng? Loại 'GLuint' được trình biên dịch tìm thấy, đọc kỹ các lỗi. – HonkyTonk

Trả lời

7

Vấn đề là đánh vần ra khá rõ ràng trên the OpenGL Wiki's Getting Started page. Vấn đề của bạn là bạn không có cơ chế tải các hàm OpenGL, đây là một bước cần thiết trong việc sử dụng OpenGL. Bạn là strongly encouraged to use a library to do that.

+0

Tôi sẽ thử thực hiện việc đó – XaitormanX

+0

Chỉ cần thêm một số chi tiết: lỗi biên dịch được đặc biệt gây ra bởi tệp tiêu đề 'GL/gl.h' của Microsoft SDK chỉ hỗ trợ OpenGL và bao gồm phiên bản 1.1. – elmindreda

+2

Đã được phê duyệt. Xin lỗi, tôi đã ra ngoài và tôi đã không thể phê duyệt câu hỏi cho đến bây giờ – XaitormanX