SHARE
TWEET

Untitled

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