Kentae
Member
Hello good people of the GMC!
I have recently added som 3D terrain to my project. The terrain is generated from a heightmap and it works perfectly. I have also added a script for finding the z value of any x/y-position that also works perfectly.
However, I also have a lighting and specular shader that I have added to the terrain and these does of course not work correctly as all the normals in the terrain are set to point directly upwards. This means that the light is just completely even all over the terrain and the specular looks like it is hitting a completley flat plane.
So I need to know how I should go about calculating the normals so that they point in the direction of the surfaces, wich in turn will make the lighting and specular effects work correctly.
Here is the code I use to create the terrain:
Any help will be greatly appreciated
I have recently added som 3D terrain to my project. The terrain is generated from a heightmap and it works perfectly. I have also added a script for finding the z value of any x/y-position that also works perfectly.
However, I also have a lighting and specular shader that I have added to the terrain and these does of course not work correctly as all the normals in the terrain are set to point directly upwards. This means that the light is just completely even all over the terrain and the specular looks like it is hitting a completley flat plane.
So I need to know how I should go about calculating the normals so that they point in the direction of the surfaces, wich in turn will make the lighting and specular effects work correctly.
Here is the code I use to create the terrain:
Code:
///terrain_create(heightmap,squaresize,height)
// argument0 = the sprite to use as heigtmap
// argument1 = the size of each square
// argument2 = the maximum height of the terrain
// get input
global.gridWidth = sprite_get_width( argument0 );
global.gridHeight = sprite_get_height( argument0 );
global.segmentSize = argument1;
// create a grid based on the heightmaps size and input the height values
global.terrainGrid = ds_grid_create( global.gridWidth, global.gridHeight );
draw_sprite( argument0, 0, 0, 0 );
for ( w = 0; w < global.gridWidth; w++ )
{
for ( h = 0; h < global.gridHeight; h++ )
{
col = draw_getpixel( w, h );
val = colour_get_value( col );
ds_grid_set( global.terrainGrid, w, h, (val / 255) * argument2 );
}
}
// create the terrain model using the information from the newly made grid
global.terrainModel = d3d_model_create();
for ( xx = 0; xx < global.gridWidth; xx++; )
{
d3d_model_primitive_begin( global.terrainModel, pr_trianglestrip );
//drawSwitch = true;
for ( yy = 0; yy < global.gridHeight; yy++ )
{
//drawSwitch = !drawSwitch;
z1 = ds_grid_get( global.terrainGrid, xx, yy );
z2 = ds_grid_get( global.terrainGrid, xx + 1, yy );
xTex = xx * ( 1 / global.gridWidth );
xTex2 = (xx + 1) * ( 1 / global.gridWidth );
yTex = yy * ( 1 / global.gridHeight );
d3d_model_vertex_normal_texture( global.terrainModel, xx * global.segmentSize, yy * global.segmentSize, z1, 0, 0, 1, xTex, yTex );
d3d_model_vertex_normal_texture( global.terrainModel, (xx + 1) * global.segmentSize, yy * global.segmentSize, z2, 0, 0, 1, xTex2, yTex );
}
d3d_model_primitive_end( global.terrainModel );
}
depth = -1000;