mirror of
https://github.com/kuhyx/engineer-thesis-WUT.git
synced 2026-11-22 15:52:27 +01:00
feat: finish exersises hello triangle
This commit is contained in:
parent
a0a3c20b03
commit
2007ac2f55
@ -36,7 +36,7 @@ namespace constants
|
|||||||
// we write vertex shader
|
// we write vertex shader
|
||||||
// version of glsl (since ogl 3.3 same as ogl so we pick 330)
|
// version of glsl (since ogl 3.3 same as ogl so we pick 330)
|
||||||
// in this shader we just forward input data to shader output
|
// in this shader we just forward input data to shader output
|
||||||
inline const char *vertexShaderSource { "#version 330 core\n"
|
inline const char *VERTEX_SHADER_SOURCE { "#version 330 core\n"
|
||||||
"layout (location = 0) in vec3 aPos;\n"
|
"layout (location = 0) in vec3 aPos;\n"
|
||||||
"void main()\n"
|
"void main()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
@ -45,7 +45,7 @@ namespace constants
|
|||||||
|
|
||||||
// write fragment shader
|
// write fragment shader
|
||||||
// we set the color of each pixel to be orange
|
// we set the color of each pixel to be orange
|
||||||
inline const char *fragmentShaderSource {
|
inline const char *FRAGMENT_SHADER_SOURCE {
|
||||||
"#version 330 core\n"
|
"#version 330 core\n"
|
||||||
"out vec4 FragColor;\n"
|
"out vec4 FragColor;\n"
|
||||||
"void main()\n"
|
"void main()\n"
|
||||||
@ -54,6 +54,15 @@ namespace constants
|
|||||||
"}\0"
|
"}\0"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
inline const char *FRAGMENT_SHADER_SOURCE_YELLOW {
|
||||||
|
"#version 330 core\n"
|
||||||
|
"out vec4 FragColor;\n"
|
||||||
|
"void main()\n"
|
||||||
|
"{\n"
|
||||||
|
"FragColor = vec4(1.0f, 1.0f, 0.0f, 1.0f);\n"
|
||||||
|
"}\0"
|
||||||
|
};
|
||||||
|
|
||||||
// we specify three vertices
|
// we specify three vertices
|
||||||
// each of them with position in 3d space
|
// each of them with position in 3d space
|
||||||
// x y z
|
// x y z
|
||||||
@ -65,6 +74,38 @@ namespace constants
|
|||||||
|
|
||||||
inline constexpr size_t TRIANGLE_VERTICES_SIZE = { sizeof(TRIANGLE_VERTICES) };
|
inline constexpr size_t TRIANGLE_VERTICES_SIZE = { sizeof(TRIANGLE_VERTICES) };
|
||||||
|
|
||||||
|
inline constexpr float TRIANGLES_VERTICES[] {
|
||||||
|
// first triangle
|
||||||
|
-0.9f, -0.5f, 0.0f, // left
|
||||||
|
-0.0f, -0.5f, 0.0f, // right
|
||||||
|
-0.45f, 0.5f, 0.0f, // top
|
||||||
|
// second triangle
|
||||||
|
0.0f, -0.5f, 0.0f, // left
|
||||||
|
0.9f, -0.5f, 0.0f, // right
|
||||||
|
0.45f, 0.5f, 0.0f // top
|
||||||
|
};
|
||||||
|
|
||||||
|
inline constexpr size_t TRIANGLES_VERTICES_SIZE = { sizeof(TRIANGLES_VERTICES) };
|
||||||
|
|
||||||
|
inline constexpr float TRIANGLE_ONE[] {
|
||||||
|
// first triangle
|
||||||
|
-0.9f, -0.5f, 0.0f, // left
|
||||||
|
-0.0f, -0.5f, 0.0f, // right
|
||||||
|
-0.45f, 0.5f, 0.0f, // top
|
||||||
|
};
|
||||||
|
|
||||||
|
inline constexpr size_t TRIANGLE_ONE_SIZE = { sizeof(TRIANGLE_ONE) };
|
||||||
|
|
||||||
|
inline constexpr float TRIANGLE_TWO[] {
|
||||||
|
// second triangle
|
||||||
|
0.0f, -0.5f, 0.0f, // left
|
||||||
|
0.9f, -0.5f, 0.0f, // right
|
||||||
|
0.45f, 0.5f, 0.0f // top
|
||||||
|
};
|
||||||
|
|
||||||
|
inline constexpr size_t TRIANGLE_TWO_SIZE = { sizeof(TRIANGLE_TWO) };
|
||||||
|
|
||||||
|
|
||||||
// compare with square done with only vertices:
|
// compare with square done with only vertices:
|
||||||
/*
|
/*
|
||||||
float vertices[] = {
|
float vertices[] = {
|
||||||
@ -95,7 +136,7 @@ namespace constants
|
|||||||
|
|
||||||
inline constexpr size_t SQUARE_VERTICES_SIZE = { sizeof(SQUARE_VERTICES) };
|
inline constexpr size_t SQUARE_VERTICES_SIZE = { sizeof(SQUARE_VERTICES) };
|
||||||
|
|
||||||
inline constexpr int MAX_DRAW_CALL = { 2 };
|
inline constexpr int MAX_DRAW_CALL = { 4 };
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -14,19 +14,26 @@ int drawFigure(const int whatToDraw)
|
|||||||
switch (whatToDraw)
|
switch (whatToDraw)
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
return drawTriangle();
|
return drawTriangle(constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, constants::VERTEX_SHADER_SOURCE, constants::FRAGMENT_SHADER_SOURCE);
|
||||||
case 1:
|
case 1:
|
||||||
return drawSquare();
|
return drawSquare(constants::VERTEX_SHADER_SOURCE, constants::FRAGMENT_SHADER_SOURCE);
|
||||||
|
case 2:
|
||||||
|
// Try to draw 2 triangles next to each other using glDrawArrays by adding more vertices to your data.
|
||||||
|
return drawTriangle(constants::TRIANGLES_VERTICES, constants::TRIANGLES_VERTICES_SIZE, constants::VERTEX_SHADER_SOURCE, constants::FRAGMENT_SHADER_SOURCE);
|
||||||
|
case 3:
|
||||||
|
// Now create the same 2 triangles using two different VAOs and VBOs for their data
|
||||||
|
return (drawTriangle(constants::TRIANGLE_ONE, constants::TRIANGLE_ONE_SIZE, constants::VERTEX_SHADER_SOURCE, constants::FRAGMENT_SHADER_SOURCE) == -1 || drawTriangle(constants::TRIANGLE_TWO, constants::TRIANGLE_TWO_SIZE, constants::VERTEX_SHADER_SOURCE, constants::FRAGMENT_SHADER_SOURCE) == -1);
|
||||||
case constants::MAX_DRAW_CALL:
|
case constants::MAX_DRAW_CALL:
|
||||||
return drawTriangle();
|
// 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 (drawTriangle(constants::TRIANGLE_ONE, constants::TRIANGLE_ONE_SIZE, constants::VERTEX_SHADER_SOURCE, constants::FRAGMENT_SHADER_SOURCE) == -1 || drawTriangle(constants::TRIANGLE_TWO, constants::TRIANGLE_TWO_SIZE, constants::VERTEX_SHADER_SOURCE, constants::FRAGMENT_SHADER_SOURCE_YELLOW) == -1);
|
||||||
default:
|
default:
|
||||||
throw "No function for this draw call";
|
throw "No function for this draw call";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int drawSquare()
|
int drawSquare(const char* vertexShaderSource, const char* fragmentShaderSource)
|
||||||
{
|
{
|
||||||
const std::pair<unsigned int, unsigned int> shaders = compileShaders();
|
const std::pair<unsigned int, unsigned int> shaders = compileShaders(vertexShaderSource, fragmentShaderSource);
|
||||||
if (shaders.first == 0 || shaders.second == 0)
|
if (shaders.first == 0 || shaders.second == 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
@ -54,11 +61,11 @@ void doDrawElements(const unsigned int shaderProgram, const unsigned int vertexA
|
|||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int drawTriangle()
|
int drawTriangle(const float triangleVertices[], const size_t triangleVerticesSize, const char* vertexShaderSource, const char* fragmentShaderSource)
|
||||||
{
|
{
|
||||||
const unsigned int vertexBufferObject = copyVerticesMemory(constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, GL_ARRAY_BUFFER);
|
const unsigned int vertexBufferObject = copyVerticesMemory(triangleVertices, triangleVerticesSize, GL_ARRAY_BUFFER);
|
||||||
|
|
||||||
const std::pair<unsigned int, unsigned int> shaders = compileShaders();
|
const std::pair<unsigned int, unsigned int> shaders = compileShaders(vertexShaderSource, fragmentShaderSource);
|
||||||
if (shaders.first == 0 || shaders.second == 0)
|
if (shaders.first == 0 || shaders.second == 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
@ -68,12 +75,12 @@ int drawTriangle()
|
|||||||
|
|
||||||
configureVertexAttribute();
|
configureVertexAttribute();
|
||||||
const unsigned int vertexArrayObject = generateBindVAO();
|
const unsigned int vertexArrayObject = generateBindVAO();
|
||||||
copyVerticesArray(vertexBufferObject, constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, GL_ARRAY_BUFFER);
|
copyVerticesArray(vertexBufferObject, triangleVertices, triangleVerticesSize, GL_ARRAY_BUFFER);
|
||||||
|
|
||||||
// set vertex attribute pointers
|
// set vertex attribute pointers
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
doDrawArrays(shaderProgram, vertexArrayObject, GL_TRIANGLES, 0, 3);
|
doDrawArrays(shaderProgram, vertexArrayObject, GL_TRIANGLES, 0, triangleVerticesSize);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -4,9 +4,9 @@
|
|||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
int drawFigure(const int whatToDraw);
|
int drawFigure(const int whatToDraw);
|
||||||
int drawSquare();
|
int drawSquare(const char* vertexShaderSource, const char* fragmentShaderSource);
|
||||||
void doDrawElements(const unsigned int shaderProgram, const unsigned int vertexArrayObject, const GLenum drawArrayMode, const GLenum drawType, const int numberOfElementsToDraw);
|
void doDrawElements(const unsigned int shaderProgram, const unsigned int vertexArrayObject, const GLenum drawArrayMode, const GLenum drawType, const int numberOfElementsToDraw);
|
||||||
int drawTriangle();
|
int drawTriangle(const float triangleVertices[], const size_t triangleVerticesSize, const char* vertexShaderSource, const char* fragmentShaderSource);
|
||||||
void doDrawArrays(const unsigned int shaderProgram, const unsigned int vertexArrayObject, const GLenum drawArrayMode, const int firstIndex, const unsigned int numberOfIndicesToBeRendered );
|
void doDrawArrays(const unsigned int shaderProgram, const unsigned int vertexArrayObject, const GLenum drawArrayMode, const int firstIndex, const unsigned int numberOfIndicesToBeRendered );
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Binary file not shown.
@ -9,16 +9,24 @@
|
|||||||
#include "constants.hpp"
|
#include "constants.hpp"
|
||||||
#include "misc.hpp"
|
#include "misc.hpp"
|
||||||
|
|
||||||
bool processInput(GLFWwindow *window, const int whatToDraw)
|
int processInput(GLFWwindow *window, const int whatToDraw)
|
||||||
{
|
{
|
||||||
|
static bool locked = false;
|
||||||
|
const bool PRESSED_CHANGE_DRAW = (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS || glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS);
|
||||||
|
|
||||||
// glfwGetKey takes window and key as an input and checks is currently being pressed
|
// glfwGetKey takes window and key as an input and checks is currently being pressed
|
||||||
// if the user pressed escape we close window
|
// if the user pressed escape we close window
|
||||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||||
glfwSetWindowShouldClose(window, true);
|
glfwSetWindowShouldClose(window, true);
|
||||||
if (glfwGetKey(window, GLFW_KEY_C) == GLFW_PRESS)
|
if (glfwGetKey(window, GLFW_KEY_C) == GLFW_PRESS)
|
||||||
glfwSetWindowShouldClose(window, true);
|
glfwSetWindowShouldClose(window, true);
|
||||||
if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS || glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS)
|
if ( !PRESSED_CHANGE_DRAW )
|
||||||
|
locked = 0;
|
||||||
|
if ( PRESSED_CHANGE_DRAW && locked == 0 )
|
||||||
|
{
|
||||||
|
locked = 1;
|
||||||
return (whatToDraw == constants::MAX_DRAW_CALL ? 0 : whatToDraw + 1);
|
return (whatToDraw == constants::MAX_DRAW_CALL ? 0 : whatToDraw + 1);
|
||||||
|
}
|
||||||
return whatToDraw;
|
return whatToDraw;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,7 +104,7 @@ void copyVerticesArray(const unsigned int vertexBufferObject, const float vertic
|
|||||||
glBufferData(boundBufferTarget, sizeOfVertices, vertices, GL_STATIC_DRAW);
|
glBufferData(boundBufferTarget, sizeOfVertices, vertices, GL_STATIC_DRAW);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool renderLoopInside(GLFWwindow *window, int whatToDraw)
|
int renderLoopInside(GLFWwindow *window, int whatToDraw)
|
||||||
{
|
{
|
||||||
// input
|
// input
|
||||||
whatToDraw = processInput(window, whatToDraw);
|
whatToDraw = processInput(window, whatToDraw);
|
||||||
@ -119,6 +127,7 @@ bool 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 checks if any event (like mouse/keyboard input was triggered), updates window state and calls functions (which we register via callback methods)
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
|
||||||
return whatToDraw;
|
return whatToDraw;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -4,11 +4,11 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
void renderLoop(GLFWwindow *window);
|
void renderLoop(GLFWwindow *window);
|
||||||
bool renderLoopInside(GLFWwindow *window, int whatToDraw);
|
int renderLoopInside(GLFWwindow *window, int whatToDraw);
|
||||||
void copyVerticesArray(unsigned int vertexBufferObject, const float vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget);
|
void copyVerticesArray(unsigned int vertexBufferObject, const float vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget);
|
||||||
unsigned int generateBindVAO();
|
unsigned int generateBindVAO();
|
||||||
void configureVertexAttribute();
|
void configureVertexAttribute();
|
||||||
bool processInput(GLFWwindow *window, int whatToDraw);
|
int processInput(GLFWwindow *window, int whatToDraw);
|
||||||
|
|
||||||
unsigned int copyVerticesMemory(const float vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget);
|
unsigned int copyVerticesMemory(const float vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget);
|
||||||
unsigned int copyVerticesMemory(const unsigned int vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget);
|
unsigned int copyVerticesMemory(const unsigned int vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget);
|
||||||
|
|||||||
@ -49,16 +49,16 @@ unsigned int compileShader(const GLenum shaderType, const char *shaderSource)
|
|||||||
return shaderID;
|
return shaderID;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<unsigned int, unsigned int> compileShaders()
|
std::pair<unsigned int, unsigned int> compileShaders(const char* vertexShaderSource, const char* fragmentShaderSource)
|
||||||
{
|
{
|
||||||
const unsigned int vertexShader = compileShader(GL_VERTEX_SHADER, constants::vertexShaderSource);
|
const unsigned int vertexShader = compileShader(GL_VERTEX_SHADER, vertexShaderSource);
|
||||||
if (vertexShader == 0)
|
if (vertexShader == 0)
|
||||||
{
|
{
|
||||||
print("Vertex Shader Compilation Failed");
|
print("Vertex Shader Compilation Failed");
|
||||||
return std::make_pair(0, 0);
|
return std::make_pair(0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
const unsigned int fragmentShader = compileShader(GL_FRAGMENT_SHADER, constants::fragmentShaderSource);
|
const unsigned int fragmentShader = compileShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
|
||||||
if (fragmentShader == 0)
|
if (fragmentShader == 0)
|
||||||
{
|
{
|
||||||
print("Fragment Shader Compilation Failed");
|
print("Fragment Shader Compilation Failed");
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
unsigned int linkShaderObjectsShaderProgram(const unsigned int vertexShaders, const unsigned int fragmentShader);
|
unsigned int linkShaderObjectsShaderProgram(const unsigned int vertexShaders, const unsigned int fragmentShader);
|
||||||
std::pair<unsigned int, unsigned int> compileShaders();
|
std::pair<unsigned int, unsigned int> compileShaders(const char* vertexShaderSource, const char* fragmentShaderSource);
|
||||||
unsigned int compileShader(const GLenum shaderType, const char *shaderSource);
|
unsigned int compileShader(const GLenum shaderType, const char *shaderSource);
|
||||||
int shaderCompilationSuccessful(const unsigned int shader);
|
int shaderCompilationSuccessful(const unsigned int shader);
|
||||||
int shaderProgramLinkingSuccessful(const unsigned int shaderProgram);
|
int shaderProgramLinkingSuccessful(const unsigned int shaderProgram);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user