PDA

View Full Version : Display Lists



05-26-2001, 11:29 PM
Dear Reader,

I am learning OpenGl with X windows. I have been trying to use display lists, but I do not get any thing on the screen. If I put the drawing code in the place where I call the list everything works.

Could someone explain what I have done wrong.

Here is the code.

#include <stdio.h>
#include <Xm/MainW.h>
#include <Xm/Form.h>
#include <Xm/DrawingA.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glx.h>
#include <GLwMDrawA.h>


XVisualInfo *vi =NULL;
GLXContext cx;
Bool doubleBuffer = True;
GLuint listname = 1;

int BufferAttributes [] =
{
GLX_DOUBLEBUFFER,
GLX_RGBA,
GLX_DEPTH_SIZE, 12,
GLX_RED_SIZE, 1,
GLX_GREEN_SIZE, 1,
GLX_BLUE_SIZE, 1,
None
};

int *singleBufferAttributes = &BufferAttributes[0];
int *doubleBufferAttributes = &BufferAttributes[1];

void resize (Widget w, XtPointer clientdata, XtPointer calldata);
void expose (Widget w, XtPointer clientdata, XtPointer calldata);
void init (Widget w, XtPointer clientdata, XtPointer calldata);
void draw (Widget w);

int main (int argc, char **argv)
{
XtAppContext appContext;
Display *display;
Widget shell;
Widget form;
Widget draw;
Arg args[10];
int n;

XtToolkitInitialize();

appContext = XtCreateApplicationContext();

display = XtOpenDisplay (appContext,NULL,"Test","Test",NULL,0,&argc,argv);

if (vi == NULL)
{
vi = glXChooseVisual(display, DefaultScreen(display), doubleBufferAttribut
es);
if (vi == NULL)
{
vi = glXChooseVisual(display, DefaultScreen(display), singleBufferAttri
butes);
if (vi == NULL) XtAppError(appContext,"No RGB visual with depth buffer"
);
doubleBuffer = False;
}
}

cx = glXCreateContext (display, vi, None, True);
if (vi == NULL) XtAppError(appContext,"Could not create rendering context");

n=0;
XtSetArg(args[n], XmNallowShellResize, True); n++;
XtSetArg(args[n], XmNtitle, "Main Window "); n++;
shell = XtAppCreateShell("shell", "shell", applicationShellWidgetClass, displ
ay, args, n);

n=0;
form = XtCreateWidget("form", xmFormWidgetClass, shell, args, n);
XtManageChild (form);

n=0;
XtSetArg(args[n], XmNwidth, 400); n++;
XtSetArg(args[n], XmNheight, 400); n++;
XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
XtSetArg(args[n], GLwNvisualInfo, vi); n++;

draw = XtCreateWidget("Draw", glwMDrawingAreaWidgetClass, form, args,n);
XtManageChild (draw);

XtAddCallback(draw, GLwNexposeCallback, expose, NULL);
XtAddCallback(draw, GLwNresizeCallback, resize, NULL);
XtAddCallback(draw, GLwNginitCallback, init , NULL);

XtRealizeWidget (shell);

XtAppMainLoop(appContext);
}

float polygon1[3][3] = { {-10.0, -10.0, 0.0,},
{ 10.0, -10.0, 0.0,},
{-10.0, 10.0, 0.0,} };

float polygon2[3][3] = { { 0.0, -10.0, -10.0,},
{ 0.0, -10.0, 10.0,},
{ 0.0, 5.0, -10.0,} };

float polygon3[4][3] = { {-10.0, 6.0, 4.0,},
{-10.0, 3.0, 4.0,},
{ 4.0, -9.0, -10.0,},
{ 4.0, -6.0, -10.0,} };

void init (Widget w, XtPointer clientdata, XtPointer calldata)
{
glNewList(listname,GL_COMPILE);
glPushMatrix();
glPushAttrib(GL_CURRENT_BIT);
glBegin(GL_POLYGON);
glColor3f(0.0, 0.0, 0.0);
glVertex3fv(&polygon1[0][0]);
glColor3f(0.7, 0.7, 0.7);
glVertex3fv(&polygon1[1][0]);
glColor3f(1.0, 1.0, 1.0);
glVertex3fv(&polygon1[2][0]);
glEnd();
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 0.0);
glVertex3fv(&polygon2[0][0]);
glColor3f(0.0, 1.0, 0.7);
glVertex3fv(&polygon2[1][0]);
glColor3f(0.0, 0.0, 1.0);
glVertex3fv(&polygon2[2][0]);
glEnd();
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 0.0);
glVertex3fv(&polygon3[0][0]);
glColor3f(1.0, 0.0, 1.0);
glVertex3fv(&polygon3[1][0]);
glColor3f(0.0, 0.0, 1.0);
glVertex3fv(&polygon3[2][0]);
glColor3f(1.0, 0.0, 1.0);
glVertex3fv(&polygon3[3][0]);
glEnd();
glPopAttrib();
glPopMatrix();
glEndList ();
}

void expose (Widget w, XtPointer clientdata, XtPointer calldata)
{
glXMakeCurrent (XtDisplay(w), XtWindow(w), cx);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluPerspective(40.0, 1.0, 30.0, 1000.0);
glTranslatef(0.0, 0.0, -40.0);
glRotatef(-58.0, 0,1,0);

glCallList (listname);

glXSwapBuffers (XtDisplay(w), XtWindow(w));
}


void resize (Widget w, XtPointer clientdata, XtPointer calldata)
{
GLwDrawingAreaCallbackStruct *callbackData;
callbackData = (GLwDrawingAreaCallbackStruct *) calldata;
glXMakeCurrent(XtDisplay(w), XtWindow (w), cx);
glXWaitX();
glViewport (0,0,callbackData->width, callbackData->height);
}

MrShoe
05-27-2001, 03:23 AM
Hmmm, I didnt go through your code in detail, but I think you are placing the object in the same position as the camera, so maybe that is why you can see anything, put the object a few units into the z axis, sorry, but I dont have the time to go through the code in detail.

Deiussum
05-27-2001, 07:50 AM
I'm not familiar with glx programming, but in Windows, you cannot use any OpenGL functions until the window is setup and enabled for OpenGL. If you try and create a display list before this initialization of the window, all the calls fail. Perhaps you are running into something similar.