在C编程中绘制3D立方体

时间:2015-09-06 20:40:56

标签: c

我目前正在练习如何在C中绘制3D立方体,但在编译过程中我遇到了这个错误。

/tmp/ccAuvH6Y.o: In function `DrawCube':
cube3D.c:(.text+0x1e4): undefined reference to `XGetWindowAttributes'
cube3D.c:(.text+0x203): undefined reference to `glViewport'
cube3D.c:(.text+0x223): undefined reference to `glClearColor'
cube3D.c:(.text+0x22d): undefined reference to `glClear'
cube3D.c:(.text+0x245): undefined reference to `glColor3f'
cube3D.c:(.text+0x24a): undefined reference to `glPushMatrix'
cube3D.c:(.text+0x26c): undefined reference to `glRotatef'
cube3D.c:(.text+0x276): undefined reference to `glEnableClientState'
cube3D.c:(.text+0x280): undefined reference to `glEnableClientState'
cube3D.c:(.text+0x29b): undefined reference to `glColorPointer'
cube3D.c:(.text+0x2b9): undefined reference to `glVertexPointer'
cube3D.c:(.text+0x2d4): undefined reference to `glDrawElements'
cube3D.c:(.text+0x2d9): undefined reference to `glPopMatrix'
cube3D.c:(.text+0x2f2): undefined reference to `glXSwapBuffers'
/tmp/ccAuvH6Y.o: In function `main':
cube3D.c:(.text+0x385): undefined reference to `XOpenDisplay'
cube3D.c:(.text+0x441): undefined reference to `glXChooseVisual'
cube3D.c:(.text+0x4c9): undefined reference to `XCreateColormap'
cube3D.c:(.text+0x548): undefined reference to `XCreateWindow'
cube3D.c:(.text+0x568): undefined reference to `XMapWindow'
collect2: error: ld returned 1 exit status

我的守则如下。

#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>

#include <GL/gl.h>
#include <GL/glx.h>
#include <GL/glu.h>



Display *dpy;
XWindowAttributes winattr;
Window win;


void DrawCube(void){

float  vertsCoords[] = {
         0.5f, 0.5f, 0.5f,
        -0.5f, 0.5f, 0.5f,
        -0.5f,-0.5f,0.5f,
        0.5f,-0.5f,0.5f,
        0.5f,-0.5f,-0.5f,
        0.5f,0.5f,-0.5f,
        -0.5f,0.5f,-0.5f,
        -0.5f,-0.5f,-0.5f,
};

GLubyte indices [] = {
        0,1,2,3,
        5,0,3,4,
        5,6,7,4,
        5,6,1,0,
        1,6,7,2,
        7,4,3,2,


};
GLubyte colors[]={
        255,0,0,
        0,255,0,
        0,0,255,
        255,255,0,
        0,255,255,
        255,0,255,
        255,255,255,
        0,0,0,


};


static int angle = 0;
        XGetWindowAttributes(dpy,win, &winattr);
        glViewport(0,0,winattr.width,winattr.height);

        glClearColor(0.7f,0.7f,0.7f,0.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glColor3f(0.0f,0.9f,1.0f);

        glPushMatrix();
        glRotatef(angle,0.5,1.0,0.0);
#if 0

        glBegin(GL_TRIANGLES);
        glVertex3f(0.0f,-0.2f,0.5f);
        glVertex3f(-0.5f,0.3f,0.5f);
        glVertex3f(0.5f,0.6f,0.5f);
        glEnd();

#endif

        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_COLOR_ARRAY);
        glColorPointer(3,GL_UNSIGNED_BYTE,0,colors);
        glVertexPointer(3,GL_FLOAT,0,vertsCoords);

        glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,indices);
        glPopMatrix();
        glXSwapBuffers(dpy,win);
        usleep(5000);
        angle += 1;

        }
int main(int argc, char *agrv[])
{
    int screen;
    Window root_win;
    XEvent event;
    unsigned depth;
    XSetWindowAttributes attrs;

    GLint att [] = {GLX_RGBA, GLX_DEPTH_SIZE,24,GLX_DOUBLEBUFFER,None};

    XVisualInfo *visual;
    GLXContext glc;

    if ((dpy = XOpenDisplay(NULL)) == NULL) {
        fprintf(stderr,"Can't Open Display\n");
        exit(1);
    }

    screen = DefaultScreen(dpy);
    depth = DefaultDepth(dpy,screen);
    root_win = RootWindow(dpy,screen);

    visual = glXChooseVisual(dpy,screen,att);

    attrs.border_pixel = BlackPixel(dpy,screen);
    attrs.background_pixel = WhitePixel(dpy,screen);
    attrs.override_redirect = True;
    attrs.colormap = XCreateColormap(dpy,root_win,visual->visual,AllocNone);
    attrs.event_mask = ExposureMask | KeyPressMask;

    win = XCreateWindow(dpy, root_win, 200, 200, 600, 500, 0,
                        visual->depth, InputOutput,
                        visual->visual,
                        CWBackPixel|CWColormap|CWBorderPixel|CWEventMask, 
                        &attrs);

    XMapWindow(dpy,win);
}

1 个答案:

答案 0 :(得分:0)

您尚未链接库。

-lgl(或类似)