![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
![]() |

|
| Programming Error ! Programming error messages |
![]() |
|
Camera Movement in OPENGL
|
LinkBack | Thread Tools | Display Modes |
|
|
#1 (permalink) |
|
Fixed Error!
Posts: 4,202
Join Date: Mar 2007
Rep Power: 6
IM:
|
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 #include #include #include #include #include #include #include // 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(te 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 bathRug_bmp.Make_Std_Texture("bath_rug.b bathDoor_bmp.Make_Std_Texture("bath_door bathwalls_bmp.Make_Std_Texture("bath_wal bathceiling_bmp.Make_Std_Texture("bath_w 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; } |
|
|
|
|
|
|
|
|
#2 (permalink) |
|
Fixed Error!
Posts: 4,202
Join Date: Mar 2007
Rep Power: 6
IM:
|
#include #include #include #include #include //#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 |
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|