forked from 0kk470/learnOpenGL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FaceCulling.cpp
158 lines (133 loc) · 4.61 KB
/
FaceCulling.cpp
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
#include "FaceCulling.h"
const float SCR_WIDTH = 800;
const float SCR_HEIGHT = 600;
void FaceCulling::OnInit()
{
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CW);
glGenVertexArrays(1, &cubeVAO);
glGenBuffers(1, &cubeVBO);
glBindVertexArray(cubeVAO);
glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), &cubeVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glBindVertexArray(0);
glGenVertexArrays(1, &planeVAO);
glGenBuffers(1, &planeVBO);
glBindVertexArray(planeVAO);
glBindBuffer(GL_ARRAY_BUFFER, planeVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(planeVertices), &planeVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glBindVertexArray(0);
cubeTexture = Resource::LoadTexture("./resources/DepthTest/marble.jpg");
floorTexture = Resource::LoadTexture("./resources/DepthTest/metal.png");
m_DepthShader = new Shader("./Shaders/Vertex/DepthTest/DepthTest.vertex", "./Shaders/Fragment/DepthTest/DepthTest.frag");
//Linear DepthTest
//m_DepthShader = new Shader("./Shaders/Vertex/DepthTest/DepthTest.vertex", "./Shaders/Fragment/DepthTest/LinearDepthTest.frag");
}
void FaceCulling::OnRender()
{
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
DrawDepthTestWindow();
m_DepthShader->Use();
auto camera = Camera::GetMainCamera();
glm::mat4 model = glm::mat4(1.0f);
glm::mat4 view = camera->GetViewMatrix();
glm::mat4 projection = glm::perspective(glm::radians(camera->Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
m_DepthShader->setMat4("view", view);
m_DepthShader->setMat4("projection", projection);
// cubes
glBindVertexArray(cubeVAO);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, cubeTexture);
model = glm::translate(model, glm::vec3(-1.0f, 0.0f, -1.0f));
m_DepthShader->setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(2.0f, 0.0f, 0.0f));
m_DepthShader->setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
// floor
glBindVertexArray(planeVAO);
glBindTexture(GL_TEXTURE_2D, floorTexture);
m_DepthShader->setMat4("model", glm::mat4(1.0f));
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
}
void FaceCulling::OnWindowAttach(GLFWwindow* wnd)
{
}
void FaceCulling::HandleInput(GLFWwindow* wnd)
{
GLfloat deltaTime = Time::deltaTime;
auto mainCamera = Camera::GetMainCamera();
if (glfwGetKey(wnd, GLFW_KEY_W))
{
mainCamera->ProcessKeyboard(FORWARD, deltaTime);
}
else if (glfwGetKey(wnd, GLFW_KEY_S))
{
mainCamera->ProcessKeyboard(BACKWARD, deltaTime);
}
if (glfwGetKey(wnd, GLFW_KEY_A))
{
mainCamera->ProcessKeyboard(LEFT, deltaTime);
}
else if (glfwGetKey(wnd, GLFW_KEY_D))
{
mainCamera->ProcessKeyboard(RIGHT, deltaTime);
}
}
void FaceCulling::OnMouseMoveCallback(GLFWwindow* window, double xpos, double ypos)
{
if (Mouse::IsFisrtMove())
{
Mouse::SetLastXY(xpos, ypos);
Mouse::SetFirstMove(false);
}
GLfloat xoffset = xpos - Mouse::GetLastX();
GLfloat yoffset = Mouse::GetLastY() - ypos; // Reversed since y-coordinates go from bottom to left
Mouse::SetLastXY(xpos, ypos);
Camera::GetMainCamera()->ProcessMouseMovement(xoffset, yoffset);
}
void FaceCulling::OnMouseScrollCallBack(GLFWwindow* window, double xoffset, double yoffset)
{
Camera::GetMainCamera()->ProcessMouseScroll(yoffset);
}
void FaceCulling::DrawDepthTestWindow()
{
ImGui::SetNextWindowPos(ImVec2(800, 600), 0, ImVec2(1, 1));
ImGui::Begin("FaceCulling Modifier", 0, ImGuiWindowFlags_AlwaysAutoResize);
ImGui::Checkbox("IsEnableFaceCulling", &IsEnableFaceCulling);
if (IsEnableFaceCulling && !glIsEnabled(GL_CULL_FACE))
{
glEnable(GL_CULL_FACE);
}
else if (!IsEnableFaceCulling && glIsEnabled(GL_CULL_FACE))
{
glDisable(GL_CULL_FACE);
}
ImGui::Combo("Functions", &CurrentCullFaceFuncIndex, FuncItemsName, 3);
glCullFace(FuncItems[CurrentCullFaceFuncIndex]);
ImGui::End();
}
void FaceCulling::OnDeInit()
{
glDeleteBuffers(1, &cubeVBO);
glDeleteBuffers(1, &planeVBO);
glDeleteVertexArrays(1, &cubeVAO);
glDeleteVertexArrays(1, &planeVAO);
glDeleteTextures(1, &cubeTexture);
glDeleteTextures(1, &floorTexture);
delete m_DepthShader;
}