Skip to content

Commit

Permalink
Cleanup opengl model example
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiashienzsch committed Jun 11, 2019
1 parent de389b0 commit 4b5adeb
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 40 deletions.
50 changes: 24 additions & 26 deletions opengl/opengl-model/Source/GL/Attributes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,60 +8,58 @@
// This class just manages the attributes that the shaders use.
struct Attributes
{
Attributes(OpenGLContext& openGLContext, OpenGLShaderProgram& shaderProgram)
Attributes(OpenGLShaderProgram& shaderProgram)
{
position.reset(createAttribute(openGLContext, shaderProgram, "position"));
normal.reset(createAttribute(openGLContext, shaderProgram, "normal"));
sourceColour.reset(createAttribute(openGLContext, shaderProgram, "sourceColour"));
textureCoordIn.reset(createAttribute(openGLContext, shaderProgram, "textureCoordIn"));
position.reset(createAttribute(shaderProgram, "position"));
normal.reset(createAttribute(shaderProgram, "normal"));
sourceColour.reset(createAttribute(shaderProgram, "sourceColour"));
textureCoordIn.reset(createAttribute(shaderProgram, "textureCoordIn"));
}

void enable(OpenGLContext& context)
void enable()
{
if (position.get() != nullptr)
{
context.extensions.glVertexAttribPointer(position->attributeID, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
context.extensions.glEnableVertexAttribArray(position->attributeID);
glVertexAttribPointer(position->attributeID, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glEnableVertexAttribArray(position->attributeID);
}

if (normal.get() != nullptr)
{
context.extensions.glVertexAttribPointer(normal->attributeID, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
(GLvoid*)(sizeof(float) * 3));
context.extensions.glEnableVertexAttribArray(normal->attributeID);
glVertexAttribPointer(normal->attributeID, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
(GLvoid*)(sizeof(float) * 3));
glEnableVertexAttribArray(normal->attributeID);
}

if (sourceColour.get() != nullptr)
{
context.extensions.glVertexAttribPointer(sourceColour->attributeID, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex),
(GLvoid*)(sizeof(float) * 6));
context.extensions.glEnableVertexAttribArray(sourceColour->attributeID);
glVertexAttribPointer(sourceColour->attributeID, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex),
(GLvoid*)(sizeof(float) * 6));
glEnableVertexAttribArray(sourceColour->attributeID);
}

if (textureCoordIn.get() != nullptr)
{
context.extensions.glVertexAttribPointer(textureCoordIn->attributeID, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
(GLvoid*)(sizeof(float) * 10));
context.extensions.glEnableVertexAttribArray(textureCoordIn->attributeID);
glVertexAttribPointer(textureCoordIn->attributeID, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
(GLvoid*)(sizeof(float) * 10));
glEnableVertexAttribArray(textureCoordIn->attributeID);
}
}

void disable(OpenGLContext& context)
void disable()
{
if (position.get() != nullptr) context.extensions.glDisableVertexAttribArray(position->attributeID);
if (normal.get() != nullptr) context.extensions.glDisableVertexAttribArray(normal->attributeID);
if (sourceColour.get() != nullptr) context.extensions.glDisableVertexAttribArray(sourceColour->attributeID);
if (textureCoordIn.get() != nullptr) context.extensions.glDisableVertexAttribArray(textureCoordIn->attributeID);
if (position.get() != nullptr) glDisableVertexAttribArray(position->attributeID);
if (normal.get() != nullptr) glDisableVertexAttribArray(normal->attributeID);
if (sourceColour.get() != nullptr) glDisableVertexAttribArray(sourceColour->attributeID);
if (textureCoordIn.get() != nullptr) glDisableVertexAttribArray(textureCoordIn->attributeID);
}

std::unique_ptr<OpenGLShaderProgram::Attribute> position, normal, sourceColour, textureCoordIn;

private:
static OpenGLShaderProgram::Attribute* createAttribute(OpenGLContext& openGLContext, OpenGLShaderProgram& shader,
const String& attributeName)
static OpenGLShaderProgram::Attribute* createAttribute(OpenGLShaderProgram& shader, const String& attributeName)
{
if (openGLContext.extensions.glGetAttribLocation(shader.getProgramID(), attributeName.toRawUTF8()) < 0)
return nullptr;
if (glGetAttribLocation(shader.getProgramID(), attributeName.toRawUTF8()) < 0) return nullptr;

return new OpenGLShaderProgram::Attribute(shader, attributeName.toRawUTF8());
}
Expand Down
1 change: 0 additions & 1 deletion opengl/opengl-model/Source/GL/Uniforms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "core/common.hpp"


// This class just manages the uniform values that the demo shaders use.
struct Uniforms
{
Expand Down
6 changes: 3 additions & 3 deletions opengl/opengl-model/Source/MainComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ void MainComponent::render()
m_uniforms->viewMatrix->setMatrix4(getViewMatrix().mat, 1, false);
}

m_shape->draw(openGLContext, *m_attributes);
m_shape->draw(*m_attributes);

// Reset the element buffers so child Components draw correctly
glBindBuffer(GL_ARRAY_BUFFER, 0);
Expand Down Expand Up @@ -226,8 +226,8 @@ void MainComponent::createShaders()
m_shader.reset(newShader.release());
m_shader->use();

m_shape.reset(new Shape(openGLContext));
m_attributes.reset(new Attributes(openGLContext, *m_shader));
m_shape.reset(new Shape());
m_attributes.reset(new Attributes(*m_shader));
m_uniforms.reset(new Uniforms(openGLContext, *m_shader));

statusText = "GLSL: v" + String(OpenGLShaderProgram::getLanguageVersion(), 2);
Expand Down
17 changes: 8 additions & 9 deletions opengl/opengl-model/Source/OBJ/Shape.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,33 @@
*/
struct Shape
{
Shape(OpenGLContext& openGLContext)
Shape()
{
if (shapeFile.load(String(BinaryData::teapot_obj)).wasOk())
{
for (auto* s : shapeFile.shapes)
{
vertexBuffers.add(new VertexBuffer(openGLContext, *s));
vertexBuffers.add(new VertexBuffer(*s));
}
}
}

void draw(OpenGLContext& context, Attributes& glAttributes)
void draw(Attributes& glAttributes)
{
for (auto* vertexBuffer : vertexBuffers)
{
vertexBuffer->bind();

glAttributes.enable(context);
glAttributes.enable();
glDrawElements(GL_TRIANGLES, vertexBuffer->numIndices, GL_UNSIGNED_INT, 0);
glAttributes.disable(context);
glAttributes.disable();
}
}

private:
struct VertexBuffer
{
VertexBuffer(OpenGLContext& context, WavefrontObjFile::Shape& aShape) : openGLContext(context)
VertexBuffer(WavefrontObjFile::Shape& aShape)
{
numIndices = aShape.mesh.indices.size();

Expand Down Expand Up @@ -73,7 +73,6 @@ struct Shape

GLuint vertexBuffer, indexBuffer;
int numIndices;
OpenGLContext& openGLContext;

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(VertexBuffer)
};
Expand All @@ -84,8 +83,8 @@ struct Shape
static void createVertexListFromMesh(const WavefrontObjFile::Mesh& mesh, Array<Vertex>& list, Colour colour)
{
auto scale = 0.2f;
WavefrontObjFile::TextureCoord defaultTexCoord{0.5f, 0.5f};
WavefrontObjFile::Vertex defaultNormal{0.5f, 0.5f, 0.5f};
WavefrontObjFile::TextureCoord defaultTexCoord {0.5f, 0.5f};
WavefrontObjFile::Vertex defaultNormal {0.5f, 0.5f, 0.5f};

for (auto i = 0; i < mesh.vertices.size(); ++i)
{
Expand Down
1 change: 0 additions & 1 deletion opengl/opengl-model/Source/OBJ/WavefrontObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include "../core/common.hpp"


//==============================================================================
/**
This is a quick-and-dirty parser for the 3D OBJ file format.
Expand Down

0 comments on commit 4b5adeb

Please sign in to comment.