Spirit77

Simple OpenGL quad using user-defined interleaved vertex

May 14th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.56 KB | None | 0 0
  1. ///-------------------------------------------------------------------------------------------------
  2. // Interleaved3DVertex.h
  3. #include <glm/glm.hpp>
  4. using glm::vec4;
  5. using glm::vec3;
  6. using glm::vec2;
  7.  
  8. struct Interleaved3DVertex
  9. {
  10.     vec4 position;          // Vertex position
  11.     vec3 normal;            // Vertex normal
  12.     vec2 texCoord;          // Texture (UV) coords
  13.  
  14.     enum MEMBER_SIZE        // Member size in bytes
  15.     {
  16.         PositionSize    = sizeof(vec4),
  17.         NormalSize  = sizeof(vec3),
  18.         TexCoordSize    = sizeof(vec2),
  19.     };
  20.  
  21.     enum MEMBER_OFFSET      // Member offset in bytes for each member
  22.     {
  23.         PositionOffset  = 0,
  24.         NormalOffset    = PositionOffset + PositionSize,
  25.         TexCoordOffset  = NormalOffset + NormalSize,
  26.     };
  27.  
  28. }; // struct Interleaved3DVertex
  29.  
  30. ///-------------------------------------------------------------------------------------------------
  31. // MyObject.h
  32.  
  33. class MyObject
  34. {
  35. public:
  36.  
  37.     MyObject() : vao(0), m_vbo(0), m_ibo(0) {};
  38.     virtual MyObject();
  39.  
  40.     bool Init();
  41.  
  42.     void Draw(unsigned int a_shader, unsigned int a_texture);
  43.  
  44.     void Release();
  45.  
  46. protected:
  47.  
  48.     unsigned int m_vao, m_vbo, m_ibo;
  49.  
  50. }; // class MyObject
  51.  
  52. ///-------------------------------------------------------------------------------------------------
  53. // MyObject.cpp
  54. #include "MyObject.h"
  55. #include "Interleaved3DVertex.h"
  56.  
  57. bool MyObject::Init()
  58. {
  59.     Interleaved3DVertex arrVertices[4];
  60.     unsigned int arrIndices[6];
  61.  
  62.     // Set vertex positions
  63.     arrVertices[0].position = glm::vec4(-1,-1,0,1);
  64.     arrVertices[1].position = glm::vec4(-1,1,0,1);
  65.     arrVertices[2].position = glm::vec4(1,1,0,1);
  66.     arrVertices[3].position = glm::vec4(1,-1,0,1);
  67.  
  68.     // Set vertex normals
  69.     arrVertices[0].normal = glm::vec3(0,0,1);
  70.     arrVertices[1].normal = glm::vec3(0,0,1);
  71.     arrVertices[2].normal = glm::vec3(0,0,1);
  72.     arrVertices[3].normal = glm::vec3(0,0,1);
  73.  
  74.     // Set vertex texture coords
  75.     arrVertices[0].texCoord = glm::vec2(0,0);
  76.     arrVertices[1].texCoord = glm::vec2(0,1);
  77.     arrVertices[2].texCoord = glm::vec2(1,1);
  78.     arrVertices[3].texCoord = glm::vec2(1,0);
  79.  
  80.     // Quad first tri
  81.     arrIndices[0] = 0;
  82.     arrIndices[1] = 3;
  83.     arrIndices[2] = 1;
  84.  
  85.     // Quad second tri
  86.     arrIndices[3] = 1;
  87.     arrIndices[4] = 3;
  88.     arrIndices[5] = 2;
  89.  
  90.     // Generate vertex buffer object & send vertices
  91.     glGenBuffers(1, &m_vbo);
  92.     glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
  93.     glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(Interleaved3DVertex), arrVertices, GL_STATIC_DRAW);
  94.    
  95.     // Generate index buffer object & send indices
  96.     glGenBuffers(1, &m_ibo);
  97.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
  98.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(unsigned int), arrIndices, GL_STATIC_DRAW);
  99.  
  100.     // Generate & bind vertex array object
  101.     glGenVertexArrays(1, &m_vao);
  102.     glBindVertexArray(m_vao);
  103.  
  104.     // Enable attribute arrays
  105.     glEnableVertexAttribArray(0);   // Vertex positions
  106.     glEnableVertexAttribArray(1);   // Normals
  107.     glEnableVertexAttribArray(2);   // Texture coords
  108.  
  109.     // Identify data size, stride & offset within the Interleaved3DVertex struct
  110.     glVertexAttribPointer(0, Interleaved3DVertex::PositionSize, GL_FLOAT, GL_FALSE, sizeof(Interleaved3DVertex), (char *)Interleaved3DVertex::PositionOffset);
  111.     glVertexAttribPointer(1, Interleaved3DVertex::NormalSize, GL_FLOAT, GL_FALSE, sizeof(Interleaved3DVertex), (char *)Interleaved2DVertex::NormalOffset);
  112.     glVertexAttribPointer(2, Interleaved3DVertex::TexCoordSize, GL_FLOAT, GL_FALSE, sizeof(Interleaved3DVertex), (char *)Interleaved2DVertex::TexCoordOffset);
  113.  
  114.     // Unbind the buffer & vertex array objects
  115.     glBindBuffer(GL_ARRAY_BUFFER, 0);
  116.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  117.     glBindVertexArray(0);
  118.  
  119.     return true;
  120. }
  121.  
  122. void MyObject::Draw(unsigned int a_shader, unsigned int a_texture)
  123. {
  124.     // Use the specified shader
  125.     glUseProgram(a_shader);
  126.  
  127.     unsigned int uiLocation = 0;
  128.  
  129.     // Tell shader which attribute pointer is associated with vertex positions
  130.     uiLocation = glGetUniformLocation(a_shader, "in_Position");
  131.     glUniform1i(uiLocation, 0);
  132.  
  133.     // Tell shader which attribute pointer is associated with vertex normals
  134.     uiLocation = glGetUniformLocation(a_shader, "in_Normal");
  135.     glUniform1i(uiLocation, 1);
  136.  
  137.     // Tell shader which attribute pointer is associated with vertex texture coords
  138.     uiLocation = glGetUniformLocation(a_shader, "in_TextureCoord");
  139.     glUniform1i(uiLocation, 2);
  140.  
  141.     // Bind texture
  142.     uiLocation = glGetUniformLocation(a_shader, "tex_Diffuse");
  143.     glBindTexture(GL_TEXTURE_2D, a_texture);
  144.     glUniform1i(uiLocation, 1);
  145.  
  146.     // Bind vertex array object & draw quad
  147.     glBindVertexArray(m_vao);
  148.     glPolygonMode(GL_FRONT, GL_TRIANGLES);
  149.     glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  150.  
  151.     // Unbind vertex array object & shader
  152.     glBindVertexArray(0);
  153.     glUseProgram(0);
  154. }
  155.  
  156. void MyObject::Release()
  157. {
  158.     glDeleteVertexArrays(1, &m_vao);
  159.     glDeleteBuffers(1, &m_vbo);
  160.     glDeleteBuffers(1, &m_ibo);
  161. }
  162.  
  163. ///-------------------------------------------------------------------------------------------------
  164. // MyShader.vert
  165.  
  166. #version 330
  167.  
  168. layout(location = 0) in vec4 in_Position;
  169. layout(location = 1) in vec3 in_Normal;
  170. layout(location = 2) in vec2 in_TextureCoord;
  171.  
  172. out vec3 Normal
  173. out vec2 TextureCoord;
  174.  
  175. void main()
  176. {
  177.     // Set position
  178.     gl_Position = in_Position;
  179.  
  180.     // Pass normal & texcoord to fragment shader
  181.     Normal = in_Normal;
  182.     TextureCoord = in_TextureCoord;
  183. }
  184.  
  185. ///-------------------------------------------------------------------------------------------------
  186. // MyShader.frag
  187.  
  188. #version 150
  189.  
  190. in vec3 Normal;         // Not used in this implementation, here as placeholder
  191. in vec2 TextureCoord;
  192.  
  193. uniform sampler2D tex_Diffuse;
  194.  
  195. void main()
  196. {
  197.     gl_FragColor = texture(tex_Diffuse, TextureCoord);
  198. }
Advertisement
Add Comment
Please, Sign In to add comment