12#include "TextureManager/TextureManager.h"
13#include "ConsoleLogger.h"
16#define SQUARE_OFFSET 4
18#define TRIANGLE_VERTICES 3
24#define TRIANGLE_MESH_IDX 0
25#define RECTANGLE_MESH_IDX 1
27#define SPHERE_MESH_IDX 3
29constexpr int INDICES_LINE_OFFSET = LINE_OFFSET;
30constexpr int INDICES_SQUARE_OFFSET = 2 * SQUARE_OFFSET;
31constexpr int INDICES_BOX_OFFSET = 3 * BOX_OFFSET;
33constexpr int ARRAY_BOX_OFFSET = 36;
36static Position triangleVertices[3] = {
38 { -0.5f, -0.5f, 0.0f },
42static GLuint triangleIndices[3] = {
46static Position quadVertices[4] = {
47 { -0.5f, 0.5f, 0.0f },
48 { -0.5f, -0.5f, 0.0f },
49 { 0.5f, -0.5f, 0.0f },
53static UV uv_quadVertices[4] = {
61 { glm::vec3(-0.5f, 0.5f, 0.0f), glm::vec2(0.0f, 1.0f) },
62 { glm::vec3(-0.5f, -0.5f, 0.0f), glm::vec2(0.0f, 0.0f) },
63 { glm::vec3(0.5f, -0.5f, 0.0f), glm::vec2(1.0f, 0.0f) },
64 { glm::vec3(0.5f, 0.5f, 0.0f), glm::vec2(1.0f, 1.0f) }
67static GLuint quadIndices[6] = {
72static Position cubeVertices[8] = {
73 { -0.5f, -0.5f, -0.5f },
74 { 0.5f, -0.5f, -0.5f },
75 { 0.5f, 0.5f, -0.5f },
76 { -0.5f, 0.5f, -0.5f },
77 { -0.5f, -0.5f, 0.5f },
78 { 0.5f, -0.5f, 0.5f },
85 { glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(0.0f, 0.0f, -1.0f) },
86 { glm::vec3(0.5f, -0.5f, -0.5f), glm::vec3(0.0f, 0.0f, -1.0f) },
87 { glm::vec3(0.5f, 0.5f, -0.5f), glm::vec3(0.0f, 0.0f, -1.0f) },
88 { glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec3(0.0f, 0.0f, -1.0f) },
91 { glm::vec3(-0.5f, -0.5f, 0.5f), glm::vec3(0.0f, 0.0f, 1.0f) },
92 { glm::vec3(0.5f, -0.5f, 0.5f), glm::vec3(0.0f, 0.0f, 1.0f) },
93 { glm::vec3(0.5f, 0.5f, 0.5f), glm::vec3(0.0f, 0.0f, 1.0f) },
94 { glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec3(0.0f, 0.0f, 1.0f) },
97 { glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(-1.0f, 0.0f, 0.0f) },
98 { glm::vec3(-0.5f, -0.5f, 0.5f), glm::vec3(-1.0f, 0.0f, 0.0f) },
99 { glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec3(-1.0f, 0.0f, 0.0f) },
100 { glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec3(-1.0f, 0.0f, 0.0f) },
103 { glm::vec3(0.5f, -0.5f, -0.5f), glm::vec3(1.0f, 0.0f, 0.0f) },
104 { glm::vec3(0.5f, -0.5f, 0.5f), glm::vec3(1.0f, 0.0f, 0.0f) },
105 { glm::vec3(0.5f, 0.5f, 0.5f), glm::vec3(1.0f, 0.0f, 0.0f) },
106 { glm::vec3(0.5f, 0.5f, -0.5f), glm::vec3(1.0f, 0.0f, 0.0f) },
109 { glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(0.0f, -1.0f, 0.0f) },
110 { glm::vec3(0.5f, -0.5f, -0.5f), glm::vec3(0.0f, -1.0f, 0.0f) },
111 { glm::vec3(0.5f, -0.5f, 0.5f), glm::vec3(0.0f, -1.0f, 0.0f) },
112 { glm::vec3(-0.5f, -0.5f, 0.5f), glm::vec3(0.0f, -1.0f, 0.0f) },
115 { glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec3(0.0f, 1.0f, 0.0f) },
116 { glm::vec3(0.5f, 0.5f, -0.5f), glm::vec3(0.0f, 1.0f, 0.0f) },
117 { glm::vec3(0.5f, 0.5f, 0.5f), glm::vec3(0.0f, 1.0f, 0.0f) },
118 { glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec3(0.0f, 1.0f, 0.0f) }
121static GLuint cubeIndices[36] = {
147static void generateSphereMesh(std::vector<Position>& vertices, std::vector<GLuint>& indices,
148 float radius = 1.0f,
unsigned int sectorCount = 36,
unsigned int stackCount = 18) {
149 const float PI = 3.14159265359f;
154 for (
unsigned int i = 0; i <= stackCount; ++i) {
155 float stackAngle = PI / 2.0f - i * PI / stackCount;
156 float xy = radius * cosf(stackAngle);
157 float z = radius * sinf(stackAngle);
159 for (
unsigned int j = 0; j <= sectorCount; ++j) {
160 float sectorAngle = j * 2.0f * PI / sectorCount;
162 float x = xy * cosf(sectorAngle);
163 float y = xy * sinf(sectorAngle);
164 glm::vec3 pos(x, y, z);
166 vertices.push_back(pos);
170 for (
unsigned int i = 0; i < stackCount; ++i) {
171 unsigned int k1 = i * (sectorCount + 1);
172 unsigned int k2 = k1 + sectorCount + 1;
174 for (
unsigned int j = 0; j < sectorCount; ++j, ++k1, ++k2) {
176 indices.push_back(k1);
177 indices.push_back(k2);
178 indices.push_back(k1 + 1);
180 if (i != (stackCount - 1)) {
181 indices.push_back(k1 + 1);
182 indices.push_back(k2);
183 indices.push_back(k2 + 1);
189static void generateSphereMesh(std::vector<LightVertex>& vertices, std::vector<GLuint>& indices,
190 float radius = 1.0f,
unsigned int sectorCount = 36,
unsigned int stackCount = 18) {
191 const float PI = 3.14159265359f;
196 for (
unsigned int i = 0; i <= stackCount; ++i) {
197 float stackAngle = PI / 2.0f - i * PI / stackCount;
198 float xy = radius * cosf(stackAngle);
199 float z = radius * sinf(stackAngle);
201 for (
unsigned int j = 0; j <= sectorCount; ++j) {
202 float sectorAngle = j * 2.0f * PI / sectorCount;
204 float x = xy * cosf(sectorAngle);
205 float y = xy * sinf(sectorAngle);
206 glm::vec3 pos(x, y, z);
208 glm::vec3 normal = glm::normalize(pos);
209 vertices.push_back({ pos, normal });
213 for (
unsigned int i = 0; i < stackCount; ++i) {
214 unsigned int k1 = i * (sectorCount + 1);
215 unsigned int k2 = k1 + sectorCount + 1;
217 for (
unsigned int j = 0; j < sectorCount; ++j, ++k1, ++k2) {
219 indices.push_back(k1);
220 indices.push_back(k2);
221 indices.push_back(k1 + 1);
223 if (i != (stackCount - 1)) {
224 indices.push_back(k1 + 1);
225 indices.push_back(k2);
226 indices.push_back(k2 + 1);
235 InstanceData(glm::vec3 mSize, Position mBodyCenter, Rotation mRotation) :
237 bodyCenter(mBodyCenter),
242 InstanceData(glm::vec2 mSize, Position mBodyCenter, Rotation mRotation) :
243 size(glm::vec3(mSize, 0.0f)),
244 bodyCenter(mBodyCenter),
251 Size size = glm::vec3(0.0f);
252 Position bodyCenter = glm::vec3(0.0f);
253 Rotation rotation = glm::vec3(0.0f);
273 TextureInstanceData(glm::vec3 mSize, Position mBodyCenter, Rotation mRotation, GLuint Texture) :
InstanceData(mSize, mBodyCenter, mRotation), texture(Texture) {
275 TextureInstanceData(glm::vec2 mSize, Position mBodyCenter, Rotation mRotation, GLuint Texture) :
InstanceData(mSize, mBodyCenter, mRotation), texture(Texture) {
281 UV uv = glm::vec2(0.0f);
285 size_t meshIndices = 0;
295 std::vector<InstanceData> instances;
299 std::vector<ColorInstanceData> instances;
303 std::vector<TextureInstanceData> instances;
308 GLSLProgram() : _programID(0), _vertexShaderID(0), _geometryShaderID(0), _fragmentShaderID(0), _numAttributes(0)
318 void compileAndLinkShaders(
const std::string& vertexShaderFilePath,
const std::string& fragmentShaderFilePath) {
319 std::string vertSource = readShaderFile(vertexShaderFilePath);
320 std::string fragSource = readShaderFile(fragmentShaderFilePath);
322 compileAndLinkShadersFromSource(vertSource.c_str(), fragSource.c_str());
326 void compileAndLinkShaders(
const std::string& vertexShaderFilePath,
const std::string& geometryShaderFilePath,
const std::string& fragmentShaderFilePath) {
327 std::string vertSource = readShaderFile(vertexShaderFilePath);
328 std::string geomSource = readShaderFile(geometryShaderFilePath);
329 std::string fragSource = readShaderFile(fragmentShaderFilePath);
331 compileAndLinkShadersFromSource(vertSource.c_str(), geomSource.c_str(), fragSource.c_str());
334 void compileAndLinkShadersFromSource(
const char* vertexSource,
const char* fragmentSource) {
336 std::vector<GLuint> shaderIDs;
338 shaderIDs.push_back(createAndCompileShader(vertexSource, GL_VERTEX_SHADER,
"Vertex"));
339 shaderIDs.push_back(createAndCompileShader(fragmentSource, GL_FRAGMENT_SHADER,
"Fragment"));
341 linkShadersInternal(shaderIDs);
344 void compileAndLinkShadersFromSource(
const char* vertexSource,
const char* geometrySource,
const char* fragmentSource) {
346 std::vector<GLuint> shaderIDs;
348 shaderIDs.push_back(createAndCompileShader(vertexSource, GL_VERTEX_SHADER,
"Vertex"));
349 shaderIDs.push_back(createAndCompileShader(geometrySource, GL_GEOMETRY_SHADER,
"Geometry"));
350 shaderIDs.push_back(createAndCompileShader(fragmentSource, GL_FRAGMENT_SHADER,
"Fragment"));
352 linkShadersInternal(shaderIDs);
356 void addAttribute(
const std::string& attributeName) {
357 glBindAttribLocation(_programID, _numAttributes++, attributeName.c_str());
360 GLint getUniformLocation(
const std::string& uniformName) {
361 GLint location = glGetUniformLocation(_programID, uniformName.c_str());
363 if (location == GL_INVALID_INDEX) {
364 TazGraphEngine::ConsoleLogger::error(
"Uniform " + uniformName +
" not found in shader!");
370 glUseProgram(_programID);
371 for (
int i = 0; i < _numAttributes; i++) {
372 glEnableVertexAttribArray(i);
378 for (
int i = 0; i < _numAttributes; i++) {
379 glDisableVertexAttribArray(i);
384 if(_programID != 0) glDeleteProgram(_programID);
387 GLuint getProgramID() {
394 GLuint _vertexShaderID;
395 GLuint _geometryShaderID;
396 GLuint _fragmentShaderID;
400 std::string readShaderFile(
const std::string& filePath) {
401 std::vector<unsigned char> buffer;
402 TextureManager::readFileToBuffer(filePath.c_str(), buffer);
403 return std::string(buffer.begin(), buffer.end());
406 GLuint createAndCompileShader(
const std::string& source, GLenum shaderType,
const std::string& typeName) {
407 GLuint shaderID = glCreateShader(shaderType);
409 TazGraphEngine::ConsoleLogger::error(typeName +
" Shader Failed to create!");
413 compileShader(source.c_str(), typeName, shaderID);
417 void compileShader(
const char* source,
const std::string& name, GLuint
id) {
419 glShaderSource(
id, 1, &source,
nullptr);
424 glGetShaderiv(
id, GL_COMPILE_STATUS, &success);
426 if (success == GL_FALSE)
429 glGetShaderiv(
id, GL_INFO_LOG_LENGTH, &maxLength);
432 std::vector<GLchar> errorLog(maxLength);
433 glGetShaderInfoLog(
id, maxLength, &maxLength, &errorLog[0]);
439 std::printf(
"%s\n", &(errorLog[0]));
440 std::cout <<
"Shader " + name +
" failed to compile" << std::endl;
445 void linkShadersInternal(
const std::vector<GLuint>& shaderIDs) {
446 _programID = glCreateProgram();
449 for (GLuint shaderID : shaderIDs) {
450 glAttachShader(_programID, shaderID);
454 glLinkProgram(_programID);
458 glGetProgramiv(_programID, GL_LINK_STATUS, &isLinked);
460 if (isLinked == GL_FALSE) {
462 glGetProgramiv(_programID, GL_INFO_LOG_LENGTH, &maxLength);
464 std::vector<GLchar> errorLog(maxLength);
465 glGetProgramInfoLog(_programID, maxLength, &maxLength, &errorLog[0]);
468 glDeleteProgram(_programID);
469 for (GLuint shaderID : shaderIDs) {
470 glDeleteShader(shaderID);
473 std::printf(
"%s\n", &errorLog[0]);
474 TazGraphEngine::ConsoleLogger::error(
"Shaders failed to link");
479 for (GLuint shaderID : shaderIDs) {
480 glDetachShader(_programID, shaderID);
481 glDeleteShader(shaderID);
Definition GLSLProgram.h:306
Definition GLSLProgram.h:284
Definition GLSLProgram.h:257
Definition GLSLProgram.h:298
Definition GLSLProgram.h:232
Definition GLSLProgram.h:294
Definition GLSLProgram.h:270
Definition GLSLProgram.h:302