From 8f622714a75a7ce2e077e32a86e713910184f140 Mon Sep 17 00:00:00 2001 From: Ian Curtis Date: Fri, 11 Aug 2017 11:59:41 +0000 Subject: [PATCH] Fix attribute locations. glBindAttribLocation must be called linking --- Src/Graphics/New3D/New3D.cpp | 8 ++++---- Src/Graphics/New3D/R3DScrollFog.cpp | 4 ++-- Src/Graphics/New3D/R3DScrollFog.h | 15 +++++++++------ Src/Graphics/New3D/R3DShader.cpp | 10 +++++----- Src/Graphics/New3D/R3DShader.h | 13 +++++++------ 5 files changed, 27 insertions(+), 23 deletions(-) diff --git a/Src/Graphics/New3D/New3D.cpp b/Src/Graphics/New3D/New3D.cpp index 9edbf7d..dc8c87a 100644 --- a/Src/Graphics/New3D/New3D.cpp +++ b/Src/Graphics/New3D/New3D.cpp @@ -284,10 +284,10 @@ void CNew3D::RenderFrame(void) glEnableVertexAttribArray(3); // before draw, specify vertex and index arrays with their offsets, offsetof is maybe evil .. - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, normal)); - glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, texcoords)); - glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, color)); + glVertexAttribPointer(m_r3dShader.GetVertexAttribPos("inVertex"), 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0); + glVertexAttribPointer(m_r3dShader.GetVertexAttribPos("inNormal"), 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, normal)); + glVertexAttribPointer(m_r3dShader.GetVertexAttribPos("inTexCoord"), 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, texcoords)); + glVertexAttribPointer(m_r3dShader.GetVertexAttribPos("inColour"), 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, color)); m_r3dShader.SetShader(true); diff --git a/Src/Graphics/New3D/R3DScrollFog.cpp b/Src/Graphics/New3D/R3DScrollFog.cpp index 3df2672..c6111c0 100644 --- a/Src/Graphics/New3D/R3DScrollFog.cpp +++ b/Src/Graphics/New3D/R3DScrollFog.cpp @@ -109,7 +109,7 @@ void R3DScrollFog::DrawScrollFog(float rgba[4], float attenuation, float ambient glUniformMatrix4fv (m_locMVP, 1, GL_FALSE, mvp); glEnableVertexAttribArray (0); - glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, sizeof(SFVertex), 0); + glVertexAttribPointer (m_locInVertex, 3, GL_FLOAT, GL_FALSE, sizeof(SFVertex), 0); glDrawArrays (GL_TRIANGLES, 0, 6); glDisableVertexAttribArray (0); @@ -131,7 +131,7 @@ void R3DScrollFog::AllocResources() m_locSpotFogColor = glGetUniformLocation(m_shaderProgram, "spotFogColor"); m_locSpotEllipse = glGetUniformLocation(m_shaderProgram, "spotEllipse"); - glBindAttribLocation(m_shaderProgram, 0, "inVertex"); + m_locInVertex = glGetAttribLocation(m_shaderProgram, "inVertex"); m_vbo.Create(GL_ARRAY_BUFFER, GL_STATIC_DRAW, sizeof(SFTriangle) * (2), m_triangles); } diff --git a/Src/Graphics/New3D/R3DScrollFog.h b/Src/Graphics/New3D/R3DScrollFog.h index 641e706..c71b9dc 100644 --- a/Src/Graphics/New3D/R3DScrollFog.h +++ b/Src/Graphics/New3D/R3DScrollFog.h @@ -46,12 +46,15 @@ private: GLuint m_vertexShader; GLuint m_fragmentShader; - GLuint m_locFogColour; - GLuint m_locMVP; - GLuint m_locFogAttenuation; - GLuint m_locFogAmbient; - GLuint m_locSpotFogColor; - GLuint m_locSpotEllipse; + GLint m_locFogColour; + GLint m_locMVP; + GLint m_locFogAttenuation; + GLint m_locFogAmbient; + GLint m_locSpotFogColor; + GLint m_locSpotEllipse; + + // vertex attrib locs + GLint m_locInVertex; VBO m_vbo; }; diff --git a/Src/Graphics/New3D/R3DShader.cpp b/Src/Graphics/New3D/R3DShader.cpp index e5c84ea..fce4fbe 100644 --- a/Src/Graphics/New3D/R3DShader.cpp +++ b/Src/Graphics/New3D/R3DShader.cpp @@ -308,15 +308,15 @@ bool R3DShader::LoadShader(const char* vertexShader, const char* fragmentShader) m_locSpotColor = glGetUniformLocation(m_shaderProgram, "spotColor"); m_locSpotFogColor = glGetUniformLocation(m_shaderProgram, "spotFogColor"); m_locModelScale = glGetUniformLocation(m_shaderProgram, "modelScale"); - - glBindAttribLocation(m_shaderProgram, 0, "inVertex"); - glBindAttribLocation(m_shaderProgram, 1, "inNormal"); - glBindAttribLocation(m_shaderProgram, 2, "inTexCoord"); - glBindAttribLocation(m_shaderProgram, 3, "inColour"); return success; } +GLint R3DShader::GetVertexAttribPos(const char* attrib) +{ + return glGetAttribLocation(m_shaderProgram, attrib); // probably should cache this but only called 1x per frame anyway +} + void R3DShader::SetShader(bool enable) { if (enable) { diff --git a/Src/Graphics/New3D/R3DShader.h b/Src/Graphics/New3D/R3DShader.h index db5dd29..0202b62 100644 --- a/Src/Graphics/New3D/R3DShader.h +++ b/Src/Graphics/New3D/R3DShader.h @@ -12,12 +12,13 @@ class R3DShader public: R3DShader(const Util::Config::Node &config); - bool LoadShader (const char* vertexShader = nullptr, const char* fragmentShader = nullptr); - void SetMeshUniforms (const Mesh* m); - void SetModelStates (const Model* model); - void SetViewportUniforms (const Viewport *vp); - void Start (); - void SetShader (bool enable = true); + bool LoadShader (const char* vertexShader = nullptr, const char* fragmentShader = nullptr); + void SetMeshUniforms (const Mesh* m); + void SetModelStates (const Model* model); + void SetViewportUniforms (const Viewport *vp); + void Start (); + void SetShader (bool enable = true); + GLint GetVertexAttribPos (const char* attrib); private: