Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///-------------------------------------------------------------------------------------------------
- // Interleaved3DVertex.h
- #include <glm/glm.hpp>
- using glm::vec4;
- using glm::vec3;
- using glm::vec2;
- struct Interleaved3DVertex
- {
- vec4 position; // Vertex position
- vec3 normal; // Vertex normal
- vec2 texCoord; // Texture (UV) coords
- enum MEMBER_SIZE // Member size in bytes
- {
- PositionSize = sizeof(vec4),
- NormalSize = sizeof(vec3),
- TexCoordSize = sizeof(vec2),
- };
- enum MEMBER_OFFSET // Member offset in bytes for each member
- {
- PositionOffset = 0,
- NormalOffset = PositionOffset + PositionSize,
- TexCoordOffset = NormalOffset + NormalSize,
- };
- }; // struct Interleaved3DVertex
- ///-------------------------------------------------------------------------------------------------
- // MyObject.h
- class MyObject
- {
- public:
- MyObject() : vao(0), m_vbo(0), m_ibo(0) {};
- virtual MyObject();
- bool Init();
- void Draw(unsigned int a_shader, unsigned int a_texture);
- void Release();
- protected:
- unsigned int m_vao, m_vbo, m_ibo;
- }; // class MyObject
- ///-------------------------------------------------------------------------------------------------
- // MyObject.cpp
- #include "MyObject.h"
- #include "Interleaved3DVertex.h"
- bool MyObject::Init()
- {
- Interleaved3DVertex arrVertices[4];
- unsigned int arrIndices[6];
- // Set vertex positions
- arrVertices[0].position = glm::vec4(-1,-1,0,1);
- arrVertices[1].position = glm::vec4(-1,1,0,1);
- arrVertices[2].position = glm::vec4(1,1,0,1);
- arrVertices[3].position = glm::vec4(1,-1,0,1);
- // Set vertex normals
- arrVertices[0].normal = glm::vec3(0,0,1);
- arrVertices[1].normal = glm::vec3(0,0,1);
- arrVertices[2].normal = glm::vec3(0,0,1);
- arrVertices[3].normal = glm::vec3(0,0,1);
- // Set vertex texture coords
- arrVertices[0].texCoord = glm::vec2(0,0);
- arrVertices[1].texCoord = glm::vec2(0,1);
- arrVertices[2].texCoord = glm::vec2(1,1);
- arrVertices[3].texCoord = glm::vec2(1,0);
- // Quad first tri
- arrIndices[0] = 0;
- arrIndices[1] = 3;
- arrIndices[2] = 1;
- // Quad second tri
- arrIndices[3] = 1;
- arrIndices[4] = 3;
- arrIndices[5] = 2;
- // Generate vertex buffer object & send vertices
- glGenBuffers(1, &m_vbo);
- glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
- glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(Interleaved3DVertex), arrVertices, GL_STATIC_DRAW);
- // Generate index buffer object & send indices
- glGenBuffers(1, &m_ibo);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(unsigned int), arrIndices, GL_STATIC_DRAW);
- // Generate & bind vertex array object
- glGenVertexArrays(1, &m_vao);
- glBindVertexArray(m_vao);
- // Enable attribute arrays
- glEnableVertexAttribArray(0); // Vertex positions
- glEnableVertexAttribArray(1); // Normals
- glEnableVertexAttribArray(2); // Texture coords
- // Identify data size, stride & offset within the Interleaved3DVertex struct
- glVertexAttribPointer(0, Interleaved3DVertex::PositionSize, GL_FLOAT, GL_FALSE, sizeof(Interleaved3DVertex), (char *)Interleaved3DVertex::PositionOffset);
- glVertexAttribPointer(1, Interleaved3DVertex::NormalSize, GL_FLOAT, GL_FALSE, sizeof(Interleaved3DVertex), (char *)Interleaved2DVertex::NormalOffset);
- glVertexAttribPointer(2, Interleaved3DVertex::TexCoordSize, GL_FLOAT, GL_FALSE, sizeof(Interleaved3DVertex), (char *)Interleaved2DVertex::TexCoordOffset);
- // Unbind the buffer & vertex array objects
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
- glBindVertexArray(0);
- return true;
- }
- void MyObject::Draw(unsigned int a_shader, unsigned int a_texture)
- {
- // Use the specified shader
- glUseProgram(a_shader);
- unsigned int uiLocation = 0;
- // Tell shader which attribute pointer is associated with vertex positions
- uiLocation = glGetUniformLocation(a_shader, "in_Position");
- glUniform1i(uiLocation, 0);
- // Tell shader which attribute pointer is associated with vertex normals
- uiLocation = glGetUniformLocation(a_shader, "in_Normal");
- glUniform1i(uiLocation, 1);
- // Tell shader which attribute pointer is associated with vertex texture coords
- uiLocation = glGetUniformLocation(a_shader, "in_TextureCoord");
- glUniform1i(uiLocation, 2);
- // Bind texture
- uiLocation = glGetUniformLocation(a_shader, "tex_Diffuse");
- glBindTexture(GL_TEXTURE_2D, a_texture);
- glUniform1i(uiLocation, 1);
- // Bind vertex array object & draw quad
- glBindVertexArray(m_vao);
- glPolygonMode(GL_FRONT, GL_TRIANGLES);
- glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
- // Unbind vertex array object & shader
- glBindVertexArray(0);
- glUseProgram(0);
- }
- void MyObject::Release()
- {
- glDeleteVertexArrays(1, &m_vao);
- glDeleteBuffers(1, &m_vbo);
- glDeleteBuffers(1, &m_ibo);
- }
- ///-------------------------------------------------------------------------------------------------
- // MyShader.vert
- #version 330
- layout(location = 0) in vec4 in_Position;
- layout(location = 1) in vec3 in_Normal;
- layout(location = 2) in vec2 in_TextureCoord;
- out vec3 Normal
- out vec2 TextureCoord;
- void main()
- {
- // Set position
- gl_Position = in_Position;
- // Pass normal & texcoord to fragment shader
- Normal = in_Normal;
- TextureCoord = in_TextureCoord;
- }
- ///-------------------------------------------------------------------------------------------------
- // MyShader.frag
- #version 150
- in vec3 Normal; // Not used in this implementation, here as placeholder
- in vec2 TextureCoord;
- uniform sampler2D tex_Diffuse;
- void main()
- {
- gl_FragColor = texture(tex_Diffuse, TextureCoord);
- }
Advertisement
Add Comment
Please, Sign In to add comment