Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: fps forward movement and gluLookAt

  1. #1
    Intern Contributor
    Join Date
    May 2017
    Posts
    64

    fps forward movement and gluLookAt

    Hi,

    I am writing code for camera control within a skeletal glut program that already has key press capability. It will be modeled after FPS shooter movement schemes. Starting with gluLookAt(Ex,Ey,Ez,Ox,Oy,Oz,0,1,0) how do I create a camera eye and virtual object position that will represent "forward". Once I learn this direction I can go to the others.
    Last edited by technologist; 11-07-2017 at 07:22 AM. Reason: more clear

  2. #2
    Intern Contributor
    Join Date
    May 2017
    Posts
    64

    followup on fps movement camera engine

    I am going to have the umbrage to answer some of my own question in case anyone else lands on this discussion. A site/tutorial that does exactly above is here:http://www.swiftless.com/tutorials/opengl/camera2.html
    The site has all of the source code to create FPS movement. However, it is a little short on explanation.

    In the event anyone else is interested in this solution I am posting the code below. It had significant whitespace, end-of-line and editing issues. I put in my include information with the source codes to make it work such as #define GLUT_DISABLE_ATEXIT_HACK used on my Eclipse Luna IDE. You might get a "stray \222" error or something similar, but this is resolved by properly editing the text to change quotations to ' from `and any other strange characters. Oh, don't forget to add your glut32.dll to your .exe folder!

    Code :
    #define GLUT_DISABLE_ATEXIT_HACK
    #ifdef __APPLE__
    #include <GLUT/glut.h>
    #else
    #include <GL/glut.h>
    #endif
    #include<iostream>
    #include<stdlib.h>	//exit(0)
    #include<math.h>
     
    //angle of rotation
    float xpos = 0, ypos = 0, zpos = 0, xrot = 0, yrot = 0, angle=0.0;
     
    float lastx, lasty;
     
    //positions of the cubes
    float positionz[10];
    float positionx[10];
     
    void cubepositions (void) { //set the positions of the cubes
     
        for (int i=0;i<10;i++)
        {
        positionz[i] = rand()%5 + 5;
        positionx[i] = rand()%5 + 5;
        }
    }
     
    //draw the cube
    void cube (void) {
        for (int i=0;i<10;i++)
        {
        glPushMatrix();
        glTranslated(-positionx[i + 1] * 10, 0, -positionz[i + 1] * 10); //translate the cube
        glutSolidCube(2); //draw the cube
        glPopMatrix();
        }
    }
     
    void init (void) {
    cubepositions();
    }
     
    void enable (void) {
        glEnable (GL_DEPTH_TEST); //enable the depth testing
        glEnable (GL_LIGHTING); //enable the lighting
        glEnable (GL_LIGHT0); //enable LIGHT0, our Diffuse Light
        glShadeModel (GL_SMOOTH); //set the shader to smooth shader
     
    }
     
    void camera (void) {
    	glRotatef(xrot,1.0,0.0,0.0);  //rotate our camera on the x-axis (left and right)
        glRotatef(yrot,0.0,1.0,0.0);  //rotate our camera on the y-axis (up and down)
        glTranslated(-xpos,-ypos,-zpos); //translate the screen to the position of our camera
    }
     
    void display (void) {
        glClearColor (0.0,0.0,0.0,1.0); //clear the screen to black
        glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear the color buffer and the depth buffer
        glLoadIdentity();
        camera();
        enable();
        cube(); //call the cube drawing function
        glutSwapBuffers(); //swap the buffers
        angle++; //increase the angle
    }
     
    void reshape (int w, int h) {
        glViewport (0, 0, (GLsizei)w, (GLsizei)h); //set the viewport to the current window specifications
        glMatrixMode (GL_PROJECTION); //set the matrix to projection
        glLoadIdentity ();
        gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 1000.0); //set the perspective (angle of sight, width, height, depth)
        glMatrixMode (GL_MODELVIEW); //set the matrix back to model
     
    }
     
    void keyboard (unsigned char key, int x, int y) {
        if (key=='q')
        {
        xrot += 1;
        if (xrot >360) xrot -= 360;
        }
     
        if (key=='z')
        {
        xrot -= 1;
        if (xrot < -360) xrot += 360;
        }
     
        if (key=='w')
        {
        float xrotrad, yrotrad;
        yrotrad = (yrot / 180 * 3.141592654f);
        xrotrad = (xrot / 180 * 3.141592654f);
        xpos += float(sin(yrotrad)) ;
        zpos -= float(cos(yrotrad)) ;
        ypos -= float(sin(xrotrad)) ;
        }
     
        if (key=='s')
        {
        float xrotrad, yrotrad;
        yrotrad = (yrot / 180 * 3.141592654f);
        xrotrad = (xrot / 180 * 3.141592654f);
        xpos -= float(sin(yrotrad));
        zpos += float(cos(yrotrad)) ;
        ypos += float(sin(xrotrad));
        }
     
        if (key=='d')
        {
        float yrotrad;
        yrotrad = (yrot / 180 * 3.141592654f);
        xpos += float(cos(yrotrad)) * 0.2;
        zpos += float(sin(yrotrad)) * 0.2;
        }
     
        if (key=='a')
        {
        float yrotrad;
        yrotrad = (yrot / 180 * 3.141592654f);
        xpos -= float(cos(yrotrad)) * 0.2;
        zpos -= float(sin(yrotrad)) * 0.2;
        }
     
        if (key==27)
        {
        exit(0);
        }
    }
     
    void mouseMovement(int x, int y) {
        int diffx=x-lastx; //check the difference between the current x and the last x position
        int diffy=y-lasty; //check the difference between the current y and the last y position
        lastx=x; //set lastx to the current x position
        lasty=y; //set lasty to the current y position
        xrot += (float) diffy; //set the xrot to xrot with the addition of the difference in the y position
        yrot += (float) diffx;    //set the xrot to yrot with the addition of the difference in the x position
    }
     
    int main (int argc, char **argv) {
        glutInit (&argc, argv);
        glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH);
        glutInitWindowSize (500, 500);
        glutInitWindowPosition (100, 100);
        glutCreateWindow ("A basic FPS OpenGL Window");
        init ();
        glutDisplayFunc (display);
        glutIdleFunc (display);
        glutReshapeFunc (reshape);
        glutPassiveMotionFunc(mouseMovement); //check for mouse movement
        glutKeyboardFunc (keyboard);
        glutMainLoop ();
        return 0;
    }
    Last edited by technologist; 11-07-2017 at 02:53 PM. Reason: DLL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •