Permalink
Browse files

Fixed so that the tileborderfix tool isn't needed anymore. Solve the …

…issue by using a 3d texture instead
  • Loading branch information...
Magnus Auvinen
Magnus Auvinen committed Sep 29, 2013
1 parent 30e6e08 commit 3703d2255d57f5f0e60c5ab4d88220d1abd986e5
@@ -164,14 +164,23 @@ void CCommandProcessorFragment_OpenGL::SetState(const CCommandBuffer::SState &St
else
glDisable(GL_SCISSOR_TEST);
// texture
glDisable(GL_TEXTURE_2D);
glDisable(GL_TEXTURE_3D);
if(State.m_Texture >= 0 && State.m_Texture < CCommandBuffer::MAX_TEXTURES)
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_aTextures[State.m_Texture].m_Tex);
if(m_aTextures[State.m_Texture].m_Dimentions == 2)
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_aTextures[State.m_Texture].m_Tex);
}
else if(m_aTextures[State.m_Texture].m_Dimentions == 3)
{
glEnable(GL_TEXTURE_3D);
glBindTexture(GL_TEXTURE_3D, m_aTextures[State.m_Texture].m_Tex);
}
}
else
glDisable(GL_TEXTURE_2D);
switch(State.m_WrapMode)
{
@@ -187,6 +196,16 @@ void CCommandProcessorFragment_OpenGL::SetState(const CCommandBuffer::SState &St
dbg_msg("render", "unknown wrapmode %d\n", State.m_WrapMode);
};
if(State.m_Texture >= 0 && State.m_Texture < CCommandBuffer::MAX_TEXTURES && m_aTextures[State.m_Texture].m_Dimentions == 3)
{
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
}
// screen mapping
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
@@ -216,6 +235,7 @@ void CCommandProcessorFragment_OpenGL::Cmd_Texture_Create(const CCommandBuffer::
{
int Width = pCommand->m_Width;
int Height = pCommand->m_Height;
int Depth = pCommand->m_Depth;
void *pTexData = pCommand->m_pData;
// resample if needed
@@ -260,30 +280,46 @@ void CCommandProcessorFragment_OpenGL::Cmd_Texture_Create(const CCommandBuffer::
default: StoreOglformat = GL_COMPRESSED_RGBA_ARB;
}
}
glGenTextures(1, &m_aTextures[pCommand->m_Slot].m_Tex);
glBindTexture(GL_TEXTURE_2D, m_aTextures[pCommand->m_Slot].m_Tex);
if(pCommand->m_Flags&CCommandBuffer::TEXFLAG_NOMIPMAPS)
glGenTextures(1, &m_aTextures[pCommand->m_Slot].m_Tex);
if(Depth > 1)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, StoreOglformat, Width, Height, 0, Oglformat, GL_UNSIGNED_BYTE, pTexData);
m_aTextures[pCommand->m_Slot].m_Dimentions = 3;
glBindTexture(GL_TEXTURE_3D, m_aTextures[pCommand->m_Slot].m_Tex);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage3D(GL_TEXTURE_3D, 0, StoreOglformat, Width, Height, Depth, 0, Oglformat, GL_UNSIGNED_BYTE, pTexData);
m_aTextures[pCommand->m_Slot].m_MemSize = Width*Height*pCommand->m_PixelSize;
}
else
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, StoreOglformat, Width, Height, Oglformat, GL_UNSIGNED_BYTE, pTexData);
}
m_aTextures[pCommand->m_Slot].m_Dimentions = 2;
glBindTexture(GL_TEXTURE_2D, m_aTextures[pCommand->m_Slot].m_Tex);
if(pCommand->m_Flags&CCommandBuffer::TEXFLAG_NOMIPMAPS)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, StoreOglformat, Width, Height, 0, Oglformat, GL_UNSIGNED_BYTE, pTexData);
}
else
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, StoreOglformat, Width, Height, Oglformat, GL_UNSIGNED_BYTE, pTexData);
}
// calculate memory usage
m_aTextures[pCommand->m_Slot].m_MemSize = Width*Height*pCommand->m_PixelSize;
while(Width > 2 && Height > 2)
{
Width>>=1;
Height>>=1;
m_aTextures[pCommand->m_Slot].m_MemSize += Width*Height*pCommand->m_PixelSize;
// calculate memory usage
m_aTextures[pCommand->m_Slot].m_MemSize = Width*Height*pCommand->m_PixelSize;
while(Width > 2 && Height > 2)
{
Width>>=1;
Height>>=1;
m_aTextures[pCommand->m_Slot].m_MemSize += Width*Height*pCommand->m_PixelSize;
}
}
*m_pTextureMemoryUsage += m_aTextures[pCommand->m_Slot].m_MemSize;
mem_free(pTexData);
@@ -300,8 +336,8 @@ void CCommandProcessorFragment_OpenGL::Cmd_Render(const CCommandBuffer::SCommand
SetState(pCommand->m_State);
glVertexPointer(3, GL_FLOAT, sizeof(CCommandBuffer::SVertex), (char*)pCommand->m_pVertices);
glTexCoordPointer(2, GL_FLOAT, sizeof(CCommandBuffer::SVertex), (char*)pCommand->m_pVertices + sizeof(float)*3);
glColorPointer(4, GL_FLOAT, sizeof(CCommandBuffer::SVertex), (char*)pCommand->m_pVertices + sizeof(float)*5);
glTexCoordPointer(3, GL_FLOAT, sizeof(CCommandBuffer::SVertex), (char*)pCommand->m_pVertices + sizeof(float)*3);
glColorPointer(4, GL_FLOAT, sizeof(CCommandBuffer::SVertex), (char*)pCommand->m_pVertices + sizeof(float)*6);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
@@ -167,6 +167,7 @@ class CCommandProcessorFragment_OpenGL
struct CTexture
{
GLuint m_Tex;
int m_Dimentions;
int m_MemSize;
};
CTexture m_aTextures[CCommandBuffer::MAX_TEXTURES];
@@ -332,6 +332,8 @@ IGraphics::CTextureHandle CGraphics_Threaded::LoadTextureRaw(int Width, int Heig
Cmd.m_PixelSize = ImageFormatToPixelSize(Format);
Cmd.m_Format = ImageFormatToTexFormat(Format);
Cmd.m_StoreFormat = ImageFormatToTexFormat(StoreFormat);
Cmd.m_Depth = 1;
// flags
Cmd.m_Flags = 0;
@@ -342,11 +344,42 @@ IGraphics::CTextureHandle CGraphics_Threaded::LoadTextureRaw(int Width, int Heig
if(g_Config.m_GfxTextureQuality || Flags&TEXLOAD_NORESAMPLE)
Cmd.m_Flags |= CCommandBuffer::TEXFLAG_QUALITY;
// copy texture data
int MemSize = Width*Height*Cmd.m_PixelSize;
void *pTmpData = mem_alloc(MemSize, sizeof(void*));
mem_copy(pTmpData, pData, MemSize);
Cmd.m_pData = pTmpData;
// 3d texture?
if(Flags&IGraphics::TEXLOAD_ARRAY_256)
{
Cmd.m_Width /= 16;
Cmd.m_Height /= 16;
Cmd.m_Depth = 256;
// copy and reorder texture data
int MemSize = Width*Height*Cmd.m_PixelSize;
char *pTmpData = (char *)mem_alloc(MemSize, sizeof(void*));
const int TileSize = (Cmd.m_Height * Cmd.m_Width) * Cmd.m_PixelSize;
const int TileRowSize = Cmd.m_Width * Cmd.m_PixelSize;
const int ImagePitch = Width * Cmd.m_PixelSize;
mem_zero(pTmpData, MemSize);
for(int i = 0; i < 256; i++)
{
const int px = (i%16) * Cmd.m_Width;
const int py = (i/16) * Cmd.m_Height;
const char *pTileData = (const char *)pData + (py * Width + px) * Cmd.m_PixelSize;
for(int y = 0; y < Cmd.m_Height; y++)
mem_copy(pTmpData + i*TileSize + y*TileRowSize, pTileData + y * ImagePitch, TileRowSize);
}
Cmd.m_pData = pTmpData;
}
else
{
// copy texture data
int MemSize = Width*Height*Cmd.m_PixelSize;
void *pTmpData = mem_alloc(MemSize, sizeof(void*));
mem_copy(pTmpData, pData, MemSize);
Cmd.m_pData = pTmpData;
}
//
m_pCommandBuffer->AddCommand(Cmd);
@@ -493,7 +526,7 @@ void CGraphics_Threaded::QuadsBegin()
dbg_assert(m_Drawing == 0, "called Graphics()->QuadsBegin twice");
m_Drawing = DRAWING_QUADS;
QuadsSetSubset(0,0,1,1);
QuadsSetSubset(0,0,1,1,0);
QuadsSetRotation(0);
SetColor(1,1,1,1);
}
@@ -546,7 +579,7 @@ void CGraphics_Threaded::SetColor4(vec4 TopLeft, vec4 TopRight, vec4 BottomLeft,
SetColorVertex(Array, 4);
}
void CGraphics_Threaded::QuadsSetSubset(float TlU, float TlV, float BrU, float BrV)
void CGraphics_Threaded::QuadsSetSubset(float TlU, float TlV, float BrU, float BrV, int TextureIndex)
{
dbg_assert(m_Drawing == DRAWING_QUADS, "called Graphics()->QuadsSetSubset without begin");
@@ -555,16 +588,20 @@ void CGraphics_Threaded::QuadsSetSubset(float TlU, float TlV, float BrU, float B
m_aTexture[3].u = TlU; m_aTexture[2].u = BrU;
m_aTexture[3].v = BrV; m_aTexture[2].v = BrV;
m_aTexture[0].i = m_aTexture[1].i = m_aTexture[2].i = m_aTexture[3].i = (0.5f + TextureIndex) / 256.0f;
}
void CGraphics_Threaded::QuadsSetSubsetFree(
float x0, float y0, float x1, float y1,
float x2, float y2, float x3, float y3)
float x2, float y2, float x3, float y3, int TextureIndex)
{
m_aTexture[0].u = x0; m_aTexture[0].v = y0;
m_aTexture[1].u = x1; m_aTexture[1].v = y1;
m_aTexture[2].u = x2; m_aTexture[2].v = y2;
m_aTexture[3].u = x3; m_aTexture[3].v = y3;
m_aTexture[0].i = m_aTexture[1].i = m_aTexture[2].i = m_aTexture[3].i = (0.5f + TextureIndex) / 256.0f;
}
void CGraphics_Threaded::QuadsDraw(CQuadItem *pArray, int Num)
@@ -121,7 +121,7 @@ class CCommandBuffer
};
struct SPoint { float x, y, z; };
struct STexCoord { float u, v; };
struct STexCoord { float u, v, i; };
struct SColor { float r, g, b, a; };
struct SVertex
@@ -213,6 +213,7 @@ class CCommandBuffer
int m_Width;
int m_Height;
int m_Depth;
int m_PixelSize;
int m_Format;
int m_StoreFormat;
@@ -412,10 +413,10 @@ class CGraphics_Threaded : public IEngineGraphics
virtual void SetColor(float r, float g, float b, float a);
virtual void SetColor4(vec4 TopLeft, vec4 TopRight, vec4 BottomLeft, vec4 BottomRight);
virtual void QuadsSetSubset(float TlU, float TlV, float BrU, float BrV);
virtual void QuadsSetSubset(float TlU, float TlV, float BrU, float BrV, int TextureIndex = 0);
virtual void QuadsSetSubsetFree(
float x0, float y0, float x1, float y1,
float x2, float y2, float x3, float y3);
float x2, float y2, float x3, float y3, int TextureIndex = 0);
virtual void QuadsDraw(CQuadItem *pArray, int Num);
virtual void QuadsDrawTL(const CQuadItem *pArray, int Num);
@@ -64,6 +64,7 @@ class IGraphics : public IInterface
{
TEXLOAD_NORESAMPLE = 1,
TEXLOAD_NOMIPMAPS = 2,
TEXLOAD_ARRAY_256 = 4,
};
@@ -122,8 +123,8 @@ class IGraphics : public IInterface
virtual void QuadsBegin() = 0;
virtual void QuadsEnd() = 0;
virtual void QuadsSetRotation(float Angle) = 0;
virtual void QuadsSetSubset(float TopLeftY, float TopLeftV, float BottomRightU, float BottomRightV) = 0;
virtual void QuadsSetSubsetFree(float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3) = 0;
virtual void QuadsSetSubset(float TopLeftY, float TopLeftV, float BottomRightU, float BottomRightV, int TextureIndex = 0) = 0;
virtual void QuadsSetSubsetFree(float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3, int TextureIndex = 0) = 0;
struct CQuadItem
{
@@ -32,18 +32,29 @@ void CMapImages::OnMapLoad()
// load new textures
for(int i = 0; i < m_Count; i++)
{
int TextureFlags = 0;
for(int k = 0; k < Layers()->NumLayers(); k++)
{
const CMapItemLayer * const pLayer = Layers()->GetLayer(k);
if(pLayer->m_Type == LAYERTYPE_TILES && ((const CMapItemLayerTilemap * const)pLayer)->m_Image == i)
{
TextureFlags = IGraphics::TEXLOAD_ARRAY_256;
break;
}
}
CMapItemImage *pImg = (CMapItemImage *)pMap->GetItem(Start+i, 0, 0);
if(pImg->m_External || (pImg->m_Version > 1 && pImg->m_Format != CImageInfo::FORMAT_RGB && pImg->m_Format != CImageInfo::FORMAT_RGBA))
{
char Buf[256];
char *pName = (char *)pMap->GetData(pImg->m_ImageName);
str_format(Buf, sizeof(Buf), "mapres/%s.png", pName);
m_aTextures[i] = Graphics()->LoadTexture(Buf, IStorage::TYPE_ALL, CImageInfo::FORMAT_AUTO, 0);
m_aTextures[i] = Graphics()->LoadTexture(Buf, IStorage::TYPE_ALL, CImageInfo::FORMAT_AUTO, TextureFlags);
}
else
{
void *pData = pMap->GetData(pImg->m_ImageData);
m_aTextures[i] = Graphics()->LoadTextureRaw(pImg->m_Width, pImg->m_Height, pImg->m_Version == 1 ? CImageInfo::FORMAT_RGBA : pImg->m_Format, pData, CImageInfo::FORMAT_RGBA, 0);
m_aTextures[i] = Graphics()->LoadTextureRaw(pImg->m_Width, pImg->m_Height, pImg->m_Version == 1 ? CImageInfo::FORMAT_RGBA : pImg->m_Format, pData, CImageInfo::FORMAT_RGBA, TextureFlags);
pMap->UnloadData(pImg->m_ImageData);
}
}
@@ -258,10 +258,10 @@ void CMapLayers::OnRender()
Graphics()->BlendNone();
vec4 Color = vec4(pTMap->m_Color.r/255.0f, pTMap->m_Color.g/255.0f, pTMap->m_Color.b/255.0f, pTMap->m_Color.a/255.0f);
RenderTools()->RenderTilemap(pTiles, pTMap->m_Width, pTMap->m_Height, 32.0f, Color, TILERENDERFLAG_EXTEND|LAYERRENDERFLAG_OPAQUE,
EnvelopeEval, this, pTMap->m_ColorEnv, pTMap->m_ColorEnvOffset);
EnvelopeEval, this, pTMap->m_ColorEnv, pTMap->m_ColorEnvOffset, true);
Graphics()->BlendNormal();
RenderTools()->RenderTilemap(pTiles, pTMap->m_Width, pTMap->m_Height, 32.0f, Color, TILERENDERFLAG_EXTEND|LAYERRENDERFLAG_TRANSPARENT,
EnvelopeEval, this, pTMap->m_ColorEnv, pTMap->m_ColorEnvOffset);
EnvelopeEval, this, pTMap->m_ColorEnv, pTMap->m_ColorEnvOffset, true);
}
else if(pLayer->m_Type == LAYERTYPE_QUADS)
{
@@ -72,7 +72,7 @@ class CRenderTools
// map render methods (gc_render_map.cpp)
static void RenderEvalEnvelope(CEnvPoint *pPoints, int NumPoints, int Channels, float Time, float *pResult);
void RenderQuads(CQuad *pQuads, int NumQuads, int Flags, ENVELOPE_EVAL pfnEval, void *pUser);
void RenderTilemap(CTile *pTiles, int w, int h, float Scale, vec4 Color, int RenderFlags, ENVELOPE_EVAL pfnEval, void *pUser, int ColorEnv, int ColorEnvOffset);
void RenderTilemap(CTile *pTiles, int w, int h, float Scale, vec4 Color, int RenderFlags, ENVELOPE_EVAL pfnEval, void *pUser, int ColorEnv, int ColorEnvOffset, bool UseArray);
// helpers
void MapscreenToWorld(float CenterX, float CenterY, float ParallaxX, float ParallaxY,
@@ -164,7 +164,7 @@ void CRenderTools::RenderQuads(CQuad *pQuads, int NumQuads, int RenderFlags, ENV
}
void CRenderTools::RenderTilemap(CTile *pTiles, int w, int h, float Scale, vec4 Color, int RenderFlags,
ENVELOPE_EVAL pfnEval, void *pUser, int ColorEnv, int ColorEnvOffset)
ENVELOPE_EVAL pfnEval, void *pUser, int ColorEnv, int ColorEnvOffset, bool UseArray)
{
//Graphics()->TextureSet(img_get(tmap->image));
float ScreenX0, ScreenY0, ScreenX1, ScreenY1;
@@ -257,15 +257,34 @@ void CRenderTools::RenderTilemap(CTile *pTiles, int w, int h, float Scale, vec4
int Py0 = ty*(1024/16);
int Px1 = Px0+(1024/16)-1;
int Py1 = Py0+(1024/16)-1;
/*
float x0 = Nudge + Px0/TexSize+Frac;
float y0 = Nudge + Py0/TexSize+Frac;
float x1 = Nudge + Px1/TexSize-Frac;
float y1 = Nudge + Py0/TexSize+Frac;
float x2 = Nudge + Px1/TexSize-Frac;
float y2 = Nudge + Py1/TexSize-Frac;
float x3 = Nudge + Px0/TexSize+Frac;
float y3 = Nudge + Py1/TexSize-Frac;
*/
float x0, y0, x1, y1, x2, y2, x3, y3;
if(UseArray)
{
x0 = 0;
y0 = 0;
x1 = 1;
y1 = 0;
x2 = 1;
y2 = 1;
x3 = 0;
y3 = 1;
}
else
{
x0 = Nudge + Px0/TexSize+Frac;
y0 = Nudge + Py0/TexSize+Frac;
x1 = Nudge + Px1/TexSize-Frac;
y1 = Nudge + Py0/TexSize+Frac;
x2 = Nudge + Px1/TexSize-Frac;
y2 = Nudge + Py1/TexSize-Frac;
x3 = Nudge + Px0/TexSize+Frac;
y3 = Nudge + Py1/TexSize-Frac;
}
if(Flags&TILEFLAG_VFLIP)
{
@@ -297,7 +316,7 @@ void CRenderTools::RenderTilemap(CTile *pTiles, int w, int h, float Scale, vec4
y1 = Tmp;
}
Graphics()->QuadsSetSubsetFree(x0, y0, x1, y1, x2, y2, x3, y3);
Graphics()->QuadsSetSubsetFree(x0, y0, x1, y1, x2, y2, x3, y3, Index);
IGraphics::CQuadItem QuadItem(x*Scale, y*Scale, Scale, Scale);
Graphics()->QuadsDrawTL(&QuadItem, 1);
}
Oops, something went wrong.

0 comments on commit 3703d22

Please sign in to comment.