Make some indentations concistent
This commit is contained in:
parent
74ac6aec9a
commit
0e348944b1
|
@ -5,53 +5,53 @@
|
||||||
|
|
||||||
namespace Uniforms
|
namespace Uniforms
|
||||||
{
|
{
|
||||||
void Init();
|
void Init();
|
||||||
void BufferSceneData();
|
void BufferSceneData();
|
||||||
|
|
||||||
//This structure mirrors the uniform block declared in the shader
|
//This structure mirrors the uniform block declared in the shader
|
||||||
struct SceneUniforms
|
struct SceneUniforms
|
||||||
{
|
{
|
||||||
glm::mat4 PV; //camera projection * view matrix
|
glm::mat4 PV; //camera projection * view matrix
|
||||||
glm::vec4 eye_w = glm::vec4(0.0f, 0.0f, 3.0f, 1.0f); //world-space eye position
|
glm::vec4 eye_w = glm::vec4(0.0f, 0.0f, 3.0f, 1.0f); //world-space eye position
|
||||||
};
|
};
|
||||||
|
|
||||||
struct LightUniforms
|
struct LightUniforms
|
||||||
{
|
{
|
||||||
glm::vec4 La = glm::vec4(0.5f, 0.5f, 0.55f, 1.0f); //ambient light color
|
glm::vec4 La = glm::vec4(0.5f, 0.5f, 0.55f, 1.0f); //ambient light color
|
||||||
glm::vec4 Ld = glm::vec4(0.5f, 0.5f, 0.25f, 1.0f); //diffuse light color
|
glm::vec4 Ld = glm::vec4(0.5f, 0.5f, 0.25f, 1.0f); //diffuse light color
|
||||||
glm::vec4 Ls = glm::vec4(0.3f); //specular light color
|
glm::vec4 Ls = glm::vec4(0.3f); //specular light color
|
||||||
glm::vec4 light_w = glm::vec4(0.0f, 1.2, 1.0f, 1.0f); //world-space light position
|
glm::vec4 light_w = glm::vec4(0.0f, 1.2, 1.0f, 1.0f); //world-space light position
|
||||||
};
|
};
|
||||||
|
|
||||||
struct MaterialUniforms
|
struct MaterialUniforms
|
||||||
{
|
{
|
||||||
glm::vec4 ka = glm::vec4(1.0f); //ambient material color
|
glm::vec4 ka = glm::vec4(1.0f); //ambient material color
|
||||||
glm::vec4 kd = glm::vec4(1.0f); //diffuse material color
|
glm::vec4 kd = glm::vec4(1.0f); //diffuse material color
|
||||||
glm::vec4 ks = glm::vec4(1.0f); //specular material color
|
glm::vec4 ks = glm::vec4(1.0f); //specular material color
|
||||||
float shininess = 20.0f; //specular exponent
|
float shininess = 20.0f; //specular exponent
|
||||||
};
|
};
|
||||||
|
|
||||||
extern SceneUniforms SceneData;
|
extern SceneUniforms SceneData;
|
||||||
extern LightUniforms LightData;
|
extern LightUniforms LightData;
|
||||||
extern MaterialUniforms MaterialData;
|
extern MaterialUniforms MaterialData;
|
||||||
|
|
||||||
//IDs for the buffer objects holding the uniform block data
|
//IDs for the buffer objects holding the uniform block data
|
||||||
extern GLuint scene_ubo;
|
extern GLuint scene_ubo;
|
||||||
extern GLuint light_ubo;
|
extern GLuint light_ubo;
|
||||||
extern GLuint material_ubo;
|
extern GLuint material_ubo;
|
||||||
|
|
||||||
namespace UboBinding
|
namespace UboBinding
|
||||||
{
|
{
|
||||||
//These values come from the binding value specified in the shader block layout
|
//These values come from the binding value specified in the shader block layout
|
||||||
extern int scene;
|
extern int scene;
|
||||||
extern int light;
|
extern int light;
|
||||||
extern int material;
|
extern int material;
|
||||||
};
|
};
|
||||||
|
|
||||||
//Locations for the uniforms which are not in uniform blocks
|
//Locations for the uniforms which are not in uniform blocks
|
||||||
namespace UniformLocs
|
namespace UniformLocs
|
||||||
{
|
{
|
||||||
extern int M; //model matrix
|
extern int M; //model matrix
|
||||||
extern int time;
|
extern int time;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -7,37 +7,37 @@
|
||||||
#include "fbo.h"
|
#include "fbo.h"
|
||||||
|
|
||||||
class Scene {
|
class Scene {
|
||||||
public:
|
public:
|
||||||
Scene(int width, int height);
|
Scene(int width, int height);
|
||||||
~Scene();
|
~Scene();
|
||||||
|
|
||||||
void Init();
|
void Init();
|
||||||
void Display(GLFWwindow* window);
|
void Display(GLFWwindow* window);
|
||||||
void DrawGui(GLFWwindow* window);
|
void DrawGui(GLFWwindow* window);
|
||||||
void Idle();
|
void Idle();
|
||||||
void ReloadShader();
|
void ReloadShader();
|
||||||
|
|
||||||
int window_width;
|
int window_width;
|
||||||
int window_height;
|
int window_height;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void InitBuffers();
|
void InitBuffers();
|
||||||
void InitShaders();
|
void InitShaders();
|
||||||
|
|
||||||
FBO fbo_;
|
FBO fbo_;
|
||||||
GLuint shader_program_;
|
GLuint shader_program_;
|
||||||
GLuint vao_;
|
GLuint vao_;
|
||||||
float angle_;
|
float angle_;
|
||||||
float scale_;
|
float scale_;
|
||||||
float aspect_;
|
float aspect_;
|
||||||
float near_z_;
|
float near_z_;
|
||||||
float far_z_;
|
float far_z_;
|
||||||
float fov_;
|
float fov_;
|
||||||
|
|
||||||
glm::mat4 view_matrix_;
|
glm::mat4 view_matrix_;
|
||||||
glm::mat4 projection_matrix_;
|
glm::mat4 projection_matrix_;
|
||||||
|
|
||||||
void UpdateCamera();
|
void UpdateCamera();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SCENE_H_
|
#endif // SCENE_H_
|
|
@ -4,56 +4,56 @@ layout(location = 1) uniform float time;
|
||||||
|
|
||||||
layout(std140, binding = 0) uniform SceneUniforms
|
layout(std140, binding = 0) uniform SceneUniforms
|
||||||
{
|
{
|
||||||
mat4 PV; //camera projection * view matrix
|
mat4 PV; //camera projection * view matrix
|
||||||
vec4 eye_w; //world-space eye position
|
vec4 eye_w; //world-space eye position
|
||||||
};
|
};
|
||||||
|
|
||||||
layout(std140, binding = 1) uniform LightUniforms
|
layout(std140, binding = 1) uniform LightUniforms
|
||||||
{
|
{
|
||||||
vec4 La; //ambient light color
|
vec4 La; //ambient light color
|
||||||
vec4 Ld; //diffuse light color
|
vec4 Ld; //diffuse light color
|
||||||
vec4 Ls; //specular light color
|
vec4 Ls; //specular light color
|
||||||
vec4 light_w; //world-space light position
|
vec4 light_w; //world-space light position
|
||||||
};
|
};
|
||||||
|
|
||||||
layout(std140, binding = 2) uniform MaterialUniforms
|
layout(std140, binding = 2) uniform MaterialUniforms
|
||||||
{
|
{
|
||||||
vec4 ka; //ambient material color
|
vec4 ka; //ambient material color
|
||||||
vec4 kd; //diffuse material color
|
vec4 kd; //diffuse material color
|
||||||
vec4 ks; //specular material color
|
vec4 ks; //specular material color
|
||||||
float shininess; //specular exponent
|
float shininess; //specular exponent
|
||||||
};
|
};
|
||||||
|
|
||||||
in VertexData
|
in VertexData
|
||||||
{
|
{
|
||||||
vec2 tex_coord;
|
vec2 tex_coord;
|
||||||
vec3 pw; //world-space vertex position
|
vec3 pw; //world-space vertex position
|
||||||
vec3 nw; //world-space normal vector
|
vec3 nw; //world-space normal vector
|
||||||
} inData; //block is named 'inData'
|
} inData; //block is named 'inData'
|
||||||
|
|
||||||
out vec4 fragcolor; //the output color for this fragment
|
out vec4 fragcolor; //the output color for this fragment
|
||||||
|
|
||||||
void main(void)
|
void main(void)
|
||||||
{
|
{
|
||||||
//Compute per-fragment Phong lighting
|
//Compute per-fragment Phong lighting
|
||||||
// vec4 ktex = texture(diffuse_tex, inData.tex_coord);
|
// vec4 ktex = texture(diffuse_tex, inData.tex_coord);
|
||||||
|
|
||||||
// vec4 ambient_term = ka*ktex*La;
|
// vec4 ambient_term = ka*ktex*La;
|
||||||
|
|
||||||
// const float eps = 1e-8; //small value to avoid division by 0
|
// const float eps = 1e-8; //small value to avoid division by 0
|
||||||
// float d = distance(light_w.xyz, inData.pw.xyz);
|
// float d = distance(light_w.xyz, inData.pw.xyz);
|
||||||
// float atten = 1.0/(d*d+eps); //d-squared attenuation
|
// float atten = 1.0/(d*d+eps); //d-squared attenuation
|
||||||
|
|
||||||
// vec3 nw = normalize(inData.nw); //world-space unit normal vector
|
// vec3 nw = normalize(inData.nw); //world-space unit normal vector
|
||||||
// vec3 lw = normalize(light_w.xyz - inData.pw.xyz); //world-space unit light vector
|
// vec3 lw = normalize(light_w.xyz - inData.pw.xyz); //world-space unit light vector
|
||||||
// vec4 diffuse_term = atten*kd*ktex*Ld*max(0.0, dot(nw, lw));
|
// vec4 diffuse_term = atten*kd*ktex*Ld*max(0.0, dot(nw, lw));
|
||||||
|
|
||||||
// vec3 vw = normalize(eye_w.xyz - inData.pw.xyz); //world-space unit view vector
|
// vec3 vw = normalize(eye_w.xyz - inData.pw.xyz); //world-space unit view vector
|
||||||
// vec3 rw = reflect(-lw, nw); //world-space unit reflection vector
|
// vec3 rw = reflect(-lw, nw); //world-space unit reflection vector
|
||||||
|
|
||||||
// vec4 specular_term = atten*ks*Ls*pow(max(0.0, dot(rw, vw)), shininess);
|
// vec4 specular_term = atten*ks*Ls*pow(max(0.0, dot(rw, vw)), shininess);
|
||||||
|
|
||||||
// fragcolor = ambient_term + diffuse_term + specular_term;
|
// fragcolor = ambient_term + diffuse_term + specular_term;
|
||||||
fragcolor = vec4(1.0, 0.0, 0.0, 1.0);
|
fragcolor = vec4(1.0, 0.0, 0.0, 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
110
source/main.cpp
110
source/main.cpp
|
@ -46,95 +46,95 @@ const int kTargetFps = 60;
|
||||||
const auto kFrameDuration = std::chrono::milliseconds(1000 / kTargetFps);
|
const auto kFrameDuration = std::chrono::milliseconds(1000 / kTargetFps);
|
||||||
|
|
||||||
bool InitializeGlfw() {
|
bool InitializeGlfw() {
|
||||||
if (!glfwInit()) {
|
if (!glfwInit()) {
|
||||||
std::cerr << "Failed to initialize GLFW" << std::endl;
|
std::cerr << "Failed to initialize GLFW" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
GLFWwindow* CreateGlfwWindow() {
|
GLFWwindow* CreateGlfwWindow() {
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
|
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
GLFWwindow* window = glfwCreateWindow(kInitWindowWidth, kInitWindowHeight, "TerraVisor", nullptr, nullptr);
|
GLFWwindow* window = glfwCreateWindow(kInitWindowWidth, kInitWindowHeight, "TerraVisor", nullptr, nullptr);
|
||||||
if (!window) {
|
if (!window) {
|
||||||
std::cerr << "Failed to create GLFW window" << std::endl;
|
std::cerr << "Failed to create GLFW window" << std::endl;
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
glfwMakeContextCurrent(window);
|
glfwMakeContextCurrent(window);
|
||||||
return window;
|
return window;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool InitializeGlew() {
|
bool InitializeGlew() {
|
||||||
glewExperimental = GL_TRUE;
|
glewExperimental = GL_TRUE;
|
||||||
if (glewInit() != GLEW_OK) {
|
if (glewInit() != GLEW_OK) {
|
||||||
std::cerr << "Failed to initialize GLEW" << std::endl;
|
std::cerr << "Failed to initialize GLEW" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void InitializeImGui(GLFWwindow* window) {
|
void InitializeImGui(GLFWwindow* window) {
|
||||||
IMGUI_CHECKVERSION();
|
IMGUI_CHECKVERSION();
|
||||||
ImGui::CreateContext();
|
ImGui::CreateContext();
|
||||||
ImGui_ImplGlfw_InitForOpenGL(window, true);
|
ImGui_ImplGlfw_InitForOpenGL(window, true);
|
||||||
ImGui_ImplOpenGL3_Init("#version 150");
|
ImGui_ImplOpenGL3_Init("#version 150");
|
||||||
}
|
}
|
||||||
|
|
||||||
void CleanupImGui() {
|
void CleanupImGui() {
|
||||||
ImGui_ImplOpenGL3_Shutdown();
|
ImGui_ImplOpenGL3_Shutdown();
|
||||||
ImGui_ImplGlfw_Shutdown();
|
ImGui_ImplGlfw_Shutdown();
|
||||||
ImGui::DestroyContext();
|
ImGui::DestroyContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainLoop(GLFWwindow* window, Scene& scene) {
|
void MainLoop(GLFWwindow* window, Scene& scene) {
|
||||||
auto last_frame_time = std::chrono::high_resolution_clock::now();
|
auto last_frame_time = std::chrono::high_resolution_clock::now();
|
||||||
|
|
||||||
while (!glfwWindowShouldClose(window)) {
|
while (!glfwWindowShouldClose(window)) {
|
||||||
auto frame_start = std::chrono::high_resolution_clock::now();
|
auto frame_start = std::chrono::high_resolution_clock::now();
|
||||||
|
|
||||||
scene.Idle();
|
scene.Idle();
|
||||||
scene.Display(window);
|
scene.Display(window);
|
||||||
|
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
|
||||||
auto frame_end = std::chrono::high_resolution_clock::now();
|
auto frame_end = std::chrono::high_resolution_clock::now();
|
||||||
auto frame_duration = frame_end - frame_start;
|
auto frame_duration = frame_end - frame_start;
|
||||||
|
|
||||||
if (frame_duration < kFrameDuration) {
|
if (frame_duration < kFrameDuration) {
|
||||||
std::this_thread::sleep_for(kFrameDuration - frame_duration);
|
std::this_thread::sleep_for(kFrameDuration - frame_duration);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto current_frame_time = std::chrono::high_resolution_clock::now();
|
||||||
|
auto actual_frame_duration = std::chrono::duration_cast<std::chrono::milliseconds>(current_frame_time - last_frame_time).count();
|
||||||
|
|
||||||
|
last_frame_time = current_frame_time;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto current_frame_time = std::chrono::high_resolution_clock::now();
|
|
||||||
auto actual_frame_duration = std::chrono::duration_cast<std::chrono::milliseconds>(current_frame_time - last_frame_time).count();
|
|
||||||
|
|
||||||
last_frame_time = current_frame_time;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
if (!InitializeGlfw()) return -1;
|
if (!InitializeGlfw()) return -1;
|
||||||
|
|
||||||
GLFWwindow* window = CreateGlfwWindow();
|
GLFWwindow* window = CreateGlfwWindow();
|
||||||
if (!window) return -1;
|
if (!window) return -1;
|
||||||
|
|
||||||
if (!InitializeGlew()) return -1;
|
if (!InitializeGlew()) return -1;
|
||||||
|
|
||||||
Scene scene(kInitWindowWidth, kInitWindowHeight);
|
Scene scene(kInitWindowWidth, kInitWindowHeight);
|
||||||
scene.Init();
|
scene.Init();
|
||||||
InitializeImGui(window);
|
InitializeImGui(window);
|
||||||
|
|
||||||
MainLoop(window, scene);
|
MainLoop(window, scene);
|
||||||
|
|
||||||
CleanupImGui();
|
CleanupImGui();
|
||||||
glfwDestroyWindow(window);
|
glfwDestroyWindow(window);
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue