Question

Camera Movement in OPENGL

Asked by: srihari1986

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
Hari

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2005-05-10 at 19:31:25ID21419886
Tags

camera

,

opengl

,

movement

Topics

Game Programming

,

3D Graphics Software

Participating Experts
1
Points
500
Comments
6

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. OpenGL question
    Hi team, I downloaded Mike's (Lischke's) OpenGL stuff (really cool by the way Mike) but I can't get it to display in anything other than a sizable window. I would like to have a TPaintBox (or some control) as the area to paint on. I suspect I'm doing something daft, but I'...
  2. How to rotate a camera in opengl using delphi
    Dear all, I have written a starter application using opengl and the following tools Gl, Glu, UrickGL, U3Dpolys. I have walls appearing on the screen and can zoom in and move around the map with my mouse but would like to be able to move the camera not the image. When I try to...
  3. OpenGL, moving the camera
    I have try to write the code to move the camera around an object,but i only achieved that by using keyborad. How can i do it by using the mouse? like when u click the mouse button and at the same time move the mouse around,u will c different angle of the object. Zooming in an...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: aqua9880Posted on 2005-05-12 at 08:08:51ID: 13987356

Ok... in your display you have this:

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


Here is your explination of this... *winks*

gluLookAt(x_at_location, y_at_location, z_at_location,

 

by: aqua9880Posted on 2005-05-12 at 08:12:52ID: 13987403

whoops... heh....  Lets try that one again.

gluLookAt(x_at_location, y_at_location, z_at_location,
               x_looking_at_location, y_looking_at_location, z_looking_at_location,
               x_direction_of_up, y_direction_of_up, z_direction_of_up);

The direction of up is around a 3d unit sphere, meaning 1.0 in a single direction means that is the direction of the camera's up.  Meaning you can twist the camera having a up direction of -1.0 of Y would mean having an upside down world.  Heh...  kinda cool and fun but... *shrugs* ...unless you are doing some racing or space game there is no point of using this.  Keep possative Y as the up direction for logical purposes.

Your camera class is following, then I'll give you the updates for everything else as well.

Post back in a second,

~Aqua

 

by: aqua9880Posted on 2005-05-12 at 08:38:18ID: 13987726

Ok:  The camera class:

#include <glut.h>
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <math.h>
//#include "Points.h"

//using namespace std;

class Camera{
   private:
            
            float currentLook;
            float currentPitch;

            static const float moveBy = 1.;
            static const float lookRadius = .5;
            static const float cameraHeight = 1.5;
            static const float PI = 3.1416;
            static const float yawn = PI / 32.0;
            static const float turnVal = PI / 32.0;
            static const float pitchMax = PI / 4.0;
            static const float pitchMin = -1.0 * PI / 4.0;

      public:

              Point atLoc;
            Point looking;

            //Default class constructor sets the camera position at
            //      0, cameraHeight, 0
            //      Sets the looking position to
            //      0 PI along the distance lookRadius
            Camera()
            {
                  float x, y, z;
                  currentLook = 0.0;
                  currentPitch = 0.0;

                  Point tLoc(0.0, cameraHeight, 0.0);
                  atLoc.copy(tLoc);
                  x = atLoc.getX() + cos(currentLook) * lookRadius;
                  y = atLoc.getY();
                  z = atLoc.getZ() + sin(currentLook) * lookRadius;
                  
                  Point tLook(x, y, z);
                  looking.copy(tLook);
            }

            //Class constructor takes in tempAt for camera location
            //      and the looking angle.
            //      +X Axis is 0 PI
            //      +Z Axis is 1/2 PI
            //      -X Axis is PI
            //      -Z Axis is 3/2 PI
            Camera(Point tempAt, float angle)
            {
                  float x, y, z;
                  currentLook = angle;
                  currentPitch = 0.0;

                  atLoc.copy(tempAt);
                  x = atLoc.getX() + cos(currentLook) * lookRadius;
                  y = atLoc.getY();
                  z = atLoc.getZ() + sin(currentLook) * lookRadius;
                  
                  Point tLook(x, y, z);
                  looking.copy(tLook);
            }

            //Copy Constructor
            //      Takes in a camera and assigns a new camera
            //      to be an identical copy of the camera passed in
            Camera(Camera &a)
            {
                  currentLook = a.currentLook;
                  currentPitch = a.currentPitch;
                  
                  atLoc.copy(a.atLoc);
                  looking.copy(a.looking);      
            }

            //Takes in a direction:
            //      1 - Increases the angle (aka CCW)
            //      -1 - Decreases the angle (aka CW)
            //      value of change is turn
            void turn(int dir){
                  float x, y, z;
                  if(dir == 1){
                        currentLook += turnVal;
                  }
                  else{
                        currentLook -= turnVal;
                  }

                  x = atLoc.getX() + sin(currentLook) * lookRadius;
                  y = looking.getY();
                  z = atLoc.getZ() + cos(currentLook) * lookRadius;
                  
                  Point tLook(x, y, z);
                  looking.copy(tLook);
            }

            //Takes in a direction:
            //      1 - Increases the angle - Y Axis (aka Up)
            //      -1 - Decreases the angle (aka down)
            //      value of change is pitch
            //      Constraints:      pitchMin <= currentPitch <= pitchMax
            void pitch(int dir){
                  float x, y, z;
                  if(dir == 1){
                        if(currentPitch <= pitchMax){
                              currentPitch += yawn;
                        }
                  }
                  else{
                        if(currentPitch >= pitchMin){
                              currentPitch -= yawn;
                        }
                  }
                  
                  y = atLoc.getY() + sin(currentPitch) * lookRadius;
                  looking.setY(y);
            }

            //Sets glu Camera with values stored in this instantiation
            void updateCamera()
            {
                  gluLookAt(atLoc.getX(), atLoc.getY(), atLoc.getZ(),
                        looking.getX(), looking.getY(), looking.getZ(),
                        0.0, 1.0, 0.0);

                  if(currentLook >= (2.0 * PI) ){
                        currentLook -= (2.0 * PI);
                  }
            }

            //Takes in a direction:
            //      1 is left
            //      -1 is right
            //      Moves by moveBy units
            void strafe(int dir){
                  Point strafeDir;
                  float x, y, z;
                  float dist;
                  if(dir == 1){
                  //Strafe left
                        x = atLoc.getX() + sin(currentLook + (PI / 2.0) ) * lookRadius;
                        z = atLoc.getZ() + cos(currentLook + (PI / 2.0) ) * lookRadius;
                        dist = moveBy;
                  }
                  else{
                  //Strafe right
                        x = atLoc.getX() + sin(currentLook + (PI / -2.0) ) * lookRadius;
                        z = atLoc.getZ() + cos(currentLook + (PI / -2.0) ) * lookRadius;
                        dist = moveBy;
                  }
                  Point sDir(x, y, z);
                  strafeDir.copy(sDir);
                  atLoc.setX(atLoc.getX() + dist * (strafeDir.getX() - atLoc.getX()) );
                  atLoc.setZ(atLoc.getZ() + dist * (strafeDir.getZ() - atLoc.getZ()) );

                  x = atLoc.getX() + sin(currentLook) * lookRadius;
                  y = looking.getY();
                  z = atLoc.getZ() + cos(currentLook) * lookRadius;

                  Point tLook(x, y, z);
                  looking.copy(tLook);
            }

            void altitude(int dir){
                  if(dir == 1){
                        looking.setY(looking.getY() + 1.0);
                        atLoc.setY(atLoc.getY() +1.0);
                  }
                  else{
                        looking.setY(looking.getY() - 1.0);
                        atLoc.setY(atLoc.getY() - 1.0);
                  }
            }      

      
            //Takes in a direction:
            //      1 is forward
            //      -1 is reverse
            //      Moves by moveBy units
            void walk(int dir){
                  float x, y, z;
                  float dist;
                  if(dir == 1){
                        dist = moveBy * 1.0;
                  }
                  else{
                        dist = moveBy * -1.0;
                  }

                  atLoc.setX(atLoc.getX() + dist * (looking.getX() - atLoc.getX()) );
                  atLoc.setZ(atLoc.getZ() + dist * (looking.getZ() - atLoc.getZ()) );

                  x = atLoc.getX() + sin(currentLook) * lookRadius;
                  y = looking.getY();
                  z = atLoc.getZ() + cos(currentLook) * lookRadius;

                  Point tLook(x, y, z);
                  looking.copy(tLook);
            }

            //Accessor for At Location
            //Stored in a Point class
            Point getAt(){ return atLoc; }

            //Accessor for Looking Location
            //Stored in a Point class
            Point getLooking() { return looking; }

            float getLookAngle() {return currentLook; }

            void setLook(Point a)
            {
                  looking.copy(a);
            }
            void setAt(Point a)
            {
                  atLoc.copy(a);
            }

};



You DO need my points class which is this:

class Point{
   private:
      float x;
      float y;
      float z;
   public:
      float pt[3];
      Point():x(0), y(0), z(0){/*Empty*/};
      Point(float xT, float yT, float zT):x(xT), y(yT), z(zT)
      {
         pt[0] = x;
         pt[1] = y;
         pt[2] = z;  
      }
        Point(const Point &a){
             x = a.x;
             y = a.y;
             z = a.z;
        }
      float getX(){return x;};
      float getY(){return y;};
      float getZ(){return z;};
      void setX(float xT){x = xT;};
      void setY(float yT){y = yT;};
      void setZ(float zT){z = zT;};
      void copy(Point a);      
};

void Point::copy(Point a)
{
   x = a.x;
   y = a.y;
   z = a.z;    
}

Then you need to add this into your keyboard switch case:

//Strafe Left
                        case '7':
                              view.strafe(1);
                              break;
                        //Strafe Right
                        case '9':
                              view.strafe(-1);                              
                              break;
                        //Look up
                        case '8':
                              view.pitch(1);                                                                                              break;
                        //Look down
                        case '5':
                              view.pitch(-1);
                              break;
                        //Turn left
                        case '4':
                              view.turn(1);
                              break;
                        //Turn right
                        case '6':
                              view.turn(-1);
                              break;
                        //Move foreward
                        case '0':
                              view.walk(1);
                              break;
                        //Move backward
                        case '1':
                              view.walk(-1);
                              break;
                        case 'q':
                              view.altitude(1);
                              break;
                        case 'a':
                              view.altitude(-1);
                              break;

Do away with that control function you copied from my player class, it doesn't work that way.  Then instead of your gluLookAt call in your display function you want view.updateCamera();

;-)

That should be everything you need, possibly some minor debugging to do because I dont have time to actually try to compile this and do it for you but that is the general idea.  For a networking guy never before using C++ and OpenGL you did pretty good.  :-P

If you have any problems ask away.

~Aqua

 

by: srihari1986Posted on 2005-05-12 at 17:47:28ID: 13992364

Aqua,

Really thanks for your help.will go home and do it.


Thanks
Hari

 

by: srihari1986Posted on 2005-05-13 at 10:03:15ID: 13997708

Hi Aqua,

Thanks for your wonderful timely help.Can you just tell me some info..How do i like make the camra default to center of the room.And also write a IF loop so that the user does not leave the room....

And how and where should  i write the IF loop and how should the i loop contructor be.

Regards
Hari

 

by: aqua9880Posted on 2005-05-13 at 14:35:13ID: 13999896

Hari that is getting into a subject and topic called collision detection...  ;-)   ...You can use the distance from the line to the player can not become .5 or such... OR you can use your camera look at and the camera location within the camera class and make sure that those points never intersect a bounding line.  Feel free to ask about collision detection... although dave may just beat me to this one... he has a fair amount of knowledge on the subject...  You can also make it so you cannot intersect your objects either.  :-D

Plus you write your intersection equations before you update the camera or within the camera.

~Aqua

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...