opengl model transformations

This is a discussion on opengl model transformations within the Game Programming forums, part of the General Programming Boards category; I am attempting to move all my game objects independantly but it isn't working. Code: void moveAstroids(void) { for_each(astroids.begin(),astroids.end(),mem_fun(&Astroid::translate)); glutPostRedisplay(); ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    450

    opengl model transformations

    I am attempting to move all my game objects independantly but it isn't working.
    Code:
    void moveAstroids(void)
    { 
     
      for_each(astroids.begin(),astroids.end(),mem_fun(&Astroid::translate));
      glutPostRedisplay();
     
    }
    Code:
    void Astroid::translate(){
      glPushMatrix();
      glTranslatef(velocity,velocity,0);
      Astroid::draw();
      glPopMatrix();
    }
    when I comment out the Push and Pop statements all the game objects move in tandem according to one translation. I am trying to get each object to move independantly. I tried using glLoadIdentity(); but this had the same effect.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    What is happening right now? Are the objects not being translated at all?

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    Also, make sure you are performing the translation on the ModelView matrix, and not the Projection matrix.

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    The gameobjects are currently not translated at all. As far as I can tell the translations are performed on the ModelView matrix it is the last thing set in the reshape function.

    Here is my current code It is a long way from complete and is basicly an adaptation of the double.c program from the redbook.
    Code:
    #include <iostream>
    #include <GL/glut.h>
    #include <stdlib.h>
    #include <list>
    #include <functional>
    #include <algorithm>
    #include <time.h>
    
    #include "gobject.h"
    #include "ship.h"
    #include "astroid.h"
    
    using namespace std;
    extern const int SCREENWIDTH = 450;
    extern const int SCREENHEIGHT = 450;
    
    static GLfloat spin = 0.0;
    list<GObject*> gameObjects;
    list<Astroid*> astroids;
    Ship* spaceShip = new Ship();
    
    void display()
    {
       
       glClear(GL_COLOR_BUFFER_BIT);
       glColor3f(1.0, 1.0, 1.0);
       for_each(gameObjects.begin(),gameObjects.end(),mem_fun(&GObject::translate));
       glutSwapBuffers();
       
    }
    
    void moveAstroids(void)
    { 
     
      // for_each(astroids.begin(),astroids.end(),mem_fun(&Astroid::translate));
      glutPostRedisplay();
     
    }
    
    
    void idleGameLoop(void)
    { 
      
      moveAstroids();
      glutPostRedisplay();
    }
    
    
    void init(void)
    {
      srand(time(NULL));
      glClearColor (0.0, 0.0, 0.0, 0.0);
      glShadeModel (GL_FLAT);
      gameObjects.push_front(spaceShip);
      for (int i = 0; i<10; i++){
       Astroid* sharedTempAstroid = new Astroid();
         gameObjects.push_back(sharedTempAstroid);
         astroids.push_back(sharedTempAstroid);
    
      }
    }
    
    void reshape(int w, int h)
    {
       glViewport (0, 0, (GLsizei) w, (GLsizei) h);
       glMatrixMode(GL_PROJECTION);
       glLoadIdentity();
       gluOrtho2D(-SCREENWIDTH/2,SCREENWIDTH/2,-SCREENHEIGHT/2,SCREENHEIGHT/2);
      
       glMatrixMode(GL_MODELVIEW);
       glLoadIdentity();
    }
    
    /* ARGSUSED2 */
    void mouse(int button, int state, int x, int y)
    {
       switch (button) {
          case GLUT_RIGHT_BUTTON:
             if (state == GLUT_DOWN)
    	    glutIdleFunc(idleGameLoop);
             break;
          case GLUT_MIDDLE_BUTTON:
             if (state == GLUT_DOWN)
                glutIdleFunc(NULL);
             break;
          default:
             break;
       }
    }
    
    		    
    
    static void
    key(unsigned char k, int x, int y)
    {
      switch (k) {
      case 27:  /* Escape */
        exit(0);
        break;
      case 65:
      case 97:
        spin = (int)spin % 360 + 2.0;
        glutIdleFunc(idleGameLoop);
        break;
      case 68:
      case 100:
        spin = (int)spin % 360 - 2.0;
        glutIdleFunc(idleGameLoop);
        break;
      default:
        return;
      }
      glutPostRedisplay();
    }
    
    /*
     *  Request double buffer display mode.
     *  Register mouse input callback functions
     */
    int main(int argc, char** argv)
    {
      
       glutInit(&argc, argv);
       glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
       glutInitWindowSize (SCREENWIDTH, SCREENHEIGHT);
       glutInitWindowPosition (10, 10);
       glutCreateWindow (argv[0]);
       init ();
      
       glutDisplayFunc(display);
       glutReshapeFunc(reshape);
       glutMouseFunc(mouse);
       glutKeyboardFunc(key);
       glutMainLoop();
       return 0;   /* ANSI C requires main to return int. */
    }
    Code:
    #include "astroid.h"
    
    extern const int SCREENWIDTH;
    extern const int SCREENHEIGHT;
    
    Astroid::Astroid()
    {
     
      xcoord=rand()%(SCREENWIDTH/2)+1;
      ycoord=rand()%(SCREENHEIGHT/2)+1;
      velocity = 1*(rand()%4);
     
    }
    
    void Astroid::draw(){
      glColor3f(256.0, 0.0 , 0.0);
     
      glRectf(xcoord, ycoord,xcoord+10,ycoord+10);
     
    }
    
    void Astroid::rotate(){}
    void Astroid::translate(){
      glPushMatrix();
      glTranslatef(velocity,velocity,0);
      Astroid::draw();
      glPopMatrix();
    }
    
    Astroid::~Astroid(){}
    Last edited by curlious; 09-06-2004 at 03:38 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 11:17 AM
  2. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 01:34 PM
  3. Help with file reading/dynamic memory allocation
    By Quasar in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2004, 04:36 PM
  4. OpenGL .dll vs video card dll
    By Silvercord in forum Game Programming
    Replies: 14
    Last Post: 02-12-2003, 07:57 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21