main cpp GLFW Lab Created by Кирилл Снегур on 19 03 2019 Copyright 201

  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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//
// main.cpp
// GLFW Lab 3.1
//
// Created by Кирилл Снегур on 19.03.2019.
// Copyright © 2019 ALLMIGHT DEVELOPMENT. All rights reserved.
//
//
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <cmath>
#include <math.h>
#include <vector>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
using namespace std;
//const GLchar* vertexShaderSource = "#version 330 core\n"
//"layout (location = 0) in vec3 position;\n"
//"void main()\n"
//"{\n"
//"gl_Position = vec4(position.x, position.y, position.z, 1.0);\n"
//"}\0";
const GLchar* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 position;\n"
"uniform mat4 matrix;"
//"in vec3 pos;"
"void main()\n"
"{\n"
"gl_Position = matrix * vec4( position, 1.0);\n"
//"gl_Position = matrix * vec4(x, y, z, 1.0);\n"
// "gl_Position = vec4(position.x, position.y, position.z, 1.0);\n"
"}\0";
const GLchar* fragmentShaderSource = "#version 330 core\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
"color = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n\0";
typedef struct {
float x, y, z;
} Point;
vector<Point> vertices;
vector<unsigned int> indices;
int vertCount;
GLuint VAO, VBO, EBO;
//GLfloat *vertices;
int WIDTH = 800, HEIGHT = 800;
float x = 0, y = 0, z = 0;
float RotateX = 0.0f, RotateY = 0.0f, RotateZ = 0.0f;
float PositionX = 1.0f, PositionY = 1.0f, PositionZ = 1.0f;
int circleValue = 30, triangleValue =20;
GLfloat PologonModeValue = GL_LINE;
GLfloat phi = asin(0.25 / sqrt(2.0));
GLfloat teta = asin(0.25 / sqrt(2.0 - 0.25 * 0.25));
GLfloat TransformationMatrix[4][4] = {
{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1},
};
static double PI = 3.1415926535897932384626433832795;
glm::mat4 project;
glm::mat4 model = glm::translate(glm::mat4(1.f), { x, y, z });
GLuint shaderProgram;
void framebuffer_size_callback(GLFWwindow* window, int width, int height){
if(width>height)
{
glViewport((width-height)/2, 0, std::min(width,height), std::min(width,height));
}
else
glViewport(0, (height-width)/2, std::min(width,height), std::min(width,height));
};
void keyCallback(GLFWwindow * window, int key, int scancode, int action, int mode) {
GLfloat step = 0.05f;
GLfloat rotateStep = 5.0f;
GLfloat valueStep = 1.0f;
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
if (action == GLFW_PRESS || action == GLFW_REPEAT) {
switch (key) {
case GLFW_KEY_D:
RotateX -= rotateStep;
break;
case GLFW_KEY_A:
RotateX += rotateStep;
break;
case GLFW_KEY_S:
RotateY -= rotateStep;
break;
case GLFW_KEY_W:
RotateY += rotateStep;
break;
case GLFW_KEY_Q:
RotateZ += rotateStep;
break;
case GLFW_KEY_E:
RotateZ -= rotateStep;
break;
case GLFW_KEY_LEFT:
x -= step;
break;
case GLFW_KEY_RIGHT:
x += step;
break;
case GLFW_KEY_UP:
y += step;
break;
case GLFW_KEY_DOWN:
y -= step;
break;
case GLFW_KEY_C:
circleValue -= valueStep;
break;
case GLFW_KEY_V:
circleValue += valueStep;
break;
case GLFW_KEY_R:
triangleValue -= valueStep;
break;
case GLFW_KEY_F:
triangleValue += valueStep;
break;
case GLFW_KEY_1:
PologonModeValue = GL_FILL;
break;
case GLFW_KEY_2:
PologonModeValue = GL_LINE;
}
}
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset){
PositionX += 0.5 * yoffset;
PositionY += 0.5 * yoffset;
PositionZ += 0.5 * yoffset;
}
void Draw_Cube(GLfloat size){
glBegin(GL_QUADS);
glVertex3f( -size / 2, -size / 2, -size / 2);
glVertex3f( -size / 2, size / 2, -size / 2);
glVertex3f( -size / 2, size / 2, size / 2);
glVertex3f( -size / 2, -size / 2, size / 2);
glVertex3f( size / 2, -size / 2, -size / 2);
glVertex3f( size / 2, -size / 2, size / 2);
glVertex3f( size / 2, size / 2, size / 2);
glVertex3f( size / 2, size / 2, -size / 2);
glVertex3f( -size / 2, -size / 2, -size / 2);
glVertex3f( -size / 2, -size / 2, size / 2);
glVertex3f( size / 2, -size / 2, size / 2);
glVertex3f( size / 2, -size / 2, -size / 2);
glVertex3f( -size / 2, size / 2, -size / 2);
glVertex3f( -size / 2, size / 2, size / 2);
glVertex3f( size / 2, size / 2, size / 2);
glVertex3f( size / 2, size / 2, -size / 2);
glVertex3f( -size / 2, -size / 2, -size / 2);
glVertex3f( size / 2, -size / 2, -size / 2);
glVertex3f( size / 2, size / 2, -size / 2);
glVertex3f( -size / 2, size / 2, -size / 2);
glVertex3f( -size / 2, -size / 2, size / 2);
glVertex3f( size / 2, -size / 2, size / 2);
glVertex3f( size / 2, size / 2, size / 2);
glVertex3f( -size / 2, size / 2, size / 2);
glEnd();
}
void Draw_Tor(int roundValue, int shapesValue, float externalRadDelta, float interiorRadDelta) {
glPolygonMode(GL_FRONT_AND_BACK, PologonModeValue);
double externalRad = 2.0 * PI / roundValue;
double interiorRad = 2.0 * PI / shapesValue;
vertices.resize(2*(roundValue * (shapesValue + 1) + 1));
indices.clear();
vertCount = 0;
for (int i = 0; i < roundValue; i++) {
double stepOne = i * externalRad;
double stepTwo = stepOne + externalRad;
GLdouble HorizontalStepOne = cos(stepOne);
GLdouble VerticalStepOne = sin(stepOne);
GLdouble HorizontalStepTwo = cos(stepTwo);
GLdouble VerticalStepTwo = sin(stepTwo);
//glBegin(GL_TRIANGLE_STRIP);
for (int j = 0; j <= shapesValue; j++) {
double b = j * interiorRad;
GLdouble externalRadValue = interiorRadDelta * cos(b) + externalRadDelta;
GLdouble interiorRadValue = interiorRadDelta * sin(b);
vertices[vertCount].x = HorizontalStepTwo * externalRadValue;
vertices[vertCount].y = VerticalStepTwo * externalRadValue;
vertices[vertCount].z = interiorRadValue;
indices.push_back(vertCount++);
vertices[vertCount].x = HorizontalStepOne * externalRadValue;
vertices[vertCount].y = VerticalStepOne * externalRadValue;
vertices[vertCount].z = interiorRadValue;
indices.push_back(vertCount++);
}
}
// glBindVertexArray(VAO);
//
// glBindBuffer(GL_ARRAY_BUFFER, VBO);
// glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(Point), &vertices[0], GL_DYNAMIC_DRAW);
//
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
// glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(unsigned int), &indices[0], GL_DYNAMIC_DRAW);
//
// GLuint posAttr = glGetAttribLocation(shaderProgram, "pos");;
// glVertexAttribPointer(posAttr, 3, GL_FLOAT, GL_FALSE, sizeof(Point), (GLvoid*)(0*sizeof(GLfloat)));
// glEnableVertexAttribArray(posAttr);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(Point), &vertices[0], GL_DYNAMIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(unsigned int), &indices[0], GL_DYNAMIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Point), (GLvoid*)(0*sizeof(GLfloat)));
glEnableVertexAttribArray(0);
}
void Draw_dimetrialProection() {
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(-phi*(180/3.14) , 1.0, 0.0, 0.0);
glRotatef(-teta*(180/3.14) , 0.0, 1.0, 0.0);
glTranslatef(-0.9, -0.7, -0.5);
glScaled(0.2, 0.2, 0.2);
Draw_Cube(1);
}
int main(void) {
if (!glfwInit()) {
return -1;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "Lab8", NULL, NULL);
glfwMakeContextCurrent(window);
//
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetKeyCallback(window, keyCallback);
glfwSetScrollCallback(window, scroll_callback);
glewExperimental = GL_TRUE;
glewInit();
glEnable(GL_DEPTH_TEST);
GLfloat teta = (float)M_PI * 30 / 180;
GLfloat matrix[16] = {
1, 0, 0, 0,
0, 1, 0, 0,
-cosf(teta), -sinf(teta), -1, 0,
0, 0, 0, 1
};
project = glm::make_mat4x4(matrix);
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glfwPollEvents();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMultMatrixf(*TransformationMatrix);
Draw_Tor(circleValue,triangleValue, 0.2f , 0.1f);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glMatrixMode(GL_MODELVIEW);
glm::mat4 model = glm::translate(glm::mat4(1.f), { x, y, z });
model *= glm::rotate(model, glm::radians(RotateX), {1, 0, 0});
model *= glm::rotate(model, glm::radians(RotateY), {0, 1, 0});
model *= glm::rotate(model, glm::radians(RotateZ), {0, 0, 1});
model = glm::scale(model, {PositionX, PositionY, PositionZ});
glPolygonMode(GL_FRONT_AND_BACK, PologonModeValue);
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "matrix"), 1, GL_FALSE, glm::value_ptr(project * model));
glDrawElements(GL_TRIANGLE_STRIP, 2* circleValue * (triangleValue + 1), GL_UNSIGNED_INT, 0);
glLoadIdentity();
glBindVertexArray(0);
Draw_dimetrialProection();
Draw_Tor(circleValue,triangleValue, 0.2f , 0.1f);
glfwPollEvents();
glfwSwapBuffers(window);
}
glfwDestroyWindow(window);
glfwTerminate();
}