7#include "TextureManager/TextureManager.h"
8#include "ConsoleLogger.h"
11#define SQUARE_OFFSET 4
13#define TRIANGLE_VERTICES 3
19#define TRIANGLE_MESH_IDX 0
20#define RECTANGLE_MESH_IDX 1
22#define SPHERE_MESH_IDX 3
24#define TOTAL_LINE_MESHES 4
26#define LINE_MESH_IDX 0
27#define LINE_RECTANGLE_MESH_IDX 1
28#define LINE_BOX_MESH_IDX 2
29#define LINE_SPHERE_MESH_IDX 3
32constexpr int INDICES_LINE_OFFSET = LINE_OFFSET;
33constexpr int INDICES_SQUARE_OFFSET = 2 * SQUARE_OFFSET;
34constexpr int INDICES_BOX_OFFSET = 3 * BOX_OFFSET;
36constexpr int ARRAY_BOX_OFFSET = 36;
38static TazPosition triangleVertices[3] = {
40 { -0.5f, -0.5f, 0.0f },
44static GLuint triangleIndices[3] = {
48static TazPosition quadVertices[4] = {
49 { -0.5f, 0.5f, 0.0f },
50 { -0.5f, -0.5f, 0.0f },
51 { 0.5f, -0.5f, 0.0f },
55static TazUV uv_quadVertices[4] = {
63 { glm::vec3(-0.5f, 0.5f, 0.0f), glm::vec2(0.0f, 1.0f) },
64 { glm::vec3(-0.5f, -0.5f, 0.0f), glm::vec2(0.0f, 0.0f) },
65 { glm::vec3(0.5f, -0.5f, 0.0f), glm::vec2(1.0f, 0.0f) },
66 { glm::vec3(0.5f, 0.5f, 0.0f), glm::vec2(1.0f, 1.0f) }
69static GLuint quadIndices[6] = {
75static GLuint quadWireframeIndices[] = {
82static TazPosition cubeVertices[8] = {
83 { -0.5f, -0.5f, -0.5f },
84 { 0.5f, -0.5f, -0.5f },
85 { 0.5f, 0.5f, -0.5f },
86 { -0.5f, 0.5f, -0.5f },
87 { -0.5f, -0.5f, 0.5f },
88 { 0.5f, -0.5f, 0.5f },
95 { glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(0.0f, 0.0f, -1.0f) },
96 { 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(0.0f, 0.0f, -1.0f) },
98 { glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec3(0.0f, 0.0f, -1.0f) },
101 { glm::vec3(-0.5f, -0.5f, 0.5f), glm::vec3(0.0f, 0.0f, 1.0f) },
102 { glm::vec3(0.5f, -0.5f, 0.5f), glm::vec3(0.0f, 0.0f, 1.0f) },
103 { glm::vec3(0.5f, 0.5f, 0.5f), glm::vec3(0.0f, 0.0f, 1.0f) },
104 { glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec3(0.0f, 0.0f, 1.0f) },
107 { glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(-1.0f, 0.0f, 0.0f) },
108 { 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(-1.0f, 0.0f, 0.0f) },
110 { glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec3(-1.0f, 0.0f, 0.0f) },
113 { glm::vec3(0.5f, -0.5f, -0.5f), glm::vec3(1.0f, 0.0f, 0.0f) },
114 { glm::vec3(0.5f, -0.5f, 0.5f), glm::vec3(1.0f, 0.0f, 0.0f) },
115 { glm::vec3(0.5f, 0.5f, 0.5f), glm::vec3(1.0f, 0.0f, 0.0f) },
116 { glm::vec3(0.5f, 0.5f, -0.5f), glm::vec3(1.0f, 0.0f, 0.0f) },
119 { glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(0.0f, -1.0f, 0.0f) },
120 { glm::vec3(0.5f, -0.5f, -0.5f), glm::vec3(0.0f, -1.0f, 0.0f) },
121 { glm::vec3(0.5f, -0.5f, 0.5f), glm::vec3(0.0f, -1.0f, 0.0f) },
122 { glm::vec3(-0.5f, -0.5f, 0.5f), glm::vec3(0.0f, -1.0f, 0.0f) },
125 { glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec3(0.0f, 1.0f, 0.0f) },
126 { glm::vec3(0.5f, 0.5f, -0.5f), glm::vec3(0.0f, 1.0f, 0.0f) },
127 { glm::vec3(0.5f, 0.5f, 0.5f), glm::vec3(0.0f, 1.0f, 0.0f) },
128 { glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec3(0.0f, 1.0f, 0.0f) }
131static GLuint cubeIndices[36] = {
156static GLuint cubeWireframeIndices[] = {
158 0, 1, 1, 2, 2, 3, 3, 0,
160 4, 5, 5, 6, 6, 7, 7, 4,
162 0, 4, 1, 5, 2, 6, 3, 7
166static void generateSphereMesh(std::vector<TazPosition>& vertices, std::vector<GLuint>& indices,
167 float radius = 1.0f,
unsigned int sectorCount = 36,
unsigned int stackCount = 18) {
168 const float PI = 3.14159265359f;
173 for (
unsigned int i = 0; i <= stackCount; ++i) {
174 float stackAngle = PI / 2.0f - i * PI / stackCount;
175 float xy = radius * cosf(stackAngle);
176 float z = radius * sinf(stackAngle);
178 for (
unsigned int j = 0; j <= sectorCount; ++j) {
179 float sectorAngle = j * 2.0f * PI / sectorCount;
181 float x = xy * cosf(sectorAngle);
182 float y = xy * sinf(sectorAngle);
183 glm::vec3 pos(x, y, z);
185 vertices.push_back(pos);
189 for (
unsigned int i = 0; i < stackCount; ++i) {
190 unsigned int k1 = i * (sectorCount + 1);
191 unsigned int k2 = k1 + sectorCount + 1;
193 for (
unsigned int j = 0; j < sectorCount; ++j, ++k1, ++k2) {
195 indices.push_back(k1);
196 indices.push_back(k2);
197 indices.push_back(k1 + 1);
199 if (i != (stackCount - 1)) {
200 indices.push_back(k1 + 1);
201 indices.push_back(k2);
202 indices.push_back(k2 + 1);
208static void generateSphereMesh(std::vector<TazLightVertex>& vertices, std::vector<GLuint>& indices,
209 float radius = 1.0f,
unsigned int sectorCount = 36,
unsigned int stackCount = 18) {
210 const float PI = 3.14159265359f;
215 for (
unsigned int i = 0; i <= stackCount; ++i) {
216 float stackAngle = PI / 2.0f - i * PI / stackCount;
217 float xy = radius * cosf(stackAngle);
218 float z = radius * sinf(stackAngle);
220 for (
unsigned int j = 0; j <= sectorCount; ++j) {
221 float sectorAngle = j * 2.0f * PI / sectorCount;
223 float x = xy * cosf(sectorAngle);
224 float y = xy * sinf(sectorAngle);
225 glm::vec3 pos(x, y, z);
227 glm::vec3 normal = glm::normalize(pos);
228 vertices.push_back({ pos, normal });
232 for (
unsigned int i = 0; i < stackCount; ++i) {
233 unsigned int k1 = i * (sectorCount + 1);
234 unsigned int k2 = k1 + sectorCount + 1;
236 for (
unsigned int j = 0; j < sectorCount; ++j, ++k1, ++k2) {
238 indices.push_back(k1);
239 indices.push_back(k2);
240 indices.push_back(k1 + 1);
242 if (i != (stackCount - 1)) {
243 indices.push_back(k1 + 1);
244 indices.push_back(k2);
245 indices.push_back(k2 + 1);
251static void generateSphereMeshWireframe(
252 std::vector<TazPosition>& vertices,
253 std::vector<GLuint>& lineIndices,
255 unsigned int sectorCount = 36,
256 unsigned int stackCount = 18)
258 const float PI = 3.14159265359f;
263 for (
unsigned int i = 0; i <= stackCount; ++i)
265 float stackAngle = PI / 2.0f - i * PI / stackCount;
266 float xy = radius * cosf(stackAngle);
267 float z = radius * sinf(stackAngle);
269 for (
unsigned int j = 0; j <= sectorCount; ++j)
271 float sectorAngle = j * 2.0f * PI / sectorCount;
273 float x = xy * cosf(sectorAngle);
274 float y = xy * sinf(sectorAngle);
276 glm::vec3 pos(x, y, z);
278 vertices.push_back(pos);
282 for (
unsigned int i = 0; i < stackCount; ++i)
284 unsigned int k1 = i * (sectorCount + 1);
285 unsigned int k2 = k1 + sectorCount + 1;
287 for (
unsigned int j = 0; j < sectorCount; ++j, ++k1, ++k2)
292 lineIndices.push_back(k1);
293 lineIndices.push_back(k2);
295 lineIndices.push_back(k2);
296 lineIndices.push_back(k1 + 1);
298 lineIndices.push_back(k1 + 1);
299 lineIndices.push_back(k1);
302 if (i != (stackCount - 1))
305 lineIndices.push_back(k1 + 1);
306 lineIndices.push_back(k2);
308 lineIndices.push_back(k2);
309 lineIndices.push_back(k2 + 1);
311 lineIndices.push_back(k2 + 1);
312 lineIndices.push_back(k1 + 1);
321 InstanceData(glm::vec3 mSize, TazPosition mBodyCenter, TazRotation mRotation) :
323 position(mBodyCenter),
328 InstanceData(glm::vec2 mSize, TazPosition mBodyCenter, TazRotation mRotation) :
329 size(glm::vec3(mSize, 0.0f)),
330 position(mBodyCenter),
337 TazSize size = glm::vec3(0.0f);
338 TazPosition position = glm::vec3(0.0f);
339 TazRotation rotation = glm::vec3(0.0f);
359 TextureInstanceData(glm::vec3 mSize, TazPosition mBodyCenter, TazRotation mRotation, GLuint Texture) :
InstanceData(mSize, mBodyCenter, mRotation), texture(Texture) {
361 TextureInstanceData(glm::vec2 mSize, TazPosition mBodyCenter, TazRotation mRotation, GLuint Texture) :
InstanceData(mSize, mBodyCenter, mRotation), texture(Texture) {
367 TazUV uv = glm::vec2(0.0f);
376 fromColor(mfromcolor),
384 TazPosition fromPos = glm::vec3(0.0f);
385 TazPosition toPos = glm::vec3(0.0f);
411 size_t meshIndices = 0;
422 std::string shaderName;
423 std::string batchName;
425 std::vector<InstanceData> instances;
429 std::string shaderName;
430 std::string batchName;
432 std::vector<ColorInstanceData> instances;
436 std::string shaderName;
437 std::string batchName;
439 std::vector<LineInstanceData> instances;
443 std::string shaderName;
444 std::string batchName;
446 std::vector<WireframeInstanceData> instances;
450 std::string shaderName;
451 std::string batchName;
453 std::vector<TextureInstanceData> instances;
463 std::vector< ColorBatch> batches;
467 std::vector< LineBatch> batches;
471 std::vector< WireFrameBatch> batches;
475 std::vector< TextureBatch> batches;
481 GLSLProgram() : _programID(0), _vertexShaderID(0), _geometryShaderID(0), _fragmentShaderID(0), _numAttributes(0)
491 void compileAndLinkShaders(
const std::string& vertexShaderFilePath,
const std::string& fragmentShaderFilePath) {
492 std::string vertSource = readShaderFile(vertexShaderFilePath);
493 std::string fragSource = readShaderFile(fragmentShaderFilePath);
495 compileAndLinkShadersFromSource(vertSource.c_str(), fragSource.c_str());
499 void compileAndLinkShaders(
const std::string& vertexShaderFilePath,
const std::string& geometryShaderFilePath,
const std::string& fragmentShaderFilePath) {
500 std::string vertSource = readShaderFile(vertexShaderFilePath);
501 std::string geomSource = readShaderFile(geometryShaderFilePath);
502 std::string fragSource = readShaderFile(fragmentShaderFilePath);
504 compileAndLinkShadersFromSource(vertSource.c_str(), geomSource.c_str(), fragSource.c_str());
507 void compileAndLinkShadersFromSource(
const char* vertexSource,
const char* fragmentSource) {
509 std::vector<GLuint> shaderIDs;
511 shaderIDs.push_back(createAndCompileShader(vertexSource, GL_VERTEX_SHADER,
"Vertex"));
512 shaderIDs.push_back(createAndCompileShader(fragmentSource, GL_FRAGMENT_SHADER,
"Fragment"));
514 linkShadersInternal(shaderIDs);
517 void compileAndLinkShadersFromSource(
const char* vertexSource,
const char* geometrySource,
const char* fragmentSource) {
519 std::vector<GLuint> shaderIDs;
521 shaderIDs.push_back(createAndCompileShader(vertexSource, GL_VERTEX_SHADER,
"Vertex"));
522 shaderIDs.push_back(createAndCompileShader(geometrySource, GL_GEOMETRY_SHADER,
"Geometry"));
523 shaderIDs.push_back(createAndCompileShader(fragmentSource, GL_FRAGMENT_SHADER,
"Fragment"));
525 linkShadersInternal(shaderIDs);
529 void addAttribute(
const std::string& attributeName) {
530 glBindAttribLocation(_programID, _numAttributes++, attributeName.c_str());
533 GLint getUniformLocation(
const std::string& uniformName) {
534 GLint location = glGetUniformLocation(_programID, uniformName.c_str());
536 if (location == GL_INVALID_INDEX) {
537 TAZ_ERROR(
"Uniform " + uniformName +
" not found in shader!");
543 glUseProgram(_programID);
544 for (
int i = 0; i < _numAttributes; i++) {
545 glEnableVertexAttribArray(i);
551 for (
int i = 0; i < _numAttributes; i++) {
552 glDisableVertexAttribArray(i);
557 if (_programID != 0) glDeleteProgram(_programID);
560 GLuint getProgramID() {
567 GLuint _vertexShaderID;
568 GLuint _geometryShaderID;
569 GLuint _fragmentShaderID;
573 std::string readShaderFile(
const std::string& filePath) {
574 std::vector<unsigned char> buffer;
575 TextureManager::readFileToBuffer(filePath.c_str(), buffer);
576 return std::string(buffer.begin(), buffer.end());
579 GLuint createAndCompileShader(
const std::string& source, GLenum shaderType,
const std::string& typeName) {
580 GLuint shaderID = glCreateShader(shaderType);
582 TAZ_ERROR(typeName +
" Shader Failed to create!");
586 compileShader(source.c_str(), typeName, shaderID);
590 void compileShader(
const char* source,
const std::string& name, GLuint
id) {
592 glShaderSource(
id, 1, &source,
nullptr);
597 glGetShaderiv(
id, GL_COMPILE_STATUS, &success);
599 if (success == GL_FALSE)
602 glGetShaderiv(
id, GL_INFO_LOG_LENGTH, &maxLength);
605 std::vector<GLchar> errorLog(maxLength);
606 glGetShaderInfoLog(
id, maxLength, &maxLength, &errorLog[0]);
612 std::printf(
"%s\n", &(errorLog[0]));
613 TAZ_ERROR(
"Shader " + name +
" failed to compile");
618 void linkShadersInternal(
const std::vector<GLuint>& shaderIDs) {
619 _programID = glCreateProgram();
622 for (GLuint shaderID : shaderIDs) {
623 glAttachShader(_programID, shaderID);
627 glLinkProgram(_programID);
631 glGetProgramiv(_programID, GL_LINK_STATUS, &isLinked);
633 if (isLinked == GL_FALSE) {
635 glGetProgramiv(_programID, GL_INFO_LOG_LENGTH, &maxLength);
637 std::vector<GLchar> errorLog(maxLength);
638 glGetProgramInfoLog(_programID, maxLength, &maxLength, &errorLog[0]);
641 glDeleteProgram(_programID);
642 for (GLuint shaderID : shaderIDs) {
643 glDeleteShader(shaderID);
646 std::printf(
"%s\n", &errorLog[0]);
647 TAZ_ERROR(
"Shaders failed to link");
652 for (GLuint shaderID : shaderIDs) {
653 glDetachShader(_programID, shaderID);
654 glDeleteShader(shaderID);
Definition GLSLProgram.h:479
Definition GLSLProgram.h:410
Definition GLSLProgram.h:428
Definition GLSLProgram.h:343
Definition GLSLProgram.h:462
Definition GLSLProgram.h:318
Definition GLSLProgram.h:435
Definition GLSLProgram.h:370
Definition GLSLProgram.h:466
Meshes.
Definition GLSLProgram.h:457
std::vector< SimpleBatch > batches
Batches.
Definition GLSLProgram.h:459
Batches.
Definition GLSLProgram.h:421
Definition GLSLProgram.h:449
Definition GLSLProgram.h:356
Definition GLSLProgram.h:474
Definition GLSLProgram.h:442
Definition GLSLProgram.h:392
Definition GLSLProgram.h:470