Newer
Older
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
//=============================================================================================
// Mintaprogram: Zld hromszg. Ervenyes 2019. osztol.
//
// A beadott program csak ebben a fajlban lehet, a fajl 1 byte-os ASCII karaktereket tartalmazhat, BOM kihuzando.
// Tilos:
// - mast "beincludolni", illetve mas konyvtarat hasznalni
// - faljmuveleteket vegezni a printf-et kiveve
// - Mashonnan atvett programresszleteket forrasmegjeloles nelkul felhasznalni es
// - felesleges programsorokat a beadott programban hagyni!!!!!!!
// - felesleges kommenteket a beadott programba irni a forrasmegjelolest kommentjeit kiveve
// ---------------------------------------------------------------------------------------------
// A feladatot ANSI C++ nyelvu forditoprogrammal ellenorizzuk, a Visual Studio-hoz kepesti elteresekrol
// es a leggyakoribb hibakrol (pl. ideiglenes objektumot nem lehet referencia tipusnak ertekul adni)
// a hazibeado portal ad egy osszefoglalot.
// ---------------------------------------------------------------------------------------------
// A feladatmegoldasokban csak olyan OpenGL fuggvenyek hasznalhatok, amelyek az oran a feladatkiadasig elhangzottak
// A keretben nem szereplo GLUT fuggvenyek tiltottak.
//
// NYILATKOZAT
// ---------------------------------------------------------------------------------------------
// Nev : Cseh Viktor
// Neptun : GG2DP5
// ---------------------------------------------------------------------------------------------
// ezennel kijelentem, hogy a feladatot magam keszitettem, es ha barmilyen segitseget igenybe vettem vagy
// mas szellemi termeket felhasznaltam, akkor a forrast es az atvett reszt kommentekben egyertelmuen jeloltem.
// A forrasmegjeloles kotelme vonatkozik az eloadas foliakat es a targy oktatoi, illetve a
// grafhazi doktor tanacsait kiveve barmilyen csatornan (szoban, irasban, Interneten, stb.) erkezo minden egyeb
// informaciora (keplet, program, algoritmus, stb.). Kijelentem, hogy a forrasmegjelolessel atvett reszeket is ertem,
// azok helyessegere matematikai bizonyitast tudok adni. Tisztaban vagyok azzal, hogy az atvett reszek nem szamitanak
// a sajat kontribucioba, igy a feladat elfogadasarol a tobbi resz mennyisege es minosege alapjan szuletik dontes.
// Tudomasul veszem, hogy a forrasmegjeloles kotelmenek megsertese eseten a hazifeladatra adhato pontokat
// negativ elojellel szamoljak el es ezzel parhuzamosan eljaras is indul velem szemben.
//=============================================================================================
#include "framework.h"
// vertex shader in GLSL: It is a Raw string (C++11) since it contains new line characters
const char* const vertexSource = R"(
#version 330 // Shader 3.3
precision highp float; // normal floats, makes no difference on desktop computers
uniform mat4 MVP; // uniform variable, the Model-View-Projection transformation matrix
layout(location = 0) in vec2 vp; // Varying input: vp = vertex position is expected in attrib array 0
void main() {
gl_Position = vec4(vp.x, vp.y, 0, 1) * MVP; // transform vp from modeling space to normalized device space
}
)";
// fragment shader in GLSL
const char* const fragmentSource = R"(
#version 330 // Shader 3.3
precision highp float; // normal floats, makes no difference on desktop computers
uniform vec3 color; // uniform variable, the color of the primitive
out vec4 outColor; // computed color of the current pixel
void main() {
outColor = vec4(color, 1); // computed color is the color of the primitive
}
)";
const int BASECIRCLESEG = 100; //how many triangle the base stands from
GPUProgram gpuProgram; // vertex and fragment shaders
void MVPInit() {
float MVPtransf[4][4] = { 1, 0, 0, 0, // MVP matrix,
0, 1, 0, 0, // row-major!
0, 0, 1, 0,
0, 0, 0, 1 };
int location = glGetUniformLocation(gpuProgram.getId(), "MVP"); // Get the GPU location of uniform variable MVP
glUniformMatrix4fv(location, 1, GL_TRUE,
&MVPtransf[0][0]); // Load a 4x4 row-major float matrix to the specified location
}
struct Color {
float r, g, b;
Color(int _r = 255, int _g = 179, int _b = 0) {
r = (float)_r / 255;
g = (float)_g / 255;
b = (float)_b / 255;
}
Color(float _r, float _g, float _b) {
r = _r; g = _g; b = _b;
}
};
void setBackgroundColor(Color color) {
glClearColor(color.r, color.g, color.b, 1); // background color
glClear(GL_COLOR_BUFFER_BIT); // clear frame buffer
}
unsigned int vao;
class drawableBase {
protected:
unsigned int vbo; //vertex buffer obj
std::vector<vec2> points;
Color color;
public:
virtual void draw() = 0;
virtual void setColor(Color newColor) = 0;
void init() {
glGenVertexArrays(1, &vao); // get 1 vao id
glBindVertexArray(vao); // make it active
glGenBuffers(1, &vbo); // Generate 1 buffer
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, // Copy to GPU target
points.size() * sizeof(vec2), // # bytes
points.data(), // address
GL_DYNAMIC_DRAW); // we do not change later
glEnableVertexAttribArray(0); // AttribArray 0
glVertexAttribPointer(0, // vbo -> AttribArray 0
2, GL_FLOAT,
GL_FALSE, // two floats/attrib, not fixed-point
0, NULL); // stride, offset: tightly packed
}
};
class GraphPoint : drawableBase {
std::vector<GraphPoint*> neighbours;
//vec3 c; //center of the point in hyperbolic splace
vec2 p; // center on the base disk
public:
GraphPoint(vec2 a = vec2(0.0f,0.0f)) {
p = a;
placeCircleToNewCoordinates();
setColor(Color(244, 164, 96));
}
vec2 getPoint() {
return p;
}
void setCoordinates(vec2 _p) {
p = _p;
placeCircleToNewCoordinates();
}
void placeCircleToNewCoordinates() {
points.clear();
float piece = 360.0 / BASECIRCLESEG;
float courrentTriangleAngle = 0;
for (int i = 0; i < BASECIRCLESEG; i++) {
points.emplace_back(p.x, p.y);
points.emplace_back(p.x +(cos(courrentTriangleAngle * M_PI / 180.0) /30),p.y + (sin(courrentTriangleAngle * M_PI / 180.0) /30));
courrentTriangleAngle += piece;
points.emplace_back(p.x + (cos(courrentTriangleAngle * M_PI / 180.0) /30), p.y + (sin(courrentTriangleAngle * M_PI / 180.0)/30));
}
}
vec2 vec3tovec2(vec3 c) {
vec2 temp;
temp.x = c.x;
temp.y = c.y;
return temp;
}
void reColor() const {
int location = glGetUniformLocation(gpuProgram.getId(), "color");
glUniform3f(location, color.r, color.g, color.b); // 3 floats
}
void setColor(Color newColor) override{
color = newColor;
}
void addNeighbour(GraphPoint* p) {
neighbours.push_back(p);
}
void draw() override
{
init();
reColor();
MVPInit();
glBindVertexArray(vao); // Draw call
glDrawArrays(GL_TRIANGLES, 0, points.size() /*# Elements*/);
}
};
GraphPoint* gps = new GraphPoint[50];
void graphPointSetNeighbour() {
for (int i = 0; i < 50; i++) {
for (int j = 0; j < 50; j++) {
if (i != j)
gps[i].addNeighbour(&gps[j]);
}
}
}
//Beallitja, hogy kinek ki lesz kozvetlen szomszedja ellel, es ki nem
void graphPointsRandomiseNeighbour() {
for (int i = 0; i < 50; i++) {
gps[i];
}
}
class Line : drawableBase {
public:
Line(vec2 _p1, vec2 _p2) {
setColor(Color(254, 254, 0));
points.push_back(_p1);
points.push_back(_p2);
}
void reColor() const {
int location = glGetUniformLocation(gpuProgram.getId(), "color");
glUniform3f(location, color.r, color.g, color.b); // 3 floats
}
void setColor(Color newColor) override {
color = newColor;
}
void draw() override {
init();
reColor();
glBindVertexArray(vao); // Draw call
glDrawArrays(GL_LINES, 0, points.size() /*# Elements*/);
}
};
// Initialization, create an OpenGL context
void onInitialization() {
glViewport(0, 0, windowWidth, windowHeight);
//graphPointSetNeighbour();
//graphPointsRandomiseNeighbour();
// create program for the GPU
gpuProgram.create(vertexSource, fragmentSource, "outColor");
}
// Window has become invalid: Redraw
void onDisplay() {
MVPInit();
setBackgroundColor(Color(0,0,0)); //set the background color to black
gps[1].setCoordinates(vec2(0.4f, 0.4f));
Line l2(gps[1].getPoint(), gps[0].getPoint());
l2.draw();
gps[1].draw();
gps[0].draw();
glutSwapBuffers(); // exchange buffers for double buffering
}
// Key of ASCII code pressed
void onKeyboard(unsigned char key, int pX, int pY) {
if (key == 'd') glutPostRedisplay(); // if d, invalidate display, i.e. redraw
}
// Key of ASCII code released
void onKeyboardUp(unsigned char key, int pX, int pY) {
}
// Move mouse with key pressed
void onMouseMotion(int pX, int pY) { // pX, pY are the pixel coordinates of the cursor in the coordinate system of the operation system
// Convert to normalized device space
float cX = 2.0f * pX / windowWidth - 1; // flip y axis
float cY = 1.0f - 2.0f * pY / windowHeight;
printf("Mouse moved to (%3.2f, %3.2f)\n", cX, cY);
}
// Mouse click event
void onMouse(int button, int state, int pX, int pY) { // pX, pY are the pixel coordinates of the cursor in the coordinate system of the operation system
// Convert to normalized device space
float cX = 2.0f * pX / windowWidth - 1; // flip y axis
float cY = 1.0f - 2.0f * pY / windowHeight;
char* buttonStat;
switch (state) {
case GLUT_DOWN: buttonStat = "pressed"; break;
case GLUT_UP: buttonStat = "released"; break;
}
switch (button) {
case GLUT_LEFT_BUTTON: printf("Left button %s at (%3.2f, %3.2f)\n", buttonStat, cX, cY); break;
case GLUT_MIDDLE_BUTTON: printf("Middle button %s at (%3.2f, %3.2f)\n", buttonStat, cX, cY); break;
case GLUT_RIGHT_BUTTON: printf("Right button %s at (%3.2f, %3.2f)\n", buttonStat, cX, cY); break;
}
}
// Idle event indicating that some time elapsed: do animation here
void onIdle() {
long time = glutGet(GLUT_ELAPSED_TIME); // elapsed time since the start of the program
}