Advertisement
orella89

Render

Apr 15th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.71 KB | None | 0 0
  1. //DebugShape class
  2.  
  3. class DebugShape
  4. {
  5. public:
  6.     DebugShape(uint num_vertices, float* vertices, uint num_indices, uint* indices);
  7.  
  8.     void CleanUp();
  9.  
  10.     void SetTransform(float4x4 transform);
  11.     void SetColour(float4 colour);
  12.     void SetMode(int mode);
  13.  
  14.     void SetStroke(uint stroke);
  15.  
  16.     uint GetNumVertices();
  17.     float* GetVertices();
  18.     uint GetNumIndices();
  19.     uint* GetIndices();
  20.     float4 GetColour();
  21.     float4x4 GetTransform();
  22.     int GetMode();
  23.  
  24.     uint GetStroke();
  25.  
  26. private:
  27.     uint     num_vertices = 0;
  28.     float*   vertices = nullptr;
  29.     uint     num_indices = 0;
  30.     uint*    indices = nullptr;
  31.  
  32.     float4   colour = float4::zero;
  33.  
  34.     float4x4 transform = float4x4::identity;
  35.     int      mode = 0x0004;
  36.  
  37.     uint     stroke = 1;
  38. };
  39.  
  40.  
  41.  
  42. DebugShape::DebugShape(uint _num_vertices, float * _vertices, uint _num_indices, uint * _indices)
  43. {
  44.     if (_num_vertices > 0)
  45.     {
  46.         // Vertices
  47.         vertices = new float[_num_vertices * 3];
  48.         memcpy(vertices, _vertices, sizeof(float) * _num_vertices * 3);
  49.         num_vertices = _num_vertices;
  50.  
  51.         if (_num_indices > 0)
  52.         {
  53.             // Indices
  54.             indices = new uint[_num_indices];
  55.             memcpy(indices, _indices, sizeof(uint) * _num_indices);
  56.             num_indices = _num_indices;
  57.         }
  58.     }
  59.  
  60.     mode = GL_TRIANGLES;
  61.     transform = float4x4::identity;
  62.     colour = float4(1.0f, 1.0f, 1.0f, 1.0f);
  63. }
  64.  
  65. void DebugDraw::Line(float3 start, float3 end, float4 colour)
  66. {
  67.     uint num_vertices = 2;
  68.     uint num_indices = 2;
  69.  
  70.     float* vertices = new float[num_vertices * 3];
  71.     uint* indices = new uint[num_indices];
  72.  
  73.     vertices[0] = start.x;
  74.     vertices[1] = start.y;
  75.     vertices[2] = start.z;
  76.  
  77.     vertices[3] = end.x;
  78.     vertices[4] = end.y;
  79.     vertices[5] = end.z;
  80.  
  81.     indices[0] = 0;
  82.     indices[1] = 1;
  83.  
  84.     DebugShape shape(num_vertices, vertices, num_indices, indices);
  85.     shape.SetMode(GL_LINES);
  86.     shape.SetColour(colour);
  87.     shape.SetStroke(line_stroke);
  88.  
  89.     AddShape(shape);
  90.  
  91.     delete[] vertices;
  92.     delete[] indices;
  93. }
  94.  
  95. void DebugDraw::Quad(float3 center, float2 size, float4 colour)
  96. {
  97.     uint num_vertices = 4;
  98.     uint num_indices = 8;
  99.  
  100.     float* vertices = new float[num_vertices * 3];
  101.     uint* indices = new uint[num_indices];
  102.  
  103.     float3 up_left = float3(center.x - (size.x/2), center.y + (size.y/2), center.z);
  104.     float3 down_left = float3(center.x - (size.x/2), center.y - (size.y/2), center.z);
  105.     float3 down_right = float3(center.x + (size.x / 2), center.y - (size.y / 2), center.z);
  106.     float3 up_right = float3(center.x + (size.x / 2), center.y + (size.y / 2), center.z);
  107.  
  108.     vertices[0] = up_left.x;
  109.     vertices[1] = up_left.y;
  110.     vertices[2] = up_left.z;
  111.  
  112.     vertices[3] = down_left.x;
  113.     vertices[4] = down_left.y;
  114.     vertices[5] = down_left.z;
  115.  
  116.     vertices[6] = down_right.x;
  117.     vertices[7] = down_right.y;
  118.     vertices[8] = down_right.z;
  119.  
  120.     vertices[9] = up_right.x;
  121.     vertices[10] = up_right.y;
  122.     vertices[11] = up_right.z;
  123.  
  124.     indices[0] = 0;
  125.     indices[1] = 1;
  126.  
  127.     indices[2] = 1;
  128.     indices[3] = 2;
  129.  
  130.     indices[4] = 2;
  131.     indices[5] = 3;
  132.  
  133.     indices[6] = 3;
  134.     indices[7] = 0;
  135.  
  136.     DebugShape shape(num_vertices, vertices, num_indices, indices);
  137.     shape.SetMode(GL_LINES);
  138.     shape.SetColour(colour);
  139.  
  140.     shape.SetStroke(line_stroke);
  141.  
  142.     AddShape(shape);
  143.  
  144.     delete[] vertices;
  145.     delete[] indices;
  146. }
  147.  
  148. void DebugDraw::Quad(float4x4 transform, float2 size, float4 colour)
  149. {
  150.     uint num_vertices = 4;
  151.     uint num_indices = 8;
  152.  
  153.     float* vertices = new float[num_vertices * 3];
  154.     uint* indices = new uint[num_indices];
  155.  
  156.     float3 up_left = float3(-(size.x / 2), (size.y / 2), 0);
  157.     float3 down_left = float3(-(size.x / 2), -(size.y / 2), 0);
  158.     float3 down_right = float3((size.x / 2), -(size.y / 2), 0);
  159.     float3 up_right = float3((size.x / 2), (size.y / 2), 0);
  160.  
  161.     vertices[0] = up_left.x;
  162.     vertices[1] = up_left.y;
  163.     vertices[2] = up_left.z;
  164.  
  165.     vertices[3] = down_left.x;
  166.     vertices[4] = down_left.y;
  167.     vertices[5] = down_left.z;
  168.  
  169.     vertices[6] = down_right.x;
  170.     vertices[7] = down_right.y;
  171.     vertices[8] = down_right.z;
  172.  
  173.     vertices[9] = up_right.x;
  174.     vertices[10] = up_right.y;
  175.     vertices[11] = up_right.z;
  176.  
  177.     indices[0] = 0;
  178.     indices[1] = 1;
  179.  
  180.     indices[2] = 1;
  181.     indices[3] = 2;
  182.  
  183.     indices[4] = 2;
  184.     indices[5] = 3;
  185.  
  186.     indices[6] = 3;
  187.     indices[7] = 0;
  188.  
  189.     DebugShape shape(num_vertices, vertices, num_indices, indices);
  190.     shape.SetMode(GL_LINES);
  191.     shape.SetTransform(transform);
  192.     shape.SetColour(colour);
  193.     shape.SetStroke(line_stroke);
  194.  
  195.     AddShape(shape);
  196.  
  197.     delete[] vertices;
  198.     delete[] indices;
  199. }
  200.  
  201. void DebugDraw::Circle(float4x4 transform, float rad, float4 colour, uint resolution)
  202. {
  203.     int slices = resolution;
  204.  
  205.     float angle_slice = 360 / slices;
  206.  
  207.     uint num_vertices = slices;
  208.     uint num_indices = slices*2;
  209.  
  210.     float* vertices = new float[num_vertices * 3];
  211.     uint* indices = new uint[num_indices];
  212.  
  213.     float curr_angle = 0;
  214.     for(int i = 0; i < slices; i++)
  215.     {
  216.         vertices[(i * 3)] = cos(curr_angle * DEGTORAD) * rad;
  217.         vertices[(i * 3) + 1] = sin(curr_angle * DEGTORAD) * rad;
  218.         vertices[(i * 3) + 2] = 0;
  219.  
  220.         curr_angle += angle_slice;
  221.  
  222.         indices[i*2] = i;
  223.  
  224.         if (i+1 < slices)
  225.             indices[(i * 2) + 1] = i + 1;
  226.         else
  227.             indices[(i * 2) + 1] = 0;
  228.     }
  229.  
  230.     DebugShape shape(num_vertices, vertices, num_indices, indices);
  231.     shape.SetMode(GL_LINES);
  232.     shape.SetTransform(transform);
  233.     shape.SetColour(colour);
  234.     shape.SetStroke(line_stroke);
  235.  
  236.     AddShape(shape);
  237.  
  238.     delete[] vertices;
  239.     delete[] indices;
  240. }
  241.  
  242. void DebugDraw::AddShape(DebugShape shape)
  243. {
  244.     shapes.push_back(shape);
  245. }
  246.  
  247. void DebugDraw::Render(ComponentCamera* camera)
  248. {
  249.     if (shapes.empty()) return;
  250.  
  251.     // Activate
  252.     GLenum last_active_texture; glGetIntegerv(GL_ACTIVE_TEXTURE, (GLint*)&last_active_texture);
  253.     glActiveTexture(GL_TEXTURE0);
  254.     GLint last_program; glGetIntegerv(GL_CURRENT_PROGRAM, &last_program);
  255.     GLint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
  256.     GLint last_sampler; glGetIntegerv(GL_SAMPLER_BINDING, &last_sampler);
  257.     GLint last_array_buffer; glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer);
  258.     GLint last_element_array_buffer; glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &last_element_array_buffer);
  259.     GLint last_vertex_array; glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array);
  260.     GLint last_polygon_mode[2]; glGetIntegerv(GL_POLYGON_MODE, last_polygon_mode);
  261.     GLint last_viewport[4]; glGetIntegerv(GL_VIEWPORT, last_viewport);
  262.     GLint last_scissor_box[4]; glGetIntegerv(GL_SCISSOR_BOX, last_scissor_box);
  263.     GLenum last_blend_src_rgb; glGetIntegerv(GL_BLEND_SRC_RGB, (GLint*)&last_blend_src_rgb);
  264.     GLenum last_blend_dst_rgb; glGetIntegerv(GL_BLEND_DST_RGB, (GLint*)&last_blend_dst_rgb);
  265.     GLenum last_blend_src_alpha; glGetIntegerv(GL_BLEND_SRC_ALPHA, (GLint*)&last_blend_src_alpha);
  266.     GLenum last_blend_dst_alpha; glGetIntegerv(GL_BLEND_DST_ALPHA, (GLint*)&last_blend_dst_alpha);
  267.     GLenum last_blend_equation_rgb; glGetIntegerv(GL_BLEND_EQUATION_RGB, (GLint*)&last_blend_equation_rgb);
  268.     GLenum last_blend_equation_alpha; glGetIntegerv(GL_BLEND_EQUATION_ALPHA, (GLint*)&last_blend_equation_alpha);
  269.     GLboolean last_enable_blend = glIsEnabled(GL_BLEND);
  270.     GLboolean last_enable_cull_face = glIsEnabled(GL_CULL_FACE);
  271.     GLboolean last_enable_depth_test = glIsEnabled(GL_DEPTH_TEST);
  272.     GLboolean last_enable_scissor_test = glIsEnabled(GL_SCISSOR_TEST);
  273.  
  274.     glEnable(GL_BLEND);
  275.     glBlendEquation(GL_FUNC_ADD);
  276.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  277.     glDisable(GL_CULL_FACE);
  278.     glDisable(GL_DEPTH_TEST);
  279.     glEnable(GL_CULL_FACE);
  280.     glEnable(GL_DEPTH_TEST);
  281.     glEnable(GL_SCISSOR_TEST);
  282.     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  283.  
  284.     // Render ----
  285.     uint program = App->resources->GetShaderProgram("default_debug_program")->GetProgramID();
  286.     App->renderer3D->UseShaderProgram(program);
  287.  
  288.     App->renderer3D->SetUniformMatrix(program, "view", camera->GetViewMatrix());
  289.     App->renderer3D->SetUniformMatrix(program, "projection", camera->GetProjectionMatrix());
  290.  
  291.     uint vao;
  292.     glGenVertexArrays(1, (GLuint*)&vao);
  293.  
  294.     float4 color = float4::zero;
  295.  
  296.     for (std::vector<DebugShape>::iterator it = shapes.begin(); it != shapes.end(); ++it)
  297.     {
  298.         glLineWidth((*it).GetStroke());
  299.  
  300.         uint id_vertices_data = 0;
  301.         uint id_indices = 0;
  302.  
  303.         float4x4 trans = (*it).GetTransform();
  304.         float4 current_colour = (*it).GetColour();
  305.         int mode = (*it).GetMode();
  306.        
  307.         App->renderer3D->SetUniformMatrix(program, "Model", trans.Transposed().ptr());
  308.  
  309.         if (!current_colour.Equals(color))
  310.         {
  311.             App->renderer3D->SetUniformVector4(program, "debug_color", current_colour);
  312.         }
  313.  
  314.         glGenBuffers(1, &id_vertices_data);
  315.         glBindBuffer(GL_ARRAY_BUFFER, id_vertices_data);
  316.         glBufferData(GL_ARRAY_BUFFER, sizeof(float) * (*it).GetNumVertices() * 3, (*it).GetVertices(), GL_DYNAMIC_DRAW);
  317.  
  318.         glBindVertexArray(vao);
  319.         glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0 * sizeof(GLfloat), (void*)(0 * sizeof(GLfloat)));
  320.         glEnableVertexAttribArray(0);
  321.  
  322.         glGenBuffers(1, &id_indices);
  323.         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id_indices);
  324.         glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(uint) * (*it).GetNumIndices(), (*it).GetIndices(), GL_DYNAMIC_DRAW);
  325.  
  326.         glDrawElements(GL_TRIANGLES, (*it).GetNumIndices(), GL_UNSIGNED_INT, nullptr);
  327.  
  328.         glBindBuffer(GL_ARRAY_BUFFER, 0);
  329.         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  330.         glDeleteBuffers(1, &id_vertices_data);
  331.         glDeleteBuffers(1, &id_indices);
  332.  
  333.         glBindVertexArray(0);
  334.        
  335.     }
  336.  
  337.     glDeleteVertexArrays(1, &vao);
  338.  
  339.     // -----------
  340.  
  341.     // DeActivate
  342.     glLineWidth(1);
  343.     App->renderer3D->UseShaderProgram(last_program);
  344.     glBindTexture(GL_TEXTURE_2D, last_texture);
  345.     glBindSampler(0, last_sampler);
  346.     glActiveTexture(last_active_texture);
  347.     glBindVertexArray(last_vertex_array);
  348.     glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer);
  349.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, last_element_array_buffer);
  350.     glBlendEquationSeparate(last_blend_equation_rgb, last_blend_equation_alpha);
  351.     glBlendFuncSeparate(last_blend_src_rgb, last_blend_dst_rgb, last_blend_src_alpha, last_blend_dst_alpha);
  352.     if (last_enable_blend) glEnable(GL_BLEND); else glDisable(GL_BLEND);
  353.     if (last_enable_cull_face) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE);
  354.     if (last_enable_depth_test) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
  355.     if (last_enable_scissor_test) glEnable(GL_SCISSOR_TEST); else glDisable(GL_SCISSOR_TEST);
  356.     glPolygonMode(GL_FRONT_AND_BACK, last_polygon_mode[0]);
  357.     glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]);
  358.     glScissor(last_scissor_box[0], last_scissor_box[1], (GLsizei)last_scissor_box[2], (GLsizei)last_scissor_box[3]);
  359. }
  360.  
  361. //////////////////////////////////
  362.  
  363. //Vertex Shader
  364.  
  365. #version 330 core
  366. layout(location = 0) in vec3 position;
  367. uniform mat4 Model;
  368. uniform mat4 view;
  369. uniform mat4 projection;
  370.  
  371. void main()
  372. {
  373.     gl_Position = projection * view * Model * vec4(position, 1.0f);
  374. }
  375.  
  376. //Fragment Shader
  377.  
  378. #version 330 core
  379. out vec4 color;
  380.  
  381. uniform vec4 debug_color;
  382. void main()
  383. {
  384.     color = debug_color;
  385. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement