Advertisement

05.10.2005 at 07:31PM PDT, ID: 21419886
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.6

Camera Movement in OPENGL

Asked by srihari1986 in Game Programming, 3D Graphics Software

Tags: , ,

Hi Auqa,

Below is my code for creation of walls and apply texture to it.All the methods below in the class are used to create the walls in a polygon grid (10 X 10) format.And also create a ambient light for Day and Night effect.Now the only thing i need to do i put in a camera so that the user can move only inside the room.

Sorry for my silly question.As i am not programmer basically and i am working in networking feild.And this is the first time i am using C++ and opengl.


#ifndef WIN32
#define WIN32
#endif
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iostream.h>


// included to enable using of images
#include "textureclass.cpp"
#include "Camera.h"

    Camera view;

    // The below 2 vriables are for the rotation
    static GLfloat spin = 0.0;
    int ifFound=0;

       // Variable to enable and disable night mode.
   // nightMode - false, enables global ambient
   // nightMode - true, disables global ambient
   static bool nightMode = false;

   GLUquadric* quad;

   float xtmp;


   // The viewing position of the camera
   static GLdouble xPos = 0.0;
   static GLdouble yPos = 0.0;  
   static GLdouble zPos = 5.0;

   // Texture Maps are declared
   static Texture_Image wall_bmp;
   static Texture_Image bathRug_bmp;
   static Texture_Image bathDoor_bmp;
   static Texture_Image bathwalls_bmp;
   static Texture_Image bathceiling_bmp;


void draw_backwall(void)
{
      // Store all the Values of the vertices x,y,z and the diffenrce between the
        // first and the consecutive polygon.
      float x= 0.978024;
        float y= 1.00523;
        float z = 0;
      float diffy = 0.197046;
      float diffx = 0.196046;
      

      
       for(int i=1;i<=10;i++)
      {
            x= 0.978024;
            for(int j=1;j<=10;j++)
            {
                  glBegin (GL_POLYGON);
                  glVertex3f(x,y,z);
                  glVertex3f(x,y-diffy,z);
                  glVertex3f(x-diffx,y-diffy,z);
                  glVertex3f(x-diffx,y,z);
                  glEnd();
                  x=x-diffx;
            }
                        y=y-diffy;
      }
            
}


void draw_ceilingwall(void)
{
      // Store all the Values of the vertices x,y,z and the diffenrce between the
        // first and the consecutive polygon.
      float x= -0.982438;
        float y= 1.005;
        float z =  0.206816;
      float diffz = 0.197046;
      float diffx = -0.196046;

                      glBindTexture( GL_TEXTURE_2D, bathceiling_bmp.Get_Texture() );
                      float fwidth, fheight;            
                        int width, height;
                        bathceiling_bmp.Get_Fraction_Used ( &fwidth, &fheight);
                        glEnable(GL_TEXTURE_2D);
      
       for(int i=1;i<=10;i++)
      {
            x= -0.982438;
            for(int j=1;j<=10;j++)
            {
                                    
                  glBegin (GL_POLYGON);
                  glTexCoord2f(1.0f, 0.0f); glVertex3f(x-diffx,y,z);
                  glTexCoord2f(1.0f, 1.0f); glVertex3f(x-diffx,y,z-diffz);
                  glTexCoord2f(0.0f, 1.0f); glVertex3f(x,y,z-diffz);
                  glTexCoord2f(0.0f, 0.0f); glVertex3f(x,y,z);
                  glEnd();
                  x=x-diffx;
      
            }
                        z=z+diffz;
      }
                  glDisable(GL_TEXTURE_2D);            
}


void draw_floorwall(void)
{
      // Store all the Values of the vertices x,y,z and the diffenrce between the
        // first and the consecutive polygon.
 
      float x= -0.786392;
        float y= -0.97277;
        float z = 1.765194;
      float diffz = 0.197046;
      float diffx = 0.196046;
      


       for(int i=1;i<=10;i++)
      {
                  glBindTexture( GL_TEXTURE_2D, wall_bmp.Get_Texture() );
                      float fwidth, fheight;            
                        int width, height;
                        wall_bmp.Get_Fraction_Used ( &fwidth, &fheight);
                        glEnable(GL_TEXTURE_2D);      
            x= -0.786392;
            for(int j=1;j<=10;j++)
            {
                                    
                  glBegin (GL_POLYGON);
                  glTexCoord2f(1.0f, 0.0f); glVertex3f(x,y,z);
                  glTexCoord2f(1.0f, 1.0f); glVertex3f(x,y,z+diffz);
                  glTexCoord2f(0.0f, 1.0f); glVertex3f(x-diffx,y,z+diffz);
                  glTexCoord2f(0.0f, 0.0f); glVertex3f(x-diffx,y,z);
                  glEnd();
                  x=x+diffx;
      
            }
                        z=z-diffz;
            
      }
                  glDisable(GL_TEXTURE_2D);                  
}



void draw_rightwall(void)
{
      // Store all the Values of the vertices x,y,z and the diffenrce between the
        // first and the consecutive polygon.
   
      float x= 0.979804;
        float y= -0.768184;
        float z = 1.76026;
      float diffy = 0.197046;
      float diffz = -0.19605;
      
      glBindTexture( GL_TEXTURE_2D, bathwalls_bmp.Get_Texture() );
          float fwidth, fheight;
            int width, height;
            bathwalls_bmp.Get_Fraction_Used ( &fwidth, &fheight);
      glEnable(GL_TEXTURE_2D);
      
       for(int i=1;i<=10;i++)
      {
            z= 1.76026;
            for(int j=1;j<=10;j++)
            {
                                    
                  glBegin (GL_POLYGON);
                  glTexCoord2f(1.0f, 0.0f); glVertex3f(x,y,z);
                  glTexCoord2f(1.0f, 1.0f); glVertex3f(x,y-diffy,z);
                  glTexCoord2f(0.0f, 1.0f); glVertex3f(x,y-diffy,z-diffz);
                  glTexCoord2f(0.0f, 0.0f); glVertex3f(x,y,z-diffz);
                  glEnd();
                  z=z+diffz;
      
            }
                        y=y+diffy;
                  
      }
                  glDisable(GL_TEXTURE_2D);
}

void draw_leftwall(void)
{
      // Store all the Values of the vertices x,y,z and the diffenrce between the
        // first and the consecutive polygon.

      glBindTexture( GL_TEXTURE_2D, bathwalls_bmp.Get_Texture() );
          float fwidth, fheight;
            int width, height;
            bathwalls_bmp.Get_Fraction_Used ( &fwidth, &fheight);
      glEnable(GL_TEXTURE_2D);
 
   
      float x= -0.990528;
        float y= 1.00523;
        float z = -0.0160207;
      float diffy = 0.197046;
      float diffz = -0.1960457;
      
       for(int i=1;i<=10;i++)
      {
            z= -0.0160207;
            for(int j=1;j<=10;j++)
            {
                                    
                  glBegin (GL_POLYGON);
                  glTexCoord2f(1.0f, 0.0f); glVertex3f(x,y,z);
                  glTexCoord2f(1.0f, 1.0f); glVertex3f(x,y-diffy,z);
                  glTexCoord2f(0.0f, 1.0f); glVertex3f(x,y-diffy,z-diffz);
                  glTexCoord2f(0.0f, 0.0f); glVertex3f(x,y,z-diffz);
                  glEnd();
                  z=z-diffz;
      
            }
                        y=y-diffy;
      }
      glDisable(GL_TEXTURE_2D);
}

void draw_mat(void)
{

                        glBindTexture( GL_TEXTURE_2D, bathRug_bmp.Get_Texture() );
          float fwidth, fheight;
            int width, height;
            bathRug_bmp.Get_Fraction_Used ( &fwidth, &fheight);
                  glEnable(GL_TEXTURE_2D);
                  glBegin (GL_POLYGON);
                  glTexCoord2f(1.0f, 0.0f); glVertex3f(0.41,-0.96,0.05);
                  glTexCoord2f(1.0f, 1.0f); glVertex3f(0.95,-0.96,0.05);
                  glTexCoord2f(0.0f, 1.0f); glVertex3f(0.95,-0.96,0.38);
                  glTexCoord2f(0.0f, 0.0f); glVertex3f(0.41,-0.96,0.38);
                  glEnd();
                  glDisable(GL_TEXTURE_2D);
}

void draw_door(void)
{

                        glBindTexture( GL_TEXTURE_2D, bathDoor_bmp.Get_Texture() );
                            float fwidth, fheight;
                              int width, height;
                              bathDoor_bmp.Get_Fraction_Used ( &fwidth, &fheight);
                              glEnable(GL_TEXTURE_2D);
                  glBegin (GL_POLYGON);
                  glTexCoord2f(1.0f, 0.0f); glVertex3f(0.25,0.86,0.01);
                  glTexCoord2f(1.0f, 1.0f); glVertex3f(0.95,0.86,0.01);
                  glTexCoord2f(0.0f, 1.0f); glVertex3f(0.95,-0.96,0.01);
                  glTexCoord2f(0.0f, 0.0f); glVertex3f(0.25,-0.96,0.01);
                  glEnd();
                  glDisable(GL_TEXTURE_2D);
}

// method to setup the lighting
   void set_light ( void)
   {


        // controlling the ambient light
        GLfloat ambientVal;
        if(!nightMode)
              ambientVal = 2.0;
        else
                ambientVal = 0.2;

        GLfloat lmodel_ambient[] = {ambientVal,ambientVal,ambientVal, 1.0 };
        glClearColor(0.0,0.0,0.0,1.0);
        glShadeModel(GL_SMOOTH);

      // enable ambient light
        glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);

            glLightModeli ( GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE );
      glLightModeli ( GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE );

       // enable all the lighting
        glEnable ( GL_LIGHTING ); // ambient
        glEnable (GL_DEPTH_TEST);
        glEnable(GL_COLOR_MATERIAL); // to enable proper transparency and color after the lighting
   }

void control(unsigned char key, int x, int y){
               Camera temp(view);
               Point a;
               
               //Point blah = temp.getLooking();
               switch(key){
                    //Strafe Left
                    case '7':
                         temp.strafe(1);
                         a.copy(temp.getLooking());
                         
                         break;
                    //Strafe Right
                    case '9':
                         temp.strafe(-1);
                         a.copy(temp.getLooking());
                         
                         break;
                    //Look up
                    case '8':
                         temp.pitch(1);
                         a.copy(temp.getLooking());              
                         break;
                    //Look down
                    case '5':
                         temp.pitch(-1);
                         a.copy(temp.getLooking());
                         break;
                    //Turn left
                    case '4':
                         temp.turn(1);
                         a.copy(temp.getLooking());
                         break;
                    //Turn right
                    case '6':
                         temp.turn(-1);
                         a.copy(temp.getLooking());
                         break;
                    //Move foreward
                    case '0':
                         temp.walk(1);
                         a.copy(temp.getLooking());
                         break;
                    //Move backward
                    case '1':
                         temp.walk(-1);
                         a.copy(temp.getLooking());
                         break;
                    case 'q':
                         temp.altitude(1);
                         break;
                    case 'a':
                         temp.altitude(-1);
                         break;
                    default:
                         break;
             }

                    bool x1 = false;
                temp.atLoc.setY(Collision::checkScene(temp.getAt()));
                    view.upd(temp,a,x1);
                    view.setStatic();
             glutPostRedisplay();
          }



void init(void)
{
     //glClearColor (.5,1,1,1); // Black is the Background
     glShadeModel (GL_SMOOTH); // The Shade Model is smooth , to create the reality.

      glClearColor (1,1,1, 1.0);
      glClearDepth (1.0);

     // set all the textures
      wall_bmp.Make_Std_Texture("wall_back.bmp",true,GL_BLEND);
      bathRug_bmp.Make_Std_Texture("bath_rug.bmp",true,GL_DECAL);
      bathDoor_bmp.Make_Std_Texture("bath_door.bmp",true,GL_DECAL);
      bathwalls_bmp.Make_Std_Texture("bath_walls.bmp",true,GL_DECAL);
      bathceiling_bmp.Make_Std_Texture("bath_walls.bmp",true,GL_DECAL);

      glShadeModel (GL_FLAT);
      quad = gluNewQuadric();
      glShadeModel (GL_SMOOTH);
}


void display(void)
{
      
     set_light(); // Call the light
      // clear colour and depth buffer(window)
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glLoadIdentity();
      glPushMatrix();
         

      // current position of viewer x,y,z
       gluLookAt ( xPos, yPos, zPos, 0., 0., 0., 0., 1.0, 0. );


      // Enabling depth testing
      glEnable (GL_DEPTH_TEST);
      
      
            glScalef(1.55,1.55,1.55);
      // Draw the Back Wall
      draw_backwall();

      // Draw the roof of the room.
      draw_ceilingwall();

      // Draw the floor of the wall
      draw_floorwall();

      // Draw the right Wall
      draw_rightwall();

      
        // Draw the left Wall
      draw_leftwall();
      
      // Draw the mat at the entrance
      draw_mat();
      
      // Draw the door the bathrroom
      draw_door();

   glutSwapBuffers();

}

   void reshape(int width, int height)
   {


           // Viewport is set to the same size of the window.
      glViewport (0, 0, (GLsizei) width, (GLsizei) height);

           // Sets the Matrix mode to Projection
      glMatrixMode(GL_PROJECTION);

      glLoadIdentity();

           // Program uses a perspective projection
      // Parameters: field of view (in angle degrees), aspect ratio, distance to near clipping plane,
      // distance to far cutoff plane

      gluPerspective(75.0, 1.0, 0.5, 1500.0);

           // Sets the Matrix mode to Modelview
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();

   }


   
  void keyboard(unsigned char key, int x, int y)
  {
      switch (key) {

       case 'o':
          nightMode = false;
          break;

       case 'O':
          nightMode = true;
          break;

           default:
            return;
            break;
       }
          glutPostRedisplay ();
  }

   int main(int argc, char** argv)
   {
      glutInit(&argc, argv);
      glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);            //set up window (double buffering, colour setup)
      glutInitWindowSize (700, 700);            // Set initial window size to 700 x 700
      glutInitWindowPosition (10, 10);      // Set initial window position of project
      glutCreateWindow (argv[0]);            // Create the application window

      init();

      glutDisplayFunc(display);

      glutKeyboardFunc(keyboard);
      glutReshapeFunc(reshape);

      glutMainLoop();
      return 0;
   }


Regards
HariStart Free Trial
[+][-]05.12.2005 at 08:08AM PDT, ID: 13987356

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.12.2005 at 08:12AM PDT, ID: 13987403

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.12.2005 at 08:38AM PDT, ID: 13987726

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Game Programming, 3D Graphics Software
Tags: camera, opengl, movement
Sign Up Now!
Solution Provided By: aqua9880
Participating Experts: 1
Solution Grade: A
 
 
[+][-]05.12.2005 at 05:47PM PDT, ID: 13992364

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.13.2005 at 10:03AM PDT, ID: 13997708

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.13.2005 at 02:35PM PDT, ID: 13999896

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20081112-EE-VQP-44