#include "stdafx.h" #define NUM_SPHERES 50 GLTFrame spheres[NUM_SPHERES]; GLTFrame frameCamera; GLfloat yRot = 0.0f; // Rotation angle for animation GLfloat fExtent = 20.0f; void gltInitFrame(GLTFrame *f) { f->vLocation[0]=0.0f; f->vLocation[1]=0.0f; f->vLocation[2]=0.0f; f->vRotation[0]=0.0f; f->vRotation[1]=0.0f; f->vRotation[2]=0.0f; return; } void gltApplyActorTransform(GLTFrame *sphere) { glLoadIdentity (); glTranslatef(sphere->vLocation[0], sphere->vLocation[1], sphere->vLocation[2]); } void gltApplyCameraTransform(GLTFrame *frameCamera) {GLdouble ep[3], //= {0.0, 0.0, 0.0, 1.0}, cp[3] ; //= {0.0, 0.0, -1.0, 1.0}, ep[0]=frameCamera->vLocation[0]; ep[1]=frameCamera->vLocation[1]; ep[2]=frameCamera->vLocation[2]; cp[0]=frameCamera->vRotation[0]; cp[1]=0.0; cp[2]=-1.0; gluLookAt (ep[0], ep[1], ep[2], cp[0], cp[1], cp[2], 0.0, 1.0, 0.0); } // This function does any needed initialization on the rendering // context. void SetupRC() { int iSphere; // Bluish background glClearColor(0.0f, 0.0f, .50f, 1.0f ); // Draw everything as wire frame /* glPolygonMode(GL_FRONT, GL_FILL); glPolygonMode(GL_BACK, GL_LINE)*/; glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); gltInitFrame(&frameCamera); // Initialize the camera // Randomly place the sphere inhabitants for(iSphere = 0; iSphere < NUM_SPHERES; iSphere++) { gltInitFrame(&spheres[iSphere]); // Initialize the frame // Pick a random location between -20 and 20 at .1 increments spheres[iSphere].vLocation[0] = (float)((rand() % 400) - 200) * 0.1f; spheres[iSphere].vLocation[1] = 0.0f; spheres[iSphere].vLocation[2] = (float)((rand() % 400) - 200) * 0.1f; } } // Draw a gridded ground void DrawGround(void) { GLfloat fStep = 1.0f; GLfloat y = -0.4f; GLint iLine; glBegin(GL_LINES); for(iLine = -fExtent; iLine <= fExtent; iLine += fStep) { glVertex3f(iLine, y, fExtent); // Draw Z lines glVertex3f(iLine, y, -fExtent); glVertex3f(fExtent, y, iLine); glVertex3f(-fExtent, y, iLine); } glEnd(); } // Called to draw scene void RenderScene(void) { int i; yRot += 0.5f; // Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); gltApplyCameraTransform(&frameCamera); // Draw the ground DrawGround(); // Draw the randomly located spheres for(i = 0; i < NUM_SPHERES; i++) { glPushMatrix(); gltApplyActorTransform(&spheres[i]); glutSolidSphere(0.1f, 13, 26); glPopMatrix(); } glPushMatrix(); glTranslatef(0.0f, 0.0f, -2.5f); glPushMatrix(); glRotatef(-yRot * 2.0f, 0.0f, 1.0f, 0.0f); glTranslatef(1.0f, 0.0f, 0.0f); glutSolidSphere(0.1f, 13, 26); glPopMatrix(); glRotatef(yRot, 0.0f, 1.0f, 0.0f); glutSolidTorus(0.15, 0.35, 40, 20); // gltDrawTorus(0.35, 0.15, 40, 20); //void glutSolidTorus(GLdouble innerRadius, GLdouble outerRadius, //GLint nsides, GLint rings); glPopMatrix(); glPopMatrix(); // Do the buffer Swap glutSwapBuffers(); } // Respond to arrow keys by moving the camera frame of reference void SpecialKeys(int key, int x, int y) { if(key == GLUT_KEY_UP) frameCamera.vLocation[2]+=0.1f; //gltMoveFrameForward(&frameCamera, 0.1f); if(key == GLUT_KEY_DOWN) frameCamera.vLocation[2]-=0.1f; //gltMoveFrameForward(&frameCamera, -0.1f); if(key == GLUT_KEY_LEFT) frameCamera.vRotation[0]+=0.005; //gltRotateFrameLocalY(&frameCamera, 0.1); if(key == GLUT_KEY_RIGHT) frameCamera.vRotation[0]-=0.005; //gltRotateFrameLocalY(&frameCamera, -0.1); // Refresh the Window glutPostRedisplay(); } void ChangeSize(int w, int h) { glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluPerspective(60.0, 1.0, 1.5, 400.0); glMatrixMode (GL_MODELVIEW); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); SetupRC(); glutDisplayFunc(RenderScene); glutReshapeFunc(ChangeSize); glutSpecialFunc (SpecialKeys); glutMainLoop(); return 0; }