use generic vertex attributes

This commit is contained in:
Ian Curtis 2017-08-10 15:43:03 +00:00
parent 57d0a513fd
commit d7403afda6
4 changed files with 72 additions and 57 deletions

View file

@ -276,16 +276,16 @@ void CNew3D::RenderFrame(void)
}
}
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glEnableVertexAttribArray(3);
// before draw, specify vertex and index arrays with their offsets, offsetof is maybe evil ..
glVertexPointer (3, GL_FLOAT, sizeof(Vertex), 0);
glNormalPointer (GL_FLOAT, sizeof(Vertex), (void*)offsetof(Vertex, normal));
glTexCoordPointer (2, GL_FLOAT, sizeof(Vertex), (void*)offsetof(Vertex, texcoords));
glColorPointer (4, GL_FLOAT, sizeof(Vertex), (void*)offsetof(Vertex, color));
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));
m_r3dShader.SetShader(true);
@ -315,10 +315,10 @@ void CNew3D::RenderFrame(void)
glDisable(GL_STENCIL_TEST);
glDisable(GL_CULL_FACE);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glDisableVertexAttribArray(3);
}
void CNew3D::BeginFrame(void)

View file

@ -7,10 +7,11 @@ namespace New3D {
static const char *vertexShaderFog = R"glsl(
uniform mat4 mvp;
attribute vec3 inVertex;
void main(void)
{
gl_Position = mvp * gl_Vertex;
gl_Position = mvp * vec4(inVertex,1.0);
};
)glsl";
@ -106,10 +107,10 @@ void R3DScrollFog::DrawScrollFog(float rgba[4], float attenuation, float ambient
glUniform4f (m_locSpotEllipse, spotEllipse[0], spotEllipse[1], spotEllipse[2], spotEllipse[3]);
glUniformMatrix4fv (m_locMVP, 1, GL_FALSE, mvp);
glEnableClientState (GL_VERTEX_ARRAY);
glVertexPointer (3, GL_FLOAT, sizeof(SFVertex), 0);
glEnableVertexAttribArray (0);
glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, sizeof(SFVertex), 0);
glDrawArrays (GL_TRIANGLES, 0, 6);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableVertexAttribArray (0);
glUseProgram (0);
m_vbo.Bind (false);
@ -129,6 +130,8 @@ void R3DScrollFog::AllocResources()
m_locSpotFogColor = glGetUniformLocation(m_shaderProgram, "spotFogColor");
m_locSpotEllipse = glGetUniformLocation(m_shaderProgram, "spotEllipse");
glBindAttribLocation(m_shaderProgram, 0, "inVertex");
m_vbo.Create(GL_ARRAY_BUFFER, GL_STATIC_DRAW, sizeof(SFTriangle) * (2), m_triangles);
}

View file

@ -13,22 +13,29 @@ uniform float fogDensity;
uniform float fogStart;
uniform float modelScale;
// attributes
attribute vec3 inVertex;
attribute vec3 inNormal;
attribute vec2 inTexCoord;
attribute vec4 inColour;
// outputs to fragment shader
varying float fsFogFactor;
varying vec3 fsViewVertex;
varying vec3 fsViewNormal; // per vertex normal vector
varying vec2 fsTexCoord;
varying vec4 fsColor;
void main(void)
{
fsViewVertex = vec3(gl_ModelViewMatrix * gl_Vertex);
fsViewNormal = (mat3(gl_ModelViewMatrix) * gl_Normal) / modelScale;
fsViewVertex = vec3(gl_ModelViewMatrix * vec4(inVertex,1.0));
fsViewNormal = (mat3(gl_ModelViewMatrix) * inNormal) / modelScale;
float z = length(fsViewVertex);
fsFogFactor = fogIntensity * clamp(fogStart + z * fogDensity, 0.0, 1.0);
fsColor = gl_Color;
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
fsColor = inColour;
fsTexCoord = inTexCoord;
gl_Position = gl_ModelViewProjectionMatrix * vec4(inVertex,1.0);
}
)glsl";
@ -69,10 +76,11 @@ varying float fsFogFactor;
varying vec3 fsViewVertex;
varying vec3 fsViewNormal; // per vertex normal vector
varying vec4 fsColor;
varying vec2 fsTexCoord;
vec4 GetTextureValue()
{
vec4 tex1Data = texture2D( tex1, gl_TexCoord[0].st);
vec4 tex1Data = texture2D( tex1, fsTexCoord.st);
if(textureInverted) {
tex1Data.rgb = vec3(1.0) - vec3(tex1Data.rgb);
@ -80,7 +88,7 @@ vec4 GetTextureValue()
if (microTexture) {
vec2 scale = baseTexSize/256.0;
vec4 tex2Data = texture2D( tex2, gl_TexCoord[0].st * scale * microTextureScale);
vec4 tex2Data = texture2D( tex2, fsTexCoord.st * scale * microTextureScale);
tex1Data = (tex1Data+tex2Data)/2.0;
}
@ -300,6 +308,10 @@ 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;
}