Skip to content

Commit

Permalink
Support texture usage
Browse files Browse the repository at this point in the history
  • Loading branch information
kissholic committed Aug 25, 2024
1 parent 355788f commit fbb8c61
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "external/stb_image"]
path = external/stb_image
url = https://github.com/nothings/stb.git
32 changes: 32 additions & 0 deletions Include/Graphics/ImageLoader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* File: ImageLoader.h
* Author: kissholic
* Copyright (c) 2024 kissholic. All wrongs reserved.
*/

#pragma once

#include <filesystem>

namespace be {


struct ImageWrapper {
ImageWrapper() = default;
ImageWrapper(unsigned char* Data, int Width, int Height, int Channels)
: Data(Data), Width(Width), Height(Height), Channels(Channels)
{}

~ImageWrapper();

unsigned char* Data;
int Width;
int Height;
int Channels;
};


ImageWrapper LoadImage(std::filesystem::path const& ImagePath);


} // namespace be
33 changes: 33 additions & 0 deletions Include/Graphics/Texture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* File: Texture.h
* Author: kissholic
* Copyright (c) 2024 kissholic. All wrongs reserved.
*/

#pragma once

#include <string>
#include "ImageLoader.h"

namespace be {


class Texture {
public:
Texture(ImageWrapper& Source);
~Texture();

unsigned int GetTextureHandle() const noexcept { return mTextureHandle; }
int GetWidth() const noexcept { return mWidth; }
int GetHeight() const noexcept { return mHeight; }
int GetChannels() const noexcept { return mChannels; }

private:
unsigned int mTextureHandle;
int mWidth;
int mHeight;
int mChannels;
};


} // namespace be
Binary file added Shader/wall.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions Source/Graphics/ImageLoader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* File: ImageLoader.cpp
* Author: kissholic
* Copyright (c) 2024 kissholic. All wrongs reserved.
*/

#include "Graphics/ImageLoader.h"
#include "spdlog/spdlog.h"

#include "stb_image.h"

namespace be {


ImageWrapper::~ImageWrapper()
{
stbi_image_free(Data);
}

ImageWrapper LoadImage(std::filesystem::path const& ImagePath) {
ImageWrapper image;

image.Data = stbi_load(ImagePath.c_str(), &image.Width, &image.Height, &image.Channels, 0);
if (image.Data == nullptr) {
spdlog::error("Failed to load image: {}", ImagePath.c_str());
}

return image;
}


} // namespace be
11 changes: 11 additions & 0 deletions Source/Graphics/STBImage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* File: STBImage.cpp
* Author: kissholic
* Copyright (c) 2024 kissholic. All wrongs reserved.
*/


#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"


36 changes: 36 additions & 0 deletions Source/Graphics/Texture.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* File: Texture.cpp
* Author: kissholic
* Copyright (c) 2024 kissholic. All wrongs reserved.
*/

#include "Graphics/Texture.h"
#include "Graphics/ImageLoader.h"
#include "glad/glad.h"



namespace be {


Texture::Texture(ImageWrapper& Source)
: mTextureHandle(0), mWidth(Source.Width), mHeight(Source.Height), mChannels(Source.Channels) {
glGenTextures(1, &mTextureHandle);
glBindTexture(GL_TEXTURE_2D, mTextureHandle);

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);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mWidth, mHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, Source.Data);
glGenerateMipmap(GL_TEXTURE_2D);
}


Texture::~Texture() {
glDeleteTextures(1, &mTextureHandle);
}


} // namespace be
1 change: 1 addition & 0 deletions external/stb_image
Submodule stb_image added at f75e8d
2 changes: 2 additions & 0 deletions xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ set_configdir("$(buildir)/Config")
add_configfiles("BEConfig.h.in")
add_includedirs("$(buildir)/Config")

add_includedirs("external/stb_image")

add_includedirs("Include")
includes("Source")

0 comments on commit fbb8c61

Please sign in to comment.