Stewart Taylor writes:
Hi All
I'm trying to start Linux programming but keep coming up against problems. The
latest one is OpenGL programming with a Geforce 3 graphics card. I'm told
that I have to use the NVidia OpenGL libraries (nvsdk) but having downloaded
it, it seems to be totally Windows. I have no idea how to use it (if it can
be used under Linux) and even the docs are in Windows formats. Can anyone
help me on this or do I have to throw this otherwise good card away and start
again?
No you don't want to use that I'm sure. NVIDIA ships with the
NVIDIA_GLX-1.0-xxxx package some header files. These should not differ
too much from a standard GL.h file except that they probably have definitions
for NVIDIA specific extensions.
NVIDIA_GLX-1.0-2960/usr/include/GL/gl/gl.h
glx.h
glxtokens.h
Copy these into /usr/include/GL or wherever you prefer. Then
refer to them with:
#include
This should get you started. If you don't know anything about OpenGL then
I suggest you look into the GLUT toolkit. If you write an OpenGL
program in Linux it will use the GLX interface whereas in windows it uses
the WGL interface. The GLUT hides this from you, and lets you just
programm OpenGL.
To get you started with glut here is an example I did
a while back that uses OpenGL to plot a koch snowflake. Compile
like so:
gcc -c -g koch.c
gcc -o koch koch.o -lGL -lGLU -lglut
Don't pay too much attention to the logic. Look in main for how
the window is setup, and then look at the different glXXXXX calls.
This is a 2d example, but it should help you get started.
Save to file koch.c
-------------------------------------------
#include
#include
#include
#include
#include
#include
#define WIN_HEIGHT 400
#define WIN_WIDTH 400
#define LEVEL 9
static void koch
(
int level,
float r,
float *xleft,
float *yleft,
float *xright,
float *yright
);
static void draw_koch
(
int level,
int x1,
int y1,
int x0,
int y0
);
static void draw_koch
(
int level,
int x1,
int y1,
int x0,
int y0
)
{
float
xleft[30],
yleft[30],
xright[30],
yright[30];
xleft[level-1] = (float)x0;
yleft[level-1] = (float)y0;
xright[level-1] = (float)x1;
yright[level-1] = (float)y1;
koch (level-1, (float)0.29, xleft, yleft, xright, yright);
}
static void koch
(
int level,
float r,
float *xleft,
float *yleft,
float *xright,
float *yright
)
{
if (level > 0)
{
level--;
/* Left Branch */
xleft[level] = xleft[level+1];
yleft[level] = yleft[level+1];
xright[level] = (0.3333f * xright[level+1]) +
(0.6667f * xleft[level+1]);
yright[level] = (0.3333f * yright[level+1]) +
(0.6667f * yleft[level+1]);
koch (level, r, xleft, yleft, xright, yright);
/* Middle Left Branch */
xleft[level] = xright[level];
yleft[level] = yright[level];
xright[level] = (0.5f * xright[level+1]) + (0.5f * xleft[level+1])
- (r * (yleft[level+1] - yright[level+1]));
yright[level] = (0.5f * yright[level+1]) + (0.5f * yleft[level+1])
+ (r * (xleft[level+1] - xright[level+1]));
koch (level, r, xleft, yleft, xright, yright);
/* Middle Right Branch */
xleft[level] = xright[level];
yleft[level] = yright[level];
xright[level] = (0.6667f * xright[level+1]) +
(0.3333f * xleft[level+1]);
yright[level] = (0.6667f * yright[level+1]) +
(0.3333f * yleft[level+1]);
koch (level, r, xleft, yleft, xright, yright);
/* Right Branch */
xleft[level] = xright[level];
yleft[level] = yright[level];
xright[level] = xright[level+1];
yright[level] = yright[level+1];
koch (level, r, xleft, yleft, xright, yright);
level++;
}
else
{
glBegin (GL_LINES);
glVertex2i ((int)xleft[0], (int)yleft[0]);
glVertex2i ((int)xright[0], (int)yright[0]);
glEnd ();
}
}
static int win_width = WIN_WIDTH;
static int win_height = WIN_HEIGHT;
static int the_level = LEVEL;
static void redraw (void)
{
time_t start_time, end_time;
printf ("Enter - redraw ()\n");
start_time = time (0);
glClearColor (0.0, 0.0, 0.0, 0.0);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glFlush ();
draw_koch (the_level, 0.0, win_height/2, win_width-1, win_height/2);
end_time = time (0);
printf ("Exit - redraw () %d secs\n", end_time - start_time);
}
static void reshape (int width, int height)
{
printf ("Enter - reshape ()\n");
win_width = width;
win_height = height;
glViewport (0, 0, width, height);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D (-0.5, (width + 0.5), -0.5, (height + 0.5));
printf ("Exit - reshape ()\n");
}
static void keyboard (int key, int x, int y)
{
int changed = 0;
switch (key)
{
case GLUT_KEY_UP:
if (++the_level > 10)
the_level = 10;
changed = 1;
break;
case GLUT_KEY_DOWN:
if (--the_level < 1)
the_level = 1;
changed = 1;
break;
}
if (changed)
glutPostRedisplay ();
}
int main (int argc, char **argv)
{
/* init window parameters. */
glutInitWindowSize (WIN_WIDTH, WIN_HEIGHT);
glutInitWindowPosition (100, 100);
glutInitDisplayMode (GLUT_RGBA | GLUT_SINGLE | GLUT_DEPTH);
/* create window. */
if (glutCreateWindow ("koch curve") <= 0)
exit (0);
/* set display and reshape functions. */
glutDisplayFunc (redraw);
glutReshapeFunc (reshape);
glutSpecialFunc (keyboard);
/* start GLUT processing loop. */
glutMainLoop ();
return 0;
}