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

View file

@ -7,10 +7,11 @@ namespace New3D {
static const char *vertexShaderFog = R"glsl( static const char *vertexShaderFog = R"glsl(
uniform mat4 mvp; uniform mat4 mvp;
attribute vec3 inVertex;
void main(void) void main(void)
{ {
gl_Position = mvp * gl_Vertex; gl_Position = mvp * vec4(inVertex,1.0);
}; };
)glsl"; )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]); glUniform4f (m_locSpotEllipse, spotEllipse[0], spotEllipse[1], spotEllipse[2], spotEllipse[3]);
glUniformMatrix4fv (m_locMVP, 1, GL_FALSE, mvp); glUniformMatrix4fv (m_locMVP, 1, GL_FALSE, mvp);
glEnableClientState (GL_VERTEX_ARRAY); glEnableVertexAttribArray (0);
glVertexPointer (3, GL_FLOAT, sizeof(SFVertex), 0); glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, sizeof(SFVertex), 0);
glDrawArrays (GL_TRIANGLES, 0, 6); glDrawArrays (GL_TRIANGLES, 0, 6);
glDisableClientState(GL_VERTEX_ARRAY); glDisableVertexAttribArray (0);
glUseProgram (0); glUseProgram (0);
m_vbo.Bind (false); m_vbo.Bind (false);
@ -129,6 +130,8 @@ void R3DScrollFog::AllocResources()
m_locSpotFogColor = glGetUniformLocation(m_shaderProgram, "spotFogColor"); m_locSpotFogColor = glGetUniformLocation(m_shaderProgram, "spotFogColor");
m_locSpotEllipse = glGetUniformLocation(m_shaderProgram, "spotEllipse"); 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); 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 fogStart;
uniform float modelScale; uniform float modelScale;
// attributes
attribute vec3 inVertex;
attribute vec3 inNormal;
attribute vec2 inTexCoord;
attribute vec4 inColour;
// outputs to fragment shader // outputs to fragment shader
varying float fsFogFactor; varying float fsFogFactor;
varying vec3 fsViewVertex; varying vec3 fsViewVertex;
varying vec3 fsViewNormal; // per vertex normal vector varying vec3 fsViewNormal; // per vertex normal vector
varying vec2 fsTexCoord;
varying vec4 fsColor; varying vec4 fsColor;
void main(void) void main(void)
{ {
fsViewVertex = vec3(gl_ModelViewMatrix * gl_Vertex); fsViewVertex = vec3(gl_ModelViewMatrix * vec4(inVertex,1.0));
fsViewNormal = (mat3(gl_ModelViewMatrix) * gl_Normal) / modelScale; fsViewNormal = (mat3(gl_ModelViewMatrix) * inNormal) / modelScale;
float z = length(fsViewVertex); float z = length(fsViewVertex);
fsFogFactor = fogIntensity * clamp(fogStart + z * fogDensity, 0.0, 1.0); fsFogFactor = fogIntensity * clamp(fogStart + z * fogDensity, 0.0, 1.0);
fsColor = gl_Color; fsColor = inColour;
gl_TexCoord[0] = gl_MultiTexCoord0; fsTexCoord = inTexCoord;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; gl_Position = gl_ModelViewProjectionMatrix * vec4(inVertex,1.0);
} }
)glsl"; )glsl";
@ -69,10 +76,11 @@ varying float fsFogFactor;
varying vec3 fsViewVertex; varying vec3 fsViewVertex;
varying vec3 fsViewNormal; // per vertex normal vector varying vec3 fsViewNormal; // per vertex normal vector
varying vec4 fsColor; varying vec4 fsColor;
varying vec2 fsTexCoord;
vec4 GetTextureValue() vec4 GetTextureValue()
{ {
vec4 tex1Data = texture2D( tex1, gl_TexCoord[0].st); vec4 tex1Data = texture2D( tex1, fsTexCoord.st);
if(textureInverted) { if(textureInverted) {
tex1Data.rgb = vec3(1.0) - vec3(tex1Data.rgb); tex1Data.rgb = vec3(1.0) - vec3(tex1Data.rgb);
@ -80,7 +88,7 @@ vec4 GetTextureValue()
if (microTexture) { if (microTexture) {
vec2 scale = baseTexSize/256.0; 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; 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_locSpotFogColor = glGetUniformLocation(m_shaderProgram, "spotFogColor");
m_locModelScale = glGetUniformLocation(m_shaderProgram, "modelScale"); 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; return success;
} }