engineer-thesis-WUT/Engine/engine/draw.cpp

303 lines
12 KiB
C++
Raw Normal View History

2023-03-12 16:24:48 +01:00
// "Copyright [2023] <Krzysztof Rudnicki>"
2022-09-05 20:17:25 +02:00
#ifndef DRAW_CPP
#define DRAW_CPP
#include "./draw.hpp"
#include <glad/glad.h>
2023-03-14 02:17:42 +01:00
#include <GLFW/glfw3.h>
2023-03-29 14:46:58 +02:00
#include <functional>
#include <cmath>
2023-03-11 17:26:16 +01:00
#include <filesystem>
#include <iostream>
2023-03-12 17:32:42 +01:00
#include <utility>
2023-03-19 16:59:13 +01:00
#include <vector>
2023-03-19 17:50:43 +01:00
#include <cstring>
2023-03-29 14:46:58 +02:00
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
2023-03-12 17:32:42 +01:00
#include "./constants.hpp"
#include "./misc.hpp"
#include "./renderLoop.hpp"
2023-03-12 17:32:42 +01:00
#include "./shader.hpp"
#include "./shaders.hpp"
2023-03-12 17:32:42 +01:00
#include "./stb_image.h"
#include "./textures.hpp"
2023-03-12 17:32:42 +01:00
drawFigureReturn drawFigure(const int whatToDraw) {
switch (whatToDraw) {
2022-09-10 13:40:58 +02:00
case 0:
2023-03-19 13:53:13 +01:00
return draw(constants::VERTEX_SHADER_SOURCE_FILENAME,
2023-03-19 16:59:13 +01:00
constants::FRAGMENT_SHADER_SOURCE_FILENAME,
constants::TRIANGLE_VERTICES,
constants::TRIANGLE_VERTICES_SIZE);
2022-09-10 13:40:58 +02:00
case 1:
2023-03-19 13:53:13 +01:00
return draw(
constants::VERTEX_SHADER_SOURCE_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_FILENAME,
constants::SQUARE_VERTICES, constants::SQUARE_VERTICES_SIZE,
constants::SQUARE_INDICES, constants::SQUARE_INDICES_SIZE);
2022-09-17 14:14:08 +02:00
case 2:
// Try to draw 2 triangles next to each other
// using glDrawArrays by adding more vertices to your data.
2023-03-19 13:53:13 +01:00
return draw(constants::VERTEX_SHADER_SOURCE_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_FILENAME,
constants::TRIANGLES_VERTICES,
2023-03-19 13:53:13 +01:00
constants::TRIANGLES_VERTICES_SIZE,
constants::TRIANGLES_INDICES,
constants::TRIANGLES_INDICES_SIZE);
2022-09-17 14:14:08 +02:00
case 3:
2023-03-19 13:53:13 +01:00
draw(constants::VERTEX_SHADER_SOURCE_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_FILENAME,
constants::TRIANGLE_ONE, constants::TRIANGLE_ONE_SIZE);
// Now create the same 2 triangles
// using two different VAOs and VBOs for their data
2023-03-19 13:53:13 +01:00
return draw(constants::VERTEX_SHADER_SOURCE_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_FILENAME,
constants::TRIANGLE_TWO,
constants::TRIANGLE_TWO_SIZE);
case 4:
2023-03-19 13:53:13 +01:00
draw(constants::VERTEX_SHADER_SOURCE_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_FILENAME,
constants::TRIANGLE_ONE, constants::TRIANGLE_ONE_SIZE);
/*
Create two shader programs where
the second program uses a different
fragment shader that outputs the color yellow;
draw both triangles again where one outputs the color yellow
*/
2023-03-19 13:53:13 +01:00
return draw(
2023-03-12 17:32:42 +01:00
constants::VERTEX_SHADER_SOURCE_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_YELLOW_FILENAME,
constants::TRIANGLE_TWO, constants::TRIANGLE_TWO_SIZE);
case 5:
// Get color from vertex shader to fragment shader
2023-03-19 13:53:13 +01:00
return draw(
2023-03-12 17:32:42 +01:00
constants::VERTEX_SHADER_COLOR_FILENAME,
constants::FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME,
constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE);
case 6:
2023-03-19 17:50:43 +01:00
{
offsetsStruct offsets = offsetsStruct();
const std::vector<std::string> uniformInfo = {"ourColor"};
// set color from opengl code to uniform value in fragment shader
2023-03-19 13:53:13 +01:00
return draw(constants::VERTEX_SHADER_COLOR_FILENAME,
2023-03-19 17:50:43 +01:00
constants::FRAGMENT_SHADER_UNIFORMS_FILENAME,
constants::TRIANGLE_VERTICES,
constants::TRIANGLE_VERTICES_SIZE,
constants::TRIANGLE_INDICES,
constants::TRIANGLE_INDICES_SIZE,
false, false, {}, offsets, uniformInfo);
}
2022-11-20 23:14:59 +01:00
case 7:
// set color from opengl code to uniform value in fragment shader
2023-03-19 13:53:13 +01:00
return draw(
constants::VERTEX_SHADER_VERTICE_COLOR_FILENAME,
constants::FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME,
2023-03-19 13:53:13 +01:00
constants::TRIANGLE_COLORS, constants::TRIANGLE_COLORS_SIZE,
constants::TRIANGLE_INDICES,
constants::TRIANGLE_INDICES_SIZE, true);
2023-01-27 21:29:04 +01:00
case 8:
// upside down triangle
2023-03-19 13:53:13 +01:00
return draw(constants::VERTEX_SHADER_UPSIDE_DOWN_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_FILENAME,
constants::TRIANGLE_VERTICES,
constants::TRIANGLE_VERTICES_SIZE);
case 9: {
// offset triangle
offsetsStruct offsets = offsetsStruct();
offsets.xOffset = 0.5f;
2023-03-19 13:53:13 +01:00
return draw(constants::VERTEX_SHADER_OFFSET_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_FILENAME,
constants::TRIANGLE_VERTICES,
2023-03-19 13:53:13 +01:00
constants::TRIANGLE_VERTICES_SIZE,
constants::TRIANGLE_INDICES,
constants::TRIANGLE_INDICES_SIZE,
2023-03-19 16:59:13 +01:00
false, false, {}, offsets);
2023-01-27 21:29:04 +01:00
}
2023-03-05 20:02:41 +01:00
case 10:
2023-03-19 13:53:13 +01:00
return draw(constants::VERTEX_SHADER_TASK_THREE_FILENAME,
constants::FRAGMENT_SHADER_TASK_THREE_FILENAME,
constants::TRIANGLE_VERTICES,
constants::TRIANGLE_VERTICES_SIZE);
2023-03-19 16:59:13 +01:00
case 11: {
std::vector<textureArgument> textureInfoArray = {
texture_constants::CONTAINER_ARGUMENT,
texture_constants::AWESOME_FACE_ARGUMENT
};
2023-03-19 13:53:13 +01:00
return draw(
constants::VERTEX_SHADER_TEXTURE_FILENAME,
constants::FRAGMENT_SHADER_TEXTURE_FILENAME,
constants::TEXTURE_VERTICES, constants::TEXTURE_VERTICES_SIZE,
2023-03-19 13:53:13 +01:00
constants::TEXTURE_INDICES,
constants::TEXTURE_INDICES_SIZE,
2023-03-19 16:59:13 +01:00
true, true, textureInfoArray);
}
case 12: {
std::vector<textureArgument> textureInfoArray = {
texture_constants::CONTAINER_ARGUMENT,
texture_constants::AWESOME_FACE_ARGUMENT
};
return draw(
constants::VERTEX_SHADER_TEXTURE_FILENAME,
constants::FRAGMENT_SHADER_TEXTURE_TASK_ONE_FILENAME,
constants::TEXTURE_VERTICES, constants::TEXTURE_VERTICES_SIZE,
constants::TEXTURE_INDICES,
constants::TEXTURE_INDICES_SIZE,
true, true, textureInfoArray);
}
2023-03-19 17:50:43 +01:00
case 13: {
2023-03-19 16:59:13 +01:00
std::vector<textureArgument> textureInfoArray = {
texture_constants::CONTAINER_ARGUMENT,
texture_constants::AWESOME_FACE_ARGUMENT
};
return draw(
constants::VERTEX_SHADER_TEXTURE_FILENAME,
constants::FRAGMENT_SHADER_TEXTURE_FILENAME,
constants::TEXTURE_VERTICES_2F, constants::TEXTURE_VERTICES_2F_SIZE,
constants::TEXTURE_INDICES,
constants::TEXTURE_INDICES_SIZE,
true, true, textureInfoArray);
2023-03-19 13:53:13 +01:00
}
2023-03-19 17:50:43 +01:00
case 14: {
std::vector<textureArgument> textureInfoArray = {
texture_constants::TEXTURE_TASK_THREE_CONTAINER_ARGUMENT,
texture_constants::TEXTURE_TASK_THREE_FACE_ARGUMENT
};
return draw(
constants::VERTEX_SHADER_TEXTURE_FILENAME,
constants::FRAGMENT_SHADER_TEXTURE_FILENAME,
constants::TEXTURE_VERTICES_TASK_THREE,
constants::TEXTURE_VERTICES_TASK_THREE_SIZE,
constants::TEXTURE_INDICES,
constants::TEXTURE_INDICES_SIZE,
true, true, textureInfoArray);
}
case constants::MAX_DRAW_CALL: {
std::vector<textureArgument> textureInfoArray = {
texture_constants::CONTAINER_ARGUMENT,
texture_constants::AWESOME_FACE_ARGUMENT
};
offsetsStruct offsets = offsetsStruct();
2023-03-29 14:46:58 +02:00
const std::vector<std::string> uniformInfo = {"mixProportions"};
2023-03-19 17:50:43 +01:00
return draw(
constants::VERTEX_SHADER_TEXTURE_FILENAME,
constants::FRAGMENT_SHADER_TEXTURE_UNIFORM_FILENAME,
constants::TEXTURE_VERTICES,
constants::TEXTURE_VERTICES_SIZE,
constants::TEXTURE_INDICES,
constants::TEXTURE_INDICES_SIZE,
true, true, textureInfoArray,
2023-03-29 14:46:58 +02:00
offsets, uniformInfo);
2023-03-19 17:50:43 +01:00
}
2022-09-10 13:40:58 +02:00
default:
throw "No function for this draw call";
}
2022-09-05 20:17:25 +02:00
}
unsigned int getShaderProgram(const char* vertexShaderSource,
const char* fragmentShaderSource) {
const std::pair<unsigned int, unsigned int> shaders =
compileShaders(vertexShaderSource, fragmentShaderSource);
if (shaders.first == 0 || shaders.second == 0) return 0;
2022-12-11 18:49:16 +01:00
const unsigned int shaderProgram =
linkShaderObjectsShaderProgram(shaders.first, shaders.second);
2023-03-12 17:32:42 +01:00
if (shaderProgram == 0) return 0;
return shaderProgram;
2022-12-11 18:49:16 +01:00
}
2023-01-27 21:29:04 +01:00
void setOffsets(Shader shader, const offsetsStruct offsets) {
shader.setFloat("xOffset", offsets.xOffset);
shader.setFloat("yOffset", offsets.yOffset);
shader.setFloat("zOffset", offsets.zOffset);
2023-01-27 21:29:04 +01:00
}
2023-03-19 13:53:13 +01:00
drawFigureReturn draw(const char* vertexPath, const char* fragmentPath,
2023-03-19 17:50:43 +01:00
const float vertices[],
const size_t verticesSize,
const unsigned int indices[],
const size_t indicesSize,
const bool colorIncluded,
const bool textureIncluded,
std::vector<textureArgument> textureInfo,
const offsetsStruct offsets,
const std::vector<std::string> uniformName) {
// https://stackoverflow.com/questions/33883609/opengl-linker-error-linking-with-uncompiled-shader
2023-03-19 13:53:13 +01:00
Shader ourShader(vertexPath,
fragmentPath);
2023-03-19 16:59:13 +01:00
ourShader.use();
2023-03-19 13:53:13 +01:00
setOffsets(ourShader, offsets);
2023-03-11 17:26:16 +01:00
unsigned int VBO =
2023-03-19 13:53:13 +01:00
copyVerticesMemory(vertices, verticesSize, GL_ARRAY_BUFFER);
unsigned int VAO = generateBindVAO();
copyVerticesArray(VBO, vertices, verticesSize,
GL_ARRAY_BUFFER);
unsigned int EBO =
copyVerticesMemory(indices, indicesSize, GL_ELEMENT_ARRAY_BUFFER);
2023-03-11 17:26:16 +01:00
2023-03-19 13:53:13 +01:00
configureVertexAttribute(colorIncluded, textureIncluded);
2023-03-19 17:50:43 +01:00
if (uniformName.size() > 0) {
2023-03-29 14:46:58 +02:00
if(uniformName.at(0) == "ourColor") {
updateUniformColor(ourShader, uniformName.at(0));
}
if(uniformName.at(0) == "mixProportions") {
srand (time(NULL));
const float randomNumber = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
ourShader.setFloat(uniformName.at(0), randomNumber);
}
2023-03-19 17:50:43 +01:00
}
2023-03-11 17:26:16 +01:00
2023-03-19 16:59:13 +01:00
drawTextureArray(textureInfo, ourShader);
// unsigned int texture1 = loadAndCreateTexture(
// textureInfo.texturePath,
// textureInfo.flipImage,
// textureInfo.wrappingMethod);
// unsigned int texture2 = loadAndCreateTexture(
// "assets/awesomeface.png", false, GL_REPEAT);
2023-03-11 17:26:16 +01:00
2023-03-19 16:59:13 +01:00
// ourShader.setInt("texture1", 0);
// ourShader.setInt("texture2", 1);
2023-03-19 16:59:13 +01:00
// don't forget to activate/use
// the shader before setting uniforms!
// set it via the texture class
// (texture1, GL_TEXTURE0);
// activateAndBindTextures(texture2, GL_TEXTURE1);
2023-03-12 14:46:56 +01:00
2023-03-19 13:53:13 +01:00
glDrawElements(GL_TRIANGLES, indicesSize, GL_UNSIGNED_INT, 0);
2023-03-19 16:59:13 +01:00
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
drawFigureReturn newReturn;
newReturn.success = 0;
newReturn.VAO = VAO;
newReturn.VBO = VBO;
newReturn.EBO = EBO;
return newReturn;
2023-03-11 16:58:17 +01:00
}
2023-03-29 14:46:58 +02:00
void updateUniformColor(const Shader shaderProgram,
2023-03-19 17:50:43 +01:00
std::string uniformName) {
// update the uniform color
const float timeValue = glfwGetTime(); // retrieve running time
const float greenValue = sin(timeValue) / 2.0f + 0.5f;
// vary the color from 0.0 to 1.0 using sin
// query the location of our uniform
2023-03-29 14:46:58 +02:00
shaderProgram.set4Float(uniformName, 0.0f, greenValue, 0.0f, 1.0f);
// if glGetUniformLocation returns -1 it could not find the location
/* we set the uniform value using glUniform4f
4f means that it expects 4 floats
few of possible postfixes:
f: the function expects a float as its value.
i: the function expects an int as its value.
ui: the function expects an unsigned int as its value.
3f: the function expects 3 floats as its value.
fv: the function expects a float vector/array as its value.
*/
}
2023-03-12 17:32:42 +01:00
#endif // #ifndef DRAW_CPP