-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenuScene.cpp
More file actions
226 lines (184 loc) · 6.54 KB
/
menuScene.cpp
File metadata and controls
226 lines (184 loc) · 6.54 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "stdafx.h"
#include "MenuScene.h"
#include "stb_image.h"
extern GameFramework GameManager;
menuScene::~menuScene()
{
//메모리 해제
vertices_sphere.clear();
vertices_sphere.shrink_to_fit();
uvs_sphere.clear();
uvs_sphere.shrink_to_fit();
normals_sphere.clear();
normals_sphere.shrink_to_fit();
vertices_back.clear();
vertices_back.shrink_to_fit();
uvs_back.clear();
uvs_back.shrink_to_fit();
normals_back.clear();
normals_back.shrink_to_fit();
glDeleteShader(ourShader.ID);
glDeleteShader(ourShader2.ID);
}
void menuScene::InitTexture() {
ourShader2.use();
glActiveTexture(GL_TEXTURE0);
int width;
int height;
int numberOfChannel;
//glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, GL_TEXTURE0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
unsigned char* data1 = stbi_load("menuScene.bmp", &width, &height, &numberOfChannel, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data1);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data1);
glUseProgram(0);
}
void menuScene::init()
{
ourShader.InitShader("vertex.glsl", "fragment.glsl"); // 쉐이더 생성
//여기에 obj로드코드
loadOBJ("sphere_.obj", vertices_sphere, uvs_sphere, normals_sphere);
loadOBJ("wall.obj", vertices_back, uvs_back, normals_back);
//여기에 InitBuffer 내용
ourShader.use();
glGenVertexArrays(1, &VAO);
glGenBuffers(2, VBO_position);
//정점버퍼
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO_position[0]);
glBufferData(GL_ARRAY_BUFFER, vertices_sphere.size() * sizeof(glm::vec3), &vertices_sphere[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
glEnableVertexAttribArray(0);
//노멀버퍼
glBindBuffer(GL_ARRAY_BUFFER, VBO_position[1]);
glBufferData(GL_ARRAY_BUFFER, normals_sphere.size() * sizeof(glm::vec3), &normals_sphere[0], GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
glEnableVertexAttribArray(1);
//uniform위치 불러오기
viewLocation = glGetUniformLocation(ourShader.ID, "viewTransform");
projectionLocation = glGetUniformLocation(ourShader.ID, "projectionTransform");
modelLocation = glGetUniformLocation(ourShader.ID, "modelTransform");
fragColor = glGetUniformLocation(ourShader.ID, "inputColor");
lightPos = glGetUniformLocation(ourShader.ID, "lightPos");
lightColor = glGetUniformLocation(ourShader.ID, "lightColor");
viewPos = glGetUniformLocation(ourShader.ID, "viewPos");
//투영행렬은 변화 없으므로 미리 설정
projection = glm::perspective(glm::radians(60.0f), (float)WINDOW_LENGTH / (float)WINDOW_HEIGHT, 0.1f, 100.0f);
glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, &projection[0][0]);
//조명의 색은 흰색으로 고정
glUniform3f(lightColor, 1.0f, 1.0f, 1.0f);
CP.x = 0.0f;
CP.y = 0.0f;
CP.z = 10.0f;
//빛 위치
LP.x = 1.0f;
LP.y = 1.5f;
LP.z = 1.0f;
//카메라 방향
CD.x = 0.0f;
CD.y = 0.0f;
CD.z = 0.0f;
//조명을 흰색으로 고정
glUniform3f(lightColor, 1.0f, 1.0f, 1.0f);
//조명 위치 초기화
glUniform3f(lightPos, LP.x, LP.y, LP.z);
//viewPos(시선벡터) 초기화
glUniform3f(viewPos, CD.x, CD.y, CD.z);
ourShader2.InitShader("vertex2.glsl", "fragment2.glsl"); // 텍스처용쉐이더 생성
ourShader2.use();
glGenVertexArrays(1, &VAO_back);
glGenBuffers(2, VBO_back_position);
//wall 정점버퍼
glBindVertexArray(VAO_back);
glBindBuffer(GL_ARRAY_BUFFER, VBO_back_position[0]);
glBufferData(GL_ARRAY_BUFFER, vertices_back.size() * sizeof(glm::vec3), &vertices_back[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
glEnableVertexAttribArray(0);
//wall uv버퍼
glBindBuffer(GL_ARRAY_BUFFER, VBO_back_position[1]);
glBufferData(GL_ARRAY_BUFFER, uvs_back.size() * sizeof(glm::vec2), &uvs_back[0], GL_STATIC_DRAW);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), 0);
glEnableVertexAttribArray(1);
InitTexture();
glBindVertexArray(0);
glUseProgram(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void menuScene::processKey(unsigned char key, int x, int y)
{
switch (key)
{
case 'q':
glutLeaveMainLoop();
break;
case 's':
scene * scene = GameManager.curScene; ////현재 씬을 tmp에 넣고 지워줌
GameManager.curScene = new gameScene;
GameManager.curScene->init();
GameManager.nowscene = GAME;
delete scene;
break;
}
}
void menuScene::Mouse(int button, int state, int x, int y)
{
}
void menuScene::MouseMotion(int x, int y)
{
}
void menuScene::Update(const float frameTime)
{
if (balljump_count >= 100 && !ballJump) {
balljump_count = 0;
ballJump = true;
}
balljump_count++;
if (ballJump) { //점프 발판을 밟았다면
ball_y -= GRAVITY * frameTime;
}
else { //점프 상태가 아니고 바닥과 닿아있지 않다면 중력 계속 작용
if (ball_y > -1.0f)
ball_y += GRAVITY * frameTime/ (1.5f);
}
if (ball_y >= 4.0f) { //점프 최고 높이 달성 시 떨어지게 만듦
ballJump = false;
}
}
void menuScene::Render()
{
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ourShader2.use();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, GL_TEXTURE0);
int tLocation1 = glGetUniformLocation(ourShader2.ID, "texture1");
glUniform1i(tLocation1, 0);
glBindVertexArray(VAO_back);
glDrawArrays(GL_TRIANGLES, 0, vertices_back.size());
glEnable(GL_DEPTH_TEST);
glFrontFace(GL_CCW);
glEnable(GL_CULL_FACE);
ourShader.use();
//카메라 업데이트
CP.cameraPos = glm::vec3(CP.x, CP.y, CP.z);
CD.cameraDirection = glm::vec3(CD.x, CD.y, CD.z);
view = glm::lookAt(CP.cameraPos, CD.cameraDirection, CP.cameraUp);
glUniformMatrix4fv(viewLocation, 1, GL_FALSE, &view[0][0]);
//여기에 그리기
glBindVertexArray(VAO);
modelmat = glm::mat4(1.0f);
modelmat = glm::translate(modelmat, glm::vec3(3.0f, ball_y, 0.0f));
modelmat = glm::scale(modelmat, glm::vec3(0.1f, 0.1f, 0.1f));
glUniformMatrix4fv(modelLocation, 1, GL_FALSE, &modelmat[0][0]);
glUniform3f(fragColor, 0.8f, 0.619f, 1.0f);
glDrawArrays(GL_TRIANGLES, 0, vertices_sphere.size());
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glBindVertexArray(0);
glUseProgram(0);
}