66 {$ENDIF} |
66 {$ENDIF} |
67 procedure RenderSetClearColor (r, g, b, a: real); |
67 procedure RenderSetClearColor (r, g, b, a: real); |
68 procedure Tint (r, g, b, a: Byte); inline; |
68 procedure Tint (r, g, b, a: Byte); inline; |
69 procedure Tint (c: Longword); inline; |
69 procedure Tint (c: Longword); inline; |
70 procedure untint(); inline; |
70 procedure untint(); inline; |
71 procedure setTintAdd (f: boolean); inline; |
71 procedure setTintAdd (enable: boolean); inline; |
72 |
72 |
73 // call this to finish the rendering of current frame |
73 // call this to finish the rendering of current frame |
74 procedure FinishRender(); |
74 procedure FinishRender(); |
75 |
75 |
76 function isAreaOffscreen(X, Y, Width, Height: LongInt): boolean; inline; |
76 function isAreaOffscreen(X, Y, Width, Height: LongInt): boolean; inline; |
104 procedure openglTranslatef (X, Y, Z: GLfloat); inline; |
104 procedure openglTranslatef (X, Y, Z: GLfloat); inline; |
105 |
105 |
106 |
106 |
107 implementation |
107 implementation |
108 uses {$IFNDEF PAS2C} StrUtils, {$ENDIF}uVariables, uUtils |
108 uses {$IFNDEF PAS2C} StrUtils, {$ENDIF}uVariables, uUtils |
109 {$IFDEF GL2}, uMatrix, uConsole{$ENDIF}, uConsts; |
109 {$IFDEF GL2}, uMatrix, uConsole, uPhysFSLayer, uDebug{$ENDIF}, uConsts; |
110 |
110 |
111 {$IFDEF USE_TOUCH_INTERFACE} |
111 {$IFDEF USE_TOUCH_INTERFACE} |
112 const |
112 const |
113 FADE_ANIM_TIME = 500; |
113 FADE_ANIM_TIME = 500; |
114 MOVE_ANIM_TIME = 500; |
114 MOVE_ANIM_TIME = 500; |
121 {$ENDIF} |
121 {$ENDIF} |
122 |
122 |
123 var VertexBuffer : array [0 ..59] of TVertex2f; |
123 var VertexBuffer : array [0 ..59] of TVertex2f; |
124 TextureBuffer: array [0 .. 7] of TVertex2f; |
124 TextureBuffer: array [0 .. 7] of TVertex2f; |
125 LastTint: LongWord = 0; |
125 LastTint: LongWord = 0; |
|
126 {$IFNDEF GL2} |
126 LastColorPointer , LastTexCoordPointer , LastVertexPointer : Pointer; |
127 LastColorPointer , LastTexCoordPointer , LastVertexPointer : Pointer; |
127 {$IFDEF GL2} |
|
128 LastColorPointerN, LastTexCoordPointerN, LastVertexPointerN: Integer; |
|
129 {$ENDIF} |
128 {$ENDIF} |
130 |
129 |
131 {$IFDEF USE_S3D_RENDERING} |
130 {$IFDEF USE_S3D_RENDERING} |
132 // texture/vertex buffers for left/right/default eye modes |
131 // texture/vertex buffers for left/right/default eye modes |
133 texLRDtb, texLvb, texRvb: array [0..3] of TVertex2f; |
132 texLRDtb, texLvb, texRvb: array [0..3] of TVertex2f; |
738 glDisableVertexAttribArray(aTexCoord); |
737 glDisableVertexAttribArray(aTexCoord); |
739 glEnableVertexAttribArray(aColor); |
738 glEnableVertexAttribArray(aColor); |
740 {$ELSE} |
739 {$ELSE} |
741 glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
740 glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
742 glEnableClientState(GL_COLOR_ARRAY); |
741 glEnableClientState(GL_COLOR_ARRAY); |
|
742 LastTexCoordPointer:= nil; |
743 {$ENDIF} |
743 {$ENDIF} |
744 LastTexCoordPointer:= nil; |
|
745 end |
744 end |
746 else |
745 else |
747 begin |
746 begin |
748 {$IFDEF GL2} |
747 {$IFDEF GL2} |
749 glDisableVertexAttribArray(aColor); |
748 glDisableVertexAttribArray(aColor); |
750 glEnableVertexAttribArray(aTexCoord); |
749 glEnableVertexAttribArray(aTexCoord); |
751 {$ELSE} |
750 {$ELSE} |
752 glDisableClientState(GL_COLOR_ARRAY); |
751 glDisableClientState(GL_COLOR_ARRAY); |
753 glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
752 glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
|
753 LastColorPointer:= nil; |
754 {$ENDIF} |
754 {$ENDIF} |
755 LastColorPointer:= nil; |
|
756 end; |
755 end; |
757 EnableTexture(not b); |
756 EnableTexture(not b); |
758 end; |
757 end; |
759 |
758 |
760 procedure UpdateModelviewProjection(); inline; |
759 procedure UpdateModelviewProjection(); inline; |
773 end; |
772 end; |
774 |
773 |
775 procedure SetTexCoordPointer(p: Pointer; n: Integer); inline; |
774 procedure SetTexCoordPointer(p: Pointer; n: Integer); inline; |
776 begin |
775 begin |
777 {$IFDEF GL2} |
776 {$IFDEF GL2} |
778 if (p = LastTexCoordPointer) and (n = LastTexCoordPointerN) then |
|
779 exit; |
|
780 glBindBuffer(GL_ARRAY_BUFFER, tBuffer); |
777 glBindBuffer(GL_ARRAY_BUFFER, tBuffer); |
781 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * n * 2, p, GL_STATIC_DRAW); |
778 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * n * 2, p, GL_STATIC_DRAW); |
782 glEnableVertexAttribArray(aTexCoord); |
779 glEnableVertexAttribArray(aTexCoord); |
783 glVertexAttribPointer(aTexCoord, 2, GL_FLOAT, GL_FALSE, 0, pointer(0)); |
780 glVertexAttribPointer(aTexCoord, 2, GL_FLOAT, GL_FALSE, 0, pointer(0)); |
784 LastTexCoordPointerN:= n; |
|
785 {$ELSE} |
781 {$ELSE} |
786 if p = LastTexCoordPointer then |
782 if p = LastTexCoordPointer then |
787 exit; |
783 exit; |
788 n:= n; |
784 n:= n; |
789 glTexCoordPointer(2, GL_FLOAT, 0, p); |
785 glTexCoordPointer(2, GL_FLOAT, 0, p); |
790 {$ENDIF} |
|
791 LastTexCoordPointer:= p; |
786 LastTexCoordPointer:= p; |
|
787 {$ENDIF} |
792 end; |
788 end; |
793 |
789 |
794 procedure SetVertexPointer(p: Pointer; n: Integer); inline; |
790 procedure SetVertexPointer(p: Pointer; n: Integer); inline; |
795 begin |
791 begin |
796 {$IFDEF GL2} |
792 {$IFDEF GL2} |
797 if (p = LastVertexPointer) and (n = LastVertexPointerN) then |
|
798 exit; |
|
799 glBindBuffer(GL_ARRAY_BUFFER, vBuffer); |
793 glBindBuffer(GL_ARRAY_BUFFER, vBuffer); |
800 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * n * 2, p, GL_STATIC_DRAW); |
794 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * n * 2, p, GL_STATIC_DRAW); |
801 glEnableVertexAttribArray(aVertex); |
795 glEnableVertexAttribArray(aVertex); |
802 glVertexAttribPointer(aVertex, 2, GL_FLOAT, GL_FALSE, 0, pointer(0)); |
796 glVertexAttribPointer(aVertex, 2, GL_FLOAT, GL_FALSE, 0, pointer(0)); |
803 LastVertexPointerN:= n; |
|
804 {$ELSE} |
797 {$ELSE} |
805 if p = LastVertexPointer then |
798 if p = LastVertexPointer then |
806 exit; |
799 exit; |
807 n:= n; |
800 n:= n; |
808 glVertexPointer(2, GL_FLOAT, 0, p); |
801 glVertexPointer(2, GL_FLOAT, 0, p); |
809 {$ENDIF} |
|
810 LastVertexPointer:= p; |
802 LastVertexPointer:= p; |
|
803 {$ENDIF} |
811 end; |
804 end; |
812 |
805 |
813 procedure SetColorPointer(p: Pointer; n: Integer); inline; |
806 procedure SetColorPointer(p: Pointer; n: Integer); inline; |
814 begin |
807 begin |
815 {$IFDEF GL2} |
808 {$IFDEF GL2} |
816 if (p = LastColorPointer) and (n = LastColorPointerN) then |
|
817 exit; |
|
818 glBindBuffer(GL_ARRAY_BUFFER, cBuffer); |
809 glBindBuffer(GL_ARRAY_BUFFER, cBuffer); |
819 glBufferData(GL_ARRAY_BUFFER, n * 4, p, GL_STATIC_DRAW); |
810 glBufferData(GL_ARRAY_BUFFER, n * 4, p, GL_STATIC_DRAW); |
820 glEnableVertexAttribArray(aColor); |
811 glEnableVertexAttribArray(aColor); |
821 glVertexAttribPointer(aColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, pointer(0)); |
812 glVertexAttribPointer(aColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, pointer(0)); |
822 LastColorPointerN:= n; |
|
823 {$ELSE} |
813 {$ELSE} |
824 if p = LastColorPointer then |
814 if p = LastColorPointer then |
825 exit; |
815 exit; |
826 n:= n; |
816 n:= n; |
827 glColorPointer(4, GL_UNSIGNED_BYTE, 0, p); |
817 glColorPointer(4, GL_UNSIGNED_BYTE, 0, p); |
828 {$ENDIF} |
|
829 LastColorPointer:= p; |
818 LastColorPointer:= p; |
|
819 {$ENDIF} |
830 end; |
820 end; |
831 |
821 |
832 procedure EnableTexture(enable:Boolean); |
822 procedure EnableTexture(enable:Boolean); |
833 begin |
823 begin |
834 {$IFDEF GL2} |
824 {$IFDEF GL2} |
965 TextureBuffer[2].X:= _r; |
957 TextureBuffer[2].X:= _r; |
966 TextureBuffer[2].Y:= _b; |
958 TextureBuffer[2].Y:= _b; |
967 TextureBuffer[3].X:= _l; |
959 TextureBuffer[3].X:= _l; |
968 TextureBuffer[3].Y:= _b; |
960 TextureBuffer[3].Y:= _b; |
969 |
961 |
|
962 |
|
963 glBindTexture(GL_TEXTURE_2D, SourceTexture^.id); |
970 SetVertexPointer(@VertexBuffer[0], 4); |
964 SetVertexPointer(@VertexBuffer[0], 4); |
971 SetTexCoordPointer(@TextureBuffer[0], 4); |
965 SetTexCoordPointer(@TextureBuffer[0], 4); |
|
966 |
972 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
967 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
|
968 |
973 end; |
969 end; |
974 |
970 |
975 procedure DrawTexture(X, Y: LongInt; Texture: PTexture); inline; |
971 procedure DrawTexture(X, Y: LongInt; Texture: PTexture); inline; |
976 begin |
972 begin |
977 DrawTexture(X, Y, Texture, 1.0); |
973 DrawTexture(X, Y, Texture, 1.0); |
978 end; |
974 end; |
979 |
975 |
980 procedure DrawTexture(X, Y: LongInt; Texture: PTexture; Scale: GLfloat); |
976 procedure DrawTexture(X, Y: LongInt; Texture: PTexture; Scale: GLfloat); |
981 begin |
977 begin |
982 |
|
983 openglPushMatrix; |
978 openglPushMatrix; |
984 openglTranslatef(X, Y, 0); |
979 openglTranslatef(X, Y, 0); |
985 |
980 |
986 if Scale <> 1.0 then |
981 if Scale <> 1.0 then |
987 openglScalef(Scale, Scale, 1); |
982 openglScalef(Scale, Scale, 1); |
1023 |
1019 |
1024 UpdateModelviewProjection; |
1020 UpdateModelviewProjection; |
1025 |
1021 |
1026 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
1022 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
1027 openglPopMatrix; |
1023 openglPopMatrix; |
|
1024 |
|
1025 UpdateModelviewProjection; |
1028 end; |
1026 end; |
1029 |
1027 |
1030 procedure DrawTextureF(Texture: PTexture; Scale: GLfloat; X, Y, Frame, Dir, w, h: LongInt); |
1028 procedure DrawTextureF(Texture: PTexture; Scale: GLfloat; X, Y, Frame, Dir, w, h: LongInt); |
1031 begin |
1029 begin |
1032 DrawTextureRotatedF(Texture, Scale, 0, 0, X, Y, Frame, Dir, w, h, 0) |
1030 DrawTextureRotatedF(Texture, Scale, 0, 0, X, Y, Frame, Dir, w, h, 0) |
1169 |
1169 |
1170 // apply angle after (conditional) mirroring |
1170 // apply angle after (conditional) mirroring |
1171 if Angle <> 0 then |
1171 if Angle <> 0 then |
1172 openglRotatef(Angle, 0, 0, 1); |
1172 openglRotatef(Angle, 0, 0, 1); |
1173 |
1173 |
|
1174 UpdateModelviewProjection; |
|
1175 |
1174 DrawSprite(Sprite, -SpritesData[Sprite].Width div 2, -SpritesData[Sprite].Height div 2, Frame); |
1176 DrawSprite(Sprite, -SpritesData[Sprite].Width div 2, -SpritesData[Sprite].Height div 2, Frame); |
1175 |
1177 |
1176 openglPopMatrix; |
1178 openglPopMatrix; |
|
1179 |
|
1180 UpdateModelviewProjection; |
1177 |
1181 |
1178 end; |
1182 end; |
1179 |
1183 |
1180 procedure DrawSpritePivotedF(Sprite: TSprite; X, Y, Frame, Dir, PivotX, PivotY: LongInt; Angle: real); |
1184 procedure DrawSpritePivotedF(Sprite: TSprite; X, Y, Frame, Dir, PivotX, PivotY: LongInt; Angle: real); |
1181 begin |
1185 begin |
1340 UpdateModelviewProjection; |
1347 UpdateModelviewProjection; |
1341 |
1348 |
1342 DrawLineOnScreen(X0, Y0, X1, Y1, Width, r, g, b, a); |
1349 DrawLineOnScreen(X0, Y0, X1, Y1, Width, r, g, b, a); |
1343 |
1350 |
1344 openglPopMatrix(); |
1351 openglPopMatrix(); |
|
1352 |
|
1353 UpdateModelviewProjection; |
1345 end; |
1354 end; |
1346 |
1355 |
1347 procedure DrawLineOnScreen(X0, Y0, X1, Y1, Width: Single; r, g, b, a: Byte); |
1356 procedure DrawLineOnScreen(X0, Y0, X1, Y1, Width: Single; r, g, b, a: Byte); |
1348 begin |
1357 begin |
1349 glEnable(GL_LINE_SMOOTH); |
1358 glEnable(GL_LINE_SMOOTH); |
1959 if cWhiteColor = LastTint then exit; |
1967 if cWhiteColor = LastTint then exit; |
1960 openglTint($FF, $FF, $FF, $FF); |
1968 openglTint($FF, $FF, $FF, $FF); |
1961 LastTint:= cWhiteColor; |
1969 LastTint:= cWhiteColor; |
1962 end; |
1970 end; |
1963 |
1971 |
1964 procedure setTintAdd(f: boolean); inline; |
1972 procedure setTintAdd(enable: boolean); inline; |
1965 begin |
1973 begin |
1966 if f then |
1974 {$IFDEF GL2} |
|
1975 if enable then |
|
1976 glUniform1i(glGetUniformLocation(shaderMain, pchar('tintAdd')), 1) |
|
1977 else |
|
1978 glUniform1i(glGetUniformLocation(shaderMain, pchar('tintAdd')), 0); |
|
1979 {$ELSE} |
|
1980 if enable then |
1967 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD) |
1981 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD) |
1968 else |
1982 else |
1969 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
1983 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
|
1984 {$ENDIF} |
1970 end; |
1985 end; |
1971 |
1986 |
1972 procedure ChangeDepth(rm: TRenderMode; d: GLfloat); |
1987 procedure ChangeDepth(rm: TRenderMode; d: GLfloat); |
1973 var tmp: LongInt; |
1988 var tmp: LongInt; |
1974 begin |
1989 begin |