-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTerrainScene.h
122 lines (90 loc) · 3.24 KB
/
TerrainScene.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#ifndef TERRAIN_SCENE_H
#define TERRAIN_SCENE_H
#include "Scene.h"
#include "Mesh.h"
#include "Model.h"
#include "Terrain.h"
#include "Skybox.h"
class TerrainScene : Scene
{
public:
TerrainScene(GLFWwindow* window, unsigned int width, unsigned int height)
: Scene(window, width, height)
{
// Init terrain
terrain = new Terrain("res/textures/heightmap_mountain.jpg", "res/textures/grass.jpg", "res/textures/snow.jpg", "res/textures/dirt.png");
terrainShader = new Shader("res/shaders/terrain/terrain.vs", "res/shaders/terrain/terrain.fs");
// Load lamp
lampShader = new Shader("res/shaders/basic/lamp.vs", "res/shaders/basic/lamp.fs");
lamp = new Model("res/models/cube.obj");
// Generate skybox
skybox = new Skybox();
glEnable(GL_DEPTH_TEST);
}
void Draw()
{
Scene::Draw();
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// View - Projection
view = camera.GetViewMatrix();
projection = glm::perspective(glm::radians(camera.Zoom), (float)s_WindowWidth / (float)s_WindowHeight, 0.1f, 100.0f);
terrainShader->use();
glm::mat4 model;
// Textures: 0->heightmap | 1->grass | 2->snow | 3->dirt
terrainShader->setInt("heightmap", 0);
terrainShader->setInt("grass", 1);
terrainShader->setInt("snow", 2);
terrainShader->setInt("dirt", 3);
//model = glm::scale(model, glm::vec3(4.0f));
terrainShader->setMat4("view", view);
terrainShader->setMat4("projection", projection);
terrainShader->setMat4("model", model);
// Debug
terrainShader->setFloat("elevation", elevation);
lightPos.x = sin(glfwGetTime()) * 10;
lightPos.z = cos(glfwGetTime()) * 10;
//lightPos = camera.Position;
terrainShader->setVec3("light.position", lightPos.x, lightPos.y, lightPos.z);
terrainShader->setVec3("light.ambient", 0.5f, 0.5f, 0.5f);
terrainShader->setVec3("light.diffuse", 0.5f, 0.5f, 0.5f);
terrain->Draw();
DrawLamp();
// Draw Skybox
skybox->Draw(projection, view);
DrawGUI();
}
private:
Terrain* terrain;
Model* lamp;
Shader* terrainShader;
Shader* lampShader;
Skybox* skybox;
glm::vec3 lightPos = glm::vec3(0.0f, 0.5f, 0.0f);
glm::mat4 projection;
glm::mat4 view;
float elevation = 0.1f;
void DrawLamp()
{
lampShader->use();
lampShader->setMat4("projection", projection);
lampShader->setMat4("view", view);
glm::mat4 model = glm::mat4();
model = glm::translate(model, lightPos);
model = glm::scale(model, glm::vec3(0.2f)); // a smaller cube
lampShader->setMat4("model", model);
lamp->Draw(*lampShader);
}
// Imgui
void DrawGUI()
{
ImGui_ImplGlfwGL3_NewFrame();
ImGui::Text("Terrain parameters");
ImGui::SliderFloat("Elevation", &elevation, 0.01f, 1.0f);
ImGui::Text("Light Pos = %.3f %.3f %.3f", lightPos.x, lightPos.y, lightPos.z);
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::Render();
ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData());
}
};
#endif