int main(int argc, char** argv) { //Init block glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize(width, height); glutInitWindowPosition(WIN_POS_X, WIN_POS_Y); glutInit(&argc, argv); //Read the object r.read("objects/f16.obj"); r.normalize(); r.center(); path = readMoves("paths/spiral.path", 0.01, false); moveObj(); //Position the object for the initial showing //Create an appropriately named window window = glutCreateWindow(r.getName().c_str()); //Set up the handlers glutReshapeFunc(reshapeHandler); glutDisplayFunc(displayHandler); glutKeyboardFunc(keypressHandler); glutMainLoop(); return 0; }
void keypressHandler(unsigned char key, int mouseX, int mouseY) { switch(key) { case 'w': ociste.y += 0.1; break; case 's' : ociste.y -= 0.1; break; case 'a' : ociste.x -= 0.1; break; case 'd' : ociste.x += 0.1; break; case 'q' : ociste.z += 0.1; break; case 'e' : ociste.z -= 0.1; break; case 'm' : moveObj(); break; case 'r' : ociste.x = 0; ociste.y = 0; ociste.z = 5; break; default: cout << "No handler for this key" << endl; } updatePerspective(); glutPostRedisplay(); }
void movePlayer() { moveObj( &ship ); // take input if( key[KEY_LEFT] ) { ship.ang -= PLAYER_TURN_SPEED; } if( key[KEY_RIGHT] ) { ship.ang += PLAYER_TURN_SPEED; } if( key[KEY_UP] ) { float thrustPower = PLAYER_THRUST_POWER; ship.xv += cos(ship.ang) * thrustPower; ship.yv += sin(ship.ang) * thrustPower; } if( key[KEY_SPACE] ) { shoot(); } // player bound checking if(ship.x > SCREEN_W) { resetPlayer(); } if(ship.x < 0) { resetPlayer(); } if(ship.y > SCREEN_H) { resetPlayer(); } if(ship.y < 0) { resetPlayer(); } }
void moveShot() { if(shot.life > 0) { shot.life--; } // update the ball's position for the next frame moveObj( &shot ); // shot screen edge checks if(shot.x > SCREEN_W) { shot.life = 0; } if(shot.x < 0) { shot.life = 0; } if(shot.y > SCREEN_H) { shot.life = 0; } if(shot.y < 0) { shot.life = 0; } }
// the right marker sphere void drawThirdSphere(glm::mat4 modelMatrix) { Sphere *sphere3 = new Sphere(); glUniformMatrix4fv(PhongShade.worldSpaceMoveMatrixUnif, 1, GL_FALSE, glm::value_ptr(moveObj(-1.0))); modelMatrix = glm::translate(modelMatrix, glm::vec3(5.0f, 0.0f, 0.0f)); modelMatrix = glm::scale(modelMatrix, glm::vec3(0.25f, 0.25f, 0.25f)); glUniformMatrix4fv(PhongShade.modelToWorldMatrixUnif, 1, GL_FALSE, glm::value_ptr(modelMatrix)); glUniform4f(PhongShade.diffuseColorUnif, 1.0f, 0.2f, 0.2f, 1.0f); glUniform4f(PhongShade.ambientIntensityUnif, 0.2f, 0.2f, 0.2f, 1.0f); glUniform1f(PhongShade.shininessFactorUnif, 20.0f); sphere3->DrawUnitSphere(); }
// this is the moving sphere void drawSecondSphere(glm::mat4 modelMatrix, float timeParameter) { Sphere *sphere2 = new Sphere(); // this time send the shader a non-identity transform to move in world space glUniformMatrix4fv(PhongShade.worldSpaceMoveMatrixUnif, 1, GL_FALSE, glm::value_ptr(moveObj(timeParameter))); modelMatrix = glm::translate(modelMatrix, glm::vec3(0.0f, 0.0f, 0.0f)); modelMatrix = glm::scale(modelMatrix, glm::vec3(1.0f, 0.5f, 1.0f)); // scale is different from the marker glUniformMatrix4fv(PhongShade.modelToWorldMatrixUnif, 1, GL_FALSE, glm::value_ptr(modelMatrix)); glUniform4f(PhongShade.diffuseColorUnif, 0.2f, 1.0f, 0.2f, 1.0f); // color is different from the marker glUniform4f(PhongShade.ambientIntensityUnif, 0.2f, 0.2f, 0.2f, 1.0f); glUniform1f(PhongShade.shininessFactorUnif, 20.0f); sphere2->DrawUnitSphere(); }
// marker sphere to the left - the transform passed in is the identity in this case void drawFirstSphere(glm::mat4 modelMatrix) { Sphere *sphere1 = new Sphere(); glUniformMatrix4fv(PhongShade.worldSpaceMoveMatrixUnif, 1, GL_FALSE, glm::value_ptr(moveObj(-1.0))); // don't move it // translate to the left 5 units modelMatrix = glm::translate(modelMatrix, glm::vec3(-5.0f, 0.0f, 0.0f)); // scale it small - recall that transforms are applied in reverse coding order, so this scale occurs first modelMatrix = glm::scale(modelMatrix, glm::vec3(0.25f, 0.25f, 0.25f)); // now set the transform in the shader glUniformMatrix4fv(PhongShade.modelToWorldMatrixUnif, 1, GL_FALSE, glm::value_ptr(modelMatrix)); // color glUniform4f(PhongShade.diffuseColorUnif, 1.0f, 0.2f, 0.2f, 1.0f); // low saturation red // couple of other reflectance parameters glUniform4f(PhongShade.ambientIntensityUnif, 0.2f, 0.2f, 0.2f, 1.0f); glUniform1f(PhongShade.shininessFactorUnif, 20.0f); // tells the shader to draw it sphere1->DrawUnitSphere(); }