Colorful triangle with shaders loaded with new class!
This commit is contained in:
parent
2cb10c9910
commit
a1ccd4e362
@ -6,7 +6,7 @@ set(CMAKE_CXX_STANDARD 17)
|
||||
find_package(OpenGL REQUIRED)
|
||||
find_package(glfw3 REQUIRED)
|
||||
|
||||
add_executable(ecsgame glad.c main.cpp Shader.cpp Shader.h)
|
||||
add_executable(ecsgame glad.c Shader.cpp Shader.h main.cpp)
|
||||
|
||||
include_directories(${OPENGL_INCLUDE_DIRS})
|
||||
|
||||
|
11
Shader.h
11
Shader.h
@ -9,20 +9,23 @@
|
||||
|
||||
class Shader {
|
||||
public:
|
||||
// the program ID
|
||||
/// Program ID
|
||||
unsigned int ID;
|
||||
|
||||
// constructor reads and builds the shader
|
||||
/// Read and build the shader from the files at vertexPath and fragmentPath.
|
||||
/// Note that an OpenGL context has to be initialized before calling this! Otherwise a SIGSEGV will be thrown.
|
||||
Shader(const char *vertexPath, const char *fragmentPath);
|
||||
|
||||
// use/activate the shader
|
||||
/// Activate the shader - usually called before rendering.
|
||||
void use();
|
||||
|
||||
// utility uniform functions
|
||||
/// Set a uniform boolean in the shader
|
||||
void setBool(const std::string &name, bool value) const;
|
||||
|
||||
/// Set a uniform int in the shader
|
||||
void setInt(const std::string &name, int value) const;
|
||||
|
||||
/// Set a uniform float in the shader
|
||||
void setFloat(const std::string &name, float value) const;
|
||||
|
||||
private:
|
||||
|
8
Shaders/default-fragment.fs
Normal file
8
Shaders/default-fragment.fs
Normal file
@ -0,0 +1,8 @@
|
||||
#version 320 es
|
||||
out mediump vec4 FragColor;
|
||||
in mediump vec3 COLOR;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(COLOR, 1.0);
|
||||
}
|
11
Shaders/default-vertex.vs
Normal file
11
Shaders/default-vertex.vs
Normal file
@ -0,0 +1,11 @@
|
||||
#version 320 es
|
||||
layout (location = 0) in vec3 aPos; // the position variable has attribute position 0
|
||||
layout (location = 1) in vec3 aColor; // the color variable has attribute position 1
|
||||
|
||||
out vec3 COLOR; // output a color to the fragment shader
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = vec4(aPos, 1.0);
|
||||
COLOR = aColor;
|
||||
}
|
85
main.cpp
85
main.cpp
@ -1,8 +1,10 @@
|
||||
#include <iostream>
|
||||
#include "ECS.h"
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <GL/gl.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "Shader.h"
|
||||
#include "ECS.h"
|
||||
|
||||
using namespace ECS;
|
||||
|
||||
@ -134,19 +136,6 @@ static void key_callback(GLFWwindow *window, int key, int scancode, int action,
|
||||
world->emit<InputEvent>({key, action});
|
||||
}
|
||||
|
||||
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 char *fragmentShaderSource = "#version 330 core\n"
|
||||
"out vec4 FragColor;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
|
||||
"}\n\0";
|
||||
|
||||
int main() {
|
||||
world->registerSystem(new GravitySystem(-9.8f));
|
||||
world->registerSystem(new PositionDebugOutputSystem());
|
||||
@ -184,57 +173,18 @@ int main() {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// build and compile our shader program
|
||||
// ------------------------------------
|
||||
// vertex shader
|
||||
int vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
|
||||
glCompileShader(vertexShader);
|
||||
// check for shader compile errors
|
||||
int success;
|
||||
char infoLog[512];
|
||||
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
|
||||
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
|
||||
}
|
||||
// fragment shader
|
||||
int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
|
||||
glCompileShader(fragmentShader);
|
||||
// check for shader compile errors
|
||||
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
|
||||
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
|
||||
}
|
||||
// link shaders
|
||||
int shaderProgram = glCreateProgram();
|
||||
glAttachShader(shaderProgram, vertexShader);
|
||||
glAttachShader(shaderProgram, fragmentShader);
|
||||
glLinkProgram(shaderProgram);
|
||||
// check for linking errors
|
||||
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
|
||||
if (!success) {
|
||||
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
|
||||
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
|
||||
}
|
||||
glDeleteShader(vertexShader);
|
||||
glDeleteShader(fragmentShader);
|
||||
Shader defaultShader("Shaders/default-vertex.vs", "Shaders/default-fragment.fs");
|
||||
|
||||
// set up vertex data (and buffer(s)) and configure vertex attributes
|
||||
// ------------------------------------------------------------------
|
||||
float vertices[] = {
|
||||
0.5f, 0.5f, 0.0f, // top right
|
||||
0.5f, -0.5f, 0.0f, // bottom right
|
||||
-0.5f, -0.5f, 0.0f, // bottom left
|
||||
-0.5f, 0.5f, 0.0f // top left
|
||||
// positions // colors
|
||||
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom right
|
||||
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left
|
||||
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f // top
|
||||
};
|
||||
unsigned int indices[] = { // note that we start from 0!
|
||||
0, 1, 3, // first Triangle
|
||||
1, 2, 3 // second Triangle
|
||||
0, 1, 2 // first Triangle
|
||||
};
|
||||
unsigned int VBO, VAO, EBO;
|
||||
glGenVertexArrays(1, &VAO);
|
||||
@ -249,7 +199,13 @@ int main() {
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
|
||||
// position attribute
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
// color attribute
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3* sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
// note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
|
||||
@ -283,10 +239,9 @@ int main() {
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
// draw our first triangle
|
||||
glUseProgram(shaderProgram);
|
||||
defaultShader.use();
|
||||
glBindVertexArray(VAO); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
|
||||
//glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
// glBindVertexArray(0); // no need to unbind it every time
|
||||
|
||||
/* Swap front and back buffers */
|
||||
|
Loading…
x
Reference in New Issue
Block a user