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

terrain viewer: simple lighting with per-face normals

This commit is contained in:
dpethes 2015-07-06 18:35:34 +02:00
parent ab91e14a2f
commit 89f88b0918
2 changed files with 42 additions and 2 deletions

View File

@ -136,15 +136,45 @@ begin
GenTexture(i); GenTexture(i);
end; end;
//cross product + normalize
function GetNormalv(const v0, v1, v2: TVertex3f): TVertex3f;
var
a, b: TVertex3f;
len: single;
begin
a.x := v0.x - v1.x;
a.y := v0.y - v1.y;
a.z := v0.z - v1.z;
b.x := v1.x - v2.x;
b.y := v1.y - v2.y;
b.z := v1.z - v2.z;
result.x := (a.y * b.z) - (a.z * b.y);
result.y := (a.z * b.x) - (a.x * b.z);
result.z := (a.x * b.y) - (a.y * b.x);
len := sqrt( sqr(result.x) + sqr(result.y) + sqr(result.z) );
if len = 0 then len := 1;
result.x /= len;
result.y /= len;
result.z /= len;
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);
procedure RenderTri(i0, i1, i2:integer); procedure RenderTri(i0, i1, i2:integer);
var var
v: TVertex3f; v, n: TVertex3f;
begin begin
n := GetNormalv(blk.vertices[i0], blk.vertices[i1], blk.vertices[i2]);
v := blk.vertices[i0]; v := blk.vertices[i0];
glNormal3f(n.x, n.y, n.z);
glTexCoord2f(v.u, v.v); glTexCoord2f(v.u, v.v);
glVertex3fv(@v); glVertex3fv(@v);
v := blk.vertices[i1]; v := blk.vertices[i1];

View File

@ -78,6 +78,10 @@ end;
// initial parameters // initial parameters
procedure InitGL; procedure InitGL;
const
LightAmbient: array[0..3] of single = (0.5, 0.5, 0.5, 1);
LightDiffuse: array[0..3] of single = (1, 1, 1, 1);
LightPosition: array[0..3] of single = (1, 1, 0, 1);
var var
ogl_info: string; ogl_info: string;
begin begin
@ -86,7 +90,7 @@ begin
ogl_info := 'version: ' + glGetString(GL_VERSION); ogl_info := 'version: ' + glGetString(GL_VERSION);
writeln(ogl_info); writeln(ogl_info);
//glShadeModel( GL_SMOOTH ); // Enable smooth shading glShadeModel( GL_SMOOTH ); // Enable smooth shading
glClearColor( 0.0, 0.0, 0.0, 0.0 ); glClearColor( 0.0, 0.0, 0.0, 0.0 );
glClearDepth( 1.0 ); // Depth buffer setup glClearDepth( 1.0 ); // Depth buffer setup
glEnable( GL_DEPTH_TEST ); // Enables Depth Testing glEnable( GL_DEPTH_TEST ); // Enables Depth Testing
@ -97,6 +101,12 @@ begin
//glCullFace( GL_BACK ); //glCullFace( GL_BACK );
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT0, GL_POSITION,LightPosition);
end; end;