-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
188 lines (149 loc) · 5.31 KB
/
main.cpp
File metadata and controls
188 lines (149 loc) · 5.31 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <iostream>
#include <stdlib.h>
#include <random>
#include <cmath>
#include <nmmintrin.h>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "common/ShaderHelper.h"
#include "camera/Camera.h"
#include "world/World.h"
#include "light/Light.h"
// Timing
float PROGRAM_START_TIME = glfwGetTime();
float deltaTime = 0.0f; // Time between current frame and last frame
float lastFrame = 0.0f; // Time of last frame
void framebufferSizeCallback(GLFWwindow* window, int width, int height);
void mouseCallback(GLFWwindow* window, double xpos, double ypos);
void processInput(GLFWwindow* window);
// Making Camera
Camera cam(
glm::vec3(0.0f, 0.0f, 0.0f),
FOV,
ASPECT_RATIO,
NEAR_PLANE,
FAR_PLANE
);
bool firstMouse = true;
float lastX = 800.0f / 2.0;
float lastY = 600.0 / 2.0;
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
glViewport(0, 0, 800, 600);
glfwSetFramebufferSizeCallback(window, framebufferSizeCallback);
glfwSetCursorPosCallback(window, mouseCallback);
// Compile and link shaders
ShaderHelper sh;
GLuint shaderProgram = sh.compileShaders("shaders/VertexShader.glsl", "shaders/FragmentShader.glsl");
GLuint lightShaderProgram = sh.compileShaders("shaders/LightCubeVertexShader.glsl", "shaders/LightCubeFragmentShader.glsl");
// Enable z-buffer depth testing
glEnable(GL_DEPTH_TEST);
// Capture cursor
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Let's start our grass.
World world;
world.generateWorld(sh, shaderProgram);
// Light source
glm::vec3 lightPos = glm::vec3(10.0f, 5.0f, 25.0f);
//glm::vec3 lightColor = rgbToNormRGB(46.0f, 128.0f, 141.0f);
glm::vec3 lightColor = glm::vec3(1.0f, 0.0f, 0.0f);
Light sun(glm::translate(glm::mat4(), lightPos), lightColor);
sh.setUniformMat4fv(
lightShaderProgram,
"transform",
glm::value_ptr(glm::translate(glm::mat4(), lightPos))
);
sh.setUniform3fv(shaderProgram, "lightPosition", glm::value_ptr(lightPos));
sh.setUniform3fv(shaderProgram, "lightColor", glm::value_ptr(lightColor));
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// FPS metrics
double prevTime = 0.0;
double currTime = 0.0;
double timeDiff;
unsigned int frameCounter = 0;
// Enable V-sync for v-blanks
glfwSwapInterval(1);
std::cout << "Starting to render" << std::endl;
while (!glfwWindowShouldClose(window))
{
// we are now around 60 fps, 16-17 ms per frame.
currTime = glfwGetTime();
timeDiff = currTime - prevTime;
frameCounter++;
if (timeDiff >= 1.0 / 30.0) {
std::string fps = std::to_string((1.0 / timeDiff) * frameCounter);
std::string ms = std::to_string((timeDiff / frameCounter) * 1000);
std::string windowtitle = "fps: " + fps + " | ms: " + ms;
glfwSetWindowTitle(window, windowtitle.c_str());
prevTime = currTime;
frameCounter = 0;
}
float currentframe = (float)glfwGetTime();
deltaTime = currentframe - lastFrame;
lastFrame = currentframe;
cam.processMovement(window, deltaTime);
processInput(window);
// clear colors on screen so we start at fresh slate
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
sh.useShaderProgram(lightShaderProgram);
sun.draw();
sh.setUniform3fv(shaderProgram, "camPos", glm::value_ptr(cam.m_pos));
sh.setUniform3fv(shaderProgram, "camDir", glm::value_ptr(cam.m_front));
world.renderGrass(
cam,
sh,
shaderProgram,
lightShaderProgram
);
glfwSwapBuffers(window);
glfwPollEvents();
}
return 0;
}
void framebufferSizeCallback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
void mouseCallback(GLFWwindow* window, double dXPos, double dYPos)
{
float xpos = static_cast<float>(dXPos);
float ypos = static_cast<float>(dYPos);
if (firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float xoffset = xpos - lastX;
float yoffset = lastY - ypos;
cam.processMouseMovement(xoffset, yoffset);
lastX = xpos;
lastY = ypos;
}