7. Combination exercise — ball on the floor

A small ball orbiting a large ball drawn on the floor and supporting movement observation

#include <stdio.h> #include "GLShaderManager.h" #include "GLTools.h" #include <GLUT/GLUT.h> #include "GLFrustum.h" #include "GLBatch.h" #include "GLMatrixStack.h" #include "GLGeometryTransform.h" #include "StopWatch.h" GLShaderManager shaderManager; // GLMatrixStack modelViewMatrix; // model view matrix GLMatrixStack projectionMatrix; // GLFrustum viewFrustum; // GLGeometryTransform transformPipeline; // CStopWatch rotTimer; // count GLBatch floorBatch; // Floor GLTriangleBatch sphere01Batch; // ball 01 GLTriangleBatch sphere02Batch; // sphere 02 #define SPHERE_COUNT 9 GLfloat sphere01Radius = 0.3f; GLfloat sphere02Radius = 0.1 f; GLfloat spheresRadius[SPHERE_COUNT]; GLfloat spheresColor[SPHERE_COUNT][4]; GLfloat spheresRotate[SPHERE_COUNT]; // Rotate Angle per second GLFrame cameraFrame; Void setupRC() {/// Set the screen clear color glClearColor(0.5, 0.5, 0.5, 1.0); / / / initialize shader manager shaderManager InitializeStockShaders (); Floorbatch.begin (GL_LINES, 164); GLfloat y = 1.0 f; for (GLfloat i = -20; i <= 20; I += 1.0) {floorBatch.vertex3f (I, y, 20.0f); FloorBatch. Vertex3f (I, y, 20.0 f); FloorBatch. Vertex3f (20.0 f, y, I); FloorBatch. Vertex3f (20.0 f, y, I); } floorBatch.End(); GltMakeSphere (sphere01Batch, sphere01Radius, 40, 80); GltMakeSphere (sphere02Batch, sphere02Radius, 20, 40); for (int i = 0; i < SPHERE_COUNT; I ++) {/// Set the trajectory radius of the spheresRadius[I] = sphere01Radius + 2.5 * (I + 1) * sphere02Radius; GLfloat red = (rand() % 10) / 10.0; GLfloat green = (rand() % 10) / 10.0; GLfloat blue = (rand() % 10) / 10.0; spheresColor[i][0] = red; spheresColor[i][1] = blue; spheresColor[i][2] = green; SpheresColor [I] [3] = 1.0 f; GLfloat rotate = (rand() % 2 + 2) * (I % 6 + 1) * 0; spheresRotate[i] = rotate; } // enable front and back culling glEnable(GL_CULL_FACE); // enable depth test glEnable(GL_DEPTH_TEST); CameraFrame. MoveForward (5.0); } void renderScene() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // press M3DMatrix44f mCamera; cameraFrame.GetCameraMatrix(mCamera); modelViewMatrix.PushMatrix(mCamera); / / to the screen in 3.0 modelViewMatrix translation. Translate (0.0 f, f 0.0, 4.0 f); M3DVector4f vLightPos = {15.0f,10.0f, 0.0f, 1.0f}; GLfloat floorColor[] = {0.0f, 1.0f, 0.0f, 1.0f}; / / / surface shader drawing ground shaderManager UseStockShader (GLT_SHADER_FLAT, transformPipeline GetModelViewProjectionMatrix (), floorColor); floorBatch.Draw(); GLfloat sphere01Color[] = {1.0f, 0.5f, 0.0f, 1.0f}; shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF, transformPipeline.GetModelViewMatrix(), transformPipeline.GetProjectionMatrix(), vLightPos, sphere01Color); sphere01Batch.Draw(); For (int I = 0; i < SPHERE_COUNT; i ++) { modelViewMatrix.PushMatrix(); / / / orbit modelViewMatrix. Rotate (rotTimer. GetElapsedSeconds () * spheresRotate [I], 0.0 f, f 1.0, 0.0 f); ModelViewMatrix. Translate (spheresRadius [I], 0.0 f, 0.0 f); shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF, transformPipeline.GetModelViewMatrix(), transformPipeline.GetProjectionMatrix(), vLightPos, spheresColor[i]); sphere02Batch.Draw(); modelViewMatrix.PopMatrix(); } / / / the stack modelViewMatrix. PopMatrix (); GlutSwapBuffers (); GlutPostRedisplay (); } void changeSize(int w, int h) { if (h == 0) { h = 1; } // set the viewport glViewport(0, 0, w, h); / / / set the projection mode viewFrustum. SetPerspective (35.0 f, float (w)/float (h), 1.0 f, 100.0 f); / / / set the projection matrix projectionMatrix. LoadMatrix (viewFrustum. GetProjectionMatrix ()); / / / the model view matrix and projection matrix is added to the geometric transformation pipeline transformPipeline. SetMatrixStacks (modelViewMatrix projectionMatrix); } void keyboardKey(unsigned char key,int x,int y){float linear = 0.1f; Float Angular = float(m3dDegToRad(5.0f)); If (key == 'w') {camerafame.moveForward (linear); } if (key == 's') {camerafame.MoveForward(-linear); } if (key == 'a') {camerafame.MoveRight(linear); } if (key == 'd') {camerafame.MoveRight(-linear); } if (key == 'q') {camerafame.RotateWorld(Angular, 0.0f, 1.0f, 0.0f); } if (key == 'e') {camerafame.RotateWorld(-Angular, 0.0f, 1.0f, 0.0f); } } int main(int argc, char * argv[]) { gltSetWorkingDirectory(argv[0]); glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(400, 400); GlutCreateWindow (" scene "); glutReshapeFunc(changeSize); glutDisplayFunc(renderScene); glutKeyboardFunc(keyboardKey); GLenum err = glewInit(); if (err ! = GLEW_OK) { fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err)); return 1; } setupRC(); glutMainLoop(); return 0; }Copy the code

Code: Github