Try to solve task 2.2
This commit is contained in:
parent
f86fc26703
commit
9a0df4e4ab
3 changed files with 125 additions and 61 deletions
|
@ -3,6 +3,8 @@
|
|||
// chair of the TU Dresden. Do not distribute!
|
||||
// Copyright (C) CGV TU Dresden - All Rights Reserved
|
||||
|
||||
in vec3 n;
|
||||
|
||||
out vec4 color;
|
||||
|
||||
uniform vec3 cameraPos;
|
||||
|
@ -31,7 +33,7 @@ vec4 getBackgroundColor()
|
|||
void main()
|
||||
{
|
||||
//surface geometry
|
||||
vec3 n = vec3(0, 1, 0);
|
||||
//vec3 n = vec3(0, 1, 0);
|
||||
vec3 dirToViewer = vec3(0, 1, 0);
|
||||
|
||||
//material properties
|
||||
|
|
|
@ -5,18 +5,31 @@
|
|||
|
||||
in vec4 position;
|
||||
|
||||
|
||||
out vec3 n;
|
||||
|
||||
uniform mat4 mvp;
|
||||
|
||||
//Returns the height of the procedural terrain at a given xz position
|
||||
float getTerrainHeight(vec2 p);
|
||||
|
||||
vec3 calculate_normal(vec4 terrain_position);
|
||||
vec4 offset_point(vec4 base, vec2 offset);
|
||||
|
||||
const vec2 offsets[6] = vec2[6](
|
||||
vec2(0.0, 0.0),
|
||||
vec2(0.0, 1.0),
|
||||
vec2(1.0, 0.0),
|
||||
vec2(0.0, 1.0),
|
||||
vec2(1.0, 0.0),
|
||||
vec2(1.0, 1.0)
|
||||
);
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = mvp * position;
|
||||
vec4 terrain_position = vec4(position.x, getTerrainHeight(vec2(position.x, position.z)), position.z, position.w);
|
||||
n = calculate_normal(position);
|
||||
|
||||
gl_Position = mvp * terrain_position;
|
||||
}
|
||||
|
||||
//source: https://gist.github.com/patriciogonzalezvivo/670c22f3966e662d2f83
|
||||
|
@ -56,3 +69,49 @@ float getTerrainHeight(vec2 p)
|
|||
}
|
||||
return 15 * total / maxAmplitude;
|
||||
}
|
||||
|
||||
vec4 offset_point(vec4 base, vec2 offset) {
|
||||
float y = getTerrainHeight(vec2(base.x, base.z) + offset);
|
||||
return vec4(base.x + offset.x, y, base.z + offset.y, base.w);
|
||||
}
|
||||
|
||||
// calculate the position of the first vertex in this square
|
||||
vec4 pos_to_base(vec4 pos, int v_id) {
|
||||
return vec4(pos.x - offsets[v_id].x, pos.y, pos.z - offsets[v_id].y, pos.w);
|
||||
}
|
||||
|
||||
// calculates the normal of terrain_position
|
||||
// it generates the complete triangle based on the gl_VertexID and then normal math
|
||||
vec3 calculate_normal(vec4 terrain_position) {
|
||||
int offset_index = gl_VertexID % 6;
|
||||
|
||||
vec4 base_vertex = pos_to_base(terrain_position, offset_index);
|
||||
|
||||
// the other 2 points from the triangle
|
||||
int points_index = 0;
|
||||
vec4 points[2];
|
||||
|
||||
int iterator_offset = 0;
|
||||
|
||||
// second triangle offset
|
||||
if (offset_index > 2) {
|
||||
iterator_offset = 3;
|
||||
}
|
||||
|
||||
for (int i = iterator_offset; i < (3 + iterator_offset); i++) {
|
||||
// skip for current vertex
|
||||
if (i == offset_index) {
|
||||
continue;
|
||||
}
|
||||
|
||||
points[points_index] = offset_point(base_vertex, offsets[i]);
|
||||
points_index++;
|
||||
}
|
||||
|
||||
// create connection vectors
|
||||
vec3 p1 = (points[0] - terrain_position).xyz;
|
||||
vec3 p2 = (points[1] - terrain_position).xyz;
|
||||
|
||||
// calculate normal
|
||||
return normalize(cross(p1, p2));
|
||||
}
|
|
@ -112,8 +112,10 @@ void Viewer::CreateGeometry()
|
|||
/*Generate positions and indices for a terrain patch with a
|
||||
single triangle strip */
|
||||
|
||||
for (int i = 0; i < PATCH_SIZE; i++) {
|
||||
for (int j = 0; j < PATCH_SIZE; j++) {
|
||||
for (int i = 0; i < PATCH_SIZE; i++)
|
||||
{
|
||||
for (int j = 0; j < PATCH_SIZE; j++)
|
||||
{
|
||||
positions.emplace_back((float)i, 0.0f, (float)j, 1.0f);
|
||||
}
|
||||
}
|
||||
|
@ -122,12 +124,14 @@ void Viewer::CreateGeometry()
|
|||
// decrease y direction by 1
|
||||
int count = (PATCH_SIZE) * (PATCH_SIZE - 1);
|
||||
|
||||
for (int k = 0; k < count; k++) {
|
||||
for (int k = 0; k < count; k++)
|
||||
{
|
||||
|
||||
// skip creating triangles on last x in row
|
||||
int end_of_x = k % (PATCH_SIZE - 1);
|
||||
|
||||
if (end_of_x == 0) {
|
||||
if (end_of_x == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -194,8 +198,7 @@ void CalculateViewFrustum(const Eigen::Matrix4f& mvp, Eigen::Vector4f* frustumPl
|
|||
|
||||
bool IsBoxCompletelyBehindPlane(const Eigen::Vector3f &boxMin, const Eigen::Vector3f &boxMax, const Eigen::Vector4f &plane)
|
||||
{
|
||||
return
|
||||
plane.dot(Eigen::Vector4f(boxMin.x(), boxMin.y(), boxMin.z(), 1)) < 0 &&
|
||||
return plane.dot(Eigen::Vector4f(boxMin.x(), boxMin.y(), boxMin.z(), 1)) < 0 &&
|
||||
plane.dot(Eigen::Vector4f(boxMin.x(), boxMin.y(), boxMax.z(), 1)) < 0 &&
|
||||
plane.dot(Eigen::Vector4f(boxMin.x(), boxMax.y(), boxMin.z(), 1)) < 0 &&
|
||||
plane.dot(Eigen::Vector4f(boxMin.x(), boxMax.y(), boxMin.z(), 1)) < 0 &&
|
||||
|
@ -225,7 +228,7 @@ void Viewer::drawContents()
|
|||
terrainShader.setUniform("cameraPos", cameraPosition, false);
|
||||
|
||||
/* Task: Render the terrain */
|
||||
//glPrimitiveRestartIndex(255);
|
||||
glPrimitiveRestartIndex(512);
|
||||
glDrawElements(GL_TRIANGLE_STRIP, (PATCH_SIZE - 1) * (PATCH_SIZE - 1) * 4, GL_UNSIGNED_INT, 0);
|
||||
|
||||
//Render text
|
||||
|
|
Loading…
Reference in a new issue