-
Notifications
You must be signed in to change notification settings - Fork 0
/
Application.cpp
79 lines (65 loc) · 2.16 KB
/
Application.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
#include "stdafx.h"
#include "Camera.h"
#include "Quad.h"
#include "Room.h"
#include "Shader.h"
#include "Window.h"
int main() {
Window window;
Camera camera(window);
Shader quadShader("quad");
Room room(quadShader);
Quad portal({ -1.5, -2, 0 }, { 3, 4 });
glm::mat4 portal1Model = glm::translate(glm::mat4(1), { 0, 2, -room.getRoomSize() });
glm::mat4 portal2Model = glm::translate(glm::mat4(1), { -room.getRoomSize(), 2, 0 });
portal2Model = glm::rotate(portal2Model, glm::radians(90.0f), { 0, 1, 0 });
; while(window.isOpen()) {
camera.update();
glClearColor(0, 0, 0, 1);
glStencilMask(0xFF);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// Set stencil buffer for portal 1
glStencilFunc(GL_ALWAYS, 1, 0xFF);
glStencilMask(0xFF);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDepthMask(GL_FALSE);
quadShader.use();
quadShader.setVec4("color", { 1, 1, 1, 1 });
quadShader.setMat4("model", portal1Model);
portal.draw();
// Draw into portal
glStencilFunc(GL_EQUAL, 1, 0xFF);
glStencilMask(0x00);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDepthMask(GL_TRUE);
const glm::mat4 oldCameraView = camera.getView();
camera.setView(oldCameraView * portal1Model *
glm::rotate(glm::mat4(1), glm::radians(180.0f), { 0, 1, 0 }) * glm::inverse(portal2Model));
room.draw();
camera.setView(oldCameraView);
// Set stencil buffer for portal 2
glStencilFunc(GL_ALWAYS, 2, 0xFF);
glStencilMask(0xFF);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDepthMask(GL_FALSE);
quadShader.use();
quadShader.setVec4("color", { 1, 1, 1, 1 });
quadShader.setMat4("model", portal2Model);
portal.draw();
// Draw into portal
glStencilFunc(GL_EQUAL, 2, 0xFF);
glStencilMask(0x00);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDepthMask(GL_TRUE);
camera.setView(oldCameraView * portal2Model *
glm::rotate(glm::mat4(1), glm::radians(180.0f), { 0, 1, 0 }) * glm::inverse(portal1Model));
room.draw();
camera.setView(oldCameraView);
// Draw rest of the room
glStencilFunc(GL_EQUAL, 0, 0xFF);
quadShader.use();
room.draw();
window.swapBuffers();
glfwPollEvents();
}
}