-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGLHandler.cpp
139 lines (107 loc) · 3.82 KB
/
GLHandler.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
#include "GLHandler.h"
#include "ShaderLoader.h"
#include <vector>
GLHandler::GLHandler(Camera const & camera) :
ErrorStateBase(),
program(0),
vertexBuffer(0),
vertexArray(0),
uniformMap(),
starField(),
camera(camera)
{
}
GLHandler::~GLHandler()
{
glDeleteBuffers(1, &vertexBuffer);
glDeleteVertexArrays(1, &vertexArray);
glDeleteProgram(program);
}
/* general OpenGL initialization function */
ErrorStateBase::ErrorCode GLHandler::initGL()
{
GLenum err = glewInit();
if (err != GLEW_OK)
{
std::cerr << "Glew initialization failed" << std::endl;
errorState = GLEW_INIT_FAILED;
}
if (checkState())
prepareProgram();
if (checkState())
prepareObjects();
return errorState;
}
void GLHandler::prepareProgram()
{
ShaderLoader loader;
loader.setShaderFile(GL_VERTEX_SHADER, "VertexStarShader.sl");
loader.setShaderFile(GL_FRAGMENT_SHADER, "FragmentStarShader.sl");
errorState = loader.generateProgram(program);
}
void GLHandler::setStarField(std::vector<Star> const & starField)
{
this->starField = starField;
}
void GLHandler::prepareObjects()
{
glUseProgram(program);
for (auto uniform : uniformMap)
{
GLint location = glGetUniformLocation(program, (GLchar*)uniform.first.c_str());
if (location < 0)
{
std::cout << "Couldn’t retrieve location for uniform " << uniform.first << std::endl;
continue;
}
glUniformMatrix4fv(location, 1, GL_FALSE, &uniform.second[0]);
if (GL_INVALID_OPERATION == glGetError())
{
std::cout << "An error occured while setting values for uniform " << uniform.first << std::endl;
}
}
GLint nbAttributes;
glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &nbAttributes);
GLint starLocation = glGetAttribLocation(program, "star");
GLint magnitudeLocation = glGetAttribLocation(program, "magnitude");
GLint colorLocation = glGetAttribLocation(program, "input_color");
glGenVertexArrays(1, &vertexArray);
glBindVertexArray(vertexArray);
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(Star)*starField.size(), &starField[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(starLocation);
glVertexAttribPointer(starLocation, 3, GL_FLOAT, GL_FALSE, sizeof(Star), (void*)offsetof(Star, position));
glEnableVertexAttribArray(magnitudeLocation);
glVertexAttribPointer(magnitudeLocation, 1, GL_FLOAT, GL_FALSE, sizeof(Star), (void*)offsetof(Star, magnitude));
glEnableVertexAttribArray(colorLocation);
glVertexAttribPointer(colorLocation, 3, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Star), (void*)offsetof(Star, color));
glEnable(GL_PROGRAM_POINT_SIZE);
glEnable(GL_POINT_SPRITE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
}
void GLHandler::resizeWindow(int width, int height)
{
/* Setup our viewport. */
glViewport( 0, 0, ( GLsizei )width, ( GLsizei )height );
}
void GLHandler::drawGLScene()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glUseProgram(program);
updateShaderCamera();
glBindVertexArray(vertexArray);
glDrawArrays(GL_POINTS, 0, starField.size());
}
void GLHandler::updateShaderCamera()
{
GLint location;
location = glGetUniformLocation(program, "camera_position");
glUniform3f(location, camera.getPosition().x, camera.getPosition().y, camera.getPosition().z);
location = glGetUniformLocation(program, "camera_orientation");
glUniform4f(location, camera.getOrientation().x, camera.getOrientation().y, camera.getOrientation().z, camera.getOrientation().w);
location = glGetUniformLocation(program, "camera_fov");
glUniform1f(location, camera.getFOV());
}