PDA

View Full Version : run opengl on Linux



Iqbal_Hossain
06-07-2005, 03:55 AM
I am working on redhat 9.1. But, I don't know how to add opengl file on linux.

Azdo
06-07-2005, 04:08 AM
Originally posted by Iqbal_Hossain:
I am working on redhat 9.1. But, I don't know how to add opengl file on linux.Include the files <GL/gl.h>, <GL/glu.h> as usual and link with -lGL and (if you need GLU) -lGLU

RigidBody
06-07-2005, 04:11 AM
if you only want to run an opengl prog, you just need the libraries. to test if the libs are properly installed, try to run glxgears (hopefully installed on your system).

if glxgears doesn't work, try

ldd /usr/X11R6/bin/glxgears

ldd shows you which libraries you need to run a program.

if you want to develop, you additionally need the header files (gl.h, glx.h, glu.h).

if you have an nvidia card, go to www.nvidia.com (http://www.nvidia.com) and download the latest linux driver. you'll find the header files in

/usr/share/doc/NVIDIA_GLX-1.0/include/GL/

when you've installed the driver.

an alternative is using MESA, which is a non-accelerated software GL lib. it should be included in your redhat distribution.

06-07-2005, 04:45 PM
is there a hello world example for opengl? i'm an experienced program just wanting to fool with it for fun. thnx

RigidBody
06-07-2005, 09:42 PM
there you are...


#include<X11/Xlib.h>
#include<GL/glx.h>
#include<GL/glu.h>

Display *dpy = XOpenDisplay(NULL);
Window root = DefaultRootWindow(dpy);
GLint att[] = {GLX_RGBA, None};
XVisualInfo *vi = glXChooseVisual(dpy, 0, att);
GLXContext glc = glXCreateContext(dpy, vi, NULL, False);
Visual *vis = DefaultVisual(dpy, 0);
Colormap cmap = XCreateColormap(dpy, root, vis, AllocNone);
unsigned int w = XDisplayWidth(dpy, 0) / 2;
unsigned int h = XDisplayHeight(dpy, 0) / 2;
int dep = DefaultDepth(dpy, 0);
int cmask = CWColormap | CWBorderPixel | CWEventMask;
int emask = ExposureMask;
XEvent xev;
XSetWindowAttributes swa;
XWindowAttributes gwa;
Window win;

int main(int argc, char *argv[]){

swa.colormap = cmap;
swa.border_pixel = 0;
swa.event_mask = emask;
win = XCreateWindow(dpy, root, 0, 0, 400, 400, 0, dep, InputOutput, vis, cmask, &amp;swa);
XStoreName(dpy, win, "SIMPLE GL QUAD");
XMapWindow(dpy, win);

glXMakeCurrent(dpy, win, glc);
glClearColor(0.00, 0.00, 0.60, 1.00);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1., 1., -1., 1., 1., 100.);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0., 0., 10., 0., 0., 0., 0., 1., 0.);

while(1) {
XNextEvent(dpy, &amp;xev);

if(xev.type == Expose) {

XGetWindowAttributes(dpy, win, &amp;gwa);
w = gwa.width;
h = gwa.height;

glViewport(0, 0, w, h);
glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_QUADS);
glColor3f(1., 0., 0.); glVertex3f(-.75, -.75, 0.);
glColor3f(0., 1., 0.); glVertex3f( .75, -.75, 0.);
glColor3f(0., 0., 1.); glVertex3f( .75, .75, 0.);
glColor3f(1., 1., 0.); glVertex3f(-.75, .75, 0.);
glEnd();

glFlush(); } } }
//
// gcc -I/usr/X11R6/include/ -L/usr/X11R6/lib -o SimpleQuad SimpleQuad.cc -lX11 -lGL -lGLU
//

jide
06-08-2005, 12:54 AM
The example RigidBody gave doesn't say 'hello world' but that's it ;)

Jing
06-10-2005, 08:57 AM
Also you can test GLUT to avoid direct interaction with X.

To use glut,

#include<GL/glut.h>

and
-lgut when linking.