Much more
This commit is contained in:
parent
562aeccc89
commit
8b4276f6ab
5 changed files with 140 additions and 33 deletions
|
@ -1,5 +1,5 @@
|
|||
// This source code is property of the Computer Graphics and Visualization
|
||||
// chair of the TU Dresden. Do not distribute!
|
||||
// This source code is property of the Computer Graphics and Visualization
|
||||
// chair of the TU Dresden. Do not distribute!
|
||||
// Copyright (C) CGV TU Dresden - All Rights Reserved
|
||||
|
||||
#pragma once
|
||||
|
@ -8,38 +8,38 @@
|
|||
|
||||
class Viewer : public nse::gui::AbstractViewer
|
||||
{
|
||||
public:
|
||||
public:
|
||||
Viewer();
|
||||
|
||||
void drawContents();
|
||||
|
||||
private:
|
||||
private:
|
||||
void SetupGUI();
|
||||
|
||||
Eigen::Matrix4f modelViewMatrix, projectionMatrix;
|
||||
|
||||
//GUI Elements for the various options
|
||||
nanogui::CheckBox* chkHasFaceCulling; //Shall back face culling be activated?
|
||||
nanogui::CheckBox* chkHasDepthTesting; //Shall depth testing be activated?
|
||||
nanogui::CheckBox *chkHasFaceCulling; //Shall back face culling be activated?
|
||||
nanogui::CheckBox *chkHasDepthTesting; //Shall depth testing be activated?
|
||||
|
||||
nanogui::Slider* sldJuliaCX; //Seed for the Julia fractal
|
||||
nanogui::Slider* sldJuliaCY;
|
||||
nanogui::Slider* sldJuliaZoom; //Zoom factor for the Julia fractal
|
||||
nanogui::Slider *sldJuliaCX; //Seed for the Julia fractal
|
||||
nanogui::Slider *sldJuliaCY;
|
||||
nanogui::Slider *sldJuliaZoom; //Zoom factor for the Julia fractal
|
||||
|
||||
nanogui::Slider* point1X;
|
||||
nanogui::Slider* point1Y;
|
||||
nanogui::Slider* point2X;
|
||||
nanogui::Slider* point2Y;
|
||||
nanogui::Slider* point3X;
|
||||
nanogui::Slider* point3Y;
|
||||
nanogui::Slider *point1X;
|
||||
nanogui::Slider *point1Y;
|
||||
nanogui::Slider *point2X;
|
||||
nanogui::Slider *point2Y;
|
||||
nanogui::Slider *point3X;
|
||||
nanogui::Slider *point3Y;
|
||||
|
||||
// The following variables hold OpenGL object IDs
|
||||
GLuint vertex_shader_id, // ID of the vertex shader
|
||||
fragment_shader_id, // ID of the fragment shader
|
||||
program_id, // ID of the shader program
|
||||
vertex_array_id, // ID of the vertex array
|
||||
position_buffer_id, // ID of the position buffer
|
||||
color_buffer_id, // ID of the color buffer
|
||||
GLuint vertex_shader_id, // ID of the vertex shader
|
||||
fragment_shader_id, // ID of the fragment shader
|
||||
program_id, // ID of the shader program
|
||||
vertex_array_id, // ID of the vertex array
|
||||
position_buffer_id, // ID of the position buffer
|
||||
color_buffer_id, // ID of the color buffer
|
||||
uv_map_buffer_id; // ID of the uv_map
|
||||
|
||||
GLint view_uniform_id;
|
||||
|
@ -49,7 +49,7 @@ private:
|
|||
|
||||
GLint julia_pos_id;
|
||||
|
||||
// Read, Compile and link the shader codes to a shader program
|
||||
// Read, Compile and link the shader codes to a shader program
|
||||
void CreateShaders();
|
||||
// Create and define the vertex array and add a number of vertex buffers
|
||||
void CreateVertexBuffers();
|
||||
|
|
|
@ -5,12 +5,17 @@
|
|||
|
||||
in vec3 n;
|
||||
in vec2 uv;
|
||||
in vec2 road_uv;
|
||||
|
||||
out vec4 color;
|
||||
|
||||
uniform vec3 cameraPos;
|
||||
|
||||
uniform sampler2D grass;
|
||||
uniform sampler2D rock;
|
||||
uniform sampler2D alpha;
|
||||
uniform sampler2D road;
|
||||
uniform sampler2D specular_road;
|
||||
|
||||
uniform sampler2D background;
|
||||
uniform vec2 screenSize;
|
||||
|
@ -40,10 +45,10 @@ void main()
|
|||
|
||||
//material properties
|
||||
//color = vec4(0.6, 0.6, 0.6, 1);
|
||||
color = texture(grass, uv);
|
||||
float specular = 0;
|
||||
|
||||
color = mix(texture(grass, uv), texture(rock, uv), dot(n, dirToViewer));
|
||||
color = mix(color, texture(road, road_uv), texture(alpha, uv).x);
|
||||
|
||||
float specular = texture(specular_road, road_uv).x;
|
||||
|
||||
//Calculate light
|
||||
color = calculateLighting(color, specular, n, dirToViewer);
|
||||
|
|
|
@ -7,6 +7,7 @@ in vec4 position;
|
|||
|
||||
out vec3 n;
|
||||
out vec2 uv;
|
||||
out vec2 road_uv;
|
||||
|
||||
uniform mat4 mvp;
|
||||
|
||||
|
@ -32,6 +33,7 @@ void main()
|
|||
//n = vec3(0.0, 1.0, 0.0);
|
||||
|
||||
uv = vec2(terrain_position.x / 255.0, terrain_position.z / 255.0);
|
||||
road_uv = offsets[gl_VertexID % 6];
|
||||
|
||||
gl_Position = mvp * terrain_position;
|
||||
}
|
||||
|
|
|
@ -33,9 +33,16 @@ class Viewer : public nse::gui::AbstractViewer
|
|||
nse::gui::GLBuffer terrainPositions;
|
||||
nse::gui::GLBuffer terrainIndices;
|
||||
|
||||
nse::gui::GLVertexArray referenceVAO;
|
||||
nse::gui::GLBuffer referenceVB, referenceIB;
|
||||
|
||||
GLuint grassTexture, rockTexture, roadColorTexture, roadNormalMap, roadSpecularMap, alphaMap;
|
||||
|
||||
GLint grass_texture_location;
|
||||
GLint rock_texture_location;
|
||||
GLint alpha_texture_location;
|
||||
GLint road_texture_location;
|
||||
GLint road_specular_location;
|
||||
|
||||
nse::gui::GLBuffer offsetBuffer;
|
||||
|
||||
|
|
|
@ -18,18 +18,25 @@
|
|||
#include "textures.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
const uint32_t PATCH_SIZE = 256; //number of vertices along one side of the terrain patch
|
||||
constexpr bool REFERENCE = true;
|
||||
constexpr uint32_t PATCH_SIZE = 256; //number of vertices along one side of the terrain patch
|
||||
|
||||
Viewer::Viewer()
|
||||
: AbstractViewer("CG1 Exercise 2"),
|
||||
terrainPositions(nse::gui::VertexBuffer), terrainIndices(nse::gui::IndexBuffer),
|
||||
offsetBuffer(nse::gui::VertexBuffer)
|
||||
offsetBuffer(nse::gui::VertexBuffer),
|
||||
referenceVB(nse::gui::VertexBuffer), referenceIB(nse::gui::IndexBuffer)
|
||||
{
|
||||
LoadShaders();
|
||||
CreateGeometry();
|
||||
|
||||
grass_texture_location = terrainShader.uniform("grass");
|
||||
rock_texture_location = terrainShader.uniform("rock");
|
||||
alpha_texture_location = terrainShader.uniform("alpha");
|
||||
road_texture_location = terrainShader.uniform("road");
|
||||
road_specular_location = terrainShader.uniform("specular_road");
|
||||
|
||||
//Create a texture and framebuffer for the background
|
||||
glGenFramebuffers(1, &backgroundFBO);
|
||||
|
@ -138,6 +145,20 @@ GLuint CreateTexture(const unsigned char *fileData, size_t fileLength, bool repe
|
|||
return textureName;
|
||||
}
|
||||
|
||||
std::string vec4_to_str(Eigen::Vector4f &v)
|
||||
{
|
||||
std::stringstream ss;
|
||||
|
||||
ss << "(" << v.x() << ", "
|
||||
<< v.y() << ", "
|
||||
<< v.z() << ", "
|
||||
<< v.w() << ")";
|
||||
|
||||
//std::cout << ss.str() << std::endl;
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
void Viewer::CreateGeometry()
|
||||
{
|
||||
//empty VAO for sky
|
||||
|
@ -157,7 +178,7 @@ void Viewer::CreateGeometry()
|
|||
{
|
||||
for (int j = 0; j < PATCH_SIZE; j++)
|
||||
{
|
||||
positions.emplace_back((float)i, 0.0f, (float)j, 1.0f);
|
||||
positions.emplace_back((float)j, 0.0f, (float)i, 1.0f);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -169,12 +190,14 @@ void Viewer::CreateGeometry()
|
|||
{
|
||||
|
||||
// skip creating triangles on last x in row
|
||||
int end_of_x = k % (PATCH_SIZE - 1);
|
||||
/*
|
||||
int end_of_x = k % (PATCH_SIZE);
|
||||
|
||||
if (end_of_x == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
*/
|
||||
|
||||
indices.emplace_back(k);
|
||||
indices.emplace_back(k + PATCH_SIZE);
|
||||
|
@ -186,6 +209,45 @@ void Viewer::CreateGeometry()
|
|||
terrainPositions.uploadData(positions).bindToAttribute("position");
|
||||
terrainIndices.uploadData(indices.size() * sizeof(uint32_t), indices.data());
|
||||
|
||||
referenceVAO.generate();
|
||||
referenceVAO.bind();
|
||||
|
||||
std::vector<Eigen::Vector4f> ref_pos;
|
||||
std::vector<uint32_t> ref_ind;
|
||||
|
||||
for (int i = 0; i < PATCH_SIZE; i++)
|
||||
{
|
||||
for (int j = 0; j < PATCH_SIZE; j++)
|
||||
{
|
||||
ref_pos.emplace_back((float)j, 0.0f, (float)i, 1.0f);
|
||||
}
|
||||
}
|
||||
|
||||
int index_width = PATCH_SIZE - 1;
|
||||
|
||||
for (int i = 0; i < index_width; i++)
|
||||
{
|
||||
for (int j = 0; j < index_width; j++)
|
||||
{
|
||||
int p1 = i + (PATCH_SIZE * j);
|
||||
int p2 = i + (PATCH_SIZE * j) + 1;
|
||||
int p3 = i + (PATCH_SIZE * (j + 1));
|
||||
int p4 = i + (PATCH_SIZE * (j + 1)) + 1;
|
||||
|
||||
ref_ind.emplace_back(p1);
|
||||
ref_ind.emplace_back(p3);
|
||||
ref_ind.emplace_back(p2);
|
||||
|
||||
ref_ind.emplace_back(p3);
|
||||
ref_ind.emplace_back(p2);
|
||||
ref_ind.emplace_back(p4);
|
||||
}
|
||||
}
|
||||
|
||||
referenceVB.uploadData(ref_pos)
|
||||
.bindToAttribute("position");
|
||||
referenceIB.uploadData(ref_ind.size() * sizeof(uint32_t), ref_ind.data());
|
||||
|
||||
//textures
|
||||
grassTexture = CreateTexture((unsigned char *)grass_jpg, grass_jpg_size);
|
||||
rockTexture = CreateTexture((unsigned char *)rock_jpg, rock_jpg_size);
|
||||
|
@ -261,7 +323,16 @@ void Viewer::drawContents()
|
|||
|
||||
//render terrain
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
terrainVAO.bind();
|
||||
|
||||
if (REFERENCE)
|
||||
{
|
||||
referenceVAO.bind();
|
||||
}
|
||||
else
|
||||
{
|
||||
terrainVAO.bind();
|
||||
}
|
||||
|
||||
terrainShader.bind();
|
||||
|
||||
terrainShader.setUniform("screenSize", Eigen::Vector2f(width(), height()), false);
|
||||
|
@ -272,9 +343,31 @@ void Viewer::drawContents()
|
|||
glBindTexture(GL_TEXTURE_2D, grassTexture);
|
||||
glUniform1i(grass_texture_location, 0);
|
||||
|
||||
/* Task: Render the terrain */
|
||||
glPrimitiveRestartIndex(512);
|
||||
glDrawElements(GL_TRIANGLE_STRIP, (PATCH_SIZE - 1) * (PATCH_SIZE - 1) * 4, GL_UNSIGNED_INT, 0);
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
glBindTexture(GL_TEXTURE_2D, rockTexture);
|
||||
glUniform1i(rock_texture_location, 1);
|
||||
|
||||
glActiveTexture(GL_TEXTURE2);
|
||||
glBindTexture(GL_TEXTURE_2D, alphaMap);
|
||||
glUniform1i(alpha_texture_location, 2);
|
||||
|
||||
glActiveTexture(GL_TEXTURE3);
|
||||
glBindTexture(GL_TEXTURE_2D, roadColorTexture);
|
||||
glUniform1i(road_texture_location, 3);
|
||||
|
||||
glActiveTexture(GL_TEXTURE4);
|
||||
glBindTexture(GL_TEXTURE_2D, roadSpecularMap);
|
||||
glUniform1i(road_specular_location, 4);
|
||||
|
||||
if (REFERENCE)
|
||||
{
|
||||
glDrawElements(GL_TRIANGLES, (PATCH_SIZE - 1) * (PATCH_SIZE - 1) * 6, GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
glPrimitiveRestartIndex(PATCH_SIZE * 2);
|
||||
glDrawElements(GL_TRIANGLE_STRIP, (PATCH_SIZE - 1) * PATCH_SIZE * 2, GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
|
||||
//Render text
|
||||
nvgBeginFrame(mNVGContext, width(), height(), mPixelRatio);
|
||||
|
|
Loading…
Reference in a new issue