Fix attribute locations. glBindAttribLocation must be called linking

This commit is contained in:
Ian Curtis 2017-08-11 11:59:41 +00:00
parent a315627401
commit 8f622714a7
5 changed files with 27 additions and 23 deletions

View file

@ -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);

View file

@ -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);
}

View file

@ -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;
};

View file

@ -309,14 +309,14 @@ bool R3DShader::LoadShader(const char* vertexShader, const char* fragmentShader)
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) {

View file

@ -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: