2
0
mirror of https://github.com/dpethes/rerogue.git synced 2025-06-07 18:58:32 +02:00

terrain viewer: fix uv coords

This commit is contained in:
dpethes 2015-07-06 17:44:12 +02:00
parent ef525fc0ff
commit ab91e14a2f

View File

@ -45,6 +45,7 @@ implementation
procedure TTerrainMesh.TransformTiles; procedure TTerrainMesh.TransformTiles;
//basex/y - offset in vertices //basex/y - offset in vertices
//TODO solve flipped coords
procedure TileToBlock(var blk: TTerrainBlock; var tile: TTile; basex, basey: integer); procedure TileToBlock(var blk: TTerrainBlock; var tile: TTile; basex, basey: integer);
const const
h_scale = 0.5; h_scale = 0.5;
@ -61,9 +62,9 @@ procedure TTerrainMesh.TransformTiles;
for x := 0 to 4 do begin for x := 0 to 4 do begin
v.x := (-width_half + basex + x) * h_scale; v.x := (-width_half + basex + x) * h_scale;
v.z := (-height_half + basey + y) * h_scale; v.z := (-height_half + basey + y) * h_scale;
v.u := -x * 1/4; v.v := (1 - x/4);
v.v := -y * 1/4; v.u := y/4;
v.y := shortint(tile.heights[y+x*5]) * v_scale; //hmm... all transforms are flipped? v.y := shortint(tile.heights[y+x*5]) * v_scale;
v.y := -v.y; v.y := -v.y;
blk.vertices[y * 5 + x] := v; blk.vertices[y * 5 + x] := v;
@ -138,89 +139,61 @@ end;
//draw vertices from each block //draw vertices from each block
procedure TTerrainMesh.DrawGL(opts: TRenderOpts); procedure TTerrainMesh.DrawGL(opts: TRenderOpts);
procedure RenderBlock(var blk: TTerrainBlock); procedure RenderBlock(var blk: TTerrainBlock);
var procedure RenderTri(i0, i1, i2:integer);
i, x, y, stride: integer; var
v: TVertex3f; v: TVertex3f;
begin begin
stride := 5; v := blk.vertices[i0];
glBindTexture(GL_TEXTURE_2D, textures_glidx[blk.texture_index]); glTexCoord2f(v.u, v.v);
{ glVertex3fv(@v);
glBegin(GL_TRIANGLES); v := blk.vertices[i1];
//glColor3f(0, 1, 0); glTexCoord2f(v.u, v.v);
for y := 0 to 3 do glVertex3fv(@v);
for x := 0 to 3 do begin v := blk.vertices[i2];
//do two triangles glTexCoord2f(v.u, v.v);
i := y * stride + x; glVertex3fv(@v);
end;
var
i, x, y, stride: integer;
begin
stride := 5;
glBindTexture(GL_TEXTURE_2D, textures_glidx[blk.texture_index]);
v := blk.vertices[i + 1]; glBegin(GL_TRIANGLES);
glVertex3fv(@v); //glColor3f(0, 1, 0);
glTexCoord2f(v.u, v.v); for y := 0 to 3 do
v := blk.vertices[i]; for x := 0 to 3 do begin
glVertex3fv(@v); //do two triangles
glTexCoord2f(v.u, v.v); i := y * stride + x;
v := blk.vertices[i + stride]; RenderTri(i+1, i, i+stride);
glVertex3fv(@v); RenderTri(i+1, i+stride, i+stride+1);
glTexCoord2f(v.u, v.v); end;
glEnd;
v := blk.vertices[i + 1]; { //only 2 tris per block
glVertex3fv(@v); glBegin(GL_TRIANGLES);
glTexCoord2f(v.u, v.v); i := y * stride + x;
v := blk.vertices[i + stride]; RenderTri(0, 4, 24);
glVertex3fv(@v); RenderTri(24, 20, 0);
glTexCoord2f(v.u, v.v); glEnd;
v := blk.vertices[i + stride + 1]; }
glVertex3fv(@v); end;
glTexCoord2f(v.u, v.v);
end;
glEnd;
}
//quad from block corners
glBegin(GL_QUADS);
i := y * stride + x;
v := blk.vertices[4];
glVertex3fv(@v);
glTexCoord2f(v.u, v.v);
v := blk.vertices[0];
glVertex3fv(@v);
glTexCoord2f(v.u, v.v);
v := blk.vertices[20];
glVertex3fv(@v);
glTexCoord2f(v.u, v.v);
v := blk.vertices[24];
glVertex3fv(@v);
glTexCoord2f(v.u, v.v);
glEnd;
end;
var var
i, x, y, stride: integer; i, x, y, stride: integer;
blk: TTerrainBlock; blk: TTerrainBlock;
v: TVertex3f; v: TVertex3f;
begin begin
if opts.points then begin if opts.wireframe then
glBegin(GL_POINTS); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
glColor3f(0, 1, 0); else
for i := 0 to terrain.vertex_count - 1 do begin glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
v := terrain.vertex_array[i]; glEnable(GL_TEXTURE_2D);
glVertex3fv(@v); for y := 0 to terrain.TileHeight - 1 do
for x := 0 to terrain.TileWidth - 1 do begin
blk := blocks[y, x];
RenderBlock(blk);
end; end;
glEnd;
end else begin
if opts.wireframe then
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
else
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_TEXTURE_2D);
for y := 0 to terrain.TileHeight - 1 do
for x := 0 to terrain.TileWidth - 1 do begin
blk := blocks[y, x];
RenderBlock(blk);
end;
end;
end; end;
end. end.