SHARE
TWEET

Untitled

a guest Sep 18th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "Scene.h"
  2.  
  3.  
  4. // Scene constructor, initilises OpenGL
  5. // You should add further variables to need initilised.
  6. Scene::Scene(Input* in)
  7. {
  8.     // Store pointer for input class
  9.     input = in;
  10.  
  11.     //OpenGL settings
  12.     glShadeModel(GL_SMOOTH);                            // Enable Smooth Shading
  13.     glClearColor(0.39f, 0.58f, 93.0f, 1.0f);            // Cornflour Blue Background
  14.     glClearDepth(1.0f);                                 // Depth Buffer Setup
  15.     glClearStencil(0);                                  // Clear stencil buffer
  16.     glEnable(GL_DEPTH_TEST);                            // Enables Depth Testing
  17.     glDepthFunc(GL_LEQUAL);                             // The Type Of Depth Testing To Do
  18.     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);  // Really Nice Perspective Calculations
  19.     glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
  20.  
  21.     // Other OpenGL / render setting should be applied here.
  22.  
  23.  
  24.     // Initialise scene variables
  25.     glPolygonMode(GL_FRONT, GL_LINE);
  26. }
  27.  
  28.  
  29. bool wireframe = false;
  30. void Scene::handleInput(float dt)
  31. {
  32.    
  33.  
  34.  
  35. }
  36.  
  37.  
  38. void Scene::update(float dt)
  39. {
  40.         // Calculate FPS for output
  41.  
  42.     calculateFPS();
  43.  
  44. }
  45.  
  46.  
  47.  
  48. void Scene::render() {
  49.  
  50.     // Clear Color and Depth Buffers
  51.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  52.  
  53.     // Reset transformations
  54.     glLoadIdentity();
  55.     // Set the camera
  56.     gluLookAt(0.0f, 0.0f, 15.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
  57.  
  58.     // Render geometry/scene here -------------------------------------
  59.  
  60.     glBegin(GL_TRIANGLES);
  61.     //0
  62.     glColor3f(1.0f, 0.0f, 0.0f);
  63.     glVertex3f(4.0f, 1.0f, 0.0f);
  64.     //1
  65.     glColor3f(0.0f, 0.0f, 1.0f);
  66.     glVertex3f(3.0f, -1.0f, 0.0f);
  67.     // 2
  68.     glColor3f(0.0f, 0.0f, 1.0f);
  69.     glVertex3f(5.0f, -1.0f, 0.0f);
  70.     glEnd();
  71.  
  72.     // End render geometry --------------------------------------
  73.  
  74.     // Render text, should be last object rendered.
  75.     renderTextOutput();
  76.  
  77.     // Swap buffers, after all objects are rendered.
  78.     glutSwapBuffers();
  79. }
  80.  
  81. // Handles the resize of the window. If the window changes size the perspective matrix requires re-calculation to match new window size.
  82. void Scene::resize(int w, int h)
  83. {
  84.     width = w;
  85.     height = h;
  86.     // Prevent a divide by zero, when window is too short
  87.     // (you cant make a window of zero width).
  88.     if (h == 0)
  89.         h = 1;
  90.  
  91.     float ratio = (float)w / (float)h;
  92.     fov = 45.0f;
  93.     nearPlane = 0.1f;
  94.     farPlane = 100.0f;
  95.  
  96.     // Use the Projection Matrix
  97.     glMatrixMode(GL_PROJECTION);
  98.  
  99.     // Reset Matrix
  100.     glLoadIdentity();
  101.  
  102.     // Set the viewport to be the entire window
  103.     glViewport(0, 0, w, h);
  104.  
  105.     // Set the correct perspective.
  106.     gluPerspective(fov, ratio, nearPlane, farPlane);
  107.  
  108.     // Get Back to the Modelview
  109.     glMatrixMode(GL_MODELVIEW);
  110.  
  111.  
  112. }
  113.  
  114. // Calculates FPS
  115. void Scene::calculateFPS()
  116. {
  117.  
  118.     frame++;
  119.     time = glutGet(GLUT_ELAPSED_TIME);
  120.  
  121.     if (time - timebase > 1000) {
  122.         sprintf_s(fps, "FPS: %4.2f", frame * 1000.0 / (time - timebase));
  123.         timebase = time;
  124.         frame = 0;
  125.     }
  126. }
  127.  
  128. // Compiles standard output text including FPS and current mouse position.
  129. void Scene::renderTextOutput()
  130. {
  131.     // Render current mouse position and frames per second.
  132.     sprintf_s(mouseText, "Mouse: %i, %i", input->getMouseX(), input->getMouseY());
  133.     displayText(-1.f, 0.96f, 1.f, 0.f, 0.f, mouseText);
  134.     displayText(-1.f, 0.90f, 1.f, 0.f, 0.f, fps);
  135. }
  136.  
  137. // Renders text to screen. Must be called last in render function (before swap buffers)
  138. void Scene::displayText(float x, float y, float r, float g, float b, char* string) {
  139.     // Get Lenth of string
  140.     int j = strlen(string);
  141.  
  142.     // Swap to 2D rendering
  143.     glMatrixMode(GL_PROJECTION);
  144.     glLoadIdentity();
  145.     glOrtho(-1.0, 1.0, -1.0, 1.0, 5, 100);
  146.     glMatrixMode(GL_MODELVIEW);
  147.     glLoadIdentity();
  148.     // Orthographic lookAt (along the z-axis).
  149.     gluLookAt(0.0f, 0.0f, 10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
  150.  
  151.     // Set text colour and position.
  152.     glColor3f(r, g, b);
  153.     glRasterPos2f(x, y);
  154.     // Render text.
  155.     for (int i = 0; i < j; i++) {
  156.         glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, string[i]);
  157.     }
  158.     // Reset colour to white.
  159.     glColor3f(1.f, 1.f, 1.f);
  160.  
  161.     // Swap back to 3D rendering.
  162.     glMatrixMode(GL_PROJECTION);
  163.     glLoadIdentity();
  164.     gluPerspective(fov, ((float)width / (float)height), nearPlane, farPlane);
  165.     glMatrixMode(GL_MODELVIEW);
  166. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top