-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRendering.cpp
More file actions
269 lines (225 loc) · 7.47 KB
/
Copy pathRendering.cpp
File metadata and controls
269 lines (225 loc) · 7.47 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
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
#include "Rendering.h"
Rendering::Rendering()
{
}
GLint Rendering::loadShader(std::string wayToVertexShader, std::string wayToFragmentShader)
{
//jaki shader chcemy zrobiæ
GLuint vertexShaderId = glCreateShader(GL_VERTEX_SHADER);
GLuint fragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER);
std::string vertex_shader_data;
std::ifstream vertexShaderFile(wayToVertexShader.c_str(), std::ios::in);
if(vertexShaderFile.is_open())
{
std::string line;
while (std::getline(vertexShaderFile, line))
{
vertex_shader_data += "\n" + line;
}
vertexShaderFile.close();
}
std::string fragment_shader_data;
std::ifstream fragmentShaderFile(wayToFragmentShader.c_str(), std::ios::in);
if(fragmentShaderFile.is_open())
{
std::string line;
while (std::getline(fragmentShaderFile, line))
fragment_shader_data += "\n" + line;
fragmentShaderFile.close();
}
const char *vertex_ptr = vertex_shader_data.c_str();
const char *fragment_ptr = fragment_shader_data.c_str();
//wstawia kod shadera do obiektu shadera
glShaderSource(vertexShaderId, 1, &vertex_ptr, NULL);
glShaderSource(fragmentShaderId, 1, &fragment_ptr, NULL);
//kompiluje shadery
glCompileShader(vertexShaderId);
glCompileShader(fragmentShaderId);
//tworzy program sahdera
GLuint shaderProgram = glCreateProgram();
//³¹czy shadery
glAttachShader(shaderProgram, vertexShaderId);
glAttachShader(shaderProgram, fragmentShaderId);
//dodaje shadery do programu
glLinkProgram(shaderProgram);
//usuwa zajêt¹ pamiêæ
glDeleteShader(vertexShaderId);
glDeleteShader(fragmentShaderId);
int vertexShaderStatus = -1;
//sprawdza stan kompilacji shaders
glGetShaderiv(vertexShaderId, GL_COMPILE_STATUS, &vertexShaderStatus);
if(vertexShaderStatus != GL_TRUE)
{
std::cout << "Vertex shader " << wayToVertexShader << "compile error" << std::endl;
return 1;
}
int fragmentShaderStatus = -1;
glGetShaderiv(fragmentShaderId, GL_COMPILE_STATUS, &fragmentShaderStatus);
if(vertexShaderStatus != GL_TRUE)
{
std::cout << "Vertex shader " << wayToVertexShader << "compile error" << std::endl;
return 2;
}
glLinkProgram(shaderProgram);
int linkStatus = -1;
//sprawdza stan linkowania shadera
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &linkStatus);
if(linkStatus != GL_TRUE)
{
std::cout << "Shader programme link error!" << std::endl;
return 3;
}
const int maxLength = 2048;
int length = 0;
char logText[maxLength];
//pokazuje szczegó³owy proces logów shadera
glGetProgramInfoLog(shaderProgram, maxLength, &length, logText);
std::cout << logText;
return shaderProgram;
}
int Rendering::loadTexture(std::string wayToTexture)
{
FREE_IMAGE_FORMAT imageFormat = FIF_UNKNOWN;
FIBITMAP *imagePTR = 0;
BYTE *bits = 0;
imageFormat = FreeImage_GetFileType(wayToTexture.c_str(), 0);
if(imageFormat == FIF_UNKNOWN)
{
imageFormat = FreeImage_GetFIFFromFilename(wayToTexture.c_str());
}
if(FreeImage_FIFSupportsReading(imageFormat))
{
imagePTR = FreeImage_Load(imageFormat, wayToTexture.c_str());
}
bits = FreeImage_GetBits(imagePTR);
int imageWidth = 0;
int imageHeight = 0;
imageWidth = FreeImage_GetWidth(imagePTR);
imageHeight = FreeImage_GetHeight(imagePTR);
GLuint texture = 0;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
unsigned int colours = FreeImage_GetBPP(imagePTR);
if (colours == 24)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, imageWidth, imageHeight, 0, GL_BGR, GL_UNSIGNED_BYTE, bits);
}
else if (colours == 32)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageWidth, imageHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, bits);
}
glGenerateMipmap(GL_TEXTURE_2D);
return 0;
}
int Rendering::loadTexture(std::string wayToFile, GLuint &handle)
{
FREE_IMAGE_FORMAT image_format = FIF_UNKNOWN;
FIBITMAP* image_ptr = 0;
BYTE* bits = 0;
image_format = FreeImage_GetFileType(wayToFile.c_str(), 0);
if (image_format == FIF_UNKNOWN)
image_format = FreeImage_GetFIFFromFilename(wayToFile.c_str());
if (FreeImage_FIFSupportsReading(image_format))
image_ptr = FreeImage_Load(image_format, wayToFile.c_str());
bits = FreeImage_GetBits(image_ptr);
int image_width = 0;
int image_height = 0;
image_width = FreeImage_GetWidth(image_ptr);
image_height = FreeImage_GetHeight(image_ptr);
if ((bits == 0) || (image_width == 0) || (image_height == 0))
return -1;
glGenTextures(1, &handle);
glBindTexture(GL_TEXTURE_2D, handle);
unsigned int colours = FreeImage_GetBPP(image_ptr);
if (colours == 24)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image_width, image_height, 0,
GL_BGR, GL_UNSIGNED_BYTE, bits);
else if (colours == 32)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image_width, image_height,
0, GL_BGRA, GL_UNSIGNED_BYTE, bits);
glGenerateMipmap(GL_TEXTURE_2D);
return 0;
}
int Rendering::loadTexture(std::string wayToFile, Texture &texture)
{
FREE_IMAGE_FORMAT image_format = FIF_UNKNOWN;
FIBITMAP* image_ptr = nullptr;
BYTE* bits = nullptr;
image_format = FreeImage_GetFileType(wayToFile.c_str(), 0);
if (image_format == FIF_UNKNOWN)
image_format = FreeImage_GetFIFFromFilename(wayToFile.c_str());
if (FreeImage_FIFSupportsReading(image_format))
image_ptr = FreeImage_Load(image_format, wayToFile.c_str());
bits = FreeImage_GetBits(image_ptr);
unsigned int image_width = 0;
unsigned int image_height = 0;
image_width = FreeImage_GetWidth(image_ptr);
image_height = FreeImage_GetHeight(image_ptr);
if ((bits == 0) || (image_width == 0) || (image_height == 0))
return -1;
texture.bits = bits;
texture.imagePtr = image_ptr;
texture.width = image_width;
texture.height = image_height;
return 0;
}
void Rendering::glfwInitialization()
{
if(!glfwInit())
{
std::cout << "Error with initialization glfw library" << std::endl;
glfwTerminate();
return;
}
}
void Rendering::glewInitialization()
{
if(glewInit() != GLEW_OK)
{
std::cout << "Error with initialization glew library" << std::endl;
return;
}
}
void Rendering::deep()
{
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glFrontFace(GL_CW);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
}
//******************************************************************************
void Rendering::ProcessWindowEvents(GLFWwindow *window)
{
glfwSwapBuffers(window);
glfwPollEvents();
}
//******************************************************************************
void Rendering::Terminate(GLFWwindow *window)
{
glfwDestroyWindow(window);
glfwTerminate();
}
//******************************************************************************
void Rendering::ClearColor(float r, float g, float b, float alpha, int windowWidth, int windowHeight)
{
glClearColor(r, g, b, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, windowWidth, windowHeight);
}
int Rendering::findUniform(GLuint shader_program, std::string uniform_name)
{
GLint result = glGetUniformLocation(shader_program, uniform_name.c_str());
if (result == -1)
std::cout << "Uniform \"" << uniform_name << "\" not found." << std::endl;
return result;
}
void Rendering::setUniform(GLint uniform_handle, GLint value)
{
glUniform1iv(uniform_handle, 1, &value);
}
//******************************************************************************
void Rendering::setUniform(GLint uniform_handle, const glm::mat4 &matrix)
{
glUniformMatrix4fv(uniform_handle, 1, GL_FALSE, glm::value_ptr(matrix));
}