feat: make texture code prettier

This commit is contained in:
Krzysztof Rudnicki 2023-03-11 19:08:15 +01:00
parent 2ec9ba6aef
commit 4d77d24c4f
7 changed files with 143 additions and 132 deletions

View File

@ -193,13 +193,7 @@ namespace constants
inline constexpr int MAX_DRAW_CALL = { 11 };
// https://learnopengl.com/img/getting-started/tex_coords.png
inline constexpr float TEXTURE_COORDINATES[] {
0.0f, 0.0f, // lower-left corner
1.0f, 0.0f, // lower-right corner
0.5f, 1.0f // top-center corner
};
inline constexpr float POSITION_COLOR_TEXTURE[] {
inline constexpr float TEXTURE_VERTICES[] {
// positions // colors // texture coords
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
@ -207,7 +201,16 @@ namespace constants
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
};
inline constexpr size_t POSITION_COLOR_TEXTURE_SIZE = { sizeof(POSITION_COLOR_TEXTURE) };
inline constexpr size_t TEXTURE_VERTICES_SIZE = { sizeof(TEXTURE_VERTICES) };
inline constexpr unsigned int TEXTURE_INDICES[] {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
inline constexpr size_t TEXTURE_INDICES_SIZE = { sizeof(TEXTURE_INDICES) };
}

View File

@ -14,7 +14,7 @@
#include "textures.hpp"
#include "stb_image.h"
int drawFigure(const int whatToDraw)
drawFigureReturn drawFigure(const int whatToDraw)
{
switch (whatToDraw)
{
@ -26,11 +26,13 @@ int drawFigure(const int whatToDraw)
// Try to draw 2 triangles next to each other using glDrawArrays by adding more vertices to your data.
return drawTriangleClass(constants::TRIANGLES_VERTICES, constants::TRIANGLES_VERTICES_SIZE, constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME);
case 3:
drawTriangleClass(constants::TRIANGLE_ONE, constants::TRIANGLE_ONE_SIZE, constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME);
// Now create the same 2 triangles using two different VAOs and VBOs for their data
return (drawTriangleClass(constants::TRIANGLE_ONE, constants::TRIANGLE_ONE_SIZE, constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME) == -1 || drawTriangleClass(constants::TRIANGLE_TWO, constants::TRIANGLE_TWO_SIZE, constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME) == -1);
return drawTriangleClass(constants::TRIANGLE_TWO, constants::TRIANGLE_TWO_SIZE, constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME);
case 4:
drawTriangleClass(constants::TRIANGLE_ONE, constants::TRIANGLE_ONE_SIZE, constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME);
// 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
return (drawTriangleClass(constants::TRIANGLE_ONE, constants::TRIANGLE_ONE_SIZE, constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME) == -1 || drawTriangleClass(constants::TRIANGLE_TWO, constants::TRIANGLE_TWO_SIZE, constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_YELLOW_FILENAME) == -1);
return drawTriangleClass(constants::TRIANGLE_TWO, constants::TRIANGLE_TWO_SIZE, constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_YELLOW_FILENAME);
case 5:
// Get color from vertex shader to fragment shader
return drawTriangleClass(constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, constants::VERTEX_SHADER_COLOR_FILENAME, constants::FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME);
@ -53,7 +55,7 @@ int drawFigure(const int whatToDraw)
case 10:
return drawTriangleClass(constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, constants::VERTEX_SHADER_TASK_THREE_FILENAME, constants::FRAGMENT_SHADER_TASK_THREE_FILENAME);
case constants::MAX_DRAW_CALL:
return drawDebilMode();
return drawDebilMode(constants::VERTEX_SHADER_TEXTURE_FILENAME, constants::FRAGMENT_SHADER_TEXTURE_FILENAME, constants::TEXTURE_VERTICES, constants::TEXTURE_VERTICES_SIZE, constants::TEXTURE_INDICES, constants::TEXTURE_INDICES_SIZE);
default:
throw "No function for this draw call";
}
@ -77,7 +79,7 @@ void setOffsets(Shader shader, const offsetsStruct offsets) {
shader.setFloat("zOffset", offsets.zOffset);
}
int drawTriangleClass(const float triangleVertices[], const size_t triangleVerticesSize, const char* vertexPath, const char* fragmentPath, const bool colorIncluded, const bool textureIncluded, const offsetsStruct offsets)
drawFigureReturn drawTriangleClass(const float triangleVertices[], const size_t triangleVerticesSize, const char* vertexPath, const char* fragmentPath, const bool colorIncluded, const bool textureIncluded, const offsetsStruct offsets)
{
// In your CPP file:
// ======================
@ -90,19 +92,26 @@ int drawTriangleClass(const float triangleVertices[], const size_t triangleVerti
const unsigned int vertexBufferObject = copyVerticesMemory(triangleVertices, triangleVerticesSize, GL_ARRAY_BUFFER);
const unsigned int vertexArrayObject = generateBindVAO();
copyVerticesArray(vertexBufferObject, triangleVertices, triangleVerticesSize, GL_ARRAY_BUFFER);
// set vertex attribute pointers
configureVertexAttribute(colorIncluded, textureIncluded);
doDrawArrays(ourShader.ID, vertexArrayObject, GL_TRIANGLES, 0, triangleVerticesSize);
return 0;
drawFigureReturn newReturn;
newReturn.success = 0;
newReturn.VAO = vertexArrayObject;
newReturn.VBO = vertexBufferObject;
newReturn.EBO = 0;
return newReturn;
}
int drawTriangle(const float triangleVertices[], const size_t triangleVerticesSize, const char* vertexShaderSource, const char* fragmentShaderSource, const bool colorIncluded, const bool textureIncluded)
drawFigureReturn drawTriangle(const float triangleVertices[], const size_t triangleVerticesSize, const char* vertexShaderSource, const char* fragmentShaderSource, const bool colorIncluded, const bool textureIncluded)
{
const unsigned int shaderProgram = getShaderProgram(vertexShaderSource, fragmentShaderSource);
if (shaderProgram == 0)
return -1;
drawFigureReturn newReturn;
if (shaderProgram == 0) {
newReturn.success = -1;
return newReturn;
}
const unsigned int vertexBufferObject = copyVerticesMemory(triangleVertices, triangleVerticesSize, GL_ARRAY_BUFFER);
const unsigned int vertexArrayObject = generateBindVAO();
@ -111,14 +120,21 @@ int drawTriangle(const float triangleVertices[], const size_t triangleVerticesSi
// set vertex attribute pointers
configureVertexAttribute(colorIncluded, textureIncluded);
doDrawArrays(shaderProgram, vertexArrayObject, GL_TRIANGLES, 0, triangleVerticesSize);
return 0;
newReturn.success = 0;
newReturn.VAO = vertexArrayObject;
newReturn.VBO = vertexBufferObject;
newReturn.EBO = 0;
return newReturn;
}
int drawSquare(const float squareVertices[], const size_t squareVerticesSize, const unsigned int squareIndices[], const size_t squareIndicesSize, const char* vertexShaderSource, const char* fragmentShaderSource, const bool colorIncluded, const bool textureIncluded, const offsetsStruct offsets)
drawFigureReturn drawSquare(const float squareVertices[], const size_t squareVerticesSize, const unsigned int squareIndices[], const size_t squareIndicesSize, const char* vertexShaderSource, const char* fragmentShaderSource, const bool colorIncluded, const bool textureIncluded, const offsetsStruct offsets)
{
const unsigned int shaderProgram = getShaderProgram(vertexShaderSource, fragmentShaderSource);
if (shaderProgram == 0)
return -1;
drawFigureReturn newReturn;
if (shaderProgram == 0) {
newReturn.success = -1;
return newReturn;
}
const unsigned int VAO = generateBindVAO();
copyVerticesMemory(squareVertices, squareVerticesSize, GL_ARRAY_BUFFER);
copyVerticesMemory(squareIndices, squareIndicesSize, GL_ELEMENT_ARRAY_BUFFER);
@ -127,10 +143,14 @@ int drawSquare(const float squareVertices[], const size_t squareVerticesSize, co
configureVertexAttribute(colorIncluded, textureIncluded);
doDrawElements(shaderProgram, VAO, GL_TRIANGLES, GL_UNSIGNED_INT, squareIndicesSize);
return 0;
newReturn.success = 0;
newReturn.VAO = VAO;
newReturn.VBO = 0;
newReturn.EBO = 0;
return newReturn;
}
int drawSquareClass(const char* vertexPath, const char* fragmentPath, const bool colorIncluded, const bool textureIncluded, const offsetsStruct offsets)
drawFigureReturn drawSquareClass(const char* vertexPath, const char* fragmentPath, const bool colorIncluded, const bool textureIncluded, const offsetsStruct offsets)
{
Shader ourShader(vertexPath, fragmentPath);
ourShader.use();
@ -141,94 +161,37 @@ int drawSquareClass(const char* vertexPath, const char* fragmentPath, const bool
// set vertex attribute pointers
configureVertexAttribute(false, false);
doDrawArrays(ourShader.ID, vertexArrayObject, GL_TRIANGLES, 0, constants::SQUARE_VERTICES_SIZE);
return 0;
drawFigureReturn newReturn;
newReturn.success = 0;
newReturn.VAO = vertexArrayObject;
newReturn.VBO = vertexBufferObject;
newReturn.EBO = 0;
return newReturn;
}
int drawDebilMode() {
drawFigureReturn drawDebilMode(const char* vertexPath, const char* fragmentPath, const float vertices[], const size_t verticesSize, const unsigned int indices[], const size_t indicesSize) {
// https://stackoverflow.com/questions/33883609/opengl-linker-error-linking-with-uncompiled-shader
Shader ourShader(constants::VERTEX_SHADER_TEXTURE_FILENAME, constants::FRAGMENT_SHADER_TEXTURE_FILENAME);
float vertices[] = {
// positions // colors // texture coords
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
};
unsigned int indices[] = {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
unsigned int VAO = generateBindVAO();
unsigned int VBO = copyVerticesMemory(vertices, verticesSize, GL_ARRAY_BUFFER);
unsigned int EBO = copyVerticesMemory(indices, indicesSize, GL_ELEMENT_ARRAY_BUFFER);
unsigned int VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
configureVertexAttribute(true, true);
glBindVertexArray(VAO);
loadAndCreateTexture();
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// color attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// texture coord attribute
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
// load and create a texture
// -------------------------
unsigned int texture1, texture2;
// texture 1
// ---------
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps
int width = 430, height = 288, nrChannels;
stbi_set_flip_vertically_on_load(true); // tell stb_image.h to flip loaded texture's on the y-axis.
// The FileSystem::getPath(...) is part of the GitHub repository so we can find files on any IDE/platform; replace it with your own image path.
unsigned char *data = stbi_load("assets/Preview.png", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
stbi_image_free(data);
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
ourShader.use(); // don't forget to activate/use the shader before setting uniforms!
// or set it via the texture class
// set it via the texture class
ourShader.setInt("texture1", 1);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
return 0;
drawFigureReturn newReturn;
newReturn.success = 0;
newReturn.VAO = VAO;
newReturn.VBO = VBO;
newReturn.EBO = EBO;
return newReturn;
}
void doDrawElements(const unsigned int shaderProgram, const unsigned int vertexArrayObject, const GLenum drawArrayMode, const GLenum drawType, const int numberOfElementsToDraw)

View File

@ -10,16 +10,23 @@ struct offsetsStruct {
float xOffset, yOffset, zOffset;
};
int drawFigure(const int whatToDraw);
int drawSquare(const float squareVertices[] = constants::SQUARE_VERTICES, const size_t squareVerticesSize = constants::SQUARE_VERTICES_SIZE, const unsigned int squareIndices[] = constants::SQUARE_INDICES, const size_t squareIndicesSize = constants::SQUARE_INDICES_SIZE, const char* vertexShaderSource = constants::VERTEX_SHADER_SOURCE, const char* fragmentShaderSource = constants::FRAGMENT_SHADER_SOURCE, const bool colorIncluded = false, const bool textureIncluded = false, const offsetsStruct offsets = offsetsStruct());
int drawSquareClass(const char* vertexPath, const char* fragmentPath, const bool colorIncluded = false, const bool textureIncluded = false, const offsetsStruct offsets = offsetsStruct());
struct drawFigureReturn {
int success;
unsigned int VAO;
unsigned int VBO;
unsigned int EBO;
};
drawFigureReturn drawFigure(const int whatToDraw);
drawFigureReturn drawSquare(const float squareVertices[] = constants::SQUARE_VERTICES, const size_t squareVerticesSize = constants::SQUARE_VERTICES_SIZE, const unsigned int squareIndices[] = constants::SQUARE_INDICES, const size_t squareIndicesSize = constants::SQUARE_INDICES_SIZE, const char* vertexShaderSource = constants::VERTEX_SHADER_SOURCE, const char* fragmentShaderSource = constants::FRAGMENT_SHADER_SOURCE, const bool colorIncluded = false, const bool textureIncluded = false, const offsetsStruct offsets = offsetsStruct());
drawFigureReturn drawSquareClass(const char* vertexPath, const char* fragmentPath, const bool colorIncluded = false, const bool textureIncluded = false, const offsetsStruct offsets = offsetsStruct());
void doDrawElements(const unsigned int shaderProgram, const unsigned int vertexArrayObject, const GLenum drawArrayMode, const GLenum drawType, const int numberOfElementsToDraw);
int drawTriangleClass(const float triangleVertices[], const size_t triangleVerticesSize, const char* vertexPath, const char* fragmentPath, const bool colorIncluded = false, const bool textureIncluded = false, const offsetsStruct offsets = offsetsStruct());
int drawTriangle(const float triangleVertices[], const size_t triangleVerticesSize, const char* vertexShaderSource, const char* fragmentShaderSource, const bool colorIncluded = false, const bool textureIncluded = false);
drawFigureReturn drawTriangleClass(const float triangleVertices[], const size_t triangleVerticesSize, const char* vertexPath, const char* fragmentPath, const bool colorIncluded = false, const bool textureIncluded = false, const offsetsStruct offsets = offsetsStruct());
drawFigureReturn drawTriangle(const float triangleVertices[], const size_t triangleVerticesSize, const char* vertexShaderSource, const char* fragmentShaderSource, const bool colorIncluded = false, const bool textureIncluded = false);
int drawTexture();
void updateUniformColor(const unsigned int shaderProgram, const GLchar* uniformName);
void doDrawArrays(const unsigned int shaderProgram, const unsigned int vertexArrayObject, const GLenum drawArrayMode, const int firstIndex, const unsigned int numberOfIndicesToBeRendered );
unsigned int drawSquareShaderProgram(const char* vertexShaderSource, const char* fragmentShaderSource);
int drawDebilMode();
drawFigureReturn drawDebilMode(const char* vertexPath, const char* fragmentPath, const float vertices[], const size_t verticesSize, const unsigned int indices[], const size_t indicesSize);
#endif

Binary file not shown.

View File

@ -160,8 +160,8 @@ int renderLoopInside(GLFWwindow *window, int whatToDraw)
glClear(GL_COLOR_BUFFER_BIT);
if (drawFigure(whatToDraw) == -1)
drawFigureReturn successAndVao = drawFigure(whatToDraw);
if (successAndVao.success == -1)
{
print("error with drawing!");
glfwSetWindowShouldClose(window, true);
@ -175,6 +175,12 @@ int renderLoopInside(GLFWwindow *window, int whatToDraw)
// glfwPollEvents checks if any event (like mouse/keyboard input was triggered), updates window state and calls functions (which we register via callback methods)
glfwPollEvents();
// std::cout << "successAndVao.VAO" << successAndVao.VAO << "successAndVao.VBO" << successAndVao.VBO << "successAndVao.EBO" << successAndVao.EBO << std::endl;
glDeleteVertexArrays(1, &successAndVao.VAO);
glDeleteBuffers(1, &successAndVao.VBO);
glDeleteBuffers(1, &successAndVao.EBO);
return whatToDraw;
}

View File

@ -81,13 +81,17 @@ unsigned char *loadTexture(const char* texturePath, int textureWidth, int textur
return data;
}
unsigned int generateTexture(const char* texturePath, const int textureWidth, const int textureHeight, const int colorChannels) {
unsigned int generateAndBindTexture(const GLenum textureTarget, const GLsizei numberOfTextures ) {
unsigned int texture;
// first argument is how many textures we want to generate
// second is an array of unsigned int in which we will store id of those textures
glGenTextures(1, &texture);
glGenTextures(numberOfTextures, &texture);
// then we bind texture
glBindTexture(GL_TEXTURE_2D, texture);
glBindTexture(textureTarget, texture);
return texture;
}
unsigned int loadAndBindTextureFile(unsigned int texture, const char* texturePath, const int textureWidth, const int textureHeight, const int colorChannels) {
unsigned char *data = loadTexture(texturePath, textureWidth, textureHeight, colorChannels);
if(data) {
@ -114,4 +118,28 @@ unsigned int generateTexture(const char* texturePath, const int textureWidth, co
}
}
unsigned int generateTexture(const char* texturePath, const int textureWidth, const int textureHeight, const int colorChannels) {
unsigned int texture = generateAndBindTexture(GL_TEXTURE_2D, 1);
texture = loadAndBindTextureFile(texture, texturePath, textureWidth, textureHeight, colorChannels);
}
// const char* texturePath, int textureWidth, int textureHeight, int colorChannels, const GLenum textureTarget, const GLint sCoordinateOption, const GLint tCoordinateOption, const GLint rCoordinateOption, const float* borderColor
unsigned int loadAndCreateTexture() {
// load and create a texture
// -------------------------
unsigned int texture = generateAndBindTexture(GL_TEXTURE_2D, 1);
setTextureParametersINT(GL_TEXTURE_2D, GL_REPEAT, GL_REPEAT);
// set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps
int width = 430, height = 288, nrChannels;
texture = loadAndBindTextureFile(texture, "assets/Preview.png", width, height, nrChannels);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
}
#endif // TEXTURES_CPP

View File

@ -9,7 +9,11 @@ void setTextureSCoordinate(const GLenum textureTarget = GL_TEXTURE_2D, const GLi
void setTextureTCoordinate(const GLenum textureTarget = GL_TEXTURE_2D, const GLint tCoordinateOption = GL_REPEAT, const float* borderColor = NULL);
void setTextureRCoordinate(const GLenum textureTarget = GL_TEXTURE_3D, const GLint rCoordinateOption = GL_REPEAT, const float* borderColor = NULL);
void setTextureFilteringAndMipMap(const GLenum textureTarget = GL_TEXTURE_2D, const GLenum filterType = GL_TEXTURE_MAG_FILTER, const GLint textureFilteringMethod = GL_NEAREST, const GLint mipMapFilteringMethod = GL_NEAREST_MIPMAP_NEAREST);
unsigned int generateAndBindTexture(const GLenum textureTarget);
unsigned int generateTexture(const char* texturePath, const int textureWidth, const int textureHeight, const int colorChannels);
unsigned char* loadTexture(const char* texturePath, int textureWidth, int textureHeight, int colorChannels);
// const char* texturePath, int textureWidth, int textureHeight, int colorChannels, const GLenum textureTarget, const GLint sCoordinateOption, const GLint tCoordinateOption, const GLint rCoordinateOption, const float* borderColor
unsigned int loadAndCreateTexture();
#endif // TEXTURES_HPP