OpenGL/GLUT Question

This is a discussion on OpenGL/GLUT Question within the Game Programming forums, part of the General Programming Boards category; I wasn't completely sure where to post this as it's graphics related but not game related and I have posted ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    208

    OpenGL/GLUT Question

    I wasn't completely sure where to post this as it's graphics related but not game related and I have posted it in C programming to no avail. I figure I would get a quicker and more likely better response in this forum however.

    What I am trying to do is read in points and plot them as spheres (using glut) in 3-D space for a visualization of particles for my physics research. Reading in points and displaying the spheres was no problem (mind you I am new to openGL programming) but I can't seem to set my camera up properly no matter how much I fiddle with it. Heres the case, I am plotting an fcc crystal which is contained in a 6x6x18 box, the bottom left corner of said box being located at the origin. I want to be able to get a good view of this box. I have read various tutorials on camera control in OpenGL and in GLUT and have tried both gluLookAt(); and just translating and rotating; so I am not sure if its just my 3-D math or my understanding of the functions that is off but for some reason I am always zoomed in too far, or can't see anything, the camera never seems to be oriented where I expect. Here is my most recent display function, I was under the assumption that this would locate my camera at (10,10,0) and point it towards the origin.



    Code:
    void display(void)
    {    
         
    //****** SET THE SYSTEM UP TO BE A 6X6X18 BOX ********************************//     
         glMatrixMode(GL_PROJECTION);
         glOrtho(0.0,6.0,0.0,6.0,0.0,25.0);
    
    //****** CLEAR BUFFERS **************************************************  *****//     
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        
    //****** SETUP THE CAMERA ANGLE*********************************************  **// 
         
         glMatrixMode(GL_PROJECTION);
    
         glLoadIdentity();
         gluLookAt(0.0,0.0,0.0,10.0,10.0,0.0,0.0,1.0,0.0);
    
                                               
         glMatrixMode(GL_MODELVIEW); 
                                     
    //****** DRAW THE PARTICLES*****************************************  **********//     
        for (int i=0;i<NUM_PARTICLES;i++){DrawParticle(XYZ[i].x,XYZ[i].y,XYZ[i].z);}
    
        
        glFlush(); 
    }

    Now I know for a fact everything gets drawn, and drawn correctly because from the various angles I have experimented with (none which give me close to what I want) the crystal structure looks correctly ordered. I was hoping someone could maybe provide a solution or even a good tutorial on camera control which could help me understand how to orient things in openGL. (I have tried nehe.gamedev, along with many others)

    Thanks
    - J. Paddon
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    208
    I thought it might help if I posted all the code and attatched the input txt file. I want the crystal to look something like THIS (only mine has different dimensions)

    Code:
    #include <windows.h>
    #include <GL/glut.h>
    #include <stdio.h>
    
    //****** DEFINE CONSTANTS AND THE POSITION STRUCTURE *************************//
    
    #define NUM_PARTICLES 2592 
    
    struct Position{
     float x;
     float y;
     float z;
    };
    
    //******* GLOBAL VARIABLES ***************************************************//
    
    Position XYZ[NUM_PARTICLES];
    
    
    
    //****** DRAWPARTICLE() A SPHERE AT THE POINT USING GLUT *********************//
    
    void DrawParticle(double x, double y, double z){
        glLoadIdentity();
        glPushMatrix();
        glTranslatef(x,y,z);
        glutSolidSphere(0.3,24,24);
        glPopMatrix();    
    }
    
    
    //****** READDATA() OPENS THE COORDINATES FILE AND STORES THEM ***************//
    
    void ReadData() {
       FILE *file;
       file=fopen("001.txt","r");
       
       for(int i=0;i<NUM_PARTICLES;i++){
                          
                  fscanf(file," %f %f %f \n", &XYZ[i].x,&XYZ[i].y,&XYZ[i].z);
               
               
       }
          fclose(file);
    }
    
    //****** MYINIT() INITALIZES THE OPENGL ENVIROMENT ***************************//
    
    void myInit(void)
    {
    
    //****** Initalize Lighting and Turn on Lights for the scene *****************//
        glEnable(GL_LIGHTING);                                                    //                      
        glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER,1.0);                           //                       
           GLfloat lightIntensity[]={0.8,0.8,0.8, 0.8};                           //                
           GLfloat lightSpecular[]={1.00,1.0,1.0, 1.0};                           //                
           GLfloat lightPosition[]={0.0,5.00,0.0,1.00};                           //                 
           GLfloat lightAmbient[]={0.50,0.50,0.50,1.0};                           //
                                                                                  //
           glLightfv(GL_LIGHT0, GL_DIFFUSE, lightIntensity);                      //
           glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpecular);                      //
           glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);                      //
           glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);                        //
        glEnable(GL_LIGHT0);                                                      //
        glDepthFunc(GL_LEQUAL);                                                   //  
        glEnable(GL_DEPTH_TEST);                                                  //    
    }                                                                             //
    //****************************************************************************//
    
    //****** ALL THE DRAWING CODE GOES INTO DISP() *******************************//
    
    void display(void)
    {    
         
    //****** SET THE SYSTEM UP TO BE A 6X6X18 BOX ********************************//     
         glMatrixMode(GL_PROJECTION);
         glOrtho(0.0,6.0,0.0,6.0,0.0,25.0);
    
    //****** CLEAR BUFFERS *******************************************************//     
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        
    //****** SETUP THE CAMERA ANGLE***********************************************// 
         
         glMatrixMode(GL_PROJECTION);
    
         glLoadIdentity();
         gluLookAt(0.0,0.0,0.0,10.0,10.0,0.0,0.0,1.0,0.0);
    
                                               
         glMatrixMode(GL_MODELVIEW); 
                                     
    //****** DRAW THE PARTICLES***************************************************//     
        for (int i=0;i<NUM_PARTICLES;i++){DrawParticle(XYZ[i].x,XYZ[i].y,XYZ[i].z);}
    
        
        glFlush(); 
    }
    
    
    //****** RESHAPE FUNCTION TO MAINTAIN ASPECT RATIOS **************************// 
    //******         WHEN THE WINDOW IS RESIZED         **************************//
    //****** FOR THE MOST PART COPY AND PASTED FROM VARIOUS TUTORIALS ************//  
    void myReshape(GLsizei w, GLsizei h)                                          //
    {                                                                             // 
        glViewport(0, 0, w, h);                                                   //
        glMatrixMode(GL_PROJECTION);                                              //
        glLoadIdentity();                                                         //
        if (w <= h)                                                               //
            glOrtho (-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,                       //
                1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);                          //
        else                                                                      //
            glOrtho (-1.5*(GLfloat)w/(GLfloat)h,                                  //
                1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);               //
        glMatrixMode(GL_MODELVIEW);                                               //
        glLoadIdentity();                                                         //
    }                                                                             //
    //****** END RESIZE FUNCTION**************************************************// 
    
    
    int main(int argc, char** argv)
    {
        ReadData();
        glutInit(&argc, argv);
        glutInitDisplayMode (GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH);
        glutInitWindowPosition (10, 10);
        glutInitWindowSize (500,500);
        glutCreateWindow("Visualization Of Crystal Face Structures");
        myInit();
        glutReshapeFunc (myReshape);
        glutDisplayFunc(display2);
        glutMainLoop();
    }
    Attached Files Attached Files
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    42
    So you just want a good view of all the spheres, right?

    Well I think your main problem is that glLoadIdentity() function in drawparticle().

    By getting rid of that and changing display(void) to:

    Code:
    void display(void)
    {    
         
    //****** SET THE SYSTEM UP TO BE A 6X6X18 BOX ********************************//     
         glMatrixMode(GL_PROJECTION);
         glOrtho(0.0,6.0,0.0,6.0,0.0,25.0);
    
    //****** CLEAR BUFFERS **************************************************  *****//     
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        
    //****** SETUP THE CAMERA ANGLE*********************************************  **// 
         
         //glMatrixMode(GL_PROJECTION);
    
      //  glLoadIdentity();
      //  gluLookAt(0.0,0.0,0.0,10.0,10.0,0.0,0.0,1.0,0.0);
    
                                               
       // glMatrixMode(GL_MODELVIEW); 
                                     
                                     
    //****** DRAW THE PARTICLES*****************************************  **********// \
     
       
        for (int i=0;i<NUM_PARTICLES;i++){DrawParticle(XYZ[i].x,XYZ[i].y,XYZ[i].z);}
     
        
        glFlush(); 
    }

    ...I obtained this view:

    http://img.villagephotos.com/p/2004-...04/spheres.JPG

    Is that kind of what you wanted?

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    208
    Yes and no, I kinda of want to look at it isometrically like in the picture I linked too in my previous post. I will try adn what you said and see if I can make it work. But if you had any suggestions about how I could orient it so I could see 3 sides at once and have the entire structure within the window that would be amazing.


    Thanks
    - J. Paddon
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    208
    I got it, it was due to the glLoadIdentity(); functions I was calling within DrawParticle. Now I was wondering if maybe someone could help me out with a small lighting issue. It would be nice to have this lit up a lot clearer. I have posted a scree shot of the output.

    Code:
    void myInit(void)
    {
    
    //****** Initalize Lighting and Turn on Lights for the scene***************//
        glEnable(GL_LIGHTING);                                                    //                      
        glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER,1.0);                           //                       
           GLfloat lightIntensity[]={0.8,0.8,0.8, 0.8};                           //                
           GLfloat lightSpecular[]={1.00,1.0,1.0, 1.0};                           //                
           GLfloat lightPosition[]={0.0,5.00,0.0,1.00};                           //                 
           GLfloat lightAmbient[]={0.50,0.50,0.50,1.0};                           //
                                                                                  //
           glLightfv(GL_LIGHT0, GL_DIFFUSE, lightIntensity);                      //
           glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpecular);                      //
           glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);                      //
           glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);                        //
        glEnable(GL_LIGHT0);                                                      //
        glDepthFunc(GL_LEQUAL);                                                   //  
        glEnable(GL_DEPTH_TEST);                                                  //    
    }                                                                             //
    //*************************************************************************//
    Attached Images Attached Images  
    Last edited by kas2002; 05-25-2006 at 12:15 PM.
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    42
    Heh I don't know how you got that, but whatever.


    Perhaps give the spheres and background a different color...?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 03:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 06:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21