GL/glx 未正确链接

发布于 2024-10-30 14:47:22 字数 5400 浏览 1 评论 0原文

系统规格和任务

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

编辑:

巨大的实现:默认的 OpenGL 项目 Code::Blocks 创建的是 C,而不是 C++。我现在正在调查这个。

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

“Draw”之前应有“=”、“,”、“;”、“asm”或“__attribute__”

一旦我注释掉 < 的 #include,它就会消失。 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 */
}

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

谢谢!

System Specs and task

I am using Code::Blocks on Ubuntu 10.10 and playing around with OpenGL and glx. I'm in the process of learning C++(from a background in C and Java), so the style of any code doesn't conform to any real good standards (but I'm open to suggestions on how to improve, even if you don't have an answer to the question)

Edit:

Huge Realization: The default OpenGL Project Code::Blocks creates is C, not C++. I'm looking into this now.

I'm currently trying to modify the default OpenGL project on Code::Blocks into a simple 3d engine. I am currently getting the error:

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

Which disappears as soon as I comment out the #include for < GL/glx.h>

I read on a forum somewhere that Code::Blocks doesn't look in usr/include/ by default, but I added that to the search directories for the compiler in the project build options and it didn't seem to fix anything.

Code:

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

Last, but not least 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 */
}

I removed a ton of comments before posting this here, but that shouldn't affect whether or not it compiles.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹂绝世的画 2024-11-06 14:47:22

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

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

Draw::Draw 不是类型。要编译它,您只需要:

Draw renderer = Draw.getDraw();

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

This line in your main file is wrong:

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

Draw::Draw is not a type. To get this to compile, you just need:

Draw renderer = Draw.getDraw();

It looks like you're trying to build a singleton. You code does not do that at all, you'll get a copy each time. (Note that you're not setting exists anywhere, but that's just an extra bug.) You should be returning a pointer or a reference to the shared instance. See for instance this article to get the syntax right: C++ Singleton design pattern.

一场信仰旅途 2024-11-06 14:47:22

我发现了链接的问题。

Code::Blocks 中 OpenGL 的默认项目是不是 C++,而是 C。我将其配置为使用 g++,它修复了 glx 未正确链接的问题。我修改了我的单例,使其看起来更像 this 并且现在也可以正常工作。我现在遇到了窗口未出现的问题,但我应该能够解决这个问题。

谢谢!

I found the issue with the linking.

The default project for OpenGL in Code::Blocks is NOT C++, it's C. I configured it to use g++ and it fixed the issue with glx not linking in correctly. I revised my singleton to look a little more like this and it works correctly now too. I have an issue now with the window not appearing, but I should be able to figure that out.

Thanks!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文