Skip to content
Merged
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
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;
}
};
59 changes: 54 additions & 5 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,31 @@ swapping window etc. Good to use in simple project or just learning shaders and
- [Usage](#usage)
- [Supported Types](#supported-types)
- [Memory Management](#memory-management)
- [Shader](#shader)
- [Textures](#textures)
- [Info](#info-6)
- [Usage](#usage-1)
- [Memory Management](#memory-management-1)
- [Texture](#texture)
- [Shader](#shader)
- [Info](#info-7)
- [Usage](#usage-2)
- [Add Geometry Shader](#add-geometry-shader)
- [Hot-Reloading](#hot-reloading)
- [Memory Management](#memory-management-1)
- [Memory Management](#memory-management-2)
- [Example](#example)
- [ComputeShader](#computeshader)
- [Info](#info-7)
- [Info](#info-8)
- [Basic Usage](#basic-usage)
- [Data Processing Example](#data-processing-example)
- [Available Functions](#available-functions)
- [Mesh](#mesh)
- [Info](#info-8)
- [Info](#info-9)
- [Storing Data](#storing-data)
- [Mesh control and functions](#mesh-control-and-functions)
- [Render Example](#render-example)
- [Also check](#also-check)
- [FreeCamera3D](#freecamera3d)
- [Info](#info-9)
- [Info](#info-10)
- [Camera control and functions](#camera-control-and-functions)
- [Code Example](#code-example)
- [Also check](#also-check-1)
Expand Down Expand Up @@ -377,6 +382,49 @@ glm::vec3 color = uniform["color"]->get<glm::vec3>();



## Textures
### Info
1. Each Texture is loaded via ```TextureLoader``` (**stb**).
2. ```TextureData``` is struct of texture data.
3. If texture is un compiled than we skip binding.
4. After creating ```Texture``` you can delete your ```TextureData``` if needed any more.
5. ```Texture``` stores data fully on **GPU**.

### Usage
#### Loading Texture
For loading texture create ```TextureLoader```
```cpp
CW::Renderer::TextureLoader loader("example.png");
```

#### Creating Texture
Texture is created via ```Texture``` class.
```cpp
CW::Renderer::Texture texture;
// GLint min_filter = GL_LINEAR, GLint max_filter = GL_LINEAR (pass optional values)
texture.compile(loader.data);
```

#### Binding Texture
To bind use:
```cpp
texture.bind(2);
uniform["uTexture"]->set<int>(2); // in shader variable
```

### Memory Management
#### Texture Loader
* ```TextureLoader(const std::string& path);``` - loads asset to ```data``` public variable.
* ```~TextureLoader();``` - destroys whole class.

### Texture
* ````void compile(TextureData data, GLint min_filter = GL_LINEAR, GLint max_filter = GL_LINEAR);```` - creates texture on **GPU**.
* ```void destroy();``` - destroys texture from **GPU**.
* ```void bind(unsigned int socket);``` - binds texture to **socket** slot.
* ```void unbind();``` - unbinds textures.



## Shader
### Info
1. Shader combines multiple shaders to one program for rendering
Expand Down Expand Up @@ -811,6 +859,7 @@ int main(){
* [glad](https://glad.dav1d.de/)
* [imgui](https://github.com/ocornut/imgui/tree/docking)
* [glm](https://github.com/g-truc/glm)
* [stb](https://github.com/nothings/stb.git)



Expand Down
Loading