PDA

View Full Version : Simple triangle



paneb
02-15-2006, 12:29 PM
Hey guys,
I am trying to draw a white triangle using GLX, but for some reason I cannot get it to show...
Here is the code (admittedly not so great). I can't seem to figure out why it is not drawing the triangle...
Thanks in advance


i just noticed that there are some useless override redirect "things" - i have removed them, but still nothing...



#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <X11/Xlib.h>
#include <GL/gl.h>
#include <GL/glx.h>
#include <GL/glu.h>
#include <X11/keysym.h>

void init ();
void resize (int, int);
void draw ();
void event_loop ();

/* globals */
const char *env_dpy = NULL;
Display *dpy = NULL;
int screen;
Window root, glwin;
int x,y;
int w,h;
XVisualInfo *vinfo = NULL;
GLXContext glctx;
XSetWindowAttributes *attr = NULL;
Colormap cmap;
static int gl_visual_attr[] = {GLX_RGBA, GLX_DOUBLEBUFFER, GLX_RED_SIZE, 4,
GLX_GREEN_SIZE, 4, GLX_BLUE_SIZE, 4,
GLX_DEPTH_SIZE, 16, None};
/* /globals */

int main (void)
{
if (NULL == (env_dpy = getenv("DISPLAY"))) {
printf("no X server available.\n");
return 1;
}

dpy = XOpenDisplay(env_dpy);
if (NULL == dpy) {
printf("cannot connect to X server.\n");
return 1;
}

screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen);

/* get an OpenGL visual */
vinfo = glXChooseVisual(dpy, DefaultScreen(dpy), gl_visual_attr);
if (NULL == vinfo) {
printf("cannot find appropriate visual.\n");
return 1;
}

/* create an OpenGL context */
glctx = glXCreateContext(dpy, vinfo, 0, GL_TRUE);
if (NULL == glctx) {
printf("cannot create an OpenGL context.\n");
return 1;
}

/* create a colormap */
cmap = XCreateColormap(dpy, root, vinfo->visual, AllocNone);
if (0 == cmap) {
printf("cannot allocate colormap.\n");
return 1;
}

attr = malloc(sizeof(XSetWindowAttributes));
if (NULL == attr) {
printf("out of memory.\n");
return 1;
}

attr->colormap = cmap;
attr->border_pixel = 0;
attr->override_redirect = True;
attr->event_mask = ExposureMask | KeyPressMask | KeyReleaseMask |
ButtonPressMask | ButtonReleaseMask | CWOverrideRedirect |
StructureNotifyMask;

x = 100;
y = 100;
w = 400;
h = 400;

glwin = XCreateWindow(dpy, root, x, y, w, h, 0, vinfo->depth, InputOutput,
vinfo->visual, CWBorderPixel | CWColormap |CWEventMask,
attr);
if (0 == glwin) {
printf("cannot create window.\n");
return 1;
}
XMapRaised(dpy, glwin);
XSync(dpy, False);

glXMakeCurrent(dpy, glwin, glctx);

init();
resize(w, h);

event_loop();

XCloseDisplay(dpy);
XFree(vinfo);
free(attr);

return 0;
}

void init ()
{
glShadeModel(GL_SMOOTH);
/* window is cleared to black */
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

resize(w, h);
glFlush();
}

void resize (int w, int h)
{

if (0 >= h) h = 1;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glViewport(0, 0, w, h);

gluPerspective(45.0f, (GLfloat)w / (GLfloat)h, 1.0f, 100.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void draw ()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();
glTranslatef(0.0f, 0.0f, 0.0f);

glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_TRIANGLES);
glVertex3f(-10.0f, -10.0f, 0.0f);
glVertex3f(0.0f, 10.0f, 0.0f);
glVertex3f(10.0f, -10.0f, 0.0f);
glEnd();
}

void event_loop ()
{
XEvent xe;
int done = 0;
KeySym ksym;

while (0 == done) {
while (0 < XPending(dpy)) {
XNextEvent(dpy, &amp;xe);
switch (xe.type) {
case Expose:
draw(dpy, glwin);
break;
case ConfigureNotify:
resize(xe.xconfigure.width, xe.xconfigure.height);
break;
case KeyPress:
ksym = XKeycodeToKeysym(dpy, xe.xkey.keycode, 0);
switch(ksym) {
case XK_q:
done = 1;
break;
case XK_i:
if (glXIsDirect(dpy, glctx))
printf("render type: direct.\n");
else
printf("render type: indirect.\n");
break;
}
break;
default:
break;
}
}
draw();
glXSwapBuffers(dpy,glwin);
}
}

satan
02-15-2006, 01:24 PM
Try glTranslatef(0.0f, 0.0f, 10.0f) instead of 0,0,0.

paneb
02-15-2006, 01:45 PM
I have change glTranslatef from 0.0,0.0,0.0 to 0.0,0.0,10.0 but still I cannot see the triangle.
Am I missing something in the setup of the window? I do see a black background...

paneb
02-15-2006, 02:13 PM
Ah you were right though with the position - I played around a little with my position and now I can see the triangle just fine :)

satan
02-15-2006, 04:44 PM
It should have been -10 I always forget that the z-axis is pointing out of the screen and not in.
Glad that it works now.