Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //includes and shit:
- #include <GLTools.h>
- #include <GLShaderManager.h>
- #include <GLBatch.h>
- #include <GLFrame.h>
- #include <GLMatrixStack.h>
- #include <GLGeometryTransform.h>
- #include <GLFrustum.h>
- #include <windows.h>
- #ifdef __APPLE__
- #include <glut/glut.h>
- #else
- #define FREEGLUT_STATIC
- #include <GL/glut.h>
- #endif
- //Objects:
- GLShaderManager shaderManager;
- GLMatrixStack modelViewStack;
- GLMatrixStack projectionStack;
- GLGeometryTransform tPipeline;
- GLFrustum viewFrustum;
- GLFrame cameraFrame;
- GLBatch floorBatch;
- GLTriangleBatch sphereBatch;
- //Global vars:
- float turnSpeed = 0.0005f;
- float stepSize = 0.001f;
- //Prototypes:
- void changeSize(int w, int h); //Runs when the window is modified
- void setupRenderingContext(); //Runs once, to setup things
- void renderScene(); //Runs every frame; basic rendering pipeline
- void keyboardInput(); //Runs every frame to check if any keys are being pressed
- //Glut functions:
- void changeSize(int w, int h)
- {
- //Redefine the viewport:
- glViewport(0, 0, w, h);
- //prevent future division by zero:
- if(h == 0) {h = 1;}
- //Set up the perspective projection:
- viewFrustum.SetPerspective(35.0f, float(w)/float(h), 1.0f, 100.0f);
- projectionStack.LoadMatrix(viewFrustum.GetProjectionMatrix());
- tPipeline.SetMatrixStacks(modelViewStack, projectionStack);
- }
- void renderScene()
- {
- //Color values:
- static GLfloat vFloorColor[] = {0.58f, 0.0f, 0.83f, 1.0f}; //Violet
- static GLfloat vSphereColor[] = {0.0f, 1.0f, 1.0f, 1.0f}; //Cyan
- //Light position:
- //static GLfloat vLightPos[] = {0.0f, 2.0f, 0.0f};
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
- modelViewStack.PushMatrix();
- M3DMatrix44f mCamera;
- cameraFrame.GetCameraMatrix(mCamera);
- modelViewStack.PushMatrix(mCamera);
- //Check for keyboard input:
- keyboardInput();
- shaderManager.UseStockShader(GLT_SHADER_FLAT, tPipeline.GetModelViewProjectionMatrix(), vFloorColor);
- //shaderManager.UseStockShader(GLT_SHADER_DEFAULT_LIGHT, tPipeline.GetModelViewMatrix(), tPipeline.GetProjectionMatrix(), vFloorColor);
- floorBatch.Draw();
- shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF, tPipeline.GetModelViewMatrix(), tPipeline.GetProjectionMatrix(), vSphereColor);
- sphereBatch.Draw();
- modelViewStack.PopMatrix();
- modelViewStack.PopMatrix();
- glutSwapBuffers();
- glutPostRedisplay();
- }
- //Regular functions:
- void setupRenderingContext()
- {
- //Black blackground
- glClearColor(0, 0, 0, 1);
- shaderManager.InitializeStockShaders();
- glEnable(GL_DEPTH_TEST);
- //glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
- //Make flor
- floorBatch.Begin(GL_LINES, 324);
- for(GLfloat a = 20.0; a >= -20.0f; a -= 0.5)
- {
- floorBatch.Vertex3f(a, -0.55f, 20.0f);
- floorBatch.Vertex3f(a, -0.55f, -20.0f);
- floorBatch.Vertex3f(20.0f, -0.55f, a);
- floorBatch.Vertex3f(-20.0f, -0.55f, a);
- }
- floorBatch.End();
- //Make sphere:
- gltMakeSphere(sphereBatch, 0.5f, 10, 20);
- }
- void keyboardInput()
- {
- short keyLeft = GetKeyState(VK_LEFT);
- bool downLeft = keyLeft < 0;
- if (downLeft == true)
- {
- cameraFrame.RotateWorld(turnSpeed, 0.0f, 1.0f, 0.0f);
- }
- short keyRight = GetKeyState(VK_RIGHT);
- bool downRight = keyRight < 0;
- if (downRight == true)
- {
- cameraFrame.RotateWorld(-turnSpeed, 0.0f, 1.0f, 0.0f);
- }
- short keyForward = GetKeyState(VK_UP);
- bool downForward = keyForward < 0;
- if (downForward == true)
- {
- cameraFrame.MoveForward(stepSize);
- }
- short keyBackward = GetKeyState(VK_DOWN);
- bool downBackward = keyBackward < 0;
- if (downBackward == true)
- {
- cameraFrame.MoveForward(-stepSize);
- }
- }
- //Main:
- int main(int argc, char* argv[])
- {
- //Setup GLTools:
- gltSetWorkingDirectory(argv[0]);
- //Setup glut:
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA |GLUT_DEPTH);
- glutInitWindowSize(800, 600);
- glutCreateWindow("La FĂȘnetre");
- //Feed glut:
- glutReshapeFunc(changeSize);
- glutDisplayFunc(renderScene);
- //Setup GLEW:
- GLenum glewStatus = glewInit();
- if (GLEW_OK != glewStatus)
- {
- fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(glewStatus));
- return 1;
- }
- //Begin:
- setupRenderingContext();
- glutMainLoop();
- return 0;
- }
Add Comment
Please, Sign In to add comment