Simple OpenGL A - Z?

simple opengl program

#include <whateverYouNeed.h>

main() {

OpenAWindowPlease();

glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
glFlush();

KeepTheWindowOnTheScreenForAWhile();
}
:slight_smile:

lovely. :stuck_out_tongue:

I don’t get what you are asking… would you like a simple glx based OpenGL program to play with?

If so, here is a very simple program that does not use glut, or anything else… Call the code below gltemplate.c if you are going to use the compile line I recommend in the comments. Also, the postings here sometimes mangles lines… so you may need to connect some lines in the code.

–Ben

/*

  • To compile use cc -o gltemplate gltemplate.c -lGL -L/usr/X11R6/lib -lX11
    */
    #include <GL/glx.h>
    #include <X11/Xutil.h>
    #include <X11/keysym.h>
    #include <math.h>
    #include <stdio.h>

Display *dpy;
Window window;

static Bool
WaitForMapNotify(Display *d, XEvent *e, XPointer arg)
{
return e->type == MapNotify && e->xmap.window == *(Window *) arg;
}

void
initialize(void)
{
int i;
XSetWindowAttributes swa;
XSizeHints hints;
XVisualInfo *vi = NULL;
XEvent ev;
GLXContext cx;
char title[256];
static int attr[] = {
GLX_RGBA,
GLX_RED_SIZE, 8,
GLX_GREEN_SIZE, 8,
GLX_BLUE_SIZE, 8,
GLX_DEPTH_SIZE, 1,
GLX_STENCIL_SIZE, 0,
None,
};

dpy = XOpenDisplay(0);
if (dpy == NULL) {
    fprintf(stderr, "Can't connect to display \"%s\"

",
getenv(“DISPLAY”));
exit(1);
}

vi = glXChooseVisual(dpy, DefaultScreen(dpy), attr);
if (vi == NULL) {
    fprintf(stderr, "No matching visual on \"%s\"

",
getenv(“DISPLAY”));
exit(1);
}

sprintf(title, "gltemplate");

swa.border_pixel = 0;
swa.colormap = XCreateColormap(dpy, RootWindow(dpy, vi-&gt;screen),
                               vi-&gt;visual, AllocNone);
swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask 

|
ButtonPressMask;
window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
50, 50,
0, vi->depth, InputOutput, vi->visual,
CWBorderPixel | CWColormap | CWEventMask,
&swa);
XStoreName(dpy, window, title);

hints.min_aspect.x = hints.min_aspect.y =
    hints.max_aspect.x = hints.max_aspect.y = 1;
hints.min_aspect.x = hints.min_aspect.y =
    hints.max_aspect.x = hints.max_aspect.y = 1;
hints.x = hints.y = 100;
hints.width = hints.height = 399;
hints.min_width = hints.min_height = 100;
hints.flags = PSize | PAspect | PPosition | PMinSize;
XSetNormalHints(dpy, window, &hints);
XMoveResizeWindow(dpy, window, 100, 100, 399, 399);

XMapWindow(dpy, window);
XIfEvent(dpy, &ev, WaitForMapNotify, (XPointer) &window);

if ((cx = glXCreateContext(dpy, vi, 0, GL_TRUE)) == NULL) {
    fprintf(stderr, "Can't create context

");
exit(1);
}
if (!glXMakeCurrent(dpy, window, cx)) {
fprintf(stderr, "Can’t make window current to context
");
exit(1);
}

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
glDisable(GL_DEPTH_TEST);

}

void
usage()
{
fprintf(stderr,"Bad option
");
exit(1);
}

void
resize_window()
{
XWindowAttributes windowattr;

XGetWindowAttributes(dpy, window, &windowattr);
glViewport(0, 0, windowattr.width, windowattr.height);

}

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

/* put all your opengl drawing stuff here… as in */

glColor3f(1.0, 1.0, 1.0);
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();

glFinish();

}

int
main(int argc, char *argv[])
{
XEvent event;
char buffer[5];
int bufsize = 5;
KeySym key;
XComposeStatus compose;
initialize();
drawScene();

while(GL_TRUE) {
    while(XPending(dpy)) {
        XNextEvent(dpy, &event);
        switch(event.type) {
          case ConfigureNotify:
            resize_window();
            drawScene();
            break;
        }
    }
}

}

When I compile this, I get the following error:

gcc -o gltemplate gltemplate.c -lGL -L/usr/X11R6/lib -lX11
/usr/bin/ld: cannot find -lX11
collect2: ld returned 1 exit status

I am using Redhat9 if that helps. Thanks.

Originally posted by <sopor>:
[b]When I compile this, I get the following error:

gcc -o gltemplate gltemplate.c -lGL -L/usr/X11R6/lib -lX11
/usr/bin/ld: cannot find -lX11
collect2: ld returned 1 exit status

I am using Redhat9 if that helps. Thanks.[/b]
Did you change -L/usr/X11R6/lib to wherever libX11 is on your system?

I see following files in that directory:
libX11.so.6
libX11.so.6.2

are these what it’s looking for? there are other files as well

Hmmm… I don’t know then. Sorry I couldn’t help.

well, I changed the -lX11 argument to -llibX11 and now it gives the following errors:

In file included from gltemplate.c:4:
/usr/include/GL/glx.h:23:22: X11/Xlib.h: No such file or directory
/usr/include/GL/glx.h:24:23: X11/Xutil.h: No such file or directory
/usr/include/GL/glx.h:25:21: X11/Xmd.h: No such file or directory
In file included from gltemplate.c:4:
/usr/include/GL/glx.h:36: parse error before “GLXContextID”

etc…

do I need to have something else for X11 installed? I didn’t compile my kernel, does that matter? What does that do? Thanks for your help.

Originally posted by <sopor>:
[b]well, I changed the -lX11 argument to -llibX11 and now it gives the following errors:

In file included from gltemplate.c:4:
/usr/include/GL/glx.h:23:22: X11/Xlib.h: No such file or directory
/usr/include/GL/glx.h:24:23: X11/Xutil.h: No such file or directory
/usr/include/GL/glx.h:25:21: X11/Xmd.h: No such file or directory
In file included from gltemplate.c:4:
/usr/include/GL/glx.h:36: parse error before “GLXContextID”

etc…

do I need to have something else for X11 installed? I didn’t compile my kernel, does that matter? What does that do? Thanks for your help.[/b]
No. Something else is wrong. You are now getting
compilation errors, not linking errors. You should
only do -lX11, no -llibX11, the linker takes care
of all that. You should be able to compile the
program with ‘gcc -L/usr/X11R6/lib -lGL -lX11’ and
all the other arguments as needed. If this returns
linking errors (i.e. errors that 0x… stuff), you
have some installation/configuration problem with
your system or gcc, etc. Fix your compilation errors
first, then try to diagnose the linking problem
again.

You propably need the ‘-I/usr/X11R6/include’ option, too…

better go throw the link for beeginer in Opengl

http://opengltutorial.co.uk/

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.