Results 1 to 4 of 4

Thread: camera position

  1. #1
    Member Contributor
    Join Date
    Oct 2008
    Posts
    63

    camera position

    I have my display function which displays my skymap and car.
    Code :
    void display(void) {
     
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	glLoadIdentity();
        glTranslatef(0.0,0.0,-3.0); 
     
     
    	aSkyBox.draw();	
     
    	glPushMatrix();
    	glScalef(0.1,0.1,0.1); // too make it fit in my skybox, calculated by finding the models co ordinates and comparing to skymaps
    	glTranslatef(0.0,0.0,-300.0);
    	glTranslatef(0.0,-100.0,0.0);
    	glTranslatef(-100.0,0.0,0.0);
    	glRotatef(270.0f,1.0f,0.0f,0.0f);
     
    	aSkyBox.draw_car();
    	glPopMatrix();
    	glFlush(); 
        glutSwapBuffers(); 
    }

    At the moment, the view is a bit too high up in the skybox, i want the camera to be more at ground level. How would i do this? Is it with glTranslate?

  2. #2
    Senior Member Frequent Contributor _NK47's Avatar
    Join Date
    Mar 2008
    Posts
    574

    Re: camera position

    theory: the model matrix and camera matrix are concatenated in OpenGL (GL_MODELVIEW). you can imagine that the camera is always at the origin and never moving. only the objects are moving to or away from the camera which actually makes no difference and looks like the camera is moving instead. if you need to move your camera down by some amount modify your matrix by the opposite amout (up) so the geometry you render will be transformed up and will look like the camera has been moved down. hope you can follow.

  3. #3
    Member Contributor
    Join Date
    Oct 2008
    Posts
    63

    Re: camera position

    Sorry, tottaly lost, have no idea about all these matrixes and everything. How can i increase the number on GL_MODELVIEW?
    I have this where i draw my car and skybox
    Code :
    #include "stdafx.h"
    #include <windows.h>
    #include <stdio.h>  //for file access	
    #include <GL/glut.h>						
    #include <gl\gl.h>													
    #include "skybox.h"
    #include "glbmp.h"
    #include "coursework2.h"
    #include "3dsloader.h"
    #include "texture.h"
     
    obj_type object;
     
    bool skybox::LoadTexture(unsigned int & aTexture, const char * bitmap_file)
    {
       glbmp_t bitmap;     //object to fill with data from glbmp
     
     
       if(!glbmp_LoadBitmap(bitmap_file, 0, &amp;bitmap))
       {
          fprintf(stderr, "Error loading bitmap file: %s\n", bitmap_file);
       }
     
       //generate and bind the OpenGL texture
       glGenTextures(1, &amp;aTexture);
       glBindTexture(GL_TEXTURE_2D, aTexture);
     
       //copy data from bitmap into texture
       glTexImage2D(GL_TEXTURE_2D, 0, 3, bitmap.width, bitmap.height,
                    0, GL_RGB, GL_UNSIGNED_BYTE, bitmap.rgb_data);
     
       //set up texture filtering
       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     
       //free the bitmap
       glbmp_FreeBitmap(&amp;bitmap);
     
       return true;
    }
     
     
     
    void skybox::Init() {
     
    	OFFSET = 1/512.0; //set offset value for skybox
     
    	LoadTexture(texture[0], "images/1.bmp");
    	LoadTexture(texture[1], "images/2.bmp");
    	LoadTexture(texture[2], "images/3.bmp");
    	LoadTexture(texture[3], "images/4.bmp");
    	LoadTexture(texture[4], "images/5.bmp");
    	LoadTexture(texture[5], "images/6.bmp");
     
    }
     
    void skybox::Init2(){
     
        glClearColor(0.0, 0.0, 0.0, 0.0); // This clear the background color to black
        glShadeModel(GL_SMOOTH); // Type of shading for the polygons
     
        // Viewport transformation
        glViewport(0,0,800,600);  
     
        // Projection transformation
        glMatrixMode(GL_PROJECTION); // Specifies which matrix stack is the target for matrix operations 
     
     
        glEnable(GL_DEPTH_TEST); // enable the depth test (also called z buffer)
        glPolygonMode (GL_FRONT_AND_BACK, GL_FILL); // Polygon rasterization mode (polygon filled)
     
        glEnable(GL_TEXTURE_2D); // This Enable the Texture mapping
     
        Load3DS (&amp;object,"GTR.3ds");
     
        object.id_texture=LoadBitmap("images/body.bmp");
     
     
    }
     
    void skybox::draw() {
     
    	glPushMatrix();
     
    	glColor3f(1.0f,1.0f,1.0f);			//Set colour to White
    	glEnable(GL_TEXTURE_2D);			//Enable texture mapping
    	glDisable (GL_DEPTH_TEST);			//Disable depth testing
     
    		//before you can use a texture you have to bind it
    		glBindTexture(GL_TEXTURE_2D, texture[0 + text_offset]);
     
    		glBegin (GL_QUADS);	
     
    				glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET,0.0 + OFFSET);	glVertex3d (-10.0,-10.0,10.0);
    				glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET,0.0 + OFFSET);	glVertex3d (-10.0,-10.0,-10.0);
    				glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET,1.0 - OFFSET);	glVertex3d (-10.0,10.0,-10.0);
    				glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET,1.0 - OFFSET);	glVertex3d (-10.0,10.0,10.0);
     
    		glEnd ();				
     
    		glBindTexture(GL_TEXTURE_2D, texture[1 + text_offset]);
    		glBegin (GL_QUADS);
     
    				glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET,0.0 + OFFSET);	glVertex3d (-10.0,-10.0,-10.0);
    				glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET,0.0 + OFFSET);	glVertex3d (10.0,-10.0,-10.0);
    				glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET,1.0 - OFFSET);	glVertex3d (10.0,10.0,-10.0);
    				glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET,1.0 - OFFSET);	glVertex3d (-10.0,10.0,-10.0);
    		glEnd ();
     
    		glBindTexture(GL_TEXTURE_2D, texture[2 + text_offset]);
    		glBegin (GL_QUADS);
     
    				glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET ,0.0 + OFFSET); glVertex3d (10.0,-10.0,-10.0);
    				glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET ,0.0 + OFFSET); glVertex3d (10.0,-10.0,10.0);
    				glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET ,1.0 - OFFSET); glVertex3d (10.0,10.0,10.0);
    				glNormal3d (0.0,0.0,10.0); glTexCoord2d (0.0 + OFFSET ,1.0 - OFFSET); glVertex3d (10.0,10.0,-10.0);
     
    		glEnd ();
     
    		glBindTexture(GL_TEXTURE_2D, texture[3 + text_offset]);	
    		glBegin (GL_QUADS);
     
    			glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET ,0.0 + OFFSET); glVertex3d (10.0,-10.0,10.0);
    			glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET ,0.0 + OFFSET); glVertex3d (-10.0,-10.0,10.0);
    			glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET ,1.0 - OFFSET); glVertex3d (-10.0,10.0,10.0);
    			glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET ,1.0 - OFFSET); glVertex3d (10.0,10.0,10.0);
     
    		glEnd ();
     
    		glBindTexture(GL_TEXTURE_2D, texture[4 + text_offset]);	
    		glBegin (GL_QUADS);
     
    			glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET ,0.0 + OFFSET); glVertex3d (-10.0,-10.0,10.0);
    			glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET ,0.0 + OFFSET); glVertex3d (10.0,-10.0,10.0);
    			glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET ,1.0 - OFFSET); glVertex3d (10.0,-10.0,-10.0);
    			glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET ,1.0 - OFFSET); glVertex3d (-10.0,-10.0,-10.0);
     
    		glEnd ();
     
    		glBindTexture(GL_TEXTURE_2D, texture[5 + text_offset]);
    		glBegin (GL_QUADS);
     
    				glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET ,0.0 + OFFSET); glVertex3d (-10.0,10.0,-10.0);
    				glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET ,0.0 + OFFSET); glVertex3d (10.0,10.0,-10.0);
    				glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET ,1.0 - OFFSET); glVertex3d (10.0,10.0,10.0);
    				glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET ,1.0 - OFFSET); glVertex3d (-10.0,10.0,10.0);
     
    		glEnd ();
     
    	//Re-Enable the depth test
    	glEnable(GL_DEPTH_TEST);
    	//disable texture mapping
    	glDisable(GL_TEXTURE_2D);
    	glPopMatrix();
     
     
    }
     
    void skybox::draw_car(){
     
    	int l_index;
     
        glMatrixMode(GL_MODELVIEW); // Modeling transformation
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, object.id_texture); // set the active texture 
     
        glBegin(GL_TRIANGLES);
        for (l_index=0;l_index<object.polygons_qty;l_index++)
        {
            //----------------- FIRST VERTEX -----------------
            // Texture coordinates of the first vertex
            glTexCoord2f( object.mapcoord[ object.polygon[l_index].a ].u,
                          object.mapcoord[ object.polygon[l_index].a ].v);
            // Coordinates of the first vertex
            glVertex3f( object.vertex[ object.polygon[l_index].a ].x,
                        object.vertex[ object.polygon[l_index].a ].y,
                        object.vertex[ object.polygon[l_index].a ].z); //Vertex definition
     
            //----------------- SECOND VERTEX -----------------
            // Texture coordinates of the second vertex
            glTexCoord2f( object.mapcoord[ object.polygon[l_index].b ].u,
                          object.mapcoord[ object.polygon[l_index].b ].v);
            // Coordinates of the second vertex
            glVertex3f( object.vertex[ object.polygon[l_index].b ].x,
                        object.vertex[ object.polygon[l_index].b ].y,
                        object.vertex[ object.polygon[l_index].b ].z);
     
            //----------------- THIRD VERTEX -----------------
            // Texture coordinates of the third vertex
            glTexCoord2f( object.mapcoord[ object.polygon[l_index].c ].u,
                          object.mapcoord[ object.polygon[l_index].c ].v);
            // Coordinates of the Third vertex
            glVertex3f( object.vertex[ object.polygon[l_index].c ].x,
                        object.vertex[ object.polygon[l_index].c ].y,
                        object.vertex[ object.polygon[l_index].c ].z);
        }
        glEnd();
     
    }

    I take it that the GL_ModelView in my drawCar method is where the camera is first located. So how can i move it down? What function do i need to use?
    cheers and sorry for the lack of knowledge

  4. #4
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,367

    Re: camera position

    IMO, you need to read a little about math related to 3D, matrices, homogeneous coordinates, etc... Then read the OpenGL specification at the paragraph named "Coordinates transformation" It tells a lot about matrices (modelview, projection and texture ones) and how coordinates are handled and transformed throw these matrices and homogenous coordinates.
    After that, I am sure you will think it easy to move objects in the scene space.

    For now, to move down your camera, you just need to change the translation along the Y axis (vertical one) as follows:


    void display(void) {
    // change the yt value to translate the camera along
    // the vertical axis.
    float yt = -0.5;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(0.0,yt,-3.0);
    ...

Similar Threads

  1. camera position
    By wenhaug in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 05-16-2018, 06:20 AM
  2. camera position
    By giugio in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 08-04-2011, 04:43 AM
  3. Camera Position / Camera Angle / Camera Target Position
    By bobGL in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 04-02-2007, 09:28 AM
  4. camera position
    By in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 04-06-2005, 04:42 AM
  5. camera position
    By stratos in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 02-26-2002, 02:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean