feat: hello triangle compiling shader

This commit is contained in:
Krzysztof Rudnicki 2022-08-31 20:40:41 +02:00
parent adaee767e7
commit 7934f833ec
3 changed files with 59 additions and 18 deletions

View File

@ -1,4 +1,5 @@
#ifndef CONSTANTS_HPP
#include <GLFW/glfw3.h>
#include <iostream>
#include <string_view>
@ -8,6 +9,26 @@ namespace constants
inline const char* MAIN_WINDOW_NAME { "Match" };
inline constexpr int MAIN_WINDOW_WIDTH { 800 };
inline constexpr int MAIN_WINDOW_HEIGHT { 600 };
inline constexpr struct {
GLfloat red = 1.0f;
GLfloat green = 0.0f;
GLfloat blue = 0.0f;
GLfloat alpha = 1.0f;
} RED;
inline constexpr struct {
GLfloat red = 1.0f;
GLfloat green = 1.0f;
GLfloat blue = 1.0f;
GLfloat alpha = 1.0f;
} WHITE;
inline constexpr struct {
GLfloat red = 0.2f;
GLfloat green = 0.3f;
GLfloat blue = 0.3f;
GLfloat alpha = 1.0f;
} LEARN_OPEN_GL_COLOR;
}

Binary file not shown.

View File

@ -3,8 +3,6 @@
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <random> // I am using standart library RNG because I am lazy and wanted to create quick code snippet
@ -83,18 +81,43 @@ void processInput(GLFWwindow *window)
glfwSetWindowShouldClose(window, true);
}
const float padaczkaSimulator()
void drawTriangle()
{
// Seed our Mersenne Twister using the
std::mt19937 mt{ static_cast<unsigned int>(
std::chrono::steady_clock::now().time_since_epoch().count()
) };
// we specify three vertices
// each of them with position in 3d space
// x y z
const float triangleVertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
// Create a reusable random number generator that generates uniform numbers between 1 and 6
std::uniform_int_distribution ten{ 1, 10 }; // for C++14, use std::uniform_int_distribution<> die6{ 1, 6 };
// stores vertices in gpu memory
unsigned int vertexBufferObject;
// this is open gl object so we refer it by its ID generated here and stored in vertexBufferObject variable
glGenBuffers(1, &vertexBufferObject);
// buffer type of vertex buffer object is GL_ARRAY_BUFFER
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
// now whenever we change GL_ARRAY_BUFFER we change bound buffer vertexBufferObject
/* we copy vertex data into buffer memory
GL_STREAM_DRAW: the data is set only once and used by the GPU at most a few times.
GL_STATIC_DRAW: the data is set only once and used many times.
GL_DYNAMIC_DRAW: the data is changed a lot and used many times.
*/
glBufferData(GL_ARRAY_BUFFER, sizeof(triangleVertices), triangleVertices, GL_STATIC_DRAW);
// we write vertex shader
// 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
const char *vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
const float MT_FLOAT = static_cast<float> (ten(mt) % 10);
return MT_FLOAT / 10;
}
void renderLoop(GLFWwindow* window)
@ -104,13 +127,10 @@ void renderLoop(GLFWwindow* window)
{
// input
processInput(window);
const float FIRST_COLOR = padaczkaSimulator();
const float SECOND_COLOR = padaczkaSimulator();
const float THIRD_COLOR = padaczkaSimulator();
const float FOUR_COLOR = padaczkaSimulator();
glClearColor( FIRST_COLOR, SECOND_COLOR, THIRD_COLOR, FOUR_COLOR);
// We specify the color to clear the screen with
// RGB and alpha value
glClearColor( constants::LEARN_OPEN_GL_COLOR.red, constants::LEARN_OPEN_GL_COLOR.green, constants::LEARN_OPEN_GL_COLOR.blue, constants::LEARN_OPEN_GL_COLOR.alpha);
// There is GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT and GL_STENCIL_BUFFER_BIT
glClear(GL_COLOR_BUFFER_BIT);