Removed leftover FFP code: We should now be GL2 GLSL1.30+ forward compatible.
authorWolfgang Steffens <WolfgangSteff@gmail.com>
Tue, 05 Jun 2012 22:37:36 +0200
changeset 7188 580cd247511e
parent 7186 013deb83086b
child 7288 5d0704f23a2a
Removed leftover FFP code: We should now be GL2 GLSL1.30+ forward compatible. Added a seperate shader for the gradient used for water drawing. Disabled 3D mode for GL2 codepath as this still has some FFP left.
hedgewars/options.inc
hedgewars/uChat.pas
hedgewars/uGearsRender.pas
hedgewars/uRender.pas
hedgewars/uStore.pas
hedgewars/uWorld.pas
share/hedgewars/Data/Shaders/default.fs
share/hedgewars/Data/Shaders/default.vs
share/hedgewars/Data/Shaders/water.fs
share/hedgewars/Data/Shaders/water.vs
--- a/hedgewars/options.inc	Tue Jun 05 22:17:06 2012 +0200
+++ b/hedgewars/options.inc	Tue Jun 05 22:37:36 2012 +0200
@@ -53,6 +53,10 @@
     {$DEFINE USE_TOUCH_INTERFACE}
 {$ENDIF}
 
+{$IFDEF GL2}
+    {$DEFINE S3D_DISABLED}
+{$ENDIF}
+
 {$IFDEF WIN32}
     {$DEFINE USE_CONTEXT_RESTORE}
 {$ENDIF}
--- a/hedgewars/uChat.pas	Tue Jun 05 22:17:06 2012 +0200
+++ b/hedgewars/uChat.pas	Tue Jun 05 22:37:36 2012 +0200
@@ -31,7 +31,7 @@
 procedure KeyPressChat(Key: Longword);
 
 implementation
-uses SDLh, uInputHandler, uTypes, uVariables, uCommands, uUtils, uTextures, uRender, uIO;
+uses SDLh, uInputHandler, uTypes, uVariables, uCommands, uUtils, uTextures, uRender, uStore, uIO;
 
 const MaxStrIndex = 27;
 
--- a/hedgewars/uGearsRender.pas	Tue Jun 05 22:17:06 2012 +0200
+++ b/hedgewars/uGearsRender.pas	Tue Jun 05 22:37:36 2012 +0200
@@ -37,7 +37,7 @@
                 end;
 
 implementation
-uses uRender, uUtils, uVariables, uAmmos, Math, uVisualGears;
+uses uRender, uStore, uUtils, uVariables, uAmmos, Math, uVisualGears;
 
 procedure DrawRopeLinesRQ(Gear: PGear);
 begin
@@ -62,7 +62,7 @@
 
     Tint($C0, $C0, $C0, $FF);
 
-    glVertexPointer(2, GL_FLOAT, 0, @RopePoints.rounded[0]);
+    SetVertexPointer(@RopePoints.rounded[0]);
     glDrawArrays(GL_LINE_STRIP, 0, RopePoints.Count + 2);
     Tint($FF, $FF, $FF, $FF);
 
--- a/hedgewars/uRender.pas	Tue Jun 05 22:17:06 2012 +0200
+++ b/hedgewars/uRender.pas	Tue Jun 05 22:37:36 2012 +0200
@@ -48,9 +48,6 @@
 procedure DrawHedgehog          (X, Y: LongInt; Dir: LongInt; Pos, Step: LongWord; Angle: real);
 procedure DrawScreenWidget      (widget: POnScreenWidget);
 
-procedure Tint                  (r, g, b, a: Byte); inline;
-procedure Tint                  (c: Longword); inline;
-
 // This is just temporary and becomes non public once everything changed to GL2
 procedure UpdateModelview;
 procedure ResetModelview;
@@ -61,8 +58,6 @@
 implementation
 uses uVariables, uStore;
 
-var LastTint: LongWord = 0;
-
 const DegToRad =  0.01745329252; // 2PI / 360
 
 procedure UpdateModelview;
@@ -169,8 +164,8 @@
 VertexBuffer[3].X:= X;
 VertexBuffer[3].Y:= rr.h + Y;
 
-glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]);
-glTexCoordPointer(2, GL_FLOAT, 0, @TextureBuffer[0]);
+SetVertexPointer(@VertexBuffer[0]);
+SetTexCoordPointer(@TextureBuffer[0]);
 glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
 end;
 
@@ -188,8 +183,8 @@
 
 glBindTexture(GL_TEXTURE_2D, Texture^.atlas^.id);
 
-glVertexPointer(2, GL_FLOAT, 0, @Texture^.vb);
-glTexCoordPointer(2, GL_FLOAT, 0, @Texture^.tb);
+SetVertexPointer(@Texture^.vb);
+SetTexCoordPointer(@Texture^.tb);
 glDrawArrays(GL_TRIANGLE_FAN, 0, Length(Texture^.vb));
 ResetModelview;
 end;
@@ -243,8 +238,8 @@
 VertexBuffer[3].X:= -hw;
 VertexBuffer[3].Y:= w / 2;
 
-glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]);
-glTexCoordPointer(2, GL_FLOAT, 0, @TextureBuffer[0]);
+SetVertexPointer(@VertexBuffer[0]);
+SetTexCoordPointer(@TextureBuffer[0]);
 glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
 
 ResetModelview;
@@ -305,8 +300,8 @@
 VertexBuffer[3].X:= -hw;
 VertexBuffer[3].Y:= hh;
 
-glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]);
-glTexCoordPointer(2, GL_FLOAT, 0, @Texture^.tb);
+SetVertexPointer(@VertexBuffer[0]);
+SetTexCoordPointer(@Texture^.tb);
 glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
 
 ResetModelview;
@@ -384,7 +379,7 @@
     VertexBuffer[1].X:= X1;
     VertexBuffer[1].Y:= Y1;
 
-    glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]);
+    SetVertexPointer(@VertexBuffer[0]);
     glDrawArrays(GL_LINES, 0, Length(VertexBuffer));
     Tint($FF, $FF, $FF, $FF);
     
@@ -420,7 +415,7 @@
 VertexBuffer[3].X:= r.x;
 VertexBuffer[3].Y:= r.y + r.h;
 
-glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]);
+SetVertexPointer(@VertexBuffer[0]);
 glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
 
 Tint($FF, $FF, $FF, $FF);
@@ -451,7 +446,7 @@
     ResetRotation;
     UpdateModelview;
     glLineWidth(Width);
-    glVertexPointer(2, GL_FLOAT, 0, @CircleVertex[0]);
+    SetVertexPointer(@CircleVertex[0]);
     glDrawArrays(GL_LINE_LOOP, 0, 60);
     glEnable(GL_TEXTURE_2D);
     glDisable(GL_LINE_SMOOTH);
@@ -490,10 +485,10 @@
 
     glBindTexture(GL_TEXTURE_2D, HHTexture^.atlas^.id);
     if Dir = -1 then
-        glVertexPointer(2, GL_FLOAT, 0, @VertexBuffers[1][0])
+        SetVertexPointer(@VertexBuffers[1][0])
     else
-        glVertexPointer(2, GL_FLOAT, 0, @VertexBuffers[0][0]);
-    glTexCoordPointer(2, GL_FLOAT, 0, @TextureBuffer[0]);
+        SetVertexPointer(@VertexBuffers[0][0]);
+    SetTexCoordPointer(@TextureBuffer[0]);
     glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
 
     ResetModelview;
@@ -546,31 +541,4 @@
 {$ENDIF}
 end;
 
-procedure Tint(r, g, b, a: Byte); inline;
-var nc, tw: Longword;
-begin
-    nc:= (a shl 24) or (b shl 16) or (g shl 8) or r;
-
-    if nc = lastTint then
-        exit;
-
-    if GrayScale then
-        begin
-        tw:= round(r * RGB_LUMINANCE_RED + g * RGB_LUMINANCE_GREEN + b * RGB_LUMINANCE_BLUE);
-        if tw > 255 then
-            tw:= 255;
-        r:= tw;
-        g:= tw;
-        b:= tw
-        end;
-
-    glColor4ub(r, g, b, a);
-    lastTint:= nc;
-end;
-
-procedure Tint(c: Longword); inline;
-begin
-    Tint(((c shr 24) and $FF), ((c shr 16) and $FF), (c shr 8) and $FF, (c and $FF))
-end;
-
 end.
--- a/hedgewars/uStore.pas	Tue Jun 05 22:17:06 2012 +0200
+++ b/hedgewars/uStore.pas	Tue Jun 05 22:37:36 2012 +0200
@@ -49,6 +49,14 @@
 procedure UpdateModelviewProjection;
 {$ENDIF}
 
+procedure Tint(r, g, b, a: Byte); inline;
+procedure Tint(c: Longword); inline;
+procedure SetTexCoordPointer(p: Pointer);
+procedure SetVertexPointer(p: Pointer);
+procedure SetColorPointer(p: Pointer);
+procedure BeginWater;
+procedure EndWater;
+
 implementation
 uses uMisc, uConsole, uMobile, uVariables, uUtils, uTextures, uRender, uRenderUtils, uCommands,
      uDebug{$IFDEF USE_CONTEXT_RESTORE}, uWorld{$ENDIF}, uMatrix;
@@ -63,9 +71,26 @@
     SDLPrimSurface: PSDL_Surface;
 {$ENDIF}
 {$IFDEF GL2}
-    Shader: GLuint;
-    uMVPLocation: GLint;
+    shaderMain: GLuint;
+    shaderWater: GLuint;
+
+    // attributes
+const
+    aVertex: GLint   = 0;
+    aTexCoord: GLint = 1;
+    aColor: GLint    = 2;
+
+var
+    uCurrentMVPLocation: GLint;
+
+    uMainMVPLocation: GLint;
+    uMainTintLocation: GLint;
+
+    uWaterMVPLocation: GLint;
+
 {$ENDIF}
+    LastTint: LongWord = 0;
+
 
 function WriteInRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: ansistring): TSDL_Rect;
 var w, h: LongInt;
@@ -664,7 +689,7 @@
     
     CloseFile(f);
 
-    writeln('Compiling shader: ' + Pathz[ptShaders] + '/' + shaderFile);
+    WriteLnToConsole('Compiling shader: ' + Pathz[ptShaders] + '/' + shaderFile);
 
     sourceA:=PChar(source);
     lengthA:=Length(source);
@@ -679,15 +704,15 @@
     begin
         GetMem(log, logLength);
         glGetShaderInfoLog(shader, logLength, nil, log);
-        writeln('========== Compiler log  ==========');
-        writeln(log);
-        writeln('===================================');
+        WriteLnToConsole('========== Compiler log  ==========');
+        WriteLnToConsole(log);
+        WriteLnToConsole('===================================');
         FreeMem(log, logLength);
     end;
 
     if compileResult <> GL_TRUE then
     begin
-        writeln('Shader compilation failed, halting');
+        WriteLnToConsole('Shader compilation failed, halting');
         halt(-1);
     end;
 
@@ -707,6 +732,11 @@
     fs:= CompileShader(shaderName + '.fs', GL_FRAGMENT_SHADER);
     glAttachShader(program_, vs);
     glAttachShader(program_, fs);
+
+    glBindAttribLocation(program_, aVertex, 'vertex');
+    glBindAttribLocation(program_, aTexCoord, 'texcoord');
+    glBindAttribLocation(program_, aColor, 'color');
+
     glLinkProgram(program_);
     glDeleteShader(vs);
     glDeleteShader(fs);
@@ -718,20 +748,21 @@
     begin
         GetMem(log, logLength);
         glGetProgramInfoLog(program_, logLength, nil, log);
-        writeln('========== Compiler log  ==========');
-        writeln(log);
-        writeln('===================================');
+        WriteLnToConsole('========== Compiler log  ==========');
+        WriteLnToConsole(log);
+        WriteLnToConsole('===================================');
         FreeMem(log, logLength);
     end;
 
     if linkResult <> GL_TRUE then
     begin
-        writeln('Linking program failed, halting');
+        WriteLnToConsole('Linking program failed, halting');
         halt(-1);
     end;
 
     CompileProgram:= program_;
 end;
+
 {$ENDIF}
 
 procedure SetupOpenGL;
@@ -793,11 +824,22 @@
 
 {$IFDEF GL2}
     Load_GL_VERSION_2_0;
-    Shader:= CompileProgram('default');
-    glUseProgram(Shader);
-    glUniform1i(glGetUniformLocation(Shader, 'tex'), 0);
-    uMVPLocation:= glGetUniformLocation(Shader, 'mvp');
+
+    shaderWater:= CompileProgram('water');
+    glUseProgram(shaderWater);
+    glUniform1i(glGetUniformLocation(shaderWater, 'tex0'), 0);
+    uWaterMVPLocation:= glGetUniformLocation(shaderWater, 'mvp');
 
+    shaderMain:= CompileProgram('default');
+    glUseProgram(shaderMain);
+    glUniform1i(glGetUniformLocation(shaderMain, 'tex0'), 0);
+    uMainMVPLocation:= glGetUniformLocation(shaderMain, 'mvp');
+    uMainTintLocation:= glGetUniformLocation(shaderMain, 'tint');
+
+    uCurrentMVPLocation:= uMainMVPLocation;
+
+    Tint(255, 255, 255, 255);
+    UpdateModelviewProjection;
 {$ENDIF}
 
 {$IFNDEF S3D_DISABLED}
@@ -858,8 +900,77 @@
     glDisable(GL_DITHER);
     // enable common states by default as they save a lot
     glEnable(GL_TEXTURE_2D);
+
+{$IFDEF GL2}
+    glEnableVertexAttribArray(aVertex);
+    glEnableVertexAttribArray(aTexCoord);
+{$ELSE}
     glEnableClientState(GL_VERTEX_ARRAY);
     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+{$ENDIF}
+end;
+
+
+procedure Tint(r, g, b, a: Byte); inline;
+var
+    nc, tw: Longword;
+const
+    scale = 1.0/255.0;
+begin
+    nc:= (a shl 24) or (b shl 16) or (g shl 8) or r;
+
+    if nc = lastTint then
+        exit;
+
+    if GrayScale then
+        begin
+        tw:= round(r * RGB_LUMINANCE_RED + g * RGB_LUMINANCE_GREEN + b * RGB_LUMINANCE_BLUE);
+        if tw > 255 then
+            tw:= 255;
+        r:= tw;
+        g:= tw;
+        b:= tw
+        end;
+
+    {$IFDEF GL2}
+    glUniform4f(uMainTintLocation, r*scale, g*scale, b*scale, a*scale);
+    glColor4ub(r, g, b, a);
+    {$ELSE}
+    glColor4ub(r, g, b, a);
+    {$ENDIF}
+    lastTint:= nc;
+end;
+
+procedure Tint(c: Longword); inline;
+begin
+    Tint(((c shr 24) and $FF), ((c shr 16) and $FF), (c shr 8) and $FF, (c and $FF))
+end;
+
+procedure SetTexCoordPointer(p: Pointer);
+begin
+    {$IFDEF GL2}
+    glVertexAttribPointer(aTexCoord, 2, GL_FLOAT, GL_FALSE, 0, p);
+    {$ELSE}
+    glTexCoordPointer(2, GL_FLOAT, 0, p);
+    {$ENDIF}
+end;
+
+procedure SetVertexPointer(p: Pointer);
+begin
+    {$IFDEF GL2}
+    glVertexAttribPointer(aVertex, 2, GL_FLOAT, GL_FALSE, 0, p);
+    {$ELSE}
+    glVertexPointer(2, GL_FLOAT, 0, p);
+    {$ENDIF}
+end;
+
+procedure SetColorPointer(p: Pointer);
+begin
+    {$IFDEF GL2}
+    glVertexAttribPointer(aColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, p);
+    {$ELSE}
+    glColorPointer(4, GL_UNSIGNED_BYTE, 0, p);
+    {$ENDIF}
 end;
 
 {$IFDEF GL2}
@@ -868,7 +979,7 @@
     mvp: TMatrix4x4f;
 begin
     MatrixMultiply(mvp, mProjection, mModelview);
-    glUniformMatrix4fv(uMVPLocation, 1, GL_FALSE, @mvp[0, 0]);
+    glUniformMatrix4fv(uCurrentMVPLocation, 1, GL_FALSE, @mvp[0, 0]);
 end;
 {$ENDIF GL2}
 
@@ -901,6 +1012,34 @@
     //end;
 end;
 
+procedure BeginWater;
+begin
+    {$IFDEF GL2}
+    glUseProgram(shaderWater);
+    uCurrentMVPLocation:=uWaterMVPLocation;
+    UpdateModelviewProjection;
+    glDisableVertexAttribArray(aTexCoord);
+    glEnableVertexAttribArray(aColor);
+    {$ELSE}
+    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
+    glEnableClientState(GL_COLOR_ARRAY);
+    {$ENDIF}
+end;
+
+procedure EndWater;
+begin
+    {$IFDEF GL2}
+    glUseProgram(shaderMain);
+    uCurrentMVPLocation:=uMainMVPLocation;
+    UpdateModelviewProjection;
+    glDisableVertexAttribArray(aColor);
+    glEnableVertexAttribArray(aTexCoord);
+    {$ELSE}
+    glDisableClientState(GL_COLOR_ARRAY);
+    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+    {$ENDIF}
+end;
+
 ////////////////////////////////////////////////////////////////////////////////
 procedure AddProgress;
 var r: TSDL_Rect;
@@ -1284,7 +1423,8 @@
 procedure freeModule;
 begin
 {$IFDEF GL2}
-    glDeleteProgram(Shader);
+    glDeleteProgram(shaderMain);
+    glDeleteProgram(shaderWater);
 {$ENDIF}
     StoreRelease(false);
     TTF_Quit();
--- a/hedgewars/uWorld.pas	Tue Jun 05 22:17:06 2012 +0200
+++ b/hedgewars/uWorld.pas	Tue Jun 05 22:37:36 2012 +0200
@@ -798,20 +798,20 @@
         VertexBuffer[3].X:= -lw;
         VertexBuffer[3].Y:= lh;
 
-        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
-        glEnableClientState(GL_COLOR_ARRAY);
+        BeginWater;        
         if SuddenDeathDmg then
-            glColorPointer(4, GL_UNSIGNED_BYTE, 0, @SDWaterColorArray[0])
+            SetColorPointer(@SDWaterColorArray[0])
         else
-            glColorPointer(4, GL_UNSIGNED_BYTE, 0, @WaterColorArray[0]);
+            SetColorPointer(@WaterColorArray[0]);
 
-        glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]);
+        SetVertexPointer(@VertexBuffer[0]);
 
         glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
 
-        glDisableClientState(GL_COLOR_ARRAY);
-        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+        EndWater;
+        {$IFNDEF GL2}
         glColor4ub($FF, $FF, $FF, $FF); // must not be Tint() as color array seems to stay active and color reset is required
+        {$ENDIF}
         glEnable(GL_TEXTURE_2D);
     end;
 end;
@@ -863,8 +863,8 @@
 ComputeTexcoords(SpritesData[sprite].Texture, @r, @TextureBuffer);
 
 
-glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]);
-glTexCoordPointer(2, GL_FLOAT, 0, @TextureBuffer[0]);
+SetVertexPointer(@VertexBuffer[0]);
+SetTexCoordPointer(@TextureBuffer[0]);
 glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
 
 Tint($FF, $FF, $FF, $FF);
@@ -1075,31 +1075,17 @@
 
 procedure ChangeDepth(rm: TRenderMode; d: GLfloat);
 begin
-{$IFDEF S3D_DISABLED}
-    rm:= rm; d:= d; // avoid hint
-    exit;
-{$ELSE}
     d:= d / 5;
-    if rm = rmDefault then
-        exit
-    else if rm = rmLeftEye then
+    if rm = rmLeftEye then
         d:= -d;
     cStereoDepth:= cStereoDepth + d;
     UpdateProjection;
-{$ENDIF}
 end;
  
 procedure ResetDepth(rm: TRenderMode);
 begin
-{$IFDEF S3D_DISABLED}
-    rm:= rm; // avoid hint
-    exit;
-{$ELSE}
-    if rm = rmDefault then
-        exit;
     cStereoDepth:= 0;
     UpdateProjection;
-{$ENDIF}
 end;
  
 procedure DrawWorldStereo(Lag: LongInt; RM: TRenderMode);
@@ -1555,7 +1541,7 @@
 
         glDisable(GL_TEXTURE_2D);
 
-        glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]);
+        SetVertexPointer(@VertexBuffer[0]);
         glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
 
         glEnable(GL_TEXTURE_2D);
--- a/share/hedgewars/Data/Shaders/default.fs	Tue Jun 05 22:17:06 2012 +0200
+++ b/share/hedgewars/Data/Shaders/default.fs	Tue Jun 05 22:37:36 2012 +0200
@@ -1,9 +1,13 @@
-// !!!just testing!!! 
-// This is not GLSL 1.3+ compatible, as its using the compatibility profile!
+#version 130
+
 uniform sampler2D tex0;
-varying vec4 tint;
+uniform vec4 tint;
+
+in vec2 tex;
+
+out vec4 color;
 
 void main()
 {
-    gl_FragColor = texture2D(tex0, gl_TexCoord[0].st) * tint;
+    color = texture(tex0, tex) * tint;
 }
--- a/share/hedgewars/Data/Shaders/default.vs	Tue Jun 05 22:17:06 2012 +0200
+++ b/share/hedgewars/Data/Shaders/default.vs	Tue Jun 05 22:37:36 2012 +0200
@@ -1,11 +1,16 @@
-// !!!just testing!!! 
-// This is not GLSL 1.3+ compatible, as its using the compatibility profile!
+#version 130
+
+in vec2 vertex;
+in vec2 texcoord;
+in vec4 colors;
+
+out vec2 tex;
+
 uniform mat4 mvp;
-varying vec4 tint;
 
 void main()
 {
-    gl_Position = mvp * gl_Vertex;
-    gl_TexCoord[0] = gl_MultiTexCoord0;
-    tint = gl_Color;
+    vec4 p = mvp * vec4(vertex, 0.0f, 1.0f);
+    gl_Position = p;
+    tex = texcoord;
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/share/hedgewars/Data/Shaders/water.fs	Tue Jun 05 22:37:36 2012 +0200
@@ -0,0 +1,9 @@
+#version 130
+
+in vec4 vcolor;
+out vec4 color;
+
+void main()
+{
+    color = vcolor;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/share/hedgewars/Data/Shaders/water.vs	Tue Jun 05 22:37:36 2012 +0200
@@ -0,0 +1,14 @@
+#version 130
+
+in vec2 vertex;
+in vec4 color;
+out vec4 vcolor;
+
+uniform mat4 mvp;
+
+void main()
+{
+    vec4 p = mvp * vec4(vertex, 0.0f, 1.0f);
+    gl_Position = p;
+    vcolor = color;
+}