Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Texture resource #24

Merged
merged 2 commits into from
Aug 2, 2022
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: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.14)

project(atta VERSION 0.0.3.4 LANGUAGES CXX C)
project(atta VERSION 0.0.3.5 LANGUAGES CXX C)

OPTION(ATTA_BUILD_TESTS
"Set to ON to build also the test executables"
Expand Down
6 changes: 3 additions & 3 deletions resources/meshes/plane.obj
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ mtllib plane.mtl
o Plane
v 0.500000 -0.500000 0.000000
v 0.500000 0.500000 0.000000
v -0.500000 -0.500000 0.000000
v -0.500000 0.500000 0.000000
vt 0.000000 0.000000
v -0.500000 -0.500000 0.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 1.000000
vt 0.000000 0.000000
vn 0.0000 0.0000 1.0000
usemtl None
s off
f 1/1/1 2/2/1 4/3/1 3/4/1
f 1/1/1 2/2/1 3/3/1 4/4/1
3 changes: 1 addition & 2 deletions resources/shaders/fastRenderer/shader.frag
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
precision mediump float;

in vec2 texCoord;
out vec4 FragColor;

uniform vec3 albedo;
uniform sampler2D albedoTexture;

out vec4 FragColor;

void main()
{
if(albedo.x < 0.0f)
Expand Down
9 changes: 4 additions & 5 deletions resources/shaders/fastRenderer/shader.vert
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#version 300 es
in vec3 inPosition;
in vec3 inNormal;
in vec2 inTexCoord;
layout (location = 0) in vec3 inPosition;
layout (location = 1) in vec3 inNormal;
layout (location = 2) in vec2 inTexCoord;
out vec2 texCoord;

uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;

out vec2 texCoord;

void main()
{
gl_Position = projection * view * model * vec4(inPosition, 1.0f);
Expand Down
23 changes: 22 additions & 1 deletion scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CMAKE_BUILD_TYPE="-DCMAKE_BUILD_TYPE=Release"
CMAKE_ATTA_STATIC=""
BUILD_TYPE="default"
RUN_AFTER="false"
PROJECT_TO_RUN=""
INSTALL_AFTER="false"

printHelp()
Expand All @@ -27,13 +28,19 @@ printHelp()
echo "-d or --debug"
echo " Build with debug information."
echo
echo "-g or --gdb"
echo " Run with gdb."
echo
echo "-s or --static <project_file>"
echo " Build statically linked to a project."
echo " The file should be a valid .atta"
echo
echo "-r or --run"
echo " Run after build."
echo
echo "-p or --project <project_file>"
echo " Specify project to run."
echo
echo "-i or --install"
echo " Install after build."
exit
Expand Down Expand Up @@ -75,7 +82,11 @@ buildDefault()
# Run
if [[ "$RUN_AFTER" == "true" ]]; then
echo "---------- Running ----------"
bin/atta
if [[ "$USE_GDB" == "true" ]]; then
gdb -ex r --args bin/atta $PROJECT_TO_RUN
else
bin/atta $PROJECT_TO_RUN
fi
fi

exit
Expand Down Expand Up @@ -127,10 +138,20 @@ while [[ $# -gt 0 ]]; do
CMAKE_BUILD_TYPE="-DCMAKE_BUILD_TYPE=Debug"
shift # past argument
;;
-g|--gdb)
USE_GDB="true"
RUN_AFTER="true"
shift # past argument
;;
-r|--run)
RUN_AFTER="true"
shift # past argument
;;
-p|--project)
PROJECT_TO_RUN="$2"
shift # past argument
shift # past value
;;
-i|--install)
INSTALL_AFTER="true"
shift # past argument
Expand Down
2 changes: 1 addition & 1 deletion src/atta/core/log.inl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ namespace atta
}

template<typename T>
std::string getArgStr(T t)
std::string getArgStr(const T& t)
{
std::stringstream ss;

Expand Down
22 changes: 22 additions & 0 deletions src/atta/eventSystem/events/textureUpdateEvent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//--------------------------------------------------
// Atta Event System
// textureUpdateEvent.h
// Date: 2021-09-06
// By Breno Cunha Queiroz
//--------------------------------------------------
#ifndef ATTA_EVENT_SYSTEM_EVENTS_TEXTURE_UPDATE_EVENT_H
#define ATTA_EVENT_SYSTEM_EVENTS_TEXTURE_UPDATE_EVENT_H
#include <atta/eventSystem/event.h>

namespace atta
{
class TextureUpdateEvent : public EventTyped<SID("TextureUpdateEvent")>
{
public:
TextureUpdateEvent(StringId sid_): sid(sid_) {}

const StringId sid;
};
}

#endif// ATTA_EVENT_SYSTEM_EVENTS_TEXTURE_UPDATE_EVENT_H
6 changes: 3 additions & 3 deletions src/atta/fileSystem/fileManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,14 @@ namespace atta
_fileWatcher->update();
}

fs::path FileManager::solveResourcePathImpl(fs::path relativePath)
fs::path FileManager::solveResourcePathImpl(fs::path relativePath, bool mustExist)
{
if(_project != nullptr)
return _project->solveResourcePath(relativePath);
return _project->solveResourcePath(relativePath, mustExist);
else
{
fs::path full = fs::path(ATTA_DIR)/"resources"/relativePath;
if(fs::exists(full))
if(!mustExist || fs::exists(full))
return full;
else
return fs::path();
Expand Down
4 changes: 2 additions & 2 deletions src/atta/fileSystem/fileManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace atta
// Receives a relative resource path and searches the registered directories for that file
// By default searches on the <ATTA_DIR>/resources and <PROJECT_DIR>/resources directories
// The return is the absolute resource path
static fs::path solveResourcePath(fs::path relativePath) { return getInstance().solveResourcePathImpl(relativePath); }
static fs::path solveResourcePath(fs::path relativePath, bool mustExist = true) { return getInstance().solveResourcePathImpl(relativePath, mustExist); }
static std::vector<fs::path> getResourcePaths() { return getInstance().getResourcePathsImpl(); }
static std::vector<fs::path> getDirectoryFilesRecursive(fs::path directory);
static fs::path getDefaultProjectFolder() { return getInstance()._defaultProjectFolder; }
Expand All @@ -55,7 +55,7 @@ namespace atta
bool isProjectOpenImpl() const;
std::shared_ptr<Project> getProjectImpl() const { return _project; }

fs::path solveResourcePathImpl(fs::path relativePath);
fs::path solveResourcePathImpl(fs::path relativePath, bool mustExist);
std::vector<fs::path> getResourcePathsImpl() const;

// Handle events
Expand Down
17 changes: 11 additions & 6 deletions src/atta/fileSystem/project/project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace atta
_directory(file.parent_path())
{
fs::path projectResourcePath = _directory/"resources";
if(exists(projectResourcePath))
if(fs::exists(projectResourcePath))
_resourceRootPaths.push_back(projectResourcePath);
_resourceRootPaths.push_back(fs::path(ATTA_DIR)/"resources");

Expand All @@ -32,13 +32,18 @@ namespace atta
return _directory/"snapshots";
}

fs::path Project::solveResourcePath(fs::path relativePath)
fs::path Project::solveResourcePath(fs::path relativePath, bool mustExist)
{
for(auto& root : _resourceRootPaths)
if(!mustExist)
return _directory/"resources"/relativePath;
else
{
fs::path full = root/relativePath;
if(fs::exists(full))
return full;
for(auto& root : _resourceRootPaths)
{
fs::path full = root/relativePath;
if(fs::exists(full))
return full;
}
}

return fs::path();
Expand Down
2 changes: 1 addition & 1 deletion src/atta/fileSystem/project/project.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace atta
std::vector<fs::path> getResourceRootPaths() const { return _resourceRootPaths; }

private:
fs::path solveResourcePath(fs::path relativePath);
fs::path solveResourcePath(fs::path relativePath, bool mustExist = true);

std::string _name;

Expand Down
3 changes: 1 addition & 2 deletions src/atta/graphicsSystem/pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
namespace atta
{
Pipeline::Pipeline(const CreateInfo& info):
_shaderGroup(info.shaderGroup), _renderPass(info.renderPass), _layout(info.layout),
_shaderGroup(info.shaderGroup), _renderPass(info.renderPass), _layout(info.layout),
_primitiveTopology(info.primitiveTopology),
_backfaceCulling(info.backfaceCulling), _wireframe(info.wireframe),
_lineWidth(info.lineWidth), _debugName(info.debugName)
{

}
}
6 changes: 4 additions & 2 deletions src/atta/graphicsSystem/rendererAPIs/openGL/openGLImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ namespace atta
{
if(!_isCubemap)
{
glBindTexture(GL_TEXTURE_2D, _id);
//LOG_DEBUG("OpenGlImage", "Writing $0 -> $1 ($2)", data, (int)_id, _debugName);
GLenum dataType = OpenGLImage::convertDataType(_format);
GLenum internalFormat = OpenGLImage::convertInternalFormat(_format);
GLenum format = OpenGLImage::convertFormat(_format);
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, _width, _height, 0, format, dataType, data);

glBindTexture(GL_TEXTURE_2D, _id);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, _width, _height, format, dataType, data);
glBindTexture(GL_TEXTURE_2D, 0);
}
else
Expand Down
25 changes: 25 additions & 0 deletions src/atta/graphicsSystem/rendererAPIs/openGL/openGLRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <atta/eventSystem/eventManager.h>
#include <atta/eventSystem/events/meshLoadEvent.h>
#include <atta/eventSystem/events/textureLoadEvent.h>
#include <atta/eventSystem/events/textureUpdateEvent.h>
#include <atta/resourceSystem/resourceManager.h>
#include <atta/resourceSystem/resources/mesh.h>
#include <GLFW/glfw3.h>
Expand Down Expand Up @@ -69,6 +70,7 @@ namespace atta
// Subscribe to events
EventManager::subscribe<MeshLoadEvent>(BIND_EVENT_FUNC(OpenGLRenderer::onMeshLoadEvent));
EventManager::subscribe<TextureLoadEvent>(BIND_EVENT_FUNC(OpenGLRenderer::onTextureLoadEvent));
EventManager::subscribe<TextureUpdateEvent>(BIND_EVENT_FUNC(OpenGLRenderer::onTextureUpdateEvent));

// Quad shader
ShaderGroup::CreateInfo shaderGroupInfo {};
Expand Down Expand Up @@ -272,6 +274,29 @@ namespace atta
initializeTexture(e.sid);
}

void OpenGLRenderer::onTextureUpdateEvent(Event& event)
{
TextureUpdateEvent& e = reinterpret_cast<TextureUpdateEvent&>(event);
//OpenGLRenderer::initializeTexture(e.sid);
Texture* texture = ResourceManager::get<Texture>(e.sid.getString());
if(texture == nullptr)
{
LOG_WARN("OpenGLRenderer", "Could not initialize OpenGL texture from [w]$0[], texture resource does not exists", e.sid.getString());
return;
}
if(_openGLImages.find(e.sid.getId()) == _openGLImages.end())
{
LOG_WARN("OpenGLRenderer", "OpenGL texture [w]$0[] was not loaded before update", e.sid.getString());
return;
}

std::shared_ptr<OpenGLImage> image = _openGLImages[e.sid.getId()];
if(texture->getWidth() != image->getWidth() || texture->getHeight() != image->getHeight())
image->resize(texture->getWidth(), texture->getHeight());
else
image->write(texture->getData());
}

void OpenGLRenderer::renderFramebufferToQuad(std::shared_ptr<Framebuffer> framebuffer)
{
glViewport(200, 200, framebuffer->getWidth(), framebuffer->getHeight());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace atta
// Handle events
void onMeshLoadEvent(Event& event);
void onTextureLoadEvent(Event& event);
void onTextureUpdateEvent(Event& event);

std::unordered_map<StringHash, std::shared_ptr<OpenGLImage>> getOpenGLImages() const { return _openGLImages; };
std::unordered_map<StringHash, OpenGLId> getOpenGLCubemaps() const { return _openGLCubemaps; };
Expand Down
9 changes: 9 additions & 0 deletions src/atta/graphicsSystem/rendererAPIs/openGL/openGLShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ namespace atta
glGetShaderInfoLog(_id, 512, NULL, infoLog);
ASSERT(false, "Failed to compile shader [*w]$0[]:[w]\n$1", _filepath.string(), infoLog);
}

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// color attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// texture coord attribute
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
}

void OpenGLShader::deleteShader()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,16 @@ namespace atta
{
std::shared_ptr<OpenGLRenderer> renderer = std::static_pointer_cast<OpenGLRenderer>(GraphicsManager::getRendererAPI());
std::shared_ptr<OpenGLImage> image = renderer->getOpenGLImages()[sid.getId()];
static std::map<StringId, bool> lastWarns;// Used to avoid spamming warn

if(!image)
{
LOG_WARN("OpenGLShaderGroup", "(setTexture) Trying to use image that was never loaded: $0 = \"$1\"", name, sid);
if(!lastWarns[sid])
LOG_WARN("OpenGLShaderGroup", "(setTexture) Trying to use image that was never loaded: $0 = \"$1\"", name, sid);
lastWarns[sid] = true;
return;
}
lastWarns[sid] = false;

int imgUnit = -1;
for(unsigned i = 0; i < _textureUnits.size(); i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ namespace atta
{
GLenum openGLType = convertBaseType(element.type);

// Enable attribute
glEnableVertexAttribArray(i);

// Define attribute format
if(openGLType == GL_INT)
{
Expand All @@ -41,6 +38,9 @@ namespace atta
_layout.getStride(),
reinterpret_cast<void*>(element.offset));
}

// Enable attribute
glEnableVertexAttribArray(i);
i++;
}
}
Expand Down
Loading