Mostly port OpenGL API to C.

This commit is contained in:
UnknownShadow200 2017-09-28 20:58:50 +10:00
parent 9556b1a384
commit a2c61c20f2
18 changed files with 537 additions and 48 deletions

View File

@ -94,7 +94,7 @@ void BordersRenderer_DrawX(Int32 x, Int32 z1, Int32 z2, Int32 y1, Int32 y2, Int3
Int32 endZ = z2, endY = y2, startY = y1;
VertexP3fT2fC4b* ptr = *vertices;
VertexP3fT2fC4b v;
v.X = (Real32)x; v.Colour = col;
v.X = (Real32)x; v.Col = col;
for (; z1 < endZ; z1 += axisSize) {
z2 = z1 + axisSize;
@ -118,7 +118,7 @@ void BordersRenderer_DrawZ(Int32 z, Int32 x1, Int32 x2, Int32 y1, Int32 y2, Int3
Int32 endX = x2, endY = y2, startY = y1;
VertexP3fT2fC4b* ptr = *vertices;
VertexP3fT2fC4b v;
v.Z = (Real32)z; v.Colour = col;
v.Z = (Real32)z; v.Col = col;
for (; x1 < endX; x1 += axisSize) {
x2 = x1 + axisSize;
@ -142,7 +142,7 @@ void BordersRenderer_DrawY(Int32 x1, Int32 z1, Int32 x2, Int32 z2, Real32 y, Int
Int32 endX = x2, endZ = z2, startZ = z1;
VertexP3fT2fC4b* ptr = *vertices;
VertexP3fT2fC4b v;
v.Y = y + yOffset; v.Colour = col;
v.Y = y + yOffset; v.Col = col;
for (; x1 < endX; x1 += axisSize) {
x2 = x1 + axisSize;

View File

@ -383,7 +383,7 @@ void Builder_DrawSprite(Int32 count) {
Builder1DPart* part = &Builder_Parts[i];
PackedCol col = Builder_FullBright ? Builder_WhiteCol : Lighting_Col_Sprite_Fast(Builder_X, Builder_Y, Builder_Z);
Block_Tint(col, Builder_Block);
VertexP3fT2fC4b v; v.Colour = col;
VertexP3fT2fC4b v; v.Col = col;
/* Draw Z axis */
Int32 index = part->sOffset;

View File

@ -99,7 +99,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<MinimumRequiredVersion>5.1</MinimumRequiredVersion>
<EntryPointSymbol>main</EntryPointSymbol>
<AdditionalDependencies>d3d9.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>d3d9.lib;opengl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@ -119,7 +119,7 @@
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EntryPointSymbol>main</EntryPointSymbol>
<AdditionalDependencies>d3d9.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>d3d9.lib;opengl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@ -145,7 +145,7 @@
<MinimumRequiredVersion>5.1</MinimumRequiredVersion>
<LinkTimeCodeGeneration>Default</LinkTimeCodeGeneration>
<EntryPointSymbol>main</EntryPointSymbol>
<AdditionalDependencies>d3d9.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>d3d9.lib;opengl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@ -169,7 +169,7 @@
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EntryPointSymbol>main</EntryPointSymbol>
<AdditionalDependencies>d3d9.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>d3d9.lib;opengl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
@ -273,6 +273,7 @@
<ClCompile Include="MapRenderer.c" />
<ClCompile Include="ModelCache.c" />
<ClCompile Include="Mouse.c" />
<ClCompile Include="OpenGLApi.c" />
<ClCompile Include="Options.c" />
<ClCompile Include="PackedCol.c" />
<ClCompile Include="Funcs.c" />

View File

@ -530,5 +530,8 @@
<ClCompile Include="Deflate.c">
<Filter>Source Files\IO</Filter>
</ClCompile>
<ClCompile Include="OpenGLApi.c">
<Filter>Source Files\Graphics</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -6,7 +6,7 @@
#include "GraphicsCommon.h"
#include "Funcs.h"
#ifdef USE_DX
#if USE_DX
//#define D3D_DISABLE_9EX causes compile errors
#include <d3d9.h>
#include <d3d9caps.h>
@ -233,6 +233,7 @@ void Gfx_Init(void) {
D3DCAPS9 caps;
Platform_MemSet(&caps, 0, sizeof(D3DCAPS9));
IDirect3DDevice9_GetDeviceCaps(device, &caps);
Gfx_MaxTextureDimensions = min(caps.MaxTextureWidth, caps.MaxTextureHeight);
Gfx_CustomMipmapsLevels = true;
viewStack.Type = D3DTS_VIEW;
@ -329,9 +330,7 @@ void D3D9_DoMipmaps(IDirect3DTexture9* texture, Int32 x, Int32 y, Bitmap* bmp, b
UInt32 size = Bitmap_DataSize(width, height);
UInt8* cur = Platform_MemAlloc(size);
if (cur == NULL) {
ErrorHandler_Fail("Allocating memory for mipmaps");
}
if (cur == NULL) ErrorHandler_Fail("Allocating memory for mipmaps");
GfxCommon_GenMipmaps(width, height, cur, prev);
Bitmap mipmap;
@ -512,6 +511,7 @@ void Gfx_SetAlphaArgBlend(bool enabled) {
ErrorHandler_CheckOrFail(hresult, "D3D9_SetAlphaArgBlend");
}
UInt32 d3d9_clearCol = 0xFF000000;
void Gfx_Clear(void) {
DWORD flags = D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER;
@ -523,8 +523,6 @@ void Gfx_ClearColour(PackedCol col) {
d3d9_clearCol = col.Packed;
}
bool d3d9_depthTest = false;
void Gfx_SetDepthTest(bool enabled) {
d3d9_depthTest = enabled;

View File

@ -22,7 +22,7 @@ void Drawer_XMin(Int32 count, PackedCol col, TextureLoc texLoc, VertexP3fT2fC4b*
ApplyTint;
VertexP3fT2fC4b* ptr = *vertices;
VertexP3fT2fC4b v; v.X = Drawer_X1; v.Colour = col;
VertexP3fT2fC4b v; v.X = Drawer_X1; v.Col = col;
v.Y = Drawer_Y2; v.Z = Drawer_Z2 + (count - 1); v.U = u2; v.V = v1; AddVertex;
v.Z = Drawer_Z1; v.U = u1; AddVertex;
v.Y = Drawer_Y1; v.V = v2; AddVertex;
@ -39,7 +39,7 @@ void Drawer_XMax(Int32 count, PackedCol col, TextureLoc texLoc, VertexP3fT2fC4b*
ApplyTint;
VertexP3fT2fC4b* ptr = *vertices;
VertexP3fT2fC4b v; v.X = Drawer_X2; v.Colour = col;
VertexP3fT2fC4b v; v.X = Drawer_X2; v.Col = col;
v.Y = Drawer_Y2; v.Z = Drawer_Z1; v.U = u1; v.V = v1; AddVertex;
v.Z = Drawer_Z2 + (count - 1); v.U = u2; AddVertex;
v.Y = Drawer_Y1; v.V = v2; AddVertex;
@ -56,7 +56,7 @@ void Drawer_ZMin(Int32 count, PackedCol col, TextureLoc texLoc, VertexP3fT2fC4b*
ApplyTint;
VertexP3fT2fC4b* ptr = *vertices;
VertexP3fT2fC4b v; v.Z = Drawer_Z1; v.Colour = col;
VertexP3fT2fC4b v; v.Z = Drawer_Z1; v.Col = col;
v.X = Drawer_X2 + (count - 1); v.Y = Drawer_Y1; v.U = u2; v.V = v2; AddVertex;
v.X = Drawer_X1; v.U = u1; AddVertex;
v.Y = Drawer_Y2; v.V = v1; AddVertex;
@ -73,7 +73,7 @@ void Drawer_ZMax(Int32 count, PackedCol col, TextureLoc texLoc, VertexP3fT2fC4b*
ApplyTint;
VertexP3fT2fC4b* ptr = *vertices;
VertexP3fT2fC4b v; v.Z = Drawer_Z2; v.Colour = col;
VertexP3fT2fC4b v; v.Z = Drawer_Z2; v.Col = col;
v.X = Drawer_X2 + (count - 1); v.Y = Drawer_Y2; v.U = u2; v.V = v1; AddVertex;
v.X = Drawer_X1; v.U = u1; AddVertex;
v.Y = Drawer_Y1; v.V = v2; AddVertex;
@ -90,7 +90,7 @@ void Drawer_YMin(Int32 count, PackedCol col, TextureLoc texLoc, VertexP3fT2fC4b*
ApplyTint;
VertexP3fT2fC4b* ptr = *vertices;
VertexP3fT2fC4b v; v.Y = Drawer_Y1; v.Colour = col;
VertexP3fT2fC4b v; v.Y = Drawer_Y1; v.Col = col;
v.X = Drawer_X2 + (count - 1); v.Z = Drawer_Z2; v.U = u2; v.V = v2; AddVertex;
v.X = Drawer_X1; v.U = u1; AddVertex;
v.Z = Drawer_Z1; v.V = v1; AddVertex;
@ -107,7 +107,7 @@ void Drawer_YMax(Int32 count, PackedCol col, TextureLoc texLoc, VertexP3fT2fC4b*
ApplyTint;
VertexP3fT2fC4b* ptr = *vertices;
VertexP3fT2fC4b v; v.Y = Drawer_Y2; v.Colour = col;
VertexP3fT2fC4b v; v.Y = Drawer_Y2; v.Col = col;
v.X = Drawer_X2 + (count - 1); v.Z = Drawer_Z1; v.U = u2; v.V = v1; AddVertex;
v.X = Drawer_X1; v.U = u1; AddVertex;
v.Z = Drawer_Z2; v.V = v2; AddVertex;

View File

@ -153,7 +153,7 @@ void EnvRenderer_Render(Real64 deltaTime) {
void EnvRenderer_DrawSkyY(Int32 x1, Int32 z1, Int32 x2, Int32 z2, Int32 y, Int32 axisSize, PackedCol col, VertexP3fC4b* vertices) {
Int32 endX = x2, endZ = z2, startZ = z1;
VertexP3fC4b v;
v.Y = (Real32)y; v.Colour = col;
v.Y = (Real32)y; v.Col = col;
for (; x1 < endX; x1 += axisSize) {
x2 = x1 + axisSize;
@ -176,7 +176,7 @@ void EnvRenderer_DrawCloudsY(Int32 x1, Int32 z1, Int32 x2, Int32 z2, Int32 y, In
/* adjust range so that largest negative uv coordinate is shifted to 0 or above. */
Real32 offset = (Real32)Math_CeilDiv(-x1, 2048);
VertexP3fT2fC4b v;
v.Y = (Real32)y + 0.1f; v.Colour = col;
v.Y = (Real32)y + 0.1f; v.Col = col;
for (; x1 < endX; x1 += axisSize) {
x2 = x1 + axisSize;

View File

@ -12,7 +12,7 @@
/* Abstracts a 3D graphics rendering API.
Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3
*/
#define ICOUNT(verticesCount) ((verticesCount) / 4 * 6)
#define ICOUNT(verticesCount) (((verticesCount) >> 2) * 6)
/* Initalises this graphics API. */
void Gfx_Init(void);
@ -87,7 +87,6 @@ void Gfx_SetAlphaArgBlend(bool enabled);
void Gfx_Clear(void);
/* Sets the colour the screen is cleared to when Clear() is called. */
void Gfx_ClearColour(PackedCol col);
/* Whether depth testing is currently enabled. */
void Gfx_SetDepthTest(bool enabled);
/* Sets the depth test compare function that is used when depth testing is enabled. */

View File

@ -90,7 +90,7 @@ void GfxCommon_Make2DQuad(Texture* tex, PackedCol col, VertexP3fT2fC4b** vertice
#endif
VertexP3fT2fC4b* ptr = *vertices;
VertexP3fT2fC4b v; v.Z = 0.0f; v.Colour = col;
VertexP3fT2fC4b v; v.Z = 0.0f; v.Col = col;
v.X = x1; v.Y = y1; v.U = tex->U1; v.V = tex->V1; ptr[0] = v;
v.X = x2; v.U = tex->U2; ptr[1] = v;
v.Y = y2; v.V = tex->V2; ptr[2] = v;
@ -134,14 +134,15 @@ void GfxCommon_MakeIndices(UInt16* indices, Int32 iCount) {
Int32 element = 0, i;
for (i = 0; i < iCount; i += 6) {
*indices = (UInt16)(element + 0); indices++;
*indices = (UInt16)(element + 1); indices++;
*indices = (UInt16)(element + 2); indices++;
indices[0] = (UInt16)(element + 0);
indices[1] = (UInt16)(element + 1);
indices[2] = (UInt16)(element + 2);
*indices = (UInt16)(element + 2); indices++;
*indices = (UInt16)(element + 3); indices++;
*indices = (UInt16)(element + 0); indices++;
element += 4;
indices[3] = (UInt16)(element + 2);
indices[4] = (UInt16)(element + 3);
indices[5] = (UInt16)(element + 0);
indices += 6; element += 4;
}
}

View File

@ -139,7 +139,7 @@ void IModel_DrawPart(ModelPart part) {
for (i = 0; i < part.Count; i++) {
ModelVertex v = *src;
dst->X = v.X; dst->Y = v.Y; dst->Z = v.Z;
dst->Colour = IModel_Cols[i >> 2];
dst->Col = IModel_Cols[i >> 2];
dst->U = (v.U & UV_POS_MASK) * IModel_uScale - (v.U >> UV_MAX_SHIFT) * 0.01f * IModel_uScale;
dst->V = (v.V & UV_POS_MASK) * IModel_vScale - (v.V >> UV_MAX_SHIFT) * 0.01f * IModel_vScale;
@ -187,7 +187,7 @@ void IModel_DrawRotate(Real32 angleX, Real32 angleY, Real32 angleZ, ModelPart pa
t = IModel_cosHead * v.X - IModel_sinHead * v.Z; v.Z = IModel_sinHead * v.X + IModel_cosHead * v.Z; v.X = t; /* Inlined RotY */
}
dst->X = v.X + x; dst->Y = v.Y + y; dst->Z = v.Z + z;
dst->Colour = IModel_Cols[i >> 2];
dst->Col = IModel_Cols[i >> 2];
dst->U = (v.U & UV_POS_MASK) * IModel_uScale - (v.U >> UV_MAX_SHIFT) * 0.01f * IModel_uScale;
dst->V = (v.V & UV_POS_MASK) * IModel_vScale - (v.V >> UV_MAX_SHIFT) * 0.01f * IModel_vScale;

View File

@ -75,8 +75,8 @@ void IsometricDrawer_SpriteZQuad(BlockID block, bool firstPart) {
if (iso_lastTexIndex != iso_texIndex) IsometricDrawer_Flush();
VertexP3fT2fC4b v;
v.Colour = iso_colNormal;
Block_Tint(v.Colour, block);
v.Col = iso_colNormal;
Block_Tint(v.Col, block);
Real32 x1 = firstPart ? 0.5f : -0.1f, x2 = firstPart ? 1.1f : 0.5f;
rec.U1 = firstPart ? 0.0f : 0.5f;
@ -100,8 +100,8 @@ void IsometricDrawer_SpriteXQuad(BlockID block, bool firstPart) {
if (iso_lastTexIndex != iso_texIndex) IsometricDrawer_Flush();
VertexP3fT2fC4b v;
v.Colour = iso_colNormal;
Block_Tint(v.Colour, block);
v.Col = iso_colNormal;
Block_Tint(v.Col, block);
Real32 z1 = firstPart ? 0.5f : -0.1f, z2 = firstPart ? 1.1f : 0.5f;
rec.U1 = firstPart ? 0.0f : 0.5f;

487
src/Client/OpenGLApi.c Normal file
View File

@ -0,0 +1,487 @@
#include "GraphicsAPI.h"
#include "ErrorHandler.h"
#include "GraphicsEnums.h"
#include "Platform.h"
#include "Window.h"
#include "GraphicsCommon.h"
#include "Funcs.h"
#include <Windows.h>
#include <GL/gl.h>
#if !USE_DX
/* Extensions from later than OpenGL 1.1 */
#define GL_TEXTURE_MAX_LEVEL 0x813D
#define GL_ARRAY_BUFFER 0x8892
#define GL_ELEMENT_ARRAY_BUFFER 0x8893
#define GL_STATIC_DRAW 0x88E4
#define GL_DYNAMIC_DRAW 0x88E8
typedef void (APIENTRY *PFNGLBINDBUFFERPROC) (GLenum target, GLuint buffer);
typedef void (APIENTRY *PFNGLDELETEBUFFERSPROC) (GLsizei n, const GLuint *buffers);
typedef void (APIENTRY *PFNGLGENBUFFERSPROC) (GLsizei n, GLuint *buffers);
typedef void (APIENTRY *PFNGLBUFFERDATAPROC) (GLenum target, const void* size, const void *data, GLenum usage);
typedef void (APIENTRY *PFNGLBUFFERSUBDATAPROC) (GLenum target, const void* offset, const void* size, const void *data);
/* TODO: NEED TO ASSIGN THESE TO VALID PROC ADDRESSES */
PFNGLBINDBUFFERPROC glBindBuffer;
PFNGLDELETEBUFFERSPROC glDeleteBuffers;
PFNGLGENBUFFERSPROC glGenBuffers;
PFNGLBUFFERDATAPROC glBufferData;
PFNGLBUFFERSUBDATAPROC glBufferSubData;
bool gl_lists = false;
Int32 gl_activeList = -1;
#define gl_DYNAMICLISTID 1234567891
void* gl_dynamicListData;
Int32 gl_blend[6] = { GL_ZERO, GL_ONE, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA };
Int32 gl_compare[8] = { GL_ALWAYS, GL_NOTEQUAL, GL_NEVER, GL_LESS, GL_LEQUAL, GL_EQUAL, GL_GEQUAL, GL_GREATER };
Int32 gl_fogModes[3] = { GL_LINEAR, GL_EXP, GL_EXP2 };
Int32 gl_matrixModes[3] = { GL_PROJECTION, GL_MODELVIEW, GL_TEXTURE };
void GL_CheckVboSupport() {
String extensions = String_FromReadonly(glGetString(GL_EXTENSIONS));
String version = String_FromReadonly(glGetString(GL_VERSION));
Int32 major = (Int32)(version.buffer[0] - '0'); /* x.y. (and so forth) */
Int32 minor = (Int32)(version.buffer[2] - '0');
if ((major > 1) || (major == 1 && minor >= 5)) return; /* Supported in core since 1.5 */
String vboExt = String_FromConstant("GL_ARB_vertex_buffer_object");
if (String_ContainsString(&extensions, &vboExt)) {
glUseArbVboAddresses();
} else {
gl_lists = true;
Gfx_CustomMipmapsLevels = false;
}
}
void Gfx_Init(void) {
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &Gfx_MaxTextureDimensions);
gl_lists = Options_GetBool(OptionsKey_ForceOldOpenGL, false);
Gfx_CustomMipmapsLevels = !gl_lists;
CheckVboSupport();
GfxCommon_Init();
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
}
#define GL_TOGGLE(cap)\
if (enabled) {\
glEnable(cap);\
} else {\
glDisable(cap);\
}
void GL_DoMipmaps(GfxResourceID texId, Int32 x, Int32 y, Bitmap* bmp, bool partial) {
UInt8* prev = bmp->Scan0;
Int32 lvls = GfxCommon_MipmapsLevels(bmp->Width, bmp->Height);
Int32 lvl, width = bmp->Width, height = bmp->Height;
for (lvl = 1; lvl <= lvls; lvl++) {
x /= 2; y /= 2;
if (width > 1) width /= 2;
if (height > 1) height /= 2;
UInt32 size = Bitmap_DataSize(width, height);
UInt8* cur = Platform_MemAlloc(size);
if (cur == NULL) ErrorHandler_Fail("Allocating memory for mipmaps");
GfxCommon_GenMipmaps(width, height, cur, prev);
if (partial) {
glTexSubImage2D(GL_TEXTURE_2D, lvl, x, y, width, height,
GL_BGRA_EXT, GL_UNSIGNED_BYTE, cur);
} else {
glTexImage2D(GL_TEXTURE_2D, lvl, GL_RGBA, width, height, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, cur);
}
if (prev != bmp->Scan0) Platform_MemFree(prev);
prev = cur;
}
if (prev != bmp->Scan0) Platform_MemFree(prev);
}
GfxResourceID Gfx_CreateTexture(Bitmap* bmp, bool managedPool, bool mipmaps) {
UInt32 texId;
glGenTextures(1, &texId);
glBindTexture(GL_TEXTURE_2D, texId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
if (mipmaps) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
if (Gfx_CustomMipmapsLevels) {
Int32 lvls = GfxCommon_MipmapsLevels(bmp->Width, bmp->Height);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, lvls);
}
} else {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bmp->Width, bmp->Height, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, bmp->Scan0);
if (mipmaps) GL_DoMipmaps(texId, 0, 0, bmp, false);
return texId;
}
void Gfx_UpdateTexturePart(GfxResourceID texId, Int32 x, Int32 y, Bitmap* part, bool mipmaps) {
glBindTexture(GL_TEXTURE_2D, texId);
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, part->Width, part->Height, GL_BGRA_EXT, GL_UNSIGNED_BYTE, part->Scan0);
if (mipmaps) GL_DoMipmaps(texId, x, y, part, true);
}
void Gfx_BindTexture(GfxResourceID texId) {
glBindTexture(GL_TEXTURE_2D, texId);
}
void Gfx_DeleteTexture(GfxResourceID* texId) {
if (*texId <= 0) return;
glDeleteTextures(1, texId);
*texId = -1;
}
void Gfx_SetTexturing(bool enabled) { GL_TOGGLE(GL_TEXTURE_2D); }
void Gfx_EnableMipmaps(void) { }
void Gfx_DisableMipmaps(void) { }
bool gl_fogEnable;
bool Gfx_GetFog(void) { return gl_fogEnable; }
void Gfx_SetFog(bool enabled) {
gl_fogEnable = enabled;
GL_TOGGLE(GL_FOG);
}
PackedCol gl_lastFogCol;
void Gfx_SetFogColour(PackedCol col) {
if (PackedCol_Equals(col, gl_lastFogCol)) return;
Real32 colRGBA[4] = { col.R / 255.0f, col.G / 255.0f, col.B / 255.0f, col.A / 255.0f };
glFogfv(GL_FOG_COLOR, colRGBA);
gl_lastFogCol = col;
}
Real32 gl_lastFogEnd = -1, gl_lastFogDensity = -1;
void Gfx_SetFogDensity(Real32 value) {
if (value == gl_lastFogDensity) return;
glFogf(GL_FOG_DENSITY, gl_lastFogDensity);
gl_lastFogDensity = value;
}
void Gfx_SetFogStart(Real32 value) {
glFogf(GL_FOG_START, value);
}
void Gfx_SetFogEnd(Real32 value) {
if (value == gl_lastFogEnd) return;
glFogf(GL_FOG_END, gl_lastFogEnd);
gl_lastFogEnd = value;
}
Fog gl_lastFogMode = -1;
void Gfx_SetFogMode(Fog mode) {
if (mode == gl_lastFogMode) return;
glFogi(GL_FOG_MODE, gl_fogModes[mode]);
gl_lastFogMode = mode;
}
void Gfx_SetFaceCulling(bool enabled) { GL_TOGGLE(GL_CULL_FACE); }
void Gfx_SetAlphaTest(bool enabled) { GL_TOGGLE(GL_ALPHA_TEST); }
void Gfx_SetAlphaTestFunc(CompareFunc func, Real32 value) {
glAlphaFunc(gl_compare[func], value);
}
void Gfx_SetAlphaBlending(bool enabled) { GL_TOGGLE(GL_BLEND); }
void Gfx_SetAlphaBlendFunc(BlendFunc srcFunc, BlendFunc dstFunc) {
glBlendFunc(gl_blend[srcFunc], gl_blend[dstFunc]);
}
void Gfx_SetAlphaArgBlend(bool enabled) { }
void Gfx_Clear(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
PackedCol gl_lastClearCol;
void Gfx_ClearColour(PackedCol col) {
if (PackedCol_Equals(col, gl_lastClearCol)) return;
glClearColor(col.R / 255.0f, col.G / 255.0f, col.B / 255.0f, col.A / 255.0f);
gl_lastClearCol = col;
}
void Gfx_SetColourWrite(bool enabled) {
glColorMask(enabled, enabled, enabled, enabled);
}
void Gfx_SetDepthWrite(bool enabled) {
glDepthMask(enabled);
}
void Gfx_SetDepthTest(bool enabled) { GL_TOGGLE(GL_DEPTH_TEST); }
void Gfx_SetDepthTestFunc(CompareFunc compareFunc) {
glDepthFunc(gl_compare[compareFunc]);
}
GfxResourceID GL_GenAndBind(GLenum target) {
Int32 id;
glGenBuffers(1, &id);
glBindBuffer(target, id);
return id;
}
GfxResourceID Gfx_CreateDynamicVb(VertexFormat vertexFormat, Int32 maxVertices) {
if (gl_lists) return gl_DYNAMICLISTID;
Int32 id = GL_GenAndBind(GL_ARRAY_BUFFER);
Int32 sizeInBytes = maxVertices * Gfx_strideSizes[vertexFormat];
glBufferData(GL_ARRAY_BUFFER, (void*)sizeInBytes, NULL, GL_DYNAMIC_DRAW);
return id;
}
#define gl_MAXINDICES ICOUNT(65536)
GfxResourceID Gfx_CreateVb(void* vertices, VertexFormat vertexFormat, Int32 count) {
if (gl_lists) {
Int32 list = glGenLists(1);
glNewList(list, GL_COMPILE);
UInt16 indices[Gfx_MaxIndices];
GfxCommon_MakeIndices(indices, ICOUNT(count));
Int32 stride = vertexFormat == VertexFormat_P3fT2fC4b ? VertexP3fT2fC4b_Size : VertexP3fC4b_Size;
glVertexPointer(3, GL_FLOAT, stride, vertices);
glColorPointer(4, GL_UNSIGNED_BYTE, stride, (void*)((UInt8*)vertices + 12));
if (vertexFormat == VertexFormat_P3fT2fC4b) {
glTexCoordPointer(2, GL_FLOAT, stride, (void*)((UInt8*)vertices + 16));
}
glDrawElements(GL_TRIANGLES, ICOUNT(count), GL_UNSIGNED_SHORT, indices);
glEndList();
return list;
}
Int32 id = GL_GenAndBind(GL_ARRAY_BUFFER);
Int32 sizeInBytes = count * Gfx_strideSizes[vertexFormat];
glBufferData(GL_ARRAY_BUFFER, (void*)sizeInBytes, vertices, GL_STATIC_DRAW);
return id;
}
GfxResourceID Gfx_CreateIb(void* indices, Int32 indicesCount) {
if (gl_lists) return 0;
Int32 id = GL_GenAndBind(GL_ELEMENT_ARRAY_BUFFER);
Int32 sizeInBytes = indicesCount * sizeof(UInt16);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, (void*)sizeInBytes, indices, GL_STATIC_DRAW);
return id;
}
void Gfx_BindVb(GfxResourceID vb) {
if (gl_lists) {
gl_activeList = vb;
} else {
glBindBuffer(GL_ARRAY_BUFFER, vb);
}
}
void Gfx_BindIb(GfxResourceID ib) {
if (gl_lists) return;
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ib);
}
void Gfx_DeleteVb(GfxResourceID* vb) {
if (*vb <= 0) return;
if (gl_lists) {
if (*vb != gl_DYNAMICLISTID) glDeleteLists(*vb, 1);
} else {
glDeleteBuffers(1, vb);
}
*vb = -1;
}
void Gfx_DeleteIb(GfxResourceID* ib) {
if (gl_lists || *ib <= 0) return;
glDeleteBuffers(1, ib);
*ib = -1;
}
typedef void (*GL_SetupVBFunc)(void);
typedef void (*GL_SetupVBRangeFunc)(Int32 startVertex);
GL_SetupVBFunc gl_setupVBFunc;
GL_SetupVBRangeFunc gl_setupVBRangeFunc;
Int32 gl_batchStride;
VertexFormat gl_batchFormat = -1;
void GL_SetupVbPos3fCol4b(void) {
glVertexPointer(3, GL_FLOAT, VertexP3fC4b_Size, (void*)0);
glColorPointer(4, GL_UNSIGNED_BYTE, VertexP3fC4b_Size, (void*)12);
}
void GL_SetupVbPos3fTex2fCol4b(void) {
glVertexPointer(3, GL_FLOAT, VertexP3fT2fC4b_Size, (void*)0);
glColorPointer(4, GL_UNSIGNED_BYTE, VertexP3fT2fC4b_Size, (void*)12);
glTexCoordPointer(2, GL_FLOAT, VertexP3fT2fC4b_Size, (void*)16);
}
void GL_SetupVbPos3fCol4b_Range(Int32 startVertex) {
Int32 offset = startVertex * VertexP3fC4b_Size;
glVertexPointer(3, GL_FLOAT, VertexP3fC4b_Size, (void*)(offset));
glColorPointer(4, GL_UNSIGNED_BYTE, VertexP3fC4b_Size, (void*)(offset + 12));
}
void GL_SetupVbPos3fTex2fCol4b_Range(Int32 startVertex) {
Int32 offset = startVertex * VertexP3fT2fC4b_Size;
glVertexPointer(3, GL_FLOAT, VertexP3fT2fC4b_Size, (void*)(offset));
glColorPointer(4, GL_UNSIGNED_BYTE, VertexP3fT2fC4b_Size, (void*)(offset + 12));
glTexCoordPointer(2, GL_FLOAT, VertexP3fT2fC4b_Size, (void*)(offset + 16));
}
void Gfx_SetBatchFormat(VertexFormat vertexFormat) {
if (vertexFormat == gl_batchFormat) return;
if (gl_batchFormat == VertexFormat_P3fT2fC4b) {
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
gl_batchFormat = vertexFormat;
gl_batchStride = Gfx_strideSizes[vertexFormat];
if (vertexFormat == VertexFormat_P3fT2fC4b) {
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
gl_setupVBFunc = GL_SetupVbPos3fTex2fCol4b;
gl_setupVBRangeFunc = GL_SetupVbPos3fTex2fCol4b_Range;
} else {
gl_setupVBFunc = GL_SetupVbPos3fCol4b;
gl_setupVBRangeFunc = GL_SetupVbPos3fCol4b_Range;
}
}
void Gfx_SetDynamicVbData(GfxResourceID vb, void* vertices, Int32 vCount) {
if (gl_lists) {
gl_activeList = gl_DYNAMICLISTID;
gl_dynamicListData = vertices;
return;
}
glBindBuffer(GL_ARRAY_BUFFER, vb);
Int32 sizeInBytes = vCount * gl_batchStride;
glBufferSubData(GL_ARRAY_BUFFER, NULL, (void*)sizeInBytes, vertices);
}
void GL_V16(VertexP3fC4b v) {
glColor4ub(v.Col.R, v.Col.G, v.Col.B, v.Col.A);
glVertex3f(v.X, v.Y, v.Z);
}
void GL_V24(VertexP3fT2fC4b v) {
glColor4ub(v.Col.R, v.Col.G, v.Col.B, v.Col.A);
glTexCoord2f(v.U, v.V);
glVertex3f(v.X, v.Y, v.Z);
}
void GL_DrawDynamicLines(Int32 verticesCount) {
glBegin(GL_LINES);
Int32 i;
if (gl_batchFormat == VertexFormat_P3fT2fC4b) {
VertexP3fT2fC4b* ptr = (VertexP3fT2fC4b*)gl_dynamicListData;
for (i = 0; i < verticesCount; i += 2) {
GL_V24(ptr[i + 0]); GL_V24(ptr[i + 1]);
}
} else {
VertexP3fC4b* ptr = (VertexP3fC4b*)gl_dynamicListData;
for (i = 0; i < verticesCount; i += 2) {
GL_V16(ptr[i + 0]); GL_V16(ptr[i + 1]);
}
}
glEnd();
}
void GL_DrawDynamicTriangles(Int32 verticesCount, Int32 startVertex) {
glBegin(GL_TRIANGLES);
Int32 i;
if (gl_batchFormat == VertexFormat_P3fT2fC4b) {
VertexP3fT2fC4b* ptr = (VertexP3fT2fC4b*)gl_dynamicListData;
for (i = startVertex; i < startVertex + verticesCount; i += 4) {
GL_V24(ptr[i + 0]); GL_V24(ptr[i + 1]); GL_V24(ptr[i + 2]);
GL_V24(ptr[i + 2]); GL_V24(ptr[i + 3]); GL_V24(ptr[i + 0]);
}
} else {
VertexP3fC4b* ptr = (VertexP3fC4b*)gl_dynamicListData;
for (i = startVertex; i < startVertex + verticesCount; i += 4) {
GL_V16(ptr[i + 0]); GL_V16(ptr[i + 1]); GL_V16(ptr[i + 2]);
GL_V16(ptr[i + 2]); GL_V16(ptr[i + 3]); GL_V16(ptr[i + 0]);
}
}
glEnd();
}
void Gfx_DrawVb_Lines(Int32 verticesCount) {
if (gl_lists) { GL_DrawDynamicLines(verticesCount); return; }
gl_setupVBFunc();
glDrawArrays(GL_LINES, 0, verticesCount);
}
void Gfx_DrawVb_IndexedTris_Range(Int32 verticesCount, Int32 startVertex) {
if (gl_lists) {
if (gl_activeList != gl_DYNAMICLISTID) { glCallList(gl_activeList); }
else { GL_DrawDynamicTriangles(verticesCount, startVertex); }
return;
}
gl_setupVBRangeFunc(startVertex);
glDrawElements(GL_TRIANGLES, ICOUNT(verticesCount), GL_UNSIGNED_SHORT, NULL);
}
void Gfx_DrawVb_IndexedTris(Int32 verticesCount) {
if (gl_lists) {
if (gl_activeList != gl_DYNAMICLISTID) { glCallList(gl_activeList); }
else { GL_DrawDynamicTriangles(verticesCount, 0); }
return;
}
gl_setupVBFunc();
glDrawElements(GL_TRIANGLES, ICOUNT(verticesCount), GL_UNSIGNED_SHORT, NULL);
}
Int32 gl_lastPartialList = -1;
void Gfx_DrawIndexedVb_TrisT2fC4b(Int32 verticesCount, Int32 startVertex) {
/* TODO: This renders the whole map, bad performance!! FIX FIX */
if (gl_lists) {
if (gl_activeList != gl_lastPartialList) {
glCallList(gl_activeList);
gl_lastPartialList = gl_activeList;
}
return;
}
Int32 offset = startVertex * VertexP3fT2fC4b_Size;
glVertexPointer(3, GL_FLOAT, VertexP3fT2fC4b_Size, (void*)(offset));
glColorPointer(4, GL_UNSIGNED_BYTE, VertexP3fT2fC4b_Size, (void*)(offset + 12));
glTexCoordPointer(2, GL_FLOAT, VertexP3fT2fC4b_Size, (void*)(offset + 16));
glDrawElements(GL_TRIANGLES, ICOUNT(verticesCount), GL_UNSIGNED_SHORT, NULL);
}
MatrixType gl_lastMatrixType = 0;
void Gfx_SetMatrixMode(MatrixType matrixType) {
if (matrixType == gl_lastMatrixType) return;
glMatrixMode(gl_matrixModes[matrixType]);
gl_lastMatrixType = matrixType;
}
void Gfx_LoadMatrix(Matrix* matrix) { glLoadMatrixf((Real32*)matrix); }
void Gfx_LoadIdentityMatrix(void) { glLoadIdentity(); }
void Gfx_MultiplyMatrix(Matrix* matrix) { glMultMatrixf((Real32*)matrix); }
void Gfx_PushMatrix(void) { glPushMatrix(); }
void Gfx_PopMatrix(void) { glPopMatrix(); }
void Gfx_LoadOrthoMatrix(Real32 width, Real32 height) {
Matrix matrix;
Matrix_OrthographicOffCenter(&matrix, 0.0f, width, height, 0.0f, -10000.0f, 10000.0f);
Gfx_LoadMatrix(&matrix);
}
void Gfx_OnWindowResize(void) {
Size2D size = Window_GetClientSize();
glViewport(0, 0, size.Width, size.Height);
}
#endif

View File

@ -5,13 +5,13 @@ void SelectionBox_Make(SelectionBox* box, Vector3I* p1, Vector3I* p2, PackedCol
box->MinDist = 0.0f; box->MaxDist = 0.0f;
Vector3I_Min(&box->Min, p1, p2);
Vector3I_Max(&box->Max, p1, p2);
box->Colour = col;
box->Col = col;
}
void SelectionBox_Render(SelectionBox* box, VertexP3fC4b** vertices, VertexP3fC4b** lineVertices) {
Real32 offset = box->MinDist < 32.0f * 32.0f ? (1.0f / 32.0f) : (1.0f / 16.0f);
Vector3 p1, p2;
PackedCol col = box->Colour;
PackedCol col = box->Col;
Vector3I_ToVector3(&p1, &box->Min);
Vector3I_ToVector3(&p2, &box->Max);

View File

@ -17,7 +17,7 @@ typedef struct SelectionBox_ {
/* Maximum corner of the box. */
Vector3I Max;
/* Colour of this box. */
PackedCol Colour;
PackedCol Col;
/* Closest distance to player of any of the eight corners of the box. */
Real32 MinDist;
/* Furthest distance to player of any of the eight corners of the box. */

View File

@ -75,7 +75,7 @@ void SkyboxRenderer_MakeVb(void) {
Gfx_DeleteVb(&skybox_vb);
VertexP3fT2fC4b vertices[SKYBOX_COUNT];
#define pos 1.0f
VertexP3fT2fC4b v; v.Colour = WorldEnv_CloudsCol;
VertexP3fT2fC4b v; v.Col = WorldEnv_CloudsCol;
/* Render the front quad */
v.Z = -pos;

View File

@ -2,11 +2,11 @@
void VertexP3fC4b_Set(VertexP3fC4b* target, Real32 x, Real32 y, Real32 z, PackedCol col) {
target->X = x; target->Y = y; target->Z = z;
target->Colour = col;
target->Col = col;
}
void VertexP3fT2fC4b_Set(VertexP3fT2fC4b* target, Real32 x, Real32 y, Real32 z,
Real32 u, Real32 v, PackedCol col) {
target->X = x; target->Y = y; target->Z = z;
target->Colour = col; target->U = u; target->V = v;
target->Col = col; target->U = u; target->V = v;
}

View File

@ -10,7 +10,7 @@
/* 3 floats for position (XYZ), 4 bytes for colour. */
typedef struct VertexP3fC4b_ {
Real32 X, Y, Z;
PackedCol Colour;
PackedCol Col;
} VertexP3fC4b;
void VertexP3fC4b_Set(VertexP3fC4b* target, Real32 x, Real32 y, Real32 z, PackedCol col);
@ -22,7 +22,7 @@ void VertexP3fC4b_Set(VertexP3fC4b* target, Real32 x, Real32 y, Real32 z, Packed
/* 3 floats for position (XYZ), 2 floats for texture coordinates (UV), 4 bytes for colour. */
typedef struct VertexP3fT2fC4b_ {
Real32 X, Y, Z;
PackedCol Colour;
PackedCol Col;
Real32 U, V;
} VertexP3fT2fC4b;
@ -30,5 +30,5 @@ void VertexP3fT2fC4b_Set(VertexP3fT2fC4b* target, Real32 x, Real32 y, Real32 z,
Real32 u, Real32 v, PackedCol col);
/* 3 * 4 + 2 * 4 + 4 * 1 */
#define VertexP3fT2fC4b_Size 24;
#define VertexP3fT2fC4b_Size 24
#endif

View File

@ -144,7 +144,7 @@ void WeatherRenderer_Render(Real64 deltaTime) {
col.A = (UInt8)alpha;
/* NOTE: Making vertex is inlined since this is called millions of times. */
v.Colour = col;
v.Col = col;
Real32 worldV = vOffset + (z & 1) / 2.0f - (x & 0x0F) / 16.0f;
Real32 v1 = y / 6.0f + worldV, v2 = (y + height) / 6.0f + worldV;
Real32 x1 = (Real32)x, y1 = (Real32)y, z1 = (Real32)z;