PDA

View Full Version : Help on Rendering a 3D SURFACE

ajit_v_r
06-21-2003, 02:46 PM
Hi,

I am getting introduced to OpenGL. I urgently need to do the following:

I have an array of about 8000 points (i.e. X, Y and Z coordinates) constituting a surface. I wish to render/ shade this entire surface. Could I get some pointers to code that would help me to do this?

Thanks.

Stonemaster
06-22-2003, 12:47 AM
Vertex arrays seem to be the best thing for you: http://www.cs.rit.edu/~ncs/Courses/570/UserGuide/OpenGLonWin-15.html http://www.codecolony.de/opengl.htm#VertexArrays

See you,
-Stone.

ajit_v_r
06-23-2003, 05:25 PM
Originally posted by Stonemaster:
Vertex arrays seem to be the best thing for you: http://www.cs.rit.edu/~ncs/Courses/570/UserGuide/OpenGLonWin-15.html http://www.codecolony.de/opengl.htm#VertexArrays

See you,
-Stone.

Thanks for this. For the time being, I am not using vertex arrays. After reading the points from a file and triangulating them, what do I do to display the surface. I have written the following code - it somehow displays some totally weird surface. Please do help me. here is the code:

// arrayu of points
Point sites[NSITES];
// array of triangles
Triangle tri[NUMTRIS];

void normal (point3 p)
{
float d = 0;
int i;

for (i=0;i<3;i++) d += p[i]*p[i];
d = sqrt(d);
if (d>0)
{
for(i=0;i<3;i++) p[i] = p[i]/d;
}
}

void display ()
{
point3 b;
int i,v1,v2,v3;

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glTranslatef (-2,0,0);
printf ("\nIn display");
for (i=0;i<NUMTRIS;i++)
{
glBegin (GL_POLYGON);
v1 = tri[i].v1; v2 = tri[i].v2; v3 = tri[i].v3;

b[0] = sites[v1].x+sites[v2].x;
b[1] = sites[v1].y+sites[v2].y;
b[2] = sites[v1].z+sites[v2].z;
normal (b);
glColor3fv (b);
glNormal3fv (b);
glVertex3fv (b);

b[0] = sites[v2].x+sites[v3].x;
b[1] = sites[v2].y+sites[v3].y;
b[2] = sites[v2].z+sites[v3].z;
normal(b);
glColor3fv (b);
glNormal3fv (b);
glVertex3fv (b);

b[0] = sites[v3].x+sites[v1].x;
b[1] = sites[v3].y+sites[v1].y;
b[2] = sites[v3].z+sites[v1].z;
normal(b);
glColor3fv (b);
glNormal3fv (b);
glVertex3fv (b);

glEnd ();
}

glFlush ();
}

void myReshape (int w, int h)
{
glViewport (0,0,w,h);
glMatrixMode (GL_PROJECTION);

if (w <= h)
glOrtho (-4,4,-4*(GLfloat)h/(GLfloat)w,4*(GLfloat)h/(GLfloat)w,
-10,10);
else
glOrtho (-4*(GLfloat)w/(GLfloat)h,4*(GLfloat)w/(GLfloat)h,-4,4,-10,10);

glMatrixMode (GL_MODELVIEW);
display ();
}

void myinit ()
{
GLfloat mat_specular [] = {1,1,1,1};
GLfloat mat_diffuse [] = {1,1,1,1};
GLfloat mat_ambient [] = {1,1,1,1};
GLfloat mat_shininess[] = {100};
GLfloat light_ambient[] = {0,0,0,1};
GLfloat light_diffuse[] = {1,1,1,1};
GLfloat light_specular [] = {1,1,1,1};

glLightfv (GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv (GL_LIGHT0, GL_SPECULAR, light_specular);

glMaterialfv (GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv (GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv (GL_FRONT, GL_SHININESS, mat_shininess);

glEnable (GL_SMOOTH);
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);
glEnable (GL_DEPTH_TEST);
glEnable (GL_NORMALIZE);

glClearColor (1,1,1,1);
glColor3f (0,0,0);
}

int main(int argc, char **argv)
{
int i;

glutInit (&argc, argv);

// read the points into the array "sites"

// obtain the triangles into the array "Tri" (contains indices into the array called "sites").

glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);

glutInitWindowSize (400,400);

glutCreateWindow ("sphere");

myinit ();

glutReshapeFunc (myReshape);

glutDisplayFunc (display);

glutMainLoop ();

return(0);
}

kehziah
06-24-2003, 12:18 AM
- Assuming your setup code is correct

* use GL_TRIANGLE instead of GL_POLYGON
* move your glBegin/glEnd pair out of the loop