GL / glx未正确链接

时间:2011-04-06 05:16:35

标签: c++ opengl x11 codeblocks ubuntu-10.10

系统规格和任务

我在Ubuntu 10.10上使用Code :: Blocks并使用OpenGL和glx。我正在学习C ++(来自C和Java的背景),所以任何代码的风格都不符合任何真正的好标准(但我愿意接受如何改进的建议,即使你没有问题的答案)

编辑:

巨大的实现:默认的OpenGL项目代码:: Blocks创建的是C,而不是C ++。我现在正在研究这个问题。

我目前正在尝试将Code :: Blocks上的默认OpenGL项目修改为一个简单的3d引擎。我目前收到错误:

  

在'Draw'之前预期'=',',',';','asm'或'__attribute__'

一旦我注释掉#include for< GL / glx.h>

我在某个论坛上看到Code :: Blocks在默认情况下没有查看usr / include /,但我在项目构建选项中将其添加到编译器的搜索目录中并且它似乎没有修复任何东西。

代码:

main.cpp: main.c:

#include <time.h>
#include "Draw.h"

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{

/*draw init here*/

Draw::Draw renderer = Draw::Draw.getDraw();

printf( "Press left mouse button to rotate around X axis\n" );
printf( "Press middle mouse button to rotate around Y axis\n" );
printf( "Press right mouse button to rotate around Z axis\n" );
printf( "Press ESC to quit the application\n" );

/* timing variable*/
/* Set it to delay half a second before rendering the first frame*/
clock_t flip_time = clock() + 0.5f * CLOCKS_PER_SEC;

while (1)
{


    /* Update models */


    /* Draw scene */

    /*  wait until it's been 1/60th of a second*/
    while(clock() < flip_time){}

    flip_time = clock() + (1.0f/60.0f) * CLOCKS_PER_SEC;
    /* Actually flip the frame */

}
}

Draw.h:

#ifndef DRAW_H
#define DRAW_H

#include <GL/glx.h>    /* This is the problem line */
#include <GL/gl.h>

#include <X11/X.h>    /* X11 constant (e.g. TrueColor) */
#include <X11/keysym.h>

class Draw
{
public:
    static Draw getDraw();
    virtual ~Draw();
    void update();
    void render();

protected:
private:
    Draw();
    bool init();

    /* The singleton*/
    static Draw *instance;
    static bool exists;

    /* X Window values */
    Display             *dpy;
    Window               win;
    GLboolean            doubleBuffer;

    /* X Parameters*/
    XVisualInfo         *vi;
    Colormap             cmap;
    XSetWindowAttributes swa;
    GLXContext           cx;
    XEvent               event;
    int                  dummy;

};

#endif // DRAW_H

最后,但并非最不重要的Draw.cpp:

#include "Draw.h"

/* Set up the singleton*/
bool Draw::exists = false;
Draw* Draw::instance = NULL;

Draw::Draw()
{
/*TODO: make this constructor */
}

Draw::~Draw()
{
//dtor
}

Draw Draw::getDraw()
{
if(!exists)
{
    instance = new Draw();
    instance->init();
    exists = true; //Thanks mat, This line was accidentally removed with extraneous comments
}

return *instance;
}

bool Draw::init()
{
/* Get the buffers ready */
static int snglBuf[] = {GLX_RGBA, GLX_DEPTH_SIZE, 16, None};
static int dblBuf[] = {GLX_RGBA, GLX_DEPTH_SIZE, 16, GLX_DOUBLEBUFFER, None};

/* Double Buffered is best*/
doubleBuffer = GL_TRUE;

/*TODO: add constructor if it hasn't been constructed already*/

dpy = XOpenDisplay(NULL);
if (dpy == NULL)
{
    return false;
}

/* make sure OpenGL's GLX extension supported */
if(!glXQueryExtension(dpy, &dummy, &dummy))
{
    return false;
}

/* find an appropriate visual */
/* find an OpenGL-capable RGB visual with depth buffer */
vi = glXChooseVisual(dpy, DefaultScreen(dpy), dblBuf);

if (vi == NULL)
{
    vi = glXChooseVisual(dpy, DefaultScreen(dpy), snglBuf);
    if (vi == NULL)
    {
        return false;
    }

    doubleBuffer = GL_FALSE;
}


/*
TODO: Fix or remove this
if(vi->class != TrueColor)
{
    return false;
}
*/
/* create an OpenGL rendering context  */

/* create an OpenGL rendering context */
cx = glXCreateContext(dpy, vi, /* no shared dlists */ None,
                    /* direct rendering if possible */ GL_TRUE);
if (cx == NULL)
{
    return false;
}

/* create an X window with the selected visual */

/* create an X colormap since probably not using default visual */
cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual, AllocNone);
swa.colormap = cmap;
swa.border_pixel = 0;
swa.event_mask = KeyPressMask    | ExposureMask
             | ButtonPressMask | StructureNotifyMask;
win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0,
                  300, 300, 0, vi->depth, InputOutput, vi->visual,
                  CWBorderPixel | CWColormap | CWEventMask, &swa);
XSetStandardProperties(dpy, win, "main", "main", None,
                     NULL, NULL, NULL);

/* bind the rendering context to the window */
glXMakeCurrent(dpy, win, cx);

/* request the X window to be displayed on the screen */
XMapWindow(dpy, win);

/* configure the OpenGL context for rendering  */
glEnable(GL_DEPTH_TEST); /* enable depth buffering */
glDepthFunc(GL_LESS);    /* pedantic, GL_LESS is the default */
glClearDepth(1.0);       /* pedantic, 1.0 is the default */

/* frame buffer clears should be to black */
glClearColor(0.0, 0.0, 0.0, 0.0);

/* set up projection transform */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 10.0);
/* establish initial viewport */

/* pedantic, full window size is default viewport */
glViewport(0, 0, 300, 300);

return true;
}

void Draw::update()
{
/*TODO: Add things to draw here*/
}

void Draw::render()
{
    /* actually flip buffers here */
}

我在此处发布之前删除了大量评论,但这不应影响它是否编译。

谢谢!

2 个答案:

答案 0 :(得分:0)

主文件中的这一行是错误的:

Draw::Draw renderer = Draw::Draw.getDraw();

Draw::Draw不是一种类型。要编译它,你只需要:

Draw renderer = Draw.getDraw();

看起来你正在尝试建立一个单身人士。您的代码根本不会这样做,每次都会得到一份副本。 (请注意,您没有在任何地方设置exists,但这只是一个额外的错误。)您应该返回指针或对共享实例的引用。例如,请参阅本文以获得正确的语法:C++ Singleton design pattern

答案 1 :(得分:0)

我发现链接存在问题。

Code :: Blocks中OpenGL的默认项目是 NOT C ++,它是C.我将它配置为使用g ++并修复了glx没有正确链接的问题。我修改了我的单身,看起来更像this,它现在也正常工作。我现在有一个问题,窗口没有出现,但我应该能够解决这个问题。

谢谢!