Added own shaders
This commit is contained in:
@@ -1,14 +1,24 @@
|
|||||||
cmake_minimum_required(VERSION 4.2)
|
cmake_minimum_required(VERSION 3.20)
|
||||||
project(Cinecraft)
|
project(Cinecraft)
|
||||||
|
|
||||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/$<CONFIGURATION>")
|
|
||||||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/$<CONFIGURATION>")
|
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 20)
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED on)
|
set(CMAKE_CXX_STANDARD_REQUIRED on)
|
||||||
|
|
||||||
find_package(SDL3 REQUIRED)
|
find_package(SDL3 REQUIRED)
|
||||||
|
find_package(GLEW REQUIRED)
|
||||||
|
|
||||||
add_executable(app src/main.cpp)
|
add_executable(${PROJECT_NAME} src/main.cpp
|
||||||
|
include/VertexBuffer.hpp
|
||||||
|
include/defines.hpp
|
||||||
|
src/Shader.cpp
|
||||||
|
include/Shader.hpp)
|
||||||
|
|
||||||
target_link_libraries(app PRIVATE SDL3::SDL3 GL)
|
target_include_directories(${PROJECT_NAME} PRIVATE include)
|
||||||
|
target_include_directories(${PROJECT_NAME} PRIVATE shaders)
|
||||||
|
|
||||||
|
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E create_symlink
|
||||||
|
${CMAKE_SOURCE_DIR}/shaders
|
||||||
|
${CMAKE_CURRENT_BINARY_DIR}/shaders)
|
||||||
|
|
||||||
|
target_link_libraries(${PROJECT_NAME} PRIVATE SDL3::SDL3 GLEW::GLEW GL)
|
||||||
19
include/Shader.hpp
Normal file
19
include/Shader.hpp
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <string>
|
||||||
|
#include <GL/glew.h>
|
||||||
|
#include "defines.hpp"
|
||||||
|
|
||||||
|
struct Shader {
|
||||||
|
Shader(const char* vertexShaderFileName, const char* fragmentShaderFileName);
|
||||||
|
virtual ~Shader();
|
||||||
|
|
||||||
|
void bind();
|
||||||
|
void unbind();
|
||||||
|
|
||||||
|
private:
|
||||||
|
GLuint compile(std::string shaderSource, GLenum shaderType);
|
||||||
|
std::string parse(const char* filename);
|
||||||
|
GLuint createShader(const char* vertexShaderFileName, const char* fragmentShaderFileName);
|
||||||
|
|
||||||
|
GLuint shaderId;
|
||||||
|
};
|
||||||
34
include/VertexBuffer.hpp
Normal file
34
include/VertexBuffer.hpp
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <GL/glew.h>
|
||||||
|
|
||||||
|
#include "defines.hpp"
|
||||||
|
|
||||||
|
struct VertexBuffer {
|
||||||
|
VertexBuffer(void* data, uint32 numVertices) {
|
||||||
|
glGenVertexArrays(1, &vao);
|
||||||
|
glBindVertexArray(vao);
|
||||||
|
|
||||||
|
glGenBuffers(1, &bufferId);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, bufferId);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, numVertices*sizeof(Vertex), data, GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), offsetof(struct Vertex,x));
|
||||||
|
|
||||||
|
glBindVertexArray(0);
|
||||||
|
};
|
||||||
|
virtual ~VertexBuffer() {
|
||||||
|
glDeleteBuffers(1, &bufferId);
|
||||||
|
}
|
||||||
|
|
||||||
|
void bind() {
|
||||||
|
glBindVertexArray(vao);
|
||||||
|
}
|
||||||
|
void unbind() {
|
||||||
|
glBindVertexArray(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
GLuint bufferId;
|
||||||
|
GLuint vao;
|
||||||
|
};
|
||||||
19
include/defines.hpp
Normal file
19
include/defines.hpp
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
typedef int8_t int8;
|
||||||
|
typedef int16_t int16;
|
||||||
|
typedef int32_t int32;
|
||||||
|
typedef int64_t int64;
|
||||||
|
|
||||||
|
typedef uint8_t uint8;
|
||||||
|
typedef uint16_t uint16;
|
||||||
|
typedef uint32_t uint32;
|
||||||
|
typedef uint64_t uint64;
|
||||||
|
|
||||||
|
typedef float float32;
|
||||||
|
typedef double float64;
|
||||||
|
|
||||||
|
struct Vertex {
|
||||||
|
float32 x, y, z;
|
||||||
|
};
|
||||||
4
shaders/README.md
Normal file
4
shaders/README.md
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
# Attributes
|
||||||
|
|Index|Description|
|
||||||
|
|---|---|
|
||||||
|
|0|Position|
|
||||||
7
shaders/basic.frag
Normal file
7
shaders/basic.frag
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#version 330 core
|
||||||
|
|
||||||
|
layout(location = 0) out vec4 color;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
color = vec4(1.0f, 1.0f, 0.0f, 1.0f);
|
||||||
|
}
|
||||||
7
shaders/basic.vert
Normal file
7
shaders/basic.vert
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#version 330 core
|
||||||
|
|
||||||
|
layout(location = 0) in vec3 position;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
gl_Position = vec4(position, 1.0f);
|
||||||
|
}
|
||||||
78
src/Shader.cpp
Normal file
78
src/Shader.cpp
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include "Shader.hpp"
|
||||||
|
|
||||||
|
Shader::Shader(const char* vertexShaderFileName, const char* fragmentShaderFileName) {
|
||||||
|
shaderId = createShader(vertexShaderFileName, fragmentShaderFileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
Shader::~Shader() {
|
||||||
|
glDeleteProgram(shaderId);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Shader::bind() {
|
||||||
|
glUseProgram(shaderId);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Shader::unbind() {
|
||||||
|
glUseProgram(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
GLuint Shader::compile(std::string shaderSource, GLenum shaderType) {
|
||||||
|
GLuint id = glCreateShader(shaderType);
|
||||||
|
const char* src = shaderSource.c_str();
|
||||||
|
glShaderSource(id, 1, &src, nullptr);
|
||||||
|
glCompileShader(id);
|
||||||
|
|
||||||
|
int result;
|
||||||
|
glGetShaderiv(id, GL_COMPILE_STATUS, &result);
|
||||||
|
if (result == GL_FALSE) {
|
||||||
|
int length = 0;
|
||||||
|
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
|
||||||
|
char* message = new char[length];
|
||||||
|
glGetShaderInfoLog(id, length, &length, message);
|
||||||
|
std::cout << "Shader Compilation Error" << message << std::endl;
|
||||||
|
delete[] message;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Shader::parse(const char* filename) {
|
||||||
|
FILE* file;
|
||||||
|
file = fopen(filename, "rb");
|
||||||
|
if (file == nullptr) {
|
||||||
|
std::cout << "File " << filename << " not found" << std::endl;
|
||||||
|
}
|
||||||
|
std::string contents;
|
||||||
|
fseek(file, 0, SEEK_END);
|
||||||
|
size_t filesize = ftell(file);
|
||||||
|
rewind(file);
|
||||||
|
contents.resize(filesize);
|
||||||
|
fread(&contents[0], 1, filesize, file);
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
|
return contents;
|
||||||
|
}
|
||||||
|
|
||||||
|
GLuint Shader::createShader(const char* vertexShaderFileName, const char* fragmentShaderFileName) {
|
||||||
|
std::string vertexShaderSource = parse(vertexShaderFileName);
|
||||||
|
std::string fragmentShaderSource = parse(fragmentShaderFileName);
|
||||||
|
|
||||||
|
GLuint program = glCreateProgram();
|
||||||
|
GLuint vert = compile(vertexShaderSource, GL_VERTEX_SHADER);
|
||||||
|
GLuint frag = compile(fragmentShaderSource, GL_FRAGMENT_SHADER);
|
||||||
|
|
||||||
|
glAttachShader(program, vert);
|
||||||
|
glAttachShader(program, frag);
|
||||||
|
glLinkProgram(program);
|
||||||
|
|
||||||
|
// ATTENTION: use only for release
|
||||||
|
// NOTE: if you do not detach and delete the program from the gpu, it's easier to debug if errors happen with the shaders
|
||||||
|
// glDetachShader(program, vert);
|
||||||
|
// glDetachShader(program, frag);
|
||||||
|
// glDeleteShader(vert);
|
||||||
|
// glDeleteShader(frag);
|
||||||
|
|
||||||
|
return program;
|
||||||
|
}
|
||||||
39
src/main.cpp
39
src/main.cpp
@@ -1,5 +1,12 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#define GLEW_STATIC
|
||||||
|
#include <GL/glew.h>
|
||||||
|
#define SDL_MAIN_HANDLED
|
||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
#include <GL/gl.h>
|
|
||||||
|
#include "defines.hpp"
|
||||||
|
#include "VertexBuffer.hpp"
|
||||||
|
#include "Shader.hpp"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
SDL_Init(SDL_INIT_VIDEO);
|
SDL_Init(SDL_INIT_VIDEO);
|
||||||
@@ -20,9 +27,28 @@ int main() {
|
|||||||
|
|
||||||
SDL_GLContext context = SDL_GL_CreateContext(window);
|
SDL_GLContext context = SDL_GL_CreateContext(window);
|
||||||
|
|
||||||
|
GLenum err = glewInit();
|
||||||
|
if (err != GLEW_OK) {
|
||||||
|
std::cout <<"Error: " << glewGetErrorString(err) << std::endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
std::cout << "OpenGL Version: " << glGetString(GL_VERSION) << std::endl;
|
||||||
|
|
||||||
|
Vertex vertices[] = {
|
||||||
|
Vertex{-0.5f, -0.5f, 0.0f},
|
||||||
|
Vertex{0.0f, 0.5f, 0.0f},
|
||||||
|
Vertex{0.5f, -0.5f, 0.0f},
|
||||||
|
};
|
||||||
|
uint32_t numVertices = 3;
|
||||||
|
|
||||||
|
VertexBuffer vertexBuffer(vertices, numVertices);
|
||||||
|
vertexBuffer.unbind();
|
||||||
|
|
||||||
|
Shader shader("shaders/basic.vert", "shaders/basic.frag");
|
||||||
|
shader.bind();
|
||||||
|
|
||||||
bool running = true;
|
bool running = true;
|
||||||
SDL_Event e;
|
SDL_Event e;
|
||||||
|
|
||||||
while (running) {
|
while (running) {
|
||||||
while (SDL_PollEvent(&e)) {
|
while (SDL_PollEvent(&e)) {
|
||||||
if (e.type == SDL_EVENT_QUIT) {
|
if (e.type == SDL_EVENT_QUIT) {
|
||||||
@@ -30,9 +56,14 @@ int main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
glClearColor(0.1f, 0.2f, 0.3f, 1.0f);
|
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
// FRAME GENERATION
|
||||||
|
vertexBuffer.bind();
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, numVertices);
|
||||||
|
vertexBuffer.unbind();
|
||||||
|
|
||||||
SDL_GL_SwapWindow(window);
|
SDL_GL_SwapWindow(window);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,4 +72,4 @@ int main() {
|
|||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user