PDA

View Full Version : Plotting surfaces



raijinken
05-20-2004, 12:55 AM
I'm making a program to plot functions using surfaces: i've got an array with points and have to create a surface using them. I've heard about an algorithm called "painter algorithm" or similar.

Anybody knows how to implement this "painter algorithm"?
Is there any OpenGL function or code to create a surface from a given set of points?

Thanks in advance

hitman
05-20-2004, 10:13 AM
Here is an example program i've currently written. It draws a 3d surface from a math function with 3 arguments - f(x, z, t). Play with it - just change the function and see how the surface changes! It's really funny!


#include <GL/glut.h>
#include <math.h>
#define PI 3.14159265

float rot, t = 0;

float deg2rad (float deg)
{
return deg*PI/180;
}

static void key (int key, int x, int y)
{
switch (key) {

case GLUT_KEY_LEFT:
rot = 2;
break;

case GLUT_KEY_RIGHT:
rot = -2;
break;

case GLUT_KEY_UP:
case GLUT_KEY_DOWN:
rot = 0;
break;
}
glutPostRedisplay();
}

void draw ()
{
glRotatef (rot, 1.0f, 1.0f, 0.0f);
float x, z;
glBegin (GL_LINES);
for (x=-20; x<=20; x+=1)
{
for (z=-20; z<=20; z+=1)
{
glVertex3f (x, t*(cos(deg2rad(x*15))+sin(deg2rad(z*15))), z);
glVertex3f (x+1, t*(cos(deg2rad((x+1)*15))+sin(deg2rad((z+1)*15))), z+1);
}
}
t += 0.01;
glutPostRedisplay ();
glEnd ();
}

void display ()
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw ();
glutSwapBuffers ();
}

void init ()
{
glEnable (GL_DEPTH_TEST);
glMatrixMode (GL_PROJECTION);
gluPerspective (45.0, 1.0, 1.0, 200.0);
glMatrixMode (GL_MODELVIEW);
gluLookAt( 0.0, 0.0, -100.0,
0.0, 0.0, 0.0,
0.0, 1.0, 1.0);
}

int main (int argc, char* argv[])
{
glutInit (&amp;argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow ("3d function graph");
glutDisplayFunc (display);
glutSpecialFunc (key);

init ();

glutMainLoop ();
return 0;
}Bye4Now