1

我正在尝试在 Windows 上使用 GLUT 编译一个简单的应用程序。在 Linux 上,它编译时没有错误。在 Windows 上,我尝试了 Visual C++ Express 2010 和 CodeBlocks,但都遇到了相同的错误。我将库、头文件和 dll 文件添加到适当的目录中。我将库添加到链接器的设置中。你知道问题是什么吗?

错误:

D:\PBL\code blocks project\shikaku\main.cpp||In function 'void DrawGLScene()':|
D:\PBL\code blocks project\shikaku\main.cpp|259|error: 'glUseProgramObjectARB' was not     declared in this scope|
D:\PBL\code blocks project\shikaku\main.cpp|262|error: 'glUniform1ui' was not declared   in this scope|
D:\PBL\code blocks project\shikaku\main.cpp|263|error: 'glVertexAttribPointerARB' was not declared in this scope|
D:\PBL\code blocks project\shikaku\main.cpp|270|error: expected initializer before '*' token|
D:\PBL\code blocks project\shikaku\main.cpp|284|error: expected initializer before '*' token|
D:\PBL\code blocks project\shikaku\main.cpp||In function 'void log(int)':|
D:\PBL\code blocks project\shikaku\main.cpp|299|error: 'GL_OBJECT_INFO_LOG_LENGTH_ARB' was not declared in this scope|
D:\PBL\code blocks project\shikaku\main.cpp|300|error: 'glGetObjectParameterivARB' was not  declared in this scope|
D:\PBL\code blocks project\shikaku\main.cpp|303|error: 'glGetInfoLogARB' was not declared in this scope|
D:\PBL\code blocks project\shikaku\main.cpp||In function 'unsigned int load_shader()':|
D:\PBL\code blocks project\shikaku\main.cpp|310|error: 'glCreateProgramObjectARB' was not declared in this scope|
D:\PBL\code blocks project\shikaku\main.cpp|311|error: 'GL_VERTEX_SHADER_ARB' was not declared in this scope|
D:\PBL\code blocks project\shikaku\main.cpp|311|error: 'glCreateShaderObjectARB' was not declared in this scope|
D:\PBL\code blocks project\shikaku\main.cpp|312|error: 'GL_FRAGMENT_SHADER_ARB' was not declared in this scope|
D:\PBL\code blocks project\shikaku\main.cpp|313|error: 'V_SHADER' was not declared in this scope|
D:\PBL\code blocks project\shikaku\main.cpp|313|error: 'glShaderSourceARB' was not   declared in this scope|
D:\PBL\code blocks project\shikaku\main.cpp|314|error: 'F_SHADER' was not declared in this scope|
D:\PBL\code blocks project\shikaku\main.cpp|315|error: 'glCompileShaderARB' was not declared in this scope|
D:\PBL\code blocks project\shikaku\main.cpp|319|error: 'glAttachObjectARB' was not declared in this scope|
D:\PBL\code blocks project\shikaku\main.cpp|321|error: 'glLinkProgramARB' was not declared in this scope|
D:\PBL\code blocks project\shikaku\main.cpp|322|error: 'glUseProgramObjectARB' was not declared in this scope|
D:\PBL\code blocks project\shikaku\main.cpp|323|error: 'glGetUniformLocationARB' was not declared in this scope|
D:\PBL\code blocks project\shikaku\main.cpp|325|error: 'glGetAttribLocation' was not declared in this scope|
D:\PBL\code blocks project\shikaku\main.cpp|330|error: 'glUniformMatrix4fv' was not declared in this scope|
D:\PBL\code blocks project\shikaku\main.cpp|332|error: 'glUseProgram' was not declared in this scope|
D:\PBL\code blocks project\shikaku\main.cpp|333|error: 'glEnableVertexAttribArray' was not declared in this scope|
D:\PBL\code blocks project\shikaku\main.cpp|335|error: 'glDeleteObjectARB' was not declared in this scope|
D:\PBL\code blocks project\shikaku\main.cpp||In function 'int main(int, char**)':|
D:\PBL\code blocks project\shikaku\main.cpp|355|error: 'glDeleteObjectARB' was not declared     in this scope|
||=== Build finished: 26 errors, 0 warnings ===|

如果需要,代码:

#define GL_GLEXT_PROTOTYPES 1
#define GL3_PROTOTYPES 1
#include <windows.h>
#include <GL/gl.h>
#include <GL/glext.h>
#include <GL/glut.h>
#include <cstdlib>
#include <iostream>
using std::cout;
using std::endl;
int window;
int shader;

unsigned char texture[]= {0xFF,0xFF,0xFF,0xFF,
                      0xFF,0xFF,0xFF,0xFF,
                      0xFF,0xFF,0xFF,0xFF,
                      0xFF,0xFF,0xFF,0xFF,
                      0xFF,0xFF,0xFF,0xFF,
                      0xFF,0x00,0x00,0xFF,
                      0xFF,0x00,0x00,0xFF,
                      0xFF,0xFF,0xFF,0xFF,
                      0xFF,0xFF,0xFF,0xFF,
                      0xFF,0x00,0x00,0xFF,
                      0xFF,0x00,0x00,0xFF,
                      0xFF,0xFF,0xFF,0xFF,
                      0xFF,0xFF,0xFF,0xFF,
                      0xFF,0xFF,0xFF,0xFF,
                      0xFF,0xFF,0xFF,0xFF,
                      0xFF,0xFF,0xFF,0xFF};
static const float Z=-1.0;

static const float vertices[]={ -0.5, -0.5, Z,
               0.5, -0.5, Z,
               0.5,  0.5, Z,
              -0.5,  0.5, Z };

static const float texcoord[]={ 0.0, 0.0,
               1.0, 0.0,
               1.0, 1.0,
               0.0, 1.0 };

static const char indices[]={ 0,1,2,3 };

unsigned int texcnt;
unsigned int projection_matrix_loc;
unsigned int modelview_matrix_loc;
unsigned int vertex_loc;
unsigned int tex_coordinates_loc;
unsigned int tex_loc;

void InitGL(int Width, int Height) // Funkcja wolana po utworzeniu okna
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // kolor tla czarny
glPolygonMode(GL_FRONT_AND_BACK,GL_POLYGON);
glClearDepth(1.0);                    // Enables Clearing Of The Depth Buffer
glEnable(GL_DEPTH_TEST);              // Enables Depth Testing
glEnable(GL_TEXTURE_2D);
glGenTextures(1,&texcnt);
glBindTexture(GL_TEXTURE_2D,texcnt);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,4,4,0,GL_RGBA,GL_UNSIGNED_BYTE,texture);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glDisable(GL_TEXTURE_2D);
}

void ReSizeGLScene(int Width, int Height)
{
if (Height==0) Height=1;
glViewport(0, 0, Width, Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0,1.0,-1.0,1.0,1.0,-1.0);
glMatrixMode(GL_MODELVIEW);
}

void DrawGLScene()
{
glutSetWindow(window); // Aktywne okno
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glUseProgramObjectARB(shader);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,texcnt);
glUniform1ui(tex_loc,0); // Jednostka teksturuj¹ca 0
glVertexAttribPointerARB(vertex_loc,3,GL_FLOAT,0,0,vertices);
glVertexAttribPointerARB(tex_coordinates_loc,2,GL_FLOAT,0,0,texcoord);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE,indices);
glUseProgramObjectARB(0);
glutSwapBuffers();
}

const GLcharARB* V_SHADER[] = { //call per vertex
"uniform mat4 projection_matrix;"
"uniform mat4 modelview_matrix;"
"attribute vec3 vertex;"
"attribute vec2 tex_coordinates;"
"varying vec2 tex_coord;"
"void main()"
"{"
"tex_coord = tex_coordinates;"
"gl_Position = projection_matrix*modelview_matrix*vec4(vertex,1.0);"
"}"
};


const GLcharARB* F_SHADER[] = { //call per pixel
"varying vec2 tex_coord;"
"uniform sampler2D tex;"
"void main()"
"{"
"vec4 color = texture2D(tex,tex_coord);"
"gl_FragColor = color;"
"}"
};


void log(int shader) {
char* info_log;
int info_log_len=0;
int chars_written=0;
glGetObjectParameterivARB(shader,GL_OBJECT_INFO_LOG_LENGTH_ARB,
        &info_log_len);
if (info_log_len>0) {
    info_log= new char[info_log_len];
    glGetInfoLogARB(shader,info_log_len,&chars_written,info_log);
    std::cout << info_log;
    delete [] info_log;
  }
}

unsigned int load_shader() {
unsigned int p = glCreateProgramObjectARB();
unsigned int v = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
unsigned int f = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
glShaderSourceARB(v, 1, V_SHADER,0);
glShaderSourceARB(f, 1, F_SHADER,0);
glCompileShaderARB(v);
log(v);
glCompileShaderARB(f);
log(f);
glAttachObjectARB(p,v);
glAttachObjectARB(p,f);
glLinkProgramARB(p);
glUseProgramObjectARB(p);
modelview_matrix_loc = glGetUniformLocationARB(p,"modelview_matrix");
projection_matrix_loc = glGetUniformLocationARB(p,"projection_matrix");
tex_coordinates_loc = glGetAttribLocation(p,"tex_coordinates");
vertex_loc = glGetAttribLocation(p,"vertex");
tex_loc = glGetUniformLocationARB(p,"tex");
   const float identity[16] =
    {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1}; // mat jednostkowa nic nie robi
glUniformMatrix4fv(projection_matrix_loc,1,GL_FALSE,identity);
glUniformMatrix4fv(modelview_matrix_loc,1,GL_FALSE,identity);
glUseProgram(0);
glEnableVertexAttribArray(vertex_loc);
glEnableVertexAttribArray(tex_coordinates_loc);
glDeleteObjectARB(v);
glDeleteObjectARB(f);
return p;
}


int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 100);
window = glutCreateWindow("Bitmapka");
glutDisplayFunc(&DrawGLScene);
//glutFullScreen();
glutIdleFunc(&DrawGLScene);
glutReshapeFunc(&ReSizeGLScene);
shader = load_shader();
InitGL(640, 480);
glutMainLoop();
glDeleteObjectARB(shader);
glutDestroyWindow(window);
return 0;
}
4

1 回答 1

0

在 Windows 中,OpenGL 接口 DLL 仅公开 OpenGL-1.1 API,仅此而已。因此,要使用较新的函数,您必须在运行时通过扩展机制加载它们。去http://glew.sourceforge.net阅读文档,安装它,使用它。

说真的,这是 StackOverflow 上每隔 2 天就被问到的一个常见问题解答。


顺便说一句:在 Linux 上,libGL.so 的官方 OpenGL-ABI 只定义了 OpenGL-1.2,所以从技术上讲,你也必须在 Linux 上使用扩展机制。然而,大多数 libGL.so 都像这样导出较新的符号,所以你所做的正是出于这个原因。

于 2013-04-21T20:15:51.213 回答