2011-04-06 85 views
1

系統規格和任務GL/glx鏈接不正確

我在Ubuntu 10.10上使用Code :: Blocks並使用OpenGL和glx。我正在學習C++(從C和Java的背景),所以任何代碼的風格都不符合任何真正的好標準(但是我願意提供如何改進的建議,即使你沒有一個問題的答案)

編輯:

巨大的實現:默認OpenGL的項目代碼:: Blocks的創建是C,而不是C++。我現在正在研究這個。

我目前正在嘗試將Code :: Blocks中的默認OpenGL項目修改爲簡單的3d引擎。我目前得到的錯誤:

expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before 'Draw'

其中就消失,我註釋掉的#include爲< GL/glx.h>

我在一個論壇上讀到某處代碼::塊沒有按」 t默認查看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 */ 
} 

在發佈這裏之前,我刪除了大量的評論,但這不應該影響它是否編譯。

謝謝!

回答

0

我發現與鏈接的問題。

Code :: Blocks中OpenGL的默認項目是不是 C++,它是C.我配置它使用g ++,它解決了glx沒有正確鏈接的問題。我修改我的單身人士看起來更像this,它現在也能正常工作。我現在還沒有出現窗口的問題,但我應該能夠弄清楚。

謝謝!

0

這條線在你的主文件是錯誤的:

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

Draw::Draw不是一個類型。要得到這個編譯,你只需要:

Draw renderer = Draw.getDraw(); 

它看起來像你試圖建立一個單身人士。你的代碼完全沒有這樣做,你每次都會得到一份副本。 (請注意,您並未在任何地方設置exists,但這只是一個額外的錯誤。)您應該返回一個指向共享實例的指針或引用。請參閱本文的實例以獲得正確的語法:C++ Singleton design pattern

+0

謝謝,我糾正了第一個錯誤,並添加了一行,我複製代碼時省略了。我早些時候看過那個頁面,但是建議使用[this](http://www.codeproject.com/KB/cpp/singletonrvs.aspx)。我會再看一遍,以確保我的模式正常工作,但我敢肯定,一旦我可以編譯和測試我正在處理的內容,就不會太困難。 – Jeremy 2011-04-06 06:36:20

+0

而不是一個指針,他也可以使用引用;相同的效果,在大多數情況下相同的二進制代碼,但使一些事情更好一點。將「static Draw getDraw()」更改爲「static Draw&getDraw()」。另外由於你的構造函數是私有的,你可以在那裏調用init。 - 一個側面說明:就我個人而言,我認爲單身人士是一種反模式,他們真的很少有人可以使用它。類工廠也許,但那些也是恕我直言反模式。 – datenwolf 2011-04-06 07:37:34