Skip to content
Merged

Dev #58

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions .github/workflows/ctests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: CMake Tests

on:
push:
pull_request:

jobs:
build-and-test:
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [ubuntu-latest, windows-latest]

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install Linux dependencies
if: runner.os == 'Linux'
run: |
sudo apt-get update
sudo apt-get install -y \
ninja-build \
wayland-protocols \
libwayland-dev \
libxkbcommon-dev \
xorg-dev

- name: Install Windows dependencies
if: runner.os == 'Windows'
run: choco install ninja -y

- name: Initialize submodules
run: git submodule update --init --recursive

- name: Configure project (Linux X11)
if: runner.os == 'Linux'
run: cmake -S . -B build -G Ninja \
-DGLFW_BUILD_WAYLAND=OFF \
-DGLFW_BUILD_X11=ON

- name: Configure project (Windows)
if: runner.os == 'Windows'
run: cmake -S . -B build -G Ninja

- name: Build project
run: cmake --build build

- name: Run tests
run: ctest --test-dir build --output-on-failure
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)

add_subdirectory(CWindow)
add_subdirectory(Example)
add_subdirectory(Example)

enable_testing()
add_subdirectory(Tests)
2 changes: 2 additions & 0 deletions CWindow/Renderer/OpenGL/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ set(src
"Uniform/Uniform.h"
"Texture/Texture.cpp"
"Texture/Texture.h"
"Texture/TextureLoader.cpp"
"Texture/TextureLoader.h"
${ROOT_DIR}/vendor/stb/deprecated/stb_image.c
${ROOT_DIR}/vendor/glad/src/glad.c
)
Expand Down
2 changes: 2 additions & 0 deletions CWindow/Renderer/OpenGL/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "Uniform/Uniform.h"
#include "Texture/Texture.h"

#include "Texture/TextureLoader.h"

#include "../Data/WindowData.h"
#include "../Data/InputData.h"

Expand Down
65 changes: 42 additions & 23 deletions CWindow/Renderer/OpenGL/Texture/Texture.cpp
Original file line number Diff line number Diff line change
@@ -1,47 +1,66 @@
#include "Texture.h"
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>

CW::Renderer::Texture::Texture(){






CW::Renderer::Texture::Texture() : is_compiled(false) {};



CW::Renderer::Texture::Texture(TextureData data, GLint min_filter, GLint max_filter)
: is_compiled(false){
compile(data, min_filter, max_filter);
};



CW::Renderer::Texture::~Texture() {
glDeleteTextures(0, &texture);
destroy();
};

bool CW::Renderer::Texture::load(const std::string &path) {


void CW::Renderer::Texture::compile(TextureData data, GLint min_filter, GLint max_filter) {
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, max_filter);

int width, height, channels;
stbi_set_flip_vertically_on_load(true);
unsigned char* data = stbi_load(path.c_str(), &width, &height, &channels, 0);
if(!data) return 1;

GLenum format = GL_RGB;
if (channels == 1) format = GL_RED;
else if (channels == 3) format = GL_RGB;
else if (channels == 4) format = GL_RGBA;

glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glTexImage2D(GL_TEXTURE_2D, 0, data.format, data.width, data.height, 0, data.format, GL_UNSIGNED_BYTE, data.data);
glGenerateMipmap(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D, 0);

is_compiled = true;
};

stbi_image_free(data);

return 0;
};

void CW::Renderer::Texture::bind() {
void CW::Renderer::Texture::destroy(){
if(is_compiled)
glDeleteTextures(1, &texture);

is_compiled = false;
}



void CW::Renderer::Texture::bind(unsigned int socket) {
if(!is_compiled)
return;

glActiveTexture(GL_TEXTURE0 + socket);
glBindTexture(GL_TEXTURE_2D, texture);
};



void CW::Renderer::Texture::unbind() {
glBindTexture(GL_TEXTURE_2D, 0);
};
};
18 changes: 15 additions & 3 deletions CWindow/Renderer/OpenGL/Texture/Texture.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
#pragma once

#include "glad/glad.h"

#include <string>

#include "TextureLoader.h"
#include "TextureData.h"







namespace CW::Renderer{
class Texture{
private:
GLuint texture;
bool is_compiled = false;

public:
Texture();
Texture(TextureData data, GLint min_filter = GL_LINEAR, GLint max_filter = GL_LINEAR);
~Texture();

bool load(const std::string& path);
void bind();
void compile(TextureData data, GLint min_filter = GL_LINEAR, GLint max_filter = GL_LINEAR);
void destroy();
void bind(unsigned int socket);
void unbind();
};
};
17 changes: 17 additions & 0 deletions CWindow/Renderer/OpenGL/Texture/TextureData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include "glad/glad.h"







namespace CW::Renderer{
struct TextureData{
GLenum format;
int width, height, channels;
unsigned char* data;
};
};
28 changes: 28 additions & 0 deletions CWindow/Renderer/OpenGL/Texture/TextureLoader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "TextureLoader.h"

#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>







CW::Renderer::TextureLoader::TextureLoader(const std::string &path)
{
stbi_set_flip_vertically_on_load(true);
data.data = stbi_load(path.c_str(), &data.width, &data.height, &data.channels, 0);
if(!data.data) return;

data.format = GL_RGB;
if (data.channels == 1) data.format = GL_RED;
else if (data.channels == 3) data.format = GL_RGB;
else if (data.channels == 4) data.format = GL_RGBA;
};



CW::Renderer::TextureLoader::~TextureLoader() {
stbi_image_free(data.data);
};
23 changes: 23 additions & 0 deletions CWindow/Renderer/OpenGL/Texture/TextureLoader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once
#include "glad/glad.h"

#include <string>

#include "TextureData.h"







namespace CW::Renderer{
class TextureLoader{
public:
TextureData data;

public:
TextureLoader(const std::string& path);
~TextureLoader();
};
}; // namespace CW
25 changes: 13 additions & 12 deletions Example/Texture/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
int main(){
CW::Renderer::Renderer window;

CW::Renderer::TextureLoader loader("../Assets/image.png");
CW::Renderer::Texture texture;
if(texture.load("../Assets/image.png"))
return -1;
texture.compile(loader.data);


CW::Renderer::Mesh viewport;

std::vector<GLfloat> vertices({
-1.0f, 1.0f, 0.0f,
-1.0f, -1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
});
viewport.addVertices(vertices);
viewport.addVertices(vertices, 3);

std::vector<GLuint> indicies({
0, 1, 2,
Expand All @@ -29,20 +30,20 @@ int main(){
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 1.0f,
1.0f, 0.0f
1.0f, 0.0f,
});
viewport.setData<GLfloat>(idtx, 2, 1, GL_FLOAT);

CW::Renderer::Shader texture_shader(Texture::vertex, Texture::fragment);
CW::Renderer::Uniform uniform;
texture_shader.getUniforms().emplace_back(&uniform);
uniform["uTexture"]->set<int>(0);




while(!window.getWindowData()->should_close){
window.beginFrame();

texture.bind();

texture.bind(2);
uniform["uTexture"]->set<int>(2);
texture_shader.bind();
viewport.render();
texture_shader.unbind();
Expand All @@ -53,4 +54,4 @@ int main(){
};

return 0;
}
};
Loading
Loading