简单的OpenGL / GLUT程序不在Windows上编译

时间:2013-04-21 20:03:32

标签: opengl glut

我正在尝试在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;
}

1 个答案:

答案 0 :(得分:0)

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

说真的,这是关于StackOverflow的第二天的常见问题解答。


BTW:在Linux上,libGL的官方OpenGL-ABI只定义了OpenGL-1.2,所以从技术上讲你也必须在Linux上使用扩展机制。但是大多数libGL.so要像那样导出较新的符号,所以你所做的就是这个原因。