Advertisement
Guest User

object.h

a guest
Jun 27th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.40 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <glew.h>
  4. #include <GLFW\glfw3.h>
  5. #include <glm\gtc\matrix_transform.hpp>
  6. #include <glm\gtc\type_ptr.hpp>
  7. #include <SOIL.h>
  8.  
  9. #include "utils.h"
  10. #include "enums.h"
  11. #include <iostream>
  12. #include <vector>
  13.  
  14. using namespace std;
  15.  
  16. // 3D object
  17. class object
  18. {
  19. public:
  20.     GLuint vbo;
  21.     GLuint texture;
  22.     GLuint prog; // shader program to use
  23.     glm::vec3 position;
  24.     float* vertices;
  25.     int count; // number of vertices
  26.     const char* texName; // texture file name
  27.     glm::mat4 model; //
  28.     glm::mat4 view;  // matrices
  29.     glm::mat4 proj;  //
  30.     GLint posAttrib;    //
  31.     GLint colAttrib;    // attributes
  32.     GLint texAttrib;    //
  33.     GLint normalAttrib; //
  34.     GLint uniModel;    //
  35.     GLint uniView;     // uniforms
  36.     GLint uniProj;     //
  37.     bool useTexture;
  38.     bool useNormals;
  39.     int n;
  40.  
  41.     object() { }
  42.  
  43.     object(float* _vertices, int _count, int _dataPerVertex, const char* _texName, GLuint _prog, glm::vec3 _pos, glm::mat4 _view, glm::mat4 _proj, bool _useColor = true, bool _useNormals = true)
  44.     {
  45.         vertices = _vertices;
  46.         prog = _prog;
  47.         position = _pos;
  48.         count = _count;
  49.         texName = _texName;
  50.         model = glm::mat4(1.0f) * glm::translate(model, position);
  51.         view = _view;
  52.         proj = _proj;
  53.         useTexture = true;
  54.         useNormals = _useNormals;
  55.         n = _dataPerVertex;
  56.     }
  57.  
  58.     object(float* _vertices, int _count, int _dataPerVertex, GLuint _prog, glm::vec3 _pos, glm::mat4 _view, glm::mat4 _proj, bool _useColor = true, bool _useNormals = true)
  59.     {
  60.         vertices = _vertices;
  61.         prog = _prog;
  62.         position = _pos;
  63.         count = _count;
  64.         model = glm::mat4(1.0f) * glm::translate(model, position);
  65.         view = _view;
  66.         proj = _proj;
  67.         useTexture = false;
  68.         useNormals = _useNormals;
  69.         n = _dataPerVertex;
  70.     }
  71.  
  72.     void init()
  73.     {
  74.         glGenBuffers(1, &vbo);
  75.         glBindBuffer(GL_ARRAY_BUFFER, vbo);
  76.         glBufferData(GL_ARRAY_BUFFER, sizeof(vertices) * count * n, vertices, GL_STATIC_DRAW);
  77.  
  78.         glUseProgram(prog);
  79.  
  80.         posAttrib = glGetAttribLocation(prog, "pos"); // position
  81.         glEnableVertexAttribArray(posAttrib);
  82.         glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, n * sizeof(float), nullptr);
  83.  
  84.         colAttrib = glGetAttribLocation(prog, "vColor"); // color
  85.         glEnableVertexAttribArray(colAttrib);
  86.         glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, n * sizeof(float), (void*) (3 * sizeof(float)));
  87.  
  88.         uniModel = glGetUniformLocation(prog, "model");
  89.         uniView = glGetUniformLocation(prog, "view");
  90.         uniProj = glGetUniformLocation(prog, "proj");
  91.  
  92.         if (useTexture)
  93.         {
  94.             texAttrib = glGetAttribLocation(prog, "tex_coord"); // tex coords
  95.             glEnableVertexAttribArray(texAttrib);
  96.             glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, n * sizeof(float), (void*) (6 * sizeof(float)));
  97.  
  98.             int w, h;
  99.             unsigned char* texData = SOIL_load_image(texName, &w, &h, nullptr, SOIL_LOAD_RGB);
  100.             if (texData == nullptr)
  101.             {
  102.                 cout << "The texture is nullptr" << endl;
  103.                 system("pause");
  104.             }
  105.             else
  106.             {
  107.                 glGenTextures(1, &texture);
  108.                 glBindTexture(GL_TEXTURE_2D, texture);
  109.                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, texData);
  110.                 SOIL_free_image_data(texData);
  111.                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  112.                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  113.                 glGenerateMipmap(GL_TEXTURE_2D);
  114.             }
  115.         }
  116.  
  117.         if (useNormals)
  118.         {
  119.             normalAttrib = glGetAttribLocation(prog, "normal");
  120.             glEnableVertexAttribArray(normalAttrib);
  121.             if (useTexture)
  122.             {
  123.                 glVertexAttribPointer(normalAttrib, 3, GL_FLOAT, GL_FALSE, n * sizeof(float), (void*) (8 * sizeof(float)));
  124.             }
  125.             else
  126.             {
  127.                 glVertexAttribPointer(normalAttrib, 3, GL_FLOAT, GL_FALSE, n * sizeof(float), (void*) (6 * sizeof(float)));
  128.             }
  129.         }
  130.  
  131.         glUniformMatrix4fv(uniModel, 1, GL_FALSE, glm::value_ptr(model));
  132.         glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view));
  133.         glUniformMatrix4fv(uniProj, 1, GL_FALSE, glm::value_ptr(proj));
  134.     }
  135.  
  136.     void update()
  137.     {
  138.         glUseProgram(prog);
  139.         model = glm::mat4(1.0f);
  140.         model = glm::translate(model, position);
  141.  
  142.         glUniformMatrix4fv(uniModel, 1, GL_FALSE, glm::value_ptr(model));
  143.         glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view));
  144.         glUniformMatrix4fv(uniProj, 1, GL_FALSE, glm::value_ptr(proj));
  145.     }
  146.  
  147.     void draw()
  148.     {
  149.         // binding stuff before drawing (in case more objects are drawn)
  150.         glUseProgram(prog);
  151.         glBindBuffer(GL_ARRAY_BUFFER, vbo);
  152.         if (useTexture)
  153.             glBindTexture(GL_TEXTURE_2D, texture);
  154.         glEnableVertexAttribArray(posAttrib);
  155.         glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, n * sizeof(float), nullptr);
  156.  
  157.         glEnableVertexAttribArray(colAttrib);
  158.         glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, n * sizeof(float), (void*) (3 * sizeof(float)));
  159.  
  160.         if (useTexture)
  161.         {
  162.             glEnableVertexAttribArray(texAttrib);
  163.             glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, n * sizeof(float), (void*) (6 * sizeof(float)));
  164.         }
  165.  
  166.         if (useNormals)
  167.         {
  168.             glEnableVertexAttribArray(normalAttrib);
  169.             if (useTexture)
  170.             {
  171.                 glVertexAttribPointer(normalAttrib, 3, GL_FLOAT, GL_FALSE, n * sizeof(float), (void*) (8 * sizeof(float)));
  172.             }
  173.             else
  174.             {
  175.                 glVertexAttribPointer(normalAttrib, 3, GL_FLOAT, GL_FALSE, n * sizeof(float), (void*) (6 * sizeof(float)));
  176.             }
  177.         }
  178.  
  179.         glDrawArrays(GL_TRIANGLES, 0, count);
  180.     }
  181.  
  182.     void drawWithOutline(GLuint outlineProg)
  183.     {
  184.         glClear(GL_STENCIL_BUFFER_BIT);
  185.         glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  186.         glStencilFunc(GL_ALWAYS, 1, 0xFF);
  187.         glStencilMask(0xFF);
  188.         glUseProgram(prog);
  189.         glBindBuffer(GL_ARRAY_BUFFER, vbo);
  190.         if (useTexture)
  191.             glBindTexture(GL_TEXTURE_2D, texture);
  192.         glEnableVertexAttribArray(posAttrib);
  193.         glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, n * sizeof(float), nullptr);
  194.  
  195.         glEnableVertexAttribArray(colAttrib);
  196.         glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, n * sizeof(float), (void*) (3 * sizeof(float)));
  197.  
  198.         if (useTexture)
  199.         {
  200.             glEnableVertexAttribArray(texAttrib);
  201.             glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, n * sizeof(float), (void*) (6 * sizeof(float)));
  202.         }
  203.  
  204.         if (useNormals)
  205.         {
  206.             glEnableVertexAttribArray(normalAttrib);
  207.             if (useTexture)
  208.             {
  209.                 glVertexAttribPointer(normalAttrib, 3, GL_FLOAT, GL_FALSE, n * sizeof(float), (void*) (8 * sizeof(float)));
  210.             }
  211.             else
  212.             {
  213.                 glVertexAttribPointer(normalAttrib, 3, GL_FLOAT, GL_FALSE, n * sizeof(float), (void*) (6 * sizeof(float)));
  214.             }
  215.         }
  216.  
  217.         glDrawArrays(GL_TRIANGLES, 0, count);
  218.  
  219.         glStencilFunc(GL_NOTEQUAL, 1, 0xFF);
  220.         glStencilMask(0x00);
  221.         glDisable(GL_DEPTH_TEST);
  222.  
  223.         glUseProgram(outlineProg);
  224.  
  225.         GLint pos2 = glGetAttribLocation(outlineProg, "pos");
  226.         glEnableVertexAttribArray(pos2);
  227.         glVertexAttribPointer(pos2, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), nullptr);
  228.         glm::mat4 m = glm::mat4(1.0f);
  229.         m = glm::scale(m, glm::vec3(1.1f, 1.1f, 1.1f));
  230.  
  231.         GLint uniModel2 = glGetUniformLocation(outlineProg, "model");
  232.         glUniformMatrix4fv(uniModel2, 1, GL_FALSE, glm::value_ptr(m));
  233.  
  234.         GLint uniView2 = glGetUniformLocation(outlineProg, "view");
  235.         glUniformMatrix4fv(uniView2, 1, GL_FALSE, glm::value_ptr(view));
  236.  
  237.         GLint uniProj2 = glGetUniformLocation(outlineProg, "proj");
  238.         glUniformMatrix4fv(uniProj2, 1, GL_FALSE, glm::value_ptr(proj));
  239.  
  240.         glDrawArrays(GL_TRIANGLES, 0, count);
  241.  
  242.         glStencilMask(0xFF);
  243.         glEnable(GL_DEPTH_TEST);
  244.     }
  245.  
  246.     ~object()
  247.     {
  248.         glDeleteBuffers(1, &vbo);
  249.         if (useTexture)
  250.         {
  251.             glDeleteTextures(1, &texture);
  252.         }
  253.     }
  254. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement