Camera rotation (OpenGL)

paintstripper
  • Camera rotation (OpenGL) paintstripper

    I am having trouble with a camera class I am trying to use in my program. When I change the camera_target of the gluLookAt call, my whole terrain is rotating instead of just the camera rotating like it should.

    Here is some code from my render method:

    camera->Place();
    
        ofSetColor(255, 255, 255, 255);
    
     //draw axis lines
     //x-axis
     glBegin(GL_LINES);
     glColor3f(1.0f,0.0f,0.0f);
     glVertex3f(0.0f,0.0f,0.0f);
     glVertex3f(100.0f, 0.0f,0.0f);
     glEnd();
    
     //y-axis
     glBegin(GL_LINES);
     glColor3f(0.0f,1.0f,0.0f);
     glVertex3f(0.0f,0.0f,0.0f);
     glVertex3f(0.0f, 100.0f,0.0f);
     glEnd();
    
     //z-axis
     glBegin(GL_LINES);
     glColor3f(0.0f,0.0f,1.0f);
     glVertex3f(0.0f,0.0f,0.0f);
     glVertex3f(0.0f, 0.0f,100.0f);
     glEnd();
    
     glColor3f(1,1,1);
    
     terrain->Draw();
    

    And the rotate and place methods from my camera class:

    void Camera::RotateCamera(float h, float v){
    hRadians += h;
    vRadians += v;
    
    cam_target.y = cam_position.y+(float)(radius*sin(vRadians));
    cam_target.x = cam_position.x+(float)(radius*cos(vRadians)*cos(hRadians));
    cam_target.z = cam_position.z+(float)(radius*cos(vRadians)*sin(hRadians));
    
    cam_up.x = cam_position.x-cam_target.x;
    cam_up.y = ABS(cam_position.y+(float)(radius*sin(vRadians+PI/2))) ;
    cam_up.z = cam_position.z-cam_target.z;
    }
    
    void Camera::Place() {
    //position, camera target, up vector
    gluLookAt(cam_position.x, cam_position.y, cam_position.z, cam_target.x, cam_target.y, cam_target.z, cam_up.x, cam_up.y, cam_up.z);
    }
    

    The problem is that the whole terrain is moving around the camera, whereas the camera should just be rotating.

    Thanks for any help!

  • Are you actually setting the identity matrix at any point?

    What other matrix ops are happening, and in what order?

  • From your code it looks as though your camera is stationary (cam_position doesn't change) and you just change cam_target. This will have the effect of spinning your camera around on the spot. If the only thing in your scene is your terrain then it might appear as though the terrain is rotating around the camera. Are you trying to rotate the camera around the terrain? In that case your camera target should be the center of the terrain and you should be changing cam_position to move the camera.

    Try initializing cam_target to the terrain position and then in the lines where you currently update cam_target swap cam_target and cam_position:

    cam_position.y = cam_target.y+(float)(radius*sin(vRadians));
    cam_position.x = cam_target.x+(float)(radius*cos(vRadians)*cos(hRadians));
    cam_position.z = cam_target.z+(float)(radius*cos(vRadians)*sin(hRadians));
    

Tags
c++ opengl
Related questions and answers
  • ); // Set The Color Of The Model ( NEW ) // ORIGINAL DRAWING CODE //Draw the model as an interpolation between the two frames glBegin(GL_TRIANGLES); for(int i = 0; i... NEHE'S TUT glBegin (GL_TRIANGLES); // Tell OpenGL What We Want To Draw for(int i = 0; i < numTriangles; i++) { MD2Triangle* triangle... Of The Lines ( NEW ) float outlineColor[3] = { 0.0f, 0.0f, 0.0f }; // Color Of The Lines ( NEW ) //ORIGINAL CODE if (visible) { glPushMatrix(); // Move

  • i wanna walk to terrain... first i created the terrain void desenha_terreno(float px, float pz){ for (int z = 0; z < iwidth-1; z++) { glBegin(GL_TRIANGLE_STRIP); for (int x = 0; x <..., 1.0f); glVertex3f(terreno[x+1][z+1][0], terreno[x+1][z+1][1], terreno[x+1][z+1][2]); } glEnd(); } } I can move the camera with the functions: void keyboard (unsigned char key, int...]); // draw vertex 2 glTexCoord2f(0.0f, 1.0f); glColor3f(terreno[x][z+1][1]/255.0f, terreno[x][z+1][1]/255.0f, terreno[x][z+1][1]/255.0f); glVertex3f(terreno[x][z+1][0

  • _FILTER, GL_LINEAR ); ...and the drawing code itself. glBindTexture(GL_TEXTURE_2D,thisIsMyTexture); glBegin( GL_QUADS ); glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 1.0f); glTexCoord2f(1.0f...; outside that space, white is drawn. Here is my OpenGL init code... SDL_SetVideoMode(windowWidth, windowHeight, 16, SDL_HWSURFACE|SDL_GL_DOUBLEBUFFER|SDL_OPENGL); glClearColor(0, 0, 0, 0... = SDL_DisplayFormat(zoomedImage); SDL_FreeSurface(zoomedImage); } return optimizedImage; } ...here is the code I actually use to create the GL texture... GLuint thisIsMyTexture

  • iBuffer-&gt;Unmap(); } cube.h #ifndef CUBE_H #define CUBE_H #include "gameObject.h" class Cube : GameObject { private: //VERTEX* OurVertices; public: Cube(); ~Cube(); void Draw(); void Init(); }; #endif If I call Init() from game.cpp, the code works fine, if I call cube->init(), just as this function is entered (not when the other one is left), the device pointer suddenly has an address...() { } Cube::~Cube() { pBuffer-&gt;Release(); // why does this only work when put here? because it's created here? I thnk so, why not iBuffer though? } void Cube::Draw() { render_frame(); } void Cube

  • onscreen, in the shape of the terrain rectangle, but there are no regular lines etc. Here's the code I use for rendering: void ShaderProgram::Draw() { using namespace AntiMatter; if( ! m... object from an array of CustomVertex objects. This array is said to be interleaved. It renders successfully with the following code: void VertexBufferObject::Draw() { if( ! m_bInitialized... ); glDisableClientState( GL_COLOR_ARRAY ); glBindBuffer( GL_ARRAY_BUFFER, 0 ); glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 ); } Back to the Vertex Array Object though. My code for creating the Vertex

  • I'm trying to load MD2 models (stolen from Cube) and my loader seems to be loading the models fine, but I can't say the same for the drawing of the model. Here's my code: typedef float vec3_t[3...) + ('D'<<8) + 'I') #define MD2_VERSION 8 class MD2 : public VertexModel { public: MD2(const std::string& Path); ~MD2(); void Draw(timestep_t Time); private... { glBegin(GL_TRIANGLE_STRIP); } for(; i &gt; 0; --i, Commands += 3) { md2_glcmd_t* Command = (md2_glcmd_t*)Commands

  • (GL_LIGHT0); glEnable(GL_LIGHT1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3f(0.0f, 0.0f, 0.0f); int vertexIndex = 0, normalIndex; glRotatef(90, 0, 1, 0); glPushMatrix(); for(int...Im writing a game for which, one of the models are loaded from an .obj file. It's a model of a plane, and I want to rotate the propeller. The object file is broken into groups, and the propeller..._aspect = (float)win_width / (float)win_height; glViewport(0, 0, win_width, win_height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(90, win_aspect, 1, 100.0); glMatrixMode(GL_MODELVIEW

  • -&gt;texCoordY); glVertex3f(pos[0], pos[1], pos[2]); } } glEnd(); What I'd like to do is to calculate all positions before hand, store them in a Vertex array and then draw..._COORD_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); vertices.clear(); textCoords.clear(); normals.clear(); Is there something extra that I am doing here? Cos this is really meant... at the same time: glBegin(GL_TRIANGLES); for(int i = 0; i < numTriangles; i++) { MD2Triangle* triangle = triangles + i; for(int j = 0; j < 3; j++) { MD2Vertex* v1

  • I'm having problems drawing a simple sprite. When I draw: void CSprite2D::render() { CHECKGL(glLoadIdentity()); CHECKGL(glEnable(GL_TEXTURE_2D)); CHECKGL(glEnable(GL_BLEND)); CHECKGL(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)); m_texture-&gt;bind(); //CHECKGL(glPushMatrix()); CHECKGL(glBegin(GL_TRIANGLE_STRIP...(glVertex3i( m_position.x, m_position.y + m_dimensions.y,0)); CHECKGL(glEnd()); //CHECKGL(glPopMatrix()); CHECKGL(glDisable(GL_BLEND)); } I'm always get an GL_INVALID

Data information