-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_window.cpp
More file actions
131 lines (93 loc) · 3.36 KB
/
game_window.cpp
File metadata and controls
131 lines (93 loc) · 3.36 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
#include <GL/glew.h>
#include "game_window.h"
#include <fstream>
#include <iostream>
#include <assert.h>
GameWindow::GameWindow():
OpenGL_Window("Game Of Life"),
texture(GL_TEXTURE_2D){
initGL();
}
void GameWindow::setField(const std::array<std::array<char, 100>, 100>* field) {
this->field = field;
}
void GameWindow::initGL() {
glewInit();
createVBOs();
createShaders();
texture.load("life_cell.bmp");
worldLocation = glGetUniformLocation(shaderProgram.getGLProgram(), "world");
assert(worldLocation != 0xFFFFFFFF);
stateLocation = glGetUniformLocation(shaderProgram.getGLProgram(), "cellState");
assert(stateLocation != 0xFFFFFFFF);
samplerLocation = glGetUniformLocation(shaderProgram.getGLProgram(), "sampler");
assert(samplerLocation != 0xFFFFFFFF);
glUniform1i(samplerLocation, 0);
}
void GameWindow::renderGL() {
glClear(GL_COLOR_BUFFER_BIT);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
pipeline.setWorldPosition(0.0f, 0.0f, 0.0f);
glUniformMatrix4fv(worldLocation, 1, GL_TRUE, (const GLfloat*)&pipeline.getTransformation());
for(short c = 0; c < 100; c++) {
for(short i = 0; i < 100; i++) {
glBindBuffer(GL_ARRAY_BUFFER, VBO[c][i]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*)(sizeof(GLfloat)*3));
texture.bind(GL_TEXTURE0);
glDrawArrays(GL_QUADS, 0, 4);
glUniform1i(stateLocation, (GLint)(*field)[c][i]);
}
}
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glFlush();
}
void GameWindow::createShaders() {
std::ifstream fin("shaders/vertex.glsl");
if(not fin.is_open()) std::cerr << "File vertex.glsl not found";
std::string vertexShader;
char c;
while(not fin.eof()) {
fin.get(c);
vertexShader += c;
}
fin.close();
fin.open("shaders/fragment.glsl");
if(not fin.is_open()) std::cerr << "File fragment.glsl not found";
std::string fragmentShader;
while(not fin.eof()) {
fin.get(c);
fragmentShader += c;
}
shaderProgram.addShader(GL_VERTEX_SHADER, vertexShader.c_str());
shaderProgram.addShader(GL_FRAGMENT_SHADER, fragmentShader.c_str());
shaderProgram.useProgram();
}
void GameWindow::createVBOs() {
for(short c = 0; c < 100; c++) {
glGenBuffers(100, VBO[c]);
}
for(short c = 0; c < 100; c++) {
for(short i = 0; i < 100; i++) {
int id[2] = {c, i};
float x = c/100.0f*2.0f;
float y = -i/100.0f*2.0f;
createVBO(id, {x-1, y+1, 0.0f});
}
}
}
void GameWindow::createVBO(int id[2], const Vertex& leftTopCorner) {
std::array<Vertex, 4> vertices;
vertices[0] = leftTopCorner;
vertices[0].u = 0.0f; vertices[0].v = 1.0f;
vertices[1] = Vertex(leftTopCorner.x+0.02f, leftTopCorner.y, 0.0f,
1.0f, 1.0f);
vertices[2] = Vertex(leftTopCorner.x+0.02f, leftTopCorner.y-0.02f, 0.0f,
1.0f, 0.0f);
vertices[3] = Vertex(leftTopCorner.x, leftTopCorner.y-0.02f, 0.0f,
0.0f, 0.0f);
glBindBuffer(GL_ARRAY_BUFFER, VBO[id[0]][id[1]]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices, GL_STATIC_DRAW);
}