-
Notifications
You must be signed in to change notification settings - Fork 1
/
DrawingPrisms.cpp
150 lines (119 loc) · 4.3 KB
/
DrawingPrisms.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
/*
Micha³ Piotrak
numer albumu: 269336
Przeciêcie graniastos³upów
*/
#include "DrawingPrisms.h"
DrawingPrisms::DrawingPrisms(vector<Prism> prismsVector, int polygonNumber)
{
srand(time(NULL)); // to draw lots color value
this->polygonNumber = polygonNumber;
for (Prism prism : prismsVector)
{
vector<GLfloat> tmpVerticesVector;
Polygon polygon = prism.getBase();
int verticesNumber = 0;
for (Vertex v : polygon.getVerticesList())
{
tmpVerticesVector.push_back((GLfloat)v.getX());
tmpVerticesVector.push_back((GLfloat)v.getY());
tmpVerticesVector.push_back((GLfloat)0.0f);
++verticesNumber;
}
verticesNumbers.push_back(verticesNumber);
verticesLists.push_back(tmpVerticesVector);
}
}
void DrawingPrisms::key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
void DrawingPrisms::drawPrisms(const char* windowName)
{
if (glfwInit() != GL_TRUE)
{
cout << "GLFW initialization failed" << endl;
}
else
{
// opengl wersja 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
try
{
// tworzymy okno - dwa ostatnie argumenty - mniej wazne ale jesli w pierwszym ustawimy glfwGetPrimaryMonitor() to bedziemy mieli okno na caly ekran
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, windowName, nullptr, nullptr);
if (window == nullptr)
throw exception("GLFW window not created");
glfwMakeContextCurrent(window); // kontekst naszego okienka glownym kontekstem w aktualnym watku
glfwSetKeyCallback(window, key_callback); // rejestrujemy ta funkcje "nasluchiwania" przycisków klawy
glewExperimental = GL_TRUE; // wiecej nowoczesnych technik do opengl
if (glewInit() != GLEW_OK)
throw exception("GLEW Initialization failed");
glViewport(0, 0, WIDTH, HEIGHT); // okno renderingu
ShaderProgram ourShader("shader.vert", "shader.frag");
GLuint VAO;
glGenVertexArrays(1, &VAO);
GLuint VBO;
glGenBuffers(1, &VBO);
vector<glm::vec3> colors;
for (int i = 0; i < polygonNumber; ++i)
{
colors.push_back(glm::vec3(fRand(0.0f, 1.0f), fRand(0.0f, 1.0f), fRand(0.0f, 1.0f)));
}
glLineWidth(2.0f);
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
glClearColor(0.3f, 0.3f, 0.3f, 1.0f); // kolor
glClear(GL_COLOR_BUFFER_BIT);
ourShader.Use();
glm::mat4 view;
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 90.0f); // polozenie kamery
glm::vec3 cameraTarget = glm::vec3(0.0f, 0.0f, 0.0f); // na co patrzy kamera
view = glm::lookAt(cameraPos, cameraTarget, glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 projection;
//projection = glm::perspective(0.0f, (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.0f, 100.0f);
projection = glm::ortho(-90.0f, 90.0f, -90.0f, 90.0f, 0.0f, 100.0f);
GLint viewLoc = glGetUniformLocation(ourShader.get_programID(), "view");
GLint projLoc = glGetUniformLocation(ourShader.get_programID(), "projection");
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
GLint colorLoc = glGetUniformLocation(ourShader.get_programID(), "ourColor");
glBindVertexArray(VAO);
for (int i = 0; i < polygonNumber; ++i)
{
glUniform3f(colorLoc, colors[i].x, colors[i].y, colors[i].z);
GLfloat* vertices = new GLfloat[verticesNumbers[i] * 3];
for (int j = 0; j < verticesNumbers[i] * 3; ++j)
vertices[j] = verticesLists[i][j];
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, verticesNumbers[i] * 3 * sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
glDrawArrays(GL_LINE_LOOP, 0, verticesNumbers[i]);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
glBindVertexArray(0);
glfwSwapBuffers(window);
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
}
catch (exception ex)
{
cout << ex.what() << endl;
}
glfwTerminate();
}
}
GLfloat DrawingPrisms::fRand(GLfloat fMin, GLfloat fMax)
{
GLfloat f = (GLfloat)rand() / RAND_MAX;
return fMin + f * (fMax - fMin);
}
DrawingPrisms::~DrawingPrisms()
{
}