<basic.vert>
1
2
3
4
5
6
7
8
9
10
11
12
|
#version 400
in vec3 VertexPosition;
in vec3 VertexColor;
out vec3 Color;
uniform mat4 MVP;
void main()
{
Color = VertexColor;
gl_Position = MVP * vec4(VertexPosition, 1.0);
}
|
cs |
<basic.frag>
1
2
3
4
5
6
7
|
#version 400
in vec3 Color;
out vec4 FragColor;
void main() {
FragColor = vec4(Color, 1.0);
}
|
cs |
<main.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
using namespace std;
#include "stdlib.h"
#include "time.h"
#include <GL/glew.h>
#include <GL/freeglut.h>
#include "glm\glm.hpp"
#include "glm\gtc\matrix_transform.hpp"
#include <string>
#include <fstream>
int setShader(char* shaderType, char* shaderFile);
char* readShader(std::string fileName);
#define BUFFER_OFFSET(x) ((const void*) (x))
#define FPS 60
#define X_AXIS glm::vec3(1,0,0)
#define Y_AXIS glm::vec3(0,1,0)
#define Z_AXIS glm::vec3(0,0,1)
#define XY_AXIS glm::vec3(1,0.9,0)
#define YZ_AXIS glm::vec3(0,1,1)
#define XZ_AXIS glm::vec3(1,0,1)
static unsigned int
program,
vertexShaderId,
fragmentShaderId;
glm::mat4 MVP, View, Projection;
GLuint vao, points_vbo, colors_vbo, modelID;
float xVal = 0.0f, yVal = 0.0f, zVal = 5.0f;;
int randomColorSeed[10];
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 5.0f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 cameraRight = glm::vec3(1.0f, 0.0f, 0.0f);
float cameraSpeed = 0.2;
GLshort cube_indices[] = {
// Front.
3, 2, 1, 0,
// Left.
0, 3, 7, 4,
// Bottom.
4, 0, 1, 5,
// Right.
5, 1, 2, 6,
// Back.
6, 5, 4, 7,
// Top.
7, 6, 2, 3
};
GLfloat cube_vertices[] = {
-0.9f, -0.9f, 0.9f, // 0.
0.9f, -0.9f, 0.9f, // 1.
0.9f, 0.9f, 0.9f, // 2.
-0.9f, 0.9f, 0.9f, // 3.
-0.9f, -0.9f, -0.9f, // 4.
0.9f, -0.9f, -0.9f, // 5.
0.9f, 0.9f, -0.9f, // 6.
-0.9f, 0.9f, -0.9f, // 7.
};
GLfloat colors[] = {
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f,
1.0f, 0.0f, 1.0f,
0.0f, 1.0f, 1.0f,
0.5f, 0.0f, 0.0f,
0.0f, 0.5f, 0.0f
};
float scale = 1.0f, inc = -0.05f, angle1 = 0.0f, angle2 = 0.0f;
void timer(int); // Prototype.
void init(void)
{
// Create shader program executable.
vertexShaderId = setShader((char*)"vertex", (char*)"basic.vert");
fragmentShaderId = setShader((char*)"fragment", (char*)"basic.frag");
program = glCreateProgram();
glAttachShader(program, vertexShaderId);
glAttachShader(program, fragmentShaderId);
glLinkProgram(program);
glUseProgram(program);
modelID = glGetUniformLocation(program, "MVP");
// Projection matrix : 45∞ Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
Projection = glm::perspective(glm::radians(45.0f), 4.0f / 3.0f, 0.1f, 100.0f);
// Or, for an ortho camera :
//Projection = glm::ortho(-3.0f, 3.0f, -3.0f, 3.0f, 0.0f, 100.0f); // In world coordinates
// Camera matrix
View = glm::lookAt(
glm::vec3(0, 0, 3), // Origin. Camera is at (0,0,3), in World Space
glm::vec3(0, 0, 0), // Look target. Looks at the origin
glm::vec3(0, 1, 0) // Up vector. Head is up (set to 0,-1,0 to look upside-down)
);
// Create and set-up the vertex array object
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
GLuint vboHandles[2];
glGenBuffers(2, vboHandles);
GLuint positionBufferHandle = vboHandles[0];
GLuint colorBufferHandle = vboHandles[1];
// Populate the position buffer
glBindBuffer(GL_ARRAY_BUFFER, positionBufferHandle);
glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(float), cube_vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLubyte*)NULL);
// Enable the vertex attribute arrays
glEnableVertexAttribArray(0); // for Vertex position
// Populate the color buffer
glBindBuffer(GL_ARRAY_BUFFER, colorBufferHandle);
glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(float), colors, GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (GLubyte*)NULL);
glEnableVertexAttribArray(1); // for Vertex color
GLuint indexBufferObjec;
glGenBuffers(1, &indexBufferObjec);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferObjec);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cube_indices), cube_indices, GL_STATIC_DRAW);
glEnable(GL_DEPTH_TEST);
timer(0);
}
//---------------------------------------------------------------------
//
// transformModel
//
void transformObject(float scale, glm::vec3 rotationAxis, float rotationAngle, glm::vec3 translation) {
glm::mat4 Model;
Model = glm::mat4(1.0f);
Model = glm::translate(Model, translation);
Model = glm::rotate(Model, glm::radians(rotationAngle), rotationAxis);
Model = glm::scale(Model, glm::vec3(scale));
View = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
//View = glm::lookAt(
// glm::vec3(xVal, yVal, 5), // Origin. Camera is at (0,0,3), in World Space
// glm::vec3(0, 0, -4), // Look target. Looks at the origin
// glm::vec3(0, 1, 0) // Up vector. Head is up (set to 0,-1,0 to look upside-down)
//);
MVP = Projection * View * Model;
glUniformMatrix4fv(modelID, 1, GL_FALSE, &MVP[0][0]);
}
//---------------------------------------------------------------------
//
// display
//
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindVertexArray(vao);
transformObject(1.0f, XY_AXIS, angle1 += 0.2f, glm::vec3(0.0f, 1.5f, 0.0f));
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_SHORT, 0);
transformObject(1.0f, XY_AXIS, angle2 -= 0.2f, glm::vec3(0.0f, -1.5f, 0.0f));
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_SHORT, 0);
glutSwapBuffers(); // Now for a potentially smoother render.
}
void idle() // Not even called.
{
glutPostRedisplay();
}
void timer(int) {
glutPostRedisplay();
glutTimerFunc(1000 / FPS, timer, 0);
}
// Keyboard input processing routine.
void keyDown(unsigned char key, int x, int y)
{
switch (key)
{
case 27:
exit(0);
break;
case 'w':
cameraPos += cameraSpeed * cameraFront;
break;
case 's':
cameraPos -= cameraSpeed * cameraFront;
break;
case 'r':
cameraPos -= glm::normalize(glm::cross(cameraFront, cameraRight)) * cameraSpeed;
break;
case 'f':
cameraPos += glm::normalize(glm::cross(cameraFront, cameraRight)) * cameraSpeed;
break;
case 'a':
cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
break;
case 'd':
cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
break;
default:
break;
}
}
//---------------------------------------------------------------------
//
// main
//
int main(int argc, char** argv)
{
//Before we can open a window, theremust be interaction between the windowing systemand OpenGL.In GLUT, this interaction is initiated by the following function call :
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
//if you comment out this line, a window is created with a default size
glutInitWindowSize(800, 600);
//the top-left corner of the display
glutInitWindowPosition(0, 0);
glutCreateWindow("GAME2012_A2_KongWoonhak, 101300258");
// background color is white
glClearColor(0.0f, 0.5f, 0.5f, 1.0f);
glewInit(); //Initializes the glew and prepares the drawing pipeline.
init(); // Our own custom function.
//If there are events in the queue, our program responds to them through functions
//called callbacks.A callback function is associated with a specific type of event.
//A display callback is generated when the application programm or the
//operating system determines that the graphics in a window need to be redrawn.
glutDisplayFunc(display); // Output.
//glutIdleFunc(idle);
glutKeyboardFunc(keyDown); // Input.
//begin an event-processing loop
glutMainLoop();
}
// Function to initialize shaders.
int setShader(char* shaderType, char* shaderFile)
{
int shaderId;
char* shader = readShader(shaderFile);
if (shaderType == "vertex") shaderId = glCreateShader(GL_VERTEX_SHADER);
if (shaderType == "tessControl") shaderId = glCreateShader(GL_TESS_CONTROL_SHADER);
if (shaderType == "tessEvaluation") shaderId = glCreateShader(GL_TESS_EVALUATION_SHADER);
if (shaderType == "geometry") shaderId = glCreateShader(GL_GEOMETRY_SHADER);
if (shaderType == "fragment") shaderId = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(shaderId, 1, (const char**)&shader, NULL);
glCompileShader(shaderId);
return shaderId;
}
// Function to read external shader file.
char* readShader(std::string fileName)
{
// Initialize input stream.
std::ifstream inFile(fileName.c_str(), std::ios::binary);
// Determine shader file length and reserve space to read it in.
inFile.seekg(0, std::ios::end);
int fileLength = inFile.tellg();
char* fileContent = (char*)malloc((fileLength + 1) * sizeof(char));
// Read in shader file, set last character to NUL, close input stream.
inFile.seekg(0, std::ios::beg);
inFile.read(fileContent, fileLength);
fileContent[fileLength] = '\0';
inFile.close();
return fileContent;
}
|
cs |
<결과>
<소스코드>
https://github.com/woonhak-kong/OpenGL_Practice11_camera_movement
'OpenGL' 카테고리의 다른 글
Opengl Texture (0) | 2021.11.07 |
---|---|
opengl dynamic polygon (0) | 2021.10.16 |
opengl perspective (0) | 2021.10.02 |
opengl 직육면체 그리기 (0) | 2021.10.02 |
Opengl mouse input (0) | 2021.10.01 |