Skip to content

Commit

Permalink
switched back to vertex struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Dav1dde committed Jul 24, 2012
1 parent 9c42574 commit 02a37a0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 45 deletions.
31 changes: 11 additions & 20 deletions brala/dine/builder/builder.d
Expand Up @@ -18,19 +18,10 @@ mixin template BlockBuilder() {
float u, float v, float u_biome, float v_biome) float u, float v, float u_biome, float v_biome)
in { assert(elements+1 <= buffer_length, "not enough allocated memory for tessellator"); } in { assert(elements+1 <= buffer_length, "not enough allocated memory for tessellator"); }
body { body {
buffer[elements++] = x; buffer[elements++] = Vertex(x, y, z, nx, ny, nz, u, v, u_biome, v_biome);
buffer[elements++] = y;
buffer[elements++] = z;
buffer[elements++] = nx;
buffer[elements++] = ny;
buffer[elements++] = nz;
buffer[elements++] = u;
buffer[elements++] = v;
buffer[elements++] = u_biome;
buffer[elements++] = v_biome;
} }


void add_template_vertices(const ref float[] vertices, void add_template_vertices(const ref Vertex[] vertices,
float x_offset, float y_offset, float z_offset, float x_offset, float y_offset, float z_offset,
float u_biome, float v_biome) float u_biome, float v_biome)
in { assert(elements+vertices.length <= buffer_length, "not enough allocated memory for tessellator"); } in { assert(elements+vertices.length <= buffer_length, "not enough allocated memory for tessellator"); }
Expand All @@ -39,16 +30,16 @@ mixin template BlockBuilder() {


size_t end = elements+vertices.length; size_t end = elements+vertices.length;
for(; elements < end;) { for(; elements < end;) {
buffer[elements++] += x_offset; Vertex* vertex = &buffer[elements++];
buffer[elements++] += y_offset; vertex.x += x_offset;
buffer[elements++] += z_offset; vertex.y += y_offset;
elements += 5; vertex.z += z_offset;
buffer[elements++] += u_biome; vertex.u_biome += u_biome;
buffer[elements++] += v_biome; vertex.v_biome += v_biome;
} }
} }


void add_vertices(const ref float[] vertices) void add_vertices(const ref Vertex[] vertices)
in { assert(elements+vertices.length <= buffer_length, "not enough allocated memory for tesselator"); } in { assert(elements+vertices.length <= buffer_length, "not enough allocated memory for tesselator"); }
body { body {
buffer[elements..(elements+(vertices.length))] = vertices; buffer[elements..(elements+(vertices.length))] = vertices;
Expand All @@ -58,15 +49,15 @@ mixin template BlockBuilder() {
// blocks // blocks
void grass_block(Side s)(const ref Block block, const ref BiomeData biome_data, void grass_block(Side s)(const ref Block block, const ref BiomeData biome_data,
float x_offset, float y_offset, float z_offset) { float x_offset, float y_offset, float z_offset) {
float[] vertices = get_vertices!(s)(block.id); Vertex[] vertices = get_vertices!(s)(block.id);


add_template_vertices(vertices, x_offset, y_offset, z_offset, add_template_vertices(vertices, x_offset, y_offset, z_offset,
biome_data.grass_uv.field); biome_data.grass_uv.field);
} }


void leave_block(Side s)(const ref Block block, const ref BiomeData biome_data, void leave_block(Side s)(const ref Block block, const ref BiomeData biome_data,
float x_offset, float y_offset, float z_offset) { float x_offset, float y_offset, float z_offset) {
float[] vertices = get_vertices!(s)(block.id); Vertex[] vertices = get_vertices!(s)(block.id);


add_template_vertices(vertices, x_offset, y_offset, z_offset, add_template_vertices(vertices, x_offset, y_offset, z_offset,
biome_data.leave_uv.field); biome_data.leave_uv.field);
Expand Down
12 changes: 6 additions & 6 deletions brala/dine/builder/tessellator.d
Expand Up @@ -36,30 +36,30 @@ align(1) struct Vertex {
float nz; float nz;
float u_terrain; float u_terrain;
float v_terrain; float v_terrain;
float u_palette; float u_biome;
float v_palette; float v_biome;
} }




struct Tessellator { struct Tessellator {
World world; World world;


float* buffer; Vertex* buffer;
size_t buffer_length; size_t buffer_length;


uint elements = 0; uint elements = 0;


mixin BlockBuilder!(); mixin BlockBuilder!();


this(World world, ref float* buffer, ref size_t buffer_length) { this(World world, ref Vertex* buffer, ref size_t buffer_length) {
this.world = world; this.world = world;
this.buffer = buffer; this.buffer = buffer;
this.buffer_length = buffer_length; this.buffer_length = buffer_length;
} }


void realloc_buffer(size_t interval) { void realloc_buffer(size_t interval) {
buffer_length += interval*float.sizeof; buffer_length += interval*Vertex.sizeof;
buffer = cast(float*)realloc(buffer, buffer_length); buffer = cast(Vertex*)realloc(buffer, buffer_length);
} }


void realloc_buffer_if_needed(size_t interval) { void realloc_buffer_if_needed(size_t interval) {
Expand Down
24 changes: 12 additions & 12 deletions brala/dine/builder/vertices.d
Expand Up @@ -83,7 +83,7 @@ immutable CubeSideData[6] CUBE_VERTICES = [
[0.0f, -1.0f, 0.0f] } [0.0f, -1.0f, 0.0f] }
]; ];


float[] simple_block(Side side, MCTextureSlice texture_slice) { Vertex[] simple_block(Side side, MCTextureSlice texture_slice) {
CubeSideData cbsd = CUBE_VERTICES[side]; CubeSideData cbsd = CUBE_VERTICES[side];


float[3][6] positions = to_triangles(cbsd.positions); float[3][6] positions = to_triangles(cbsd.positions);
Expand All @@ -92,26 +92,26 @@ float[] simple_block(Side side, MCTextureSlice texture_slice) {
// vertex normal texcoords palette // vertex normal texcoords palette
Vertex[] data; Vertex[] data;


/*foreach(i; 0..6) { foreach(i; 0..6) {
data ~= Vertex(positions[i][0], positions[i][1], positions[i][2], data ~= Vertex(positions[i][0], positions[i][1], positions[i][2],
cbsd.normal[0], cbsd.normal[1], cbsd.normal[2], cbsd.normal[0], cbsd.normal[1], cbsd.normal[2],
texcoords[i][0], texcoords[i][1], texcoords[i][0], texcoords[i][1],
0, 0); 0, 0);
} }


return data;*/ return data;

/*
return join([positions[0], cbsd.normal, texcoords[0], [0.0f, 0.0f], return join([positions[0], cbsd.normal, texcoords[0], [0.0f, 0.0f],
positions[1], cbsd.normal, texcoords[1], [0.0f, 0.0f], positions[1], cbsd.normal, texcoords[1], [0.0f, 0.0f],
positions[2], cbsd.normal, texcoords[2], [0.0f, 0.0f], positions[2], cbsd.normal, texcoords[2], [0.0f, 0.0f],
positions[3], cbsd.normal, texcoords[3], [0.0f, 0.0f], positions[3], cbsd.normal, texcoords[3], [0.0f, 0.0f],
positions[4], cbsd.normal, texcoords[4], [0.0f, 0.0f], positions[4], cbsd.normal, texcoords[4], [0.0f, 0.0f],
positions[5], cbsd.normal, texcoords[5], [0.0f, 0.0f]]); positions[5], cbsd.normal, texcoords[5], [0.0f, 0.0f]]);*/
} }


private alias MCTextureSlice t; private alias MCTextureSlice t;


float[][] BLOCK_VERTICES_LEFT = [ Vertex[][] BLOCK_VERTICES_LEFT = [
[], // air [], // air
simple_block(Side.LEFT, t(1, 1)), // stone simple_block(Side.LEFT, t(1, 1)), // stone
simple_block(Side.LEFT, t(3, 1)), // grass simple_block(Side.LEFT, t(3, 1)), // grass
Expand Down Expand Up @@ -243,7 +243,7 @@ float[][] BLOCK_VERTICES_LEFT = [
[] []
]; ];


float[][] BLOCK_VERTICES_RIGHT = [ Vertex[][] BLOCK_VERTICES_RIGHT = [
[], // air [], // air
simple_block(Side.RIGHT, t(1, 1)), // stone simple_block(Side.RIGHT, t(1, 1)), // stone
simple_block(Side.RIGHT, t(3, 1)), // grass simple_block(Side.RIGHT, t(3, 1)), // grass
Expand Down Expand Up @@ -375,7 +375,7 @@ float[][] BLOCK_VERTICES_RIGHT = [
[] []
]; ];


float[][] BLOCK_VERTICES_NEAR = [ Vertex[][] BLOCK_VERTICES_NEAR = [
[], // air [], // air
simple_block(Side.NEAR, t(1, 1)), // stone simple_block(Side.NEAR, t(1, 1)), // stone
simple_block(Side.NEAR, t(3, 1)), // grass simple_block(Side.NEAR, t(3, 1)), // grass
Expand Down Expand Up @@ -507,7 +507,7 @@ float[][] BLOCK_VERTICES_NEAR = [
[] []
]; ];


float[][] BLOCK_VERTICES_FAR = [ Vertex[][] BLOCK_VERTICES_FAR = [
[], // air [], // air
simple_block(Side.FAR, t(1, 1)), // stone simple_block(Side.FAR, t(1, 1)), // stone
simple_block(Side.FAR, t(3, 1)), // grass simple_block(Side.FAR, t(3, 1)), // grass
Expand Down Expand Up @@ -639,7 +639,7 @@ float[][] BLOCK_VERTICES_FAR = [
[] []
]; ];


float[][] BLOCK_VERTICES_TOP = [ Vertex[][] BLOCK_VERTICES_TOP = [
[], // air [], // air
simple_block(Side.TOP, t(1, 1)), // stone simple_block(Side.TOP, t(1, 1)), // stone
simple_block(Side.TOP, t(0, 1)), // grass simple_block(Side.TOP, t(0, 1)), // grass
Expand Down Expand Up @@ -771,7 +771,7 @@ float[][] BLOCK_VERTICES_TOP = [
[] []
]; ];


float[][] BLOCK_VERTICES_BOTTOM = [ Vertex[][] BLOCK_VERTICES_BOTTOM = [
[], // air [], // air
simple_block(Side.BOTTOM, t(1, 1)), // stone simple_block(Side.BOTTOM, t(1, 1)), // stone
simple_block(Side.BOTTOM, t(2, 1)), // grass simple_block(Side.BOTTOM, t(2, 1)), // grass
Expand Down Expand Up @@ -903,7 +903,7 @@ float[][] BLOCK_VERTICES_BOTTOM = [
[] []
]; ];


ref float[] get_vertices(Side side, T)(T index) if(isIntegral!T) { ref Vertex[] get_vertices(Side side, T)(T index) if(isIntegral!T) {
static if(side == Side.LEFT) { static if(side == Side.LEFT) {
return BLOCK_VERTICES_LEFT[index]; return BLOCK_VERTICES_LEFT[index];
} else static if(side == Side.RIGHT) { } else static if(side == Side.RIGHT) {
Expand Down
14 changes: 7 additions & 7 deletions brala/dine/world.d
Expand Up @@ -17,12 +17,12 @@ private {
private const Block AIR_BLOCK = Block(0); private const Block AIR_BLOCK = Block(0);


class World { class World {
static float* tessellate_buffer; static Vertex* tessellate_buffer;
static size_t tessellate_buffer_length; static size_t tessellate_buffer_length;


static this() { static this() {
tessellate_buffer_length = width*height*depth*20; // this value is the result of testing! tessellate_buffer_length = width*height*depth*5; // this value is the result of testing!
tessellate_buffer = cast(float*)malloc(tessellate_buffer_length*float.sizeof); tessellate_buffer = cast(Vertex*)malloc(tessellate_buffer_length*Vertex.sizeof);
} }


static ~this() { static ~this() {
Expand Down Expand Up @@ -162,7 +162,7 @@ class World {


// fills the vbo with the chunk content // fills the vbo with the chunk content
// original version from florian boesch - http://codeflow.org/ // original version from florian boesch - http://codeflow.org/
void tessellate(Chunk chunk, vec3i chunkc, ref float* v, ref size_t length, bool force=false) { void tessellate(Chunk chunk, vec3i chunkc, ref Vertex* v, ref size_t length, bool force=false) {
Tessellator tessellator = Tessellator(this, v, length); Tessellator tessellator = Tessellator(this, v, length);


if(chunk.vbo is null) { if(chunk.vbo is null) {
Expand Down Expand Up @@ -250,8 +250,8 @@ class World {
} }
} }


//chunk.vbo_vcount = tessellator.elements * __traits(allMembers, Vertex).length; chunk.vbo_vcount = tessellator.elements * __traits(allMembers, Vertex).length;
chunk.vbo_vcount = tessellator.elements / 10; //chunk.vbo_vcount = tessellator.elements / 10;
tessellator.fill_vbo(chunk.vbo); tessellator.fill_vbo(chunk.vbo);


chunk.dirty = false; chunk.dirty = false;
Expand All @@ -272,7 +272,7 @@ class World {
chunk.vbo.bind(position, GL_FLOAT, 3, 0, stride); chunk.vbo.bind(position, GL_FLOAT, 3, 0, stride);
chunk.vbo.bind(normal, GL_FLOAT, 3, Vertex().nx.offsetof, stride); chunk.vbo.bind(normal, GL_FLOAT, 3, Vertex().nx.offsetof, stride);
chunk.vbo.bind(texcoord, GL_FLOAT, 2, Vertex().u_terrain.offsetof, stride); chunk.vbo.bind(texcoord, GL_FLOAT, 2, Vertex().u_terrain.offsetof, stride);
chunk.vbo.bind(palettecoord, GL_FLOAT, 2, Vertex().u_palette.offsetof, stride); chunk.vbo.bind(palettecoord, GL_FLOAT, 2, Vertex().u_biome.offsetof, stride);

This comment has been minimized.

Copy link
@dnadlinger

dnadlinger Jul 24, 2012

This?

This comment has been minimized.

Copy link
@Dav1dde

Dav1dde Jul 24, 2012

Author Owner

.u_platte was the old name, only thing changed is the name.

} }


void draw(BraLaEngine engine) { void draw(BraLaEngine engine) {
Expand Down

0 comments on commit 02a37a0

Please sign in to comment.