Skip to content

Commit

Permalink
ecere/gfx/3D/Mesh: Array textures support for 3D models
Browse files Browse the repository at this point in the history
  • Loading branch information
jerstlouis committed Apr 29, 2020
1 parent c3661c0 commit 752a88f
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 21 deletions.
2 changes: 1 addition & 1 deletion butterbur/src/opengl/ButterburShader.ec
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import "VersionedShader"

private: // FIXME: eC bug

define squishFactorAttribute = 100;
define squishFactorAttribute = 13;

Size resetDisplaySize;
Size displaySize;
Expand Down
58 changes: 54 additions & 4 deletions ecere/src/gfx/3D/Mesh.ec
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ namespace gfx3D;

import "Display"

import "GLMultiDraw"

public class MeshFeatures
{
public:
Expand Down Expand Up @@ -974,7 +976,7 @@ public:
data = value;
}

bool Upload(DisplaySystem displaySystem, bool uploadTextures, GLMB mab, GLMB meab)
bool Upload(DisplaySystem displaySystem, bool uploadTextures, GLMB mab, GLMB meab, int nAT, GLArrayTexture * mAT)
{
bool result = false;
PrimitiveGroup g;
Expand All @@ -999,16 +1001,64 @@ public:

for(g = groups.first; g; g = g.next)
{
Material mat = g.material;
if(!g.type.vertexRange)
{
if(clearData)
g.data = null;
UnlockPrimitiveGroup(g);
}
if(uploadTextures)
if(mat && uploadTextures)
{
Material mat = g.material;
if(mat && mat.baseMap && mat.baseMap.displaySystem != displaySystem)
if(nAT && mAT != null)
{
int i;
for(i = 0; i < Min(1, nAT); i++)
{
Bitmap bitmap = i == 0 ? mat.baseMap : null;
if(bitmap && bitmap.displaySystem != displaySystem)
{
Bitmap convBitmap = bitmap;
GLArrayTexture * at = &mAT[i];
if(convBitmap.pixelFormat != pixelFormatRGBAGL && convBitmap.pixelFormat != pixelFormatETC2RGBA8)
convBitmap = bitmap.ProcessDD(true, 0, false, 16384, false); //oglSystem.maxTextureSize, !capabilities.nonPow2Textures);
if(convBitmap)
{
if(convBitmap.bitmaps)
{
int layer = at->allocateLayer(0);
int j;
int numLevels = at->numLevels;
int skipLevel = Max(0, convBitmap.numMipMaps - numLevels);
if(layer != -1)
{
for(j = 0; j < convBitmap.numMipMaps; j++)
{
Bitmap bmp = convBitmap.bitmaps[j];
if(bmp)
{
int level = j - skipLevel;
if(level >= 0)
at->setLayer(level, 0, 0, layer, bmp.picture, 0);
delete bmp.picture;
delete bmp;
}
}
delete convBitmap.bitmaps;

bitmap.displaySystem = displaySystem;
bitmap.driver = displaySystem.driver;
bitmap.driverData = (void *)(intptr)layer; // TOFIX: *not* a texture in this case! Don't free as one.
}
}
}

if(convBitmap != bitmap)
delete convBitmap;
}
}
}
else if(mat.baseMap && mat.baseMap.displaySystem != displaySystem)
mat.baseMap.MakeMipMaps(displaySystem);
}
delete g.indices;
Expand Down
8 changes: 4 additions & 4 deletions ecere/src/gfx/3D/Object.ec
Original file line number Diff line number Diff line change
Expand Up @@ -1875,19 +1875,19 @@ private:

public property DisplaySystem displaySystem
{
set { Upload(value, null, null); }
set { Upload(value, null, null, 0, null); }
get { return displaySystem; }
}

public void Upload(DisplaySystem displaySystem, GLMB mab, GLMB meab)
public void Upload(DisplaySystem displaySystem, GLMB mab, GLMB meab, int nAT, GLArrayTexture * mAT)
{
Object o;

this.displaySystem = displaySystem;
if(flags.mesh && mesh)
mesh.Upload(displaySystem, true, mab, meab);
mesh.Upload(displaySystem, true, mab, meab, nAT, mAT);
for(o = children.first; o; o = o.next)
o.Upload(displaySystem, mab, meab);
o.Upload(displaySystem, mab, meab, nAT, mAT);
}

void setTransform(Matrix sm, Matrix svm, Vector3D cp) // Start-up matrix, Start-up X View Matrix, Camera Position
Expand Down
37 changes: 25 additions & 12 deletions ecere/src/gfx/drivers/gl3/GLMultiDraw.ec
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ public struct FreeSpots

void init(uint count)
{
int i;
spots = new int[count];
for(i = 0; i < count-1; i++)
spots[i] = i+1;
spots[i] = -1;
nextSpot = 0;
size = count;
if(count)
{
int i;
spots = new int[count];
for(i = 0; i < (int)count-1; i++)
spots[i] = i+1;
spots[i] = -1;
nextSpot = 0;
size = count;
}
}

void markFree(int spot)
Expand Down Expand Up @@ -162,6 +165,7 @@ public struct GLArrayTexture
width = w;
height = h;
numLayers = count;

glBindTexture(target, texture);

if(setMaxLevel)
Expand All @@ -187,9 +191,16 @@ public struct GLArrayTexture
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, levels > 1 ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexParameteri(target, GL_TEXTURE_WRAP_S, glClampFunction(glVersion)); //GL_CLAMP_TO_EDGE
glTexParameteri(target, GL_TEXTURE_WRAP_T, glClampFunction(glVersion)); //GL_CLAMP_TO_EDGE

if(levels > 3) // TODO: Wrap options as a property?
{
glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
else
{
glTexParameteri(target, GL_TEXTURE_WRAP_S, glClampFunction(glVersion)); //GL_CLAMP_TO_EDGE
glTexParameteri(target, GL_TEXTURE_WRAP_T, glClampFunction(glVersion)); //GL_CLAMP_TO_EDGE
}
glBindTexture(target, 0);
}

Expand Down Expand Up @@ -261,11 +272,13 @@ public struct GLArrayTexture
void setLayerFormat(int level, int x, int y, int layer, byte * c, uint targetFBO, int format, int type)
{
int target = GL_TEXTURE_2D_ARRAY;

if(layer >= numLayers)
resize(layer + Max(2, layer)/2, targetFBO);

glBindTexture(target, texture);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexSubImage3D(target, level, x, y, layer, width, height, 1, format, type, c);
glTexSubImage3D(target, level, x, y, layer, width >> level, height >> level, 1, format, type, c);
glBindTexture(target, 0);
}

Expand All @@ -285,7 +298,7 @@ public struct GLArrayTexture
int layer;
if(!spots.spots)
spots.init(numLayers);
layer = spots.next();
layer = spots.spots ? spots.next() : -1;
if(layer == -1 && numLayers < 2048)
{
resize(Min(2048, Max(8, numLayers + numLayers/2)), targetFBO);
Expand Down

0 comments on commit 752a88f

Please sign in to comment.