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