#include #include #include #define NUM_CUBES 200 #define TEXTURE_SIZE 64 GLfloat light_position[] = {0.0f, 0.0f, 0.0f, 1.0f}; GLfloat diffuse_light[] = {0.9f, 0.9, 0.9f, 0.0f}; static int iteration = 0; static GLint x = 30, y = 30; float camX = 0.0f; float camY = 0.0f; float camZ = 0.0f; float camAlpha = 0.0f; float camBeta = 0.0f; float camZeta = 60.0f; float camera_state[4][4]; float oldX, oldY; int firstTime = 1; int controlling = 0; GLfloat deltaX = 0; GLfloat deltaY = 0; //GLfloat fuerzaDeAvion = 0.01; // Physics stuff neSimulator* gSim = NULL; neRigidBody* gCubes[NUM_CUBES]; neRigidBody* sphere; neAnimatedBody* floor1 = NULL; //SYSTEMTIME time, oldTime; neT3 t; float degree = 0.0f; float vel = 3.0f; GLuint check_id; GLuint sphere_id; GLubyte check_image[TEXTURE_SIZE][TEXTURE_SIZE][3]; // TIMING_INFO LARGE_INTEGER time; LONGLONG freq; long double lastFrameTime = 0; long double currentFrameTime; LONGLONG timer; double fps; char fpsString[255]; // brass material GLfloat brass_diffuse[]={0.78, 0.57, 0.11, 1.0}; GLfloat brass_specular[]={0.99, 0.00, 0.0, 1.0}; GLfloat brass_shininess = 27.8; void init_check_image() { int c; for (int i = 0; i < TEXTURE_SIZE; i++) { for (int j = 0; j < TEXTURE_SIZE; j++) { c = ((((i&0x8)==0)^((j&0x8))==0))*255; check_image[i][j][0] = (GLubyte) c; check_image[i][j][1] = (GLubyte) c; check_image[i][j][2] = (GLubyte) c; } } } void drawString(char *string) { int len, i; len = (int) strlen(string); for (i = 0; i < len; i++) { glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, string[i]); } } float counterValue() { LARGE_INTEGER i; QueryPerformanceCounter(&i); return (float)((long double)((long double)(i.QuadPart-timer)/(long double)freq)); } void drawSphere(double resolution) { double steps = resolution; double c = 3.1415926/180.0; double x, y, z; // back slice to front slice (starting z deep) for (double phi = -90.0; phi<=90.0; phi+=steps) { glBegin(GL_QUAD_STRIP); // circle around z axis for (float theta=0.0; theta<=360.0; theta+=steps) { x=sin(c*theta)*cos(c*phi); y=cos(c*theta)*cos(c*phi); z=sin(c*phi); glNormal3d (x, y, z); glTexCoord2f(1-theta/360.0, (phi+90)/180); glVertex3d(x, y, z); x=sin(c*theta)*cos(c*(phi+steps)); y=cos(c*theta)*cos(c*(phi+steps)); z=sin(c*(phi+steps)); glNormal3d(x, y, z); glTexCoord2f(1-theta/360.0, (phi+90+steps)/180); glVertex3d(x, y, z); } glEnd(); } } void push_sphere_to_card () { sphere_id = glGenLists(1); glNewList(sphere_id, GL_COMPILE); drawSphere(10); glEndList(); } void setupTextures() { //init_static_image(); init_check_image(); glGenTextures (1, &check_id); // Start binding a texture, and store on graphics card glBindTexture (GL_TEXTURE_2D, check_id); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TEXTURE_SIZE, TEXTURE_SIZE, 0, GL_RGB, GL_UNSIGNED_BYTE, check_image); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /*glGenTextures (1, &static_id); // Start binding a texture, and store on graphics card glBindTexture (GL_TEXTURE_2D, static_id); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TEXTURE_SIZE, TEXTURE_SIZE, 0, GL_RGB, GL_UNSIGNED_BYTE, static_image); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); */ } void brass(GLfloat x, GLfloat y, GLfloat z) { GLfloat brass_ambient[]={x, y, z, 1.0}; glMaterialfv(GL_FRONT, GL_AMBIENT, brass_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, brass_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, brass_specular); glMaterialf(GL_FRONT, GL_SHININESS, brass_shininess); } void clearMat() { GLfloat brass_ambient[]={1.0, 1.0, 1.0, 1.0}; glMaterialfv(GL_FRONT, GL_AMBIENT, brass_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, brass_ambient); glMaterialfv(GL_FRONT, GL_SPECULAR, brass_ambient); glMaterialf(GL_FRONT, GL_SHININESS, brass_shininess); } void display () { // Time mess float cv = counterValue(); //fps = 1.000000/(cv - lastFrameTime); fps = (cv - lastFrameTime)*2; lastFrameTime = cv; glMatrixMode(GL_MODELVIEW); degree += 0.1f; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //GetSystemTime(&time); //gSim->Advance(getElapsedTime()/100000.0f); gSim->Advance(fps); glPushMatrix(); glTranslatef (0, -5, -10.0f); //Floor glPushMatrix(); glEnable(GL_TEXTURE_2D); glBindTexture (GL_TEXTURE_2D, check_id); t = floor1->GetTransform(); camera_state[0][0] = t.rot[0][0]; camera_state[1][0] = t.rot[1][0]; camera_state[2][0] = t.rot[2][0]; camera_state[3][0] = t.pos[0]; camera_state[0][1] = t.rot[0][1]; camera_state[1][1] = t.rot[1][1]; camera_state[2][1] = t.rot[2][1]; camera_state[3][1] = t.pos[1]; camera_state[0][2] = t.rot[0][2]; camera_state[1][2] = t.rot[1][2]; camera_state[2][2] = t.rot[2][2]; camera_state[3][2] = t.pos[2]; camera_state[0][3] = 0.0f; camera_state[1][3] = 0.0f; camera_state[2][3] = 0.0f; camera_state[3][3] = 1.0f; glMultMatrixf ((float *)camera_state); glBegin(GL_POLYGON); glTexCoord2f (0.0, 0.0); glVertex3f (-50.0f, 0, -50.0f); glTexCoord2f (0.0, 1.0); glVertex3f (50.0f, 0, -50.0f); glTexCoord2f (1.0, 1.0); glVertex3f (50.0f, 0, 50.0f); glTexCoord2f (1.0, 0.0); glVertex3f (-50.0f, 0, 50.0f); glEnd(); glDisable (GL_TEXTURE_2D); glPopMatrix(); //Cubes for (int i = 0; i < NUM_CUBES; i++) { glPushMatrix(); t = gCubes[i]->GetTransform(); camera_state[0][0] = t.rot[0][0]; camera_state[1][0] = t.rot[1][0]; camera_state[2][0] = t.rot[2][0]; camera_state[3][0] = t.pos[0]; camera_state[0][1] = t.rot[0][1]; camera_state[1][1] = t.rot[1][1]; camera_state[2][1] = t.rot[2][1]; camera_state[3][1] = t.pos[1]; camera_state[0][2] = t.rot[0][2]; camera_state[1][2] = t.rot[1][2]; camera_state[2][2] = t.rot[2][2]; camera_state[3][2] = t.pos[2]; camera_state[0][3] = 0.0f; camera_state[1][3] = 0.0f; camera_state[2][3] = 0.0f; camera_state[3][3] = 1.0f; glMultMatrixf ((float *)camera_state); //glTranslatef (t.pos[0], t.pos[1], t.pos[2]); brass(i%5/5.0, i%3/3.0, i%10/10.0); glutSolidCube(1.0f); glPopMatrix(); } clearMat(); // Sphere glPushMatrix(); glEnable(GL_TEXTURE_2D); glBindTexture (GL_TEXTURE_2D, check_id); t = sphere->GetTransform(); camera_state[0][0] = t.rot[0][0]; camera_state[1][0] = t.rot[1][0]; camera_state[2][0] = t.rot[2][0]; camera_state[3][0] = t.pos[0]; camera_state[0][1] = t.rot[0][1]; camera_state[1][1] = t.rot[1][1]; camera_state[2][1] = t.rot[2][1]; camera_state[3][1] = t.pos[1]; camera_state[0][2] = t.rot[0][2]; camera_state[1][2] = t.rot[1][2]; camera_state[2][2] = t.rot[2][2]; camera_state[3][2] = t.pos[2]; camera_state[0][3] = 0.0f; camera_state[1][3] = 0.0f; camera_state[2][3] = 0.0f; camera_state[3][3] = 1.0f; glMultMatrixf ((float *)camera_state); //glTranslatef (t.pos[0], t.pos[1], t.pos[2]); //glScalef(1, 1, 1); //drawSphere(5); glCallList (sphere_id); glDisable(GL_TEXTURE_2D); glPopMatrix(); glPopMatrix(); glutSwapBuffers(); glutPostRedisplay(); } void setupPhysicsEngine() { // This will define the size and shape of each cube neGeometry* geom; // length, width and height of the cube neV3 boxSize1; neV3 gravity; neV3 pos; float mass = 1.0f; float fmass = 0.4f; // The number of total objects the simulator has to keep track of... neSimulatorSizeInfo sizeInfo; // Fill in the size info about the environment sizeInfo.rigidBodiesCount = NUM_CUBES+1; sizeInfo.animatedBodiesCount = 1; // total number of objects sizeInfo.geometriesCount = sizeInfo.rigidBodiesCount + sizeInfo.animatedBodiesCount; // total number of collisions possible n*(n-1)/2 sizeInfo.overlappedPairsCount = sizeInfo.geometriesCount*(sizeInfo.geometriesCount-1)/2; sizeInfo.rigidParticleCount = 0; sizeInfo.constraintsCount = 0; sizeInfo.terrainNodesStartCount = 0; gravity.Set(0.0f, -3.0f, 0.0f); gSim = neSimulator::CreateSimulator(sizeInfo, NULL, &gravity); // Setup a box - using loop for (int i = 0; i < NUM_CUBES; i++) { gCubes[i] = gSim->CreateRigidBody(); // Get the geometry object from the cube geom = gCubes[i]->AddGeometry(); boxSize1.Set(1.0f, 1.0f, 1.0f); geom->SetBoxSize(boxSize1[0], boxSize1[1], boxSize1[2]); gCubes[i]->UpdateBoundingInfo(); gCubes[i]->SetInertiaTensor(neBoxInertiaTensor(boxSize1[0], boxSize1[1], boxSize1[2], mass)); gCubes[i]->SetMass(mass); pos.Set(i%10-5, i/10+0.5, -30); gCubes[i]->SetPos(pos); //pos.Set(0, i, 0); //gCubes[0]->SetRotation(pos); //gCubes[0]->SetVelocity(pos); // end loop } // Create the sphere sphere = gSim->CreateRigidBody(); geom = sphere->AddGeometry(); geom->SetSphereDiameter(2); sphere->UpdateBoundingInfo(); sphere->SetInertiaTensor(neSphereInertiaTensor(2, fmass)); sphere->SetMass(fmass); pos.Set(0, 1, -4); sphere->SetPos(pos); sphere->SetAngularDamping(0.01f); // Create the floor floor1 = gSim->CreateAnimatedBody(); geom = floor1->AddGeometry(); boxSize1.Set(100, 0.001, 100); geom->SetBoxSize(boxSize1[0], boxSize1[1], boxSize1[2]); floor1->UpdateBoundingInfo(); pos.Set(0, 0, 0); floor1->SetPos(pos); } void teclas (unsigned char c, int x1, int y1) { neV3 pos; neV3 ballVel; char d = (char)c; switch (d) { case 27: exit(1); break; case 'r': setupPhysicsEngine(); break; case '1': vel = 1; break; case '2': vel = 2; break; case '3': vel = 3; break; case '4': vel = 4; break; case '5': vel = 5; break; case '6': vel = 6; break; case '7': vel = 7; break; case '8': vel = 8; break; case '9': vel = 20; break; case 'm': pos = gCubes[0]->GetPos(); pos.Set(-10, 0, 0); gCubes[10]->SetPos(pos); break; case 'j': ballVel.Set (0, 5, -vel*20); sphere->SetVelocity(ballVel); break; } } void killPhysics(void) { if (gSim) { // Destroy the simulator. // Note that this will release all related resources that we've allocated. neSimulator::DestroySimulator(gSim); gSim = NULL; } } int main (int argv, char** argc) { glutInit (&argv, argc); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); glutCreateWindow ("Tokamak Test"); glutDisplayFunc (display); // Setup viewing glMatrixMode (GL_PROJECTION); glLoadIdentity(); gluPerspective (60.0f, 1.5, 0.1, 1000); glMatrixMode (GL_MODELVIEW); glEnable (GL_DEPTH_TEST); glEnable (GL_LIGHTING); glEnable (GL_LIGHT0); // Setup Physics engine setupPhysicsEngine(); push_sphere_to_card(); setupTextures(); // Time stuff lastFrameTime = 0.000000; fps = 0.000000; LARGE_INTEGER i, f; QueryPerformanceFrequency( &f ); freq = f.QuadPart; QueryPerformanceCounter(&i); timer = i.QuadPart; glLightfv (GL_LIGHT0, GL_POSITION, light_position); glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse_light); glClearColor(0.0f, 0.5f, 0.7f, 0.0f); glutKeyboardFunc(teclas); glutFullScreen(); glutMainLoop(); }