-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathStencilTest.cpp
More file actions
201 lines (167 loc) · 5.6 KB
/
StencilTest.cpp
File metadata and controls
201 lines (167 loc) · 5.6 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
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "StencilTest.h"
const float SCR_WIDTH = 800;
const float SCR_HEIGHT = 600;
void StencilTest::OnInit()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_STENCIL_TEST);
glDepthFunc(GL_LESS);
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_StencilShader = new Shader("./Shaders/Vertex/DepthTest/DepthTest.vertex", "./Shaders/Fragment/DepthTest/DepthTest.frag");
m_BorderShader = new Shader("./Shaders/Vertex/StencilTest/StencilTest.vertex", "./Shaders/Fragment/StencilTest/StencilTest.frag");
}
void StencilTest::OnRender()
{
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
DrawBorderParamWindow();
auto camera = Camera::GetMainCamera();
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_StencilShader->Use();
m_StencilShader->setMat4("view", view);
m_StencilShader->setMat4("projection", projection);
m_BorderShader->Use();
m_BorderShader->setMat4("view", view);
m_BorderShader->setMat4("projection", projection);
//if(glStencilFunc(compareFunc, refValue))
//{
// glStencilOp(operation,refValue & Mask)
//}
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
// floor
glStencilMask(0);
DrawFloor();
// cubes
glStencilFunc(GL_ALWAYS, 1, 0xff);
glStencilMask(0xff);
DrawCubes();
glStencilFunc(GL_NOTEQUAL, 1, 0xff);
glStencilMask(0);
glDisable(GL_DEPTH_TEST);
DrawBorder();
glStencilMask(0xff);
glStencilFunc(GL_ALWAYS, 0, 0xFF);
glEnable(GL_DEPTH_TEST);
}
void StencilTest::DrawCubes()
{
m_StencilShader->Use();
glm::mat4 model = glm::mat4(1.0f);
glBindVertexArray(cubeVAO);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, cubeTexture);
model = glm::translate(model, glm::vec3(-1.0f, 0.0f, -1.0f));
m_StencilShader->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_StencilShader->setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
}
void StencilTest::DrawBorder()
{
m_BorderShader->Use();
m_BorderShader->setVec3("borderColor", BorderColor[0], BorderColor[1], BorderColor[2]);
glm::mat4 model = glm::mat4(1.0f);
glBindVertexArray(cubeVAO);
model = glm::translate(model, glm::vec3(-1.0f, 0.0f, -1.0f));
model = glm::scale(model, glm::vec3(1.05f));
m_BorderShader->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));
model = glm::scale(model, glm::vec3(1.05f));
m_BorderShader->setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
}
void StencilTest::DrawFloor()
{
m_StencilShader->Use();
glBindVertexArray(planeVAO);
glBindTexture(GL_TEXTURE_2D, floorTexture);
m_StencilShader->setMat4("model", glm::mat4(1.0f));
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
}
void StencilTest::DrawBorderParamWindow()
{
ImGui::SetNextWindowPos(ImVec2(800, 600), 0, ImVec2(1, 1));
ImGui::Begin("StecilTest Modifier", 0, ImGuiWindowFlags_AlwaysAutoResize);
ImGui::ColorEdit3("Border Color", BorderColor);
ImGui::End();
}
void StencilTest::OnWindowAttach(GLFWwindow* wnd)
{
}
void StencilTest::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 StencilTest::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 StencilTest::OnMouseScrollCallBack(GLFWwindow* window, double xoffset, double yoffset)
{
Camera::GetMainCamera()->ProcessMouseScroll(yoffset);
}
void StencilTest::OnDeInit()
{
glDeleteBuffers(1, &cubeVBO);
glDeleteBuffers(1, &planeVBO);
glDeleteVertexArrays(1, &cubeVAO);
glDeleteVertexArrays(1, &planeVAO);
glDeleteTextures(1, &cubeTexture);
glDeleteTextures(1, &floorTexture);
delete m_StencilShader;
delete m_BorderShader;
}