Create normals and actually use texture

This commit is contained in:
hodasemi 2019-06-01 14:35:43 +02:00
parent 71c1b73ec3
commit 9737818f81
2 changed files with 76 additions and 29 deletions

View file

@ -92,6 +92,9 @@ private:
// Create height lines for the level "level" // Create height lines for the level "level"
void create_level_line(int level); void create_level_line(int level);
void push_point(int x, int y);
vec3d create_point(int x, int y);
vec2d texture_coord(int x, int y);
// Load the height map from the file "filename" // Load the height map from the file "filename"
bool load_heightmap(const char *filename); bool load_heightmap(const char *filename);

View file

@ -112,8 +112,9 @@ void terrain::render_solid_terrain()
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE0, texture_handle);
// Set the material color to white // Set the material color to white
glColor3d(1, 1, 1); glColor3d(1, 1, 1);
@ -147,7 +148,12 @@ void terrain::render_wireframe_terrain()
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
} }
void terrain::push_point(int x, int y)
{
set_normal(x, y);
glTexCoord2dv(texture_coord(x, y));
glVertex3dv(create_point(x, y));
}
// Render the terrain // Render the terrain
void terrain::render_terrain() void terrain::render_terrain()
@ -214,8 +220,8 @@ void terrain::render_terrain()
// Draw one strip // Draw one strip
for (int x = 0; x < map_width; x++) { for (int x = 0; x < map_width; x++) {
glVertex3f(static_cast<float>(x), get_heightmap_value(x, y), static_cast<float>(y)); push_point(x, y);
glVertex3f(static_cast<float>(x), get_heightmap_value(x, y + 1), static_cast<float>(y) + 1.0f); push_point(x, y + 1);
} }
glEnd(); glEnd();
@ -224,8 +230,30 @@ void terrain::render_terrain()
glPopMatrix(); glPopMatrix();
} }
vec2d terrain::texture_coord(int x, int y)
{
return vec2d(
static_cast<double>(x) / static_cast<double>(get_heightmap_width()),
static_cast<double>(y) / static_cast<double>(get_heightmap_height())
);
}
vec3d terrain::create_point(int x, int y)
{
return vec3d(
static_cast<double>(x),
static_cast<double>(get_heightmap_value(x, y)),
static_cast<double>(y)
);
}
vec3d calculate_normal(vec3d middle, vec3d p0, vec3d p1)
{
vec3d v0 = middle - p0;
vec3d v1 = p1 - middle;
return cross(v0, v1);
}
// Calculate and set the normal for height map entry (x,y) // Calculate and set the normal for height map entry (x,y)
void terrain::set_normal(int x, int y) void terrain::set_normal(int x, int y)
@ -270,6 +298,22 @@ void terrain::set_normal(int x, int y)
double y = vec3.y(); double y = vec3.y();
double z = vec3.z(); double z = vec3.z();
*****************/ *****************/
vec3d middle = create_point(x, y);
vec3d right = create_point(x + 1, y);
vec3d top = create_point(x, y + 1);
vec3d left = create_point(x - 1, y);
vec3d bottom = create_point(x, y - 1);
vec3d normal0 = calculate_normal(middle, right, top);
vec3d normal1 = calculate_normal(middle, top, left);
vec3d normal2 = calculate_normal(middle, left, bottom);
vec3d normal3 = calculate_normal(middle, bottom, right);
vec3d final_normal = (normal0 + normal1 + normal2 + normal3) / 4;
glNormal3dv(final_normal);
} }