Results 1 to 4 of 4

Thread: Help on Rendering a 3D SURFACE

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2003
    Location
    India
    Posts
    3

    Help on Rendering a 3D SURFACE

    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.

  2. #2

    Re: Help on Rendering a 3D SURFACE

    Vertex arrays seem to be the best thing for you: http://www.cs.rit.edu/~ncs/Courses/5...LonWin-15.html http://www.codecolony.de/opengl.htm#VertexArrays

    See you,
    -Stone.

  3. #3
    Junior Member Newbie
    Join Date
    Jun 2003
    Location
    India
    Posts
    3

    Re: Help on Rendering a 3D SURFACE

    Originally posted by Stonemaster:
    Vertex arrays seem to be the best thing for you: http://www.cs.rit.edu/~ncs/Courses/5...LonWin-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);

    glLoadIdentity ();
    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);
    glLoadIdentity ();

    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);
    }

  4. #4
    Senior Member Regular Contributor
    Join Date
    Apr 2001
    Posts
    340

    Re: Help on Rendering a 3D SURFACE

    - Assuming your setup code is correct

    * use GL_TRIANGLE instead of GL_POLYGON
    * move your glBegin/glEnd pair out of the loop
    * double check the way you build your triangles; this figure might help you http://fly.cc.fer.hr/~unreal/theredb...res/fig2-6.gif

Similar Threads

  1. Replies: 0
    Last Post: 10-05-2008, 10:44 PM
  2. Help with rendering a 3D surface
    By marceloc in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 06-08-2007, 10:10 AM
  3. Surface Rendering form Volume data
    By jacobl in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 11-22-2003, 01:57 PM
  4. Water surface rendering
    By ioquan in forum OpenGL: Advanced Coding
    Replies: 19
    Last Post: 09-18-2003, 02:17 AM
  5. surface Rendering
    By Rucha in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 01-16-2001, 09:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean