void idle() { keyboardEvent(); int time_ = glutGet(GLUT_ELAPSED_TIME); float angle = time_ / 1000.0 * 10; Vector3 pos = player.getPosition(); float angX = player.getRotationX(), angY = player.getRotationY(); Vector3 cpos = cube.getPosition(), cpos2= cube2.getPosition(); player.updateForce(); Matrix4 model = Matrix4().Translate(cpos).Rotate(1.0f, cube.getRotation()), model2 = Matrix4().Translate(cpos2).Rotate(1.0f, cube2.getRotation()); Matrix4 projection = Matrix4().Perspective(45.0f, 1.0f*screen_width/screen_height, 0.1f, 50.0f), view = Matrix4().lookAt(pos, Vector3(pos.x-cos(angX), pos.y + sin(angY), pos.z + sin(angX)), Vector3(0.0f, 1.0f, 0.0f)); Matrix4 anim = Matrix4().RotateA(angle*3.0f, Vector3(1, 0, 0)) * // X axis Matrix4().RotateA(angle*2.0f, Vector3(0, 1, 0)) * // Y axis Matrix4().RotateA(angle*4.0f, Vector3(0, 0, 1)); // Z axis Matrix4 m_transform = projection * view * model, m_transform2= projection * view * model2;// * anim; glUseProgram(cube.getProgram()); glUniformMatrix4fv(cube.getUniform("m_transform"), 1, GL_FALSE, m_transform); glUseProgram(cube2.getProgram()); glUniformMatrix4fv(cube2.getUniform("m_transform"), 1, GL_FALSE, m_transform2); glutPostRedisplay(); }
int init_resources(void) { try{ player.moveLeft(9); player.moveForward(7); player.rotateX(-M_PI*3/4); player.rotateY(M_PI/12); player.addForce(gravity*-1); player.loadObj("crazy.o"); cube.loadObj("cube.o"); cube2.loadObj("cube.o"); setupBuffers(player); setupBuffers(cube); setupBuffers(cube2); addObjectToShader(&cube); addObjectToShader(&cube2); bool c1 = cube.loadShaders(vertexShaderFileName, fragmentShaderFileName), c2 = cube2.loadShaders(vertexShaderFileName, fragmentShaderFileName); Vector3 pos = cube2.getPosition()+ Vector3(0.5f, 3.5f, 1.5f); cube2.setPosition(pos); //cube2.setRotation(Vector3(30*M_PI/180, 0, 40*M_PI/180)); return c1 * c2; }catch(int i){ return 0; } }
int init_resources(void) { try{ player.moveLeft(9); player.moveForward(7); player.rotateX(-M_PI*3/4); player.rotateY(M_PI/12); player.addForce(gravity*-1); player.loadObj("crazy.o"); cube.loadObj("cube.o"); cube2.loadObj("cube.o"); setupBuffers(player); setupBuffers(cube); setupBuffers(cube2); addObjectToShader(&cube); addObjectToShader(&cube2); bool c1 = cube.loadShaders(vertexShaderFileName, fragmentShaderFileName), c2 = cube2.loadShaders(vertexShaderFileName, fragmentShaderFileName), c3 = 1; for(int i=0; i< BALLS; i++){ balls[i].loadObj("crazy.o"); balls[i].setScale(Vector3(0.25f,0.25f,0.25f)); setupBuffers(balls[i]); addObjectToShader(&(balls[i])); c3 *= balls[i].loadShaders(vertexShaderFileName, fragmentShaderFileName); balls[i].addVelocity(Vector3(m_rand()-0.5f, m_rand()-0.5f, m_rand()-0.5f).normalize().mult(60.0)); } GLfloat scale_grid = ((GLfloat)DETAIL_GRID*2.0f)/((GLfloat)BOUNDARY); for(int i=0; i< DETAIL_GRID; i++){ for(int j = 0;j < DETAIL_GRID; j++){ grid[i].loadObj("cube.o"); grid[i].setScale(Vector3(scale_grid, scale_grid, scale_grid)); setupBuffers(grid[i]); addObjectToShader(&(grid[i])); c3 *= grid[i].loadShaders(vertexShaderFileName, fragmentShaderFileName); grid[i].setPosition(Vector3(i*scale_grid, j*scale_grid, k*scale_grid - DETAIL_GRID-2.0f)); /*if(i%6 == 0) grid[i].setPosition(Vector3(DETAIL_GRID + 1.0f, (i-3)*scale_grid-DETAIL_GRID, (i-3)*scale_grid-DETAIL_GRID)); else if(i%6 == 1) grid[i].setPosition(Vector3((i-2)*scale_grid-DETAIL_GRID, DETAIL_GRID + 1.0f, (i-2)*scale_grid-DETAIL_GRID)); else if(i%6 == 2) grid[i].setPosition(Vector3((i-1)*scale_grid-DETAIL_GRID, (i-1)*scale_grid-DETAIL_GRID, DETAIL_GRID + 1.0f)); else if(i%6 == 3) grid[i].setPosition(Vector3(-DETAIL_GRID - 1.0f , i*scale_grid-DETAIL_GRID, i*scale_grid-DETAIL_GRID)); else if(i%6 == 4) grid[i].setPosition(Vector3((i+1)*scale_grid-DETAIL_GRID, -DETAIL_GRID - 1.0f, (i+1)*scale_grid-DETAIL_GRID)); else if(i%6 == 5) grid[i].setPosition(Vector3((i+2)*scale_grid-DETAIL_GRID, (i+2)*scale_grid-DETAIL_GRID, -DETAIL_GRID - 1.0f));*/ } } Vector3 pos = cube2.getPosition()+ Vector3(0.5f, 3.5f, 1.5f); cube2.setPosition(pos); //cube2.setRotation(Vector3(30*M_PI/180, 0, 40*M_PI/180)); return c1 * c2 * c3; }catch(int i){ return 0; } }
void idle() { keyboardEvent(); int time_ = glutGet(GLUT_ELAPSED_TIME); float angle = time_ / 1000.0 * 10; Vector3 pos = player.getPosition(); float angX = player.getRotationX(), angY = player.getRotationY(); Vector3 cpos = cube.getPosition(), cpos2= cube2.getPosition(); GLfloat D = cpos.distance(&cpos2), W = (cube.dimension() + cube2.dimension())/2; float bound = 6.0; //if(time_%100 < 10) // gravity = Vector3(rand()%10-5, rand()%10-5, rand()%10-5); cvelocity.add(&gravity); Vector3 s = cvelocity; s.mult(-mass/1000).add(&cpos2); bool collides = true; if(cpos2.x < -bound && cvelocity.x > 0.0f){ cvelocity.x = -abs(cvelocity.x)*0.5; gravity.x = -abs(gravity.x); } else if(cpos2.x > bound && cvelocity.x < 0.0f){ cvelocity.x = abs(cvelocity.x)*0.5; gravity.x = abs(gravity.x); } else if(cpos2.y < -bound && cvelocity.y > 0.0f){ cvelocity.y = -abs(cvelocity.y)*0.5; gravity.y = -abs(gravity.y); } else if(cpos2.y > bound && cvelocity.y < 0.0f){ cvelocity.y = abs(cvelocity.y)*0.5; gravity.y = abs(gravity.y); } else if(cpos2.z < -bound && cvelocity.z > 0.0f){ cvelocity.z = -abs(cvelocity.z)*0.5; gravity.z = -abs(gravity.z); } else if(cpos2.z > bound && cvelocity.z < 0.0f){ cvelocity.z = abs(cvelocity.z)*0.5; gravity.z = abs(gravity.z); } else{ collides = false; } if(!collides){ if(cpos.x + cube.min.x <= cpos2.x + cube2.max.x && cpos.x + cube.max.x >= cpos2.x + cube2.min.x && cpos.z + cube.min.z <= cpos2.z + cube2.max.z && cpos.z + cube.max.z >= cpos2.z + cube2.min.z && cpos.y + cube.min.y <= cpos2.y + cube2.max.y && cpos.y + cube.max.y >= cpos2.y + cube2.min.y){ collides = true; cvelocity.x = cvelocity.y = cvelocity.z = 0.0f; } if(!collides) cube2.setPosition(s); } player.updateForce(); for (int i = 0; i < BALLS; i++){ balls[i].updateForce(); Vector3 ball_pos = balls[i].getPosition(), ball_vel = balls[i].dynamics.velocity; //printf("x: %f, y: %f, z %f: %f\n", ball_pos.x, ball_pos.y, ball_pos.z, BOUNDARY); if(ball_pos.x < -BOUNDARY || ball_pos.x > BOUNDARY) balls[i].addVelocity(Vector3(-2 * ball_vel.x, 0.0f, 0.0f)); if(ball_pos.y < -BOUNDARY || ball_pos.y > BOUNDARY) balls[i].addVelocity(Vector3(0.0f, -2 * ball_vel.y, 0.0f)); if(ball_pos.z < -BOUNDARY || ball_pos.z > BOUNDARY) balls[i].addVelocity(Vector3(0.0f, 0.0f, -2 * ball_vel.z)); } Matrix4 model = cube.getTransformMatrix(), model2 = cube2.getTransformMatrix(); Matrix4 projection = Matrix4().Perspective(45.0f, 1.0f*screen_width/screen_height, 0.1f, 50.0f), view = Matrix4().lookAt(pos, Vector3(pos.x-cos(angX), pos.y + sin(angY), pos.z + sin(angX)), Vector3(0.0f, 1.0f, 0.0f)); Matrix4 anim = Matrix4().RotateA(angle*3.0f, Vector3(1, 0, 0)) * // X axis Matrix4().RotateA(angle*2.0f, Vector3(0, 1, 0)) * // Y axis Matrix4().RotateA(angle*4.0f, Vector3(0, 0, 1)); // Z axis Matrix4 m_transform = projection * view; glUseProgram(cube.getProgram()); glUniformMatrix4fv(cube.getUniform("m_transform"), 1, GL_FALSE, m_transform * model); glUseProgram(cube2.getProgram()); glUniformMatrix4fv(cube2.getUniform("m_transform"), 1, GL_FALSE, m_transform * model2); for (int i = 0; i < BALLS; i++){ glUseProgram(balls[i].getProgram()); glUniformMatrix4fv( balls[i].getUniform("m_transform"), // m_transform is the transformation of balls[i] in shaders 1, // one uniform-buffer GL_FALSE, // no nprmalization m_transform * balls[i].getTransformMatrix() ); } for (int i = 0; i < DETAIL_GRID*3; i++){ grid[i].updateForce(); glUseProgram(grid[i].getProgram()); glUniformMatrix4fv( grid[i].getUniform("m_transform"), // m_transform is the transformation of grid[i] in shaders 1, // one uniform-buffer GL_FALSE, // no nprmalization m_transform * grid[i].getTransformMatrix() ); } glutPostRedisplay(); }