Commit dc75592b authored by Dario Seyb's avatar Dario Seyb

setting up gl state for ui drawing

parent e5597b6c
#version 330
uniform sampler2D Texture;
in vec2 Frag_UV;
in vec4 Frag_Color;
out vec4 Out_Color;
void main()
{
Out_Color = Frag_Color * texture( Texture, Frag_UV.st);
}
\ No newline at end of file
#version 330
uniform mat4 ProjMtx;
in vec2 Position;
in vec2 UV;
in vec4 Color;
out vec2 Frag_UV;
out vec4 Frag_Color;
void main()
{
Frag_UV = UV;
Frag_Color = Color;
gl_Position = ProjMtx * vec4(Position.xy,0,1);
}
\ No newline at end of file
......@@ -30,7 +30,7 @@ class TextureData
// ========================================================================================================= \/
public:
TextureData(void)
: mData(NULL),
: mData(nullptr, [](GLubyte* ptr) { delete[] ptr; }),
mWidth(0),
mHeight(0),
mDepth(0),
......@@ -39,17 +39,13 @@ public:
mPaddingBytesPerRow(0),
mColorSpace(ColorSpace::AUTO_DETECT)
{}
virtual ~TextureData(void)
{
delete[] mData;
}
// ========================================================================================================= \/
// ================================================================================================= GETTERS \/
// ========================================================================================================= \/
public:
//! pointer to the raw pixel data
GLubyte* getData() const { return mData; }
GLubyte* getData() const { return mData.get(); }
//! width in pixels
GLsizei getWidth() const { return mWidth; }
......@@ -107,8 +103,20 @@ public:
// ================================================================================================= SETTERS \/
// ========================================================================================================= \/
public:
typedef void(*data_deleter)(GLubyte*);
typedef std::unique_ptr<GLubyte, data_deleter> DataPtr;
void setData (DataPtr&& _data) { mData = std::move(_data); }
//! mData has to be created by new GLubyte[...] and will get deleted by this TextureData object!
void setData (GLubyte* _data) { mData = _data; }
void setData(GLubyte* _data) {
mData = DataPtr(_data, [](GLubyte* ptr) { delete[] ptr; });
}
void setData(GLubyte* _data, data_deleter _deleter) {
mData = DataPtr(_data, _deleter);
}
void setWidth (GLsizei _width) { mWidth = _width; }
void setHeight (GLsizei _height) { mHeight = _height; }
void setDepth (GLsizei _depth) { mDepth = _depth; }
......@@ -119,7 +127,7 @@ public:
void setSize (const glm::uvec3& _size) { mWidth = _size.x; mHeight = _size.y; mDepth = _size.z; }
//! deletes the data attached to this object
void deleteData() { delete[] mData; mData = NULL; }
void deleteData() { mData.reset(nullptr); }
private:
size_t getBytesPerScanline() const;
......@@ -130,7 +138,7 @@ private:
// ========================================================================================================= \/
// ================================================================================================== FIELDS \/
// ========================================================================================================= \/
GLubyte* mData; // TODO: make this a std::unique_ptr to allow custom deleters
DataPtr mData;
GLsizei mWidth;
GLsizei mHeight;
GLsizei mDepth;
......
......@@ -12,8 +12,8 @@ namespace OpenGL {
GLsizei TextureData::getPackAlignment() const
{
size_t dataAlignment = Utils::pointerAlignment( mData );
size_t rowAlignment = Utils::pointerAlignment( mData + getBytesPerScanline() );
size_t dataAlignment = Utils::pointerAlignment( mData.get());
size_t rowAlignment = Utils::pointerAlignment( mData.get() + getBytesPerScanline() );
return (GLsizei) std::min(dataAlignment, rowAlignment); //minimum of the data and the begining of the second row
}
......@@ -39,8 +39,8 @@ void TextureData::flipVertically()
for (GLsizei line = 0; line < mHeight/2; ++line) {
size_t topLine = line;
size_t bottomLine = mHeight - line - 1;
void *topLinePtr = mData + topLine*scanlineInBytes;
void *bottomLinePtr = mData + bottomLine*scanlineInBytes;
void *topLinePtr = mData.get() + topLine*scanlineInBytes;
void *bottomLinePtr = mData.get() + bottomLine*scanlineInBytes;
memcpy( tmpScanLine, topLinePtr, scanlineInBytes ); // top -> tmp
memcpy( topLinePtr, bottomLinePtr, scanlineInBytes ); // bottom -> top
memcpy( bottomLinePtr, tmpScanLine, scanlineInBytes ); // tmp -> bottom
......@@ -145,49 +145,49 @@ glm::vec4 TextureData::getTexel( glm::uvec2 _texCoord )
glm::vec4 result = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
if ( mType == GL_BYTE ) {
GLbyte *data = (GLbyte *) (mData+texelOffset);
GLbyte *data = (GLbyte *) (mData.get()+texelOffset);
result.r = data[0]/128.0f; // to -1..1
if ( getNumberOfChannels() > 1 ) result.g = data[1]/128.0f;
if ( getNumberOfChannels() > 2 ) result.b = data[2]/128.0f;
if ( getNumberOfChannels() > 3 ) result.a = data[3]/128.0f;
} else if ( mType == GL_UNSIGNED_BYTE ) {
GLubyte *data = (GLubyte *) (mData+texelOffset);
GLubyte *data = (GLubyte *) (mData.get() +texelOffset);
result.r = data[0]/255.0f; // to 0..1
if ( getNumberOfChannels() > 1 ) result.g = data[1]/255.0f;
if ( getNumberOfChannels() > 2 ) result.b = data[2]/255.0f;
if ( getNumberOfChannels() > 3 ) result.a = data[3]/255.0f;
} else if ( mType == GL_SHORT ) {
GLshort *data = (GLshort *) (mData+texelOffset);
GLshort *data = (GLshort *) (mData.get() +texelOffset);
result.r = data[0]/32768.0f; // to -1..1
if ( getNumberOfChannels() > 1 ) result.g = data[1]/32768.0f;
if ( getNumberOfChannels() > 2 ) result.b = data[2]/32768.0f;
if ( getNumberOfChannels() > 3 ) result.a = data[3]/32768.0f;
} else if ( mType == GL_UNSIGNED_SHORT ) {
GLushort *data = (GLushort *) (mData+texelOffset);
GLushort *data = (GLushort *) (mData.get() +texelOffset);
result.r = data[0]/65535.0f; // to 0..1
if ( getNumberOfChannels() > 1 ) result.g = data[1]/65535.0f;
if ( getNumberOfChannels() > 2 ) result.b = data[2]/65535.0f;
if ( getNumberOfChannels() > 3 ) result.a = data[3]/65535.0f;
} else if ( mType == GL_INT ) {
GLint *data = (GLint *) (mData+texelOffset);
GLint *data = (GLint *) (mData.get() +texelOffset);
result.r = data[0]/2147483648.0f; // to -1..1
if ( getNumberOfChannels() > 1 ) result.g = data[1]/2147483648.0f;
if ( getNumberOfChannels() > 2 ) result.b = data[2]/2147483648.0f;
if ( getNumberOfChannels() > 3 ) result.a = data[3]/2147483648.0f;
} else if ( mType == GL_UNSIGNED_INT ) {
GLuint *data = (GLuint *) (mData+texelOffset);
GLuint *data = (GLuint *) (mData.get() +texelOffset);
result.r = data[0]/4294967295.0f; // to 0..1
if ( getNumberOfChannels() > 1 ) result.g = data[1]/4294967295.0f;
if ( getNumberOfChannels() > 2 ) result.b = data[2]/4294967295.0f;
if ( getNumberOfChannels() > 3 ) result.a = data[3]/4294967295.0f;
} else if ( mType == GL_FLOAT ) {
GLfloat *data = (GLfloat *) (mData+texelOffset);
GLfloat *data = (GLfloat *) (mData.get() +texelOffset);
result.r = data[0];
if ( getNumberOfChannels() > 1 ) result.g = data[1];
if ( getNumberOfChannels() > 2 ) result.b = data[2];
......@@ -196,7 +196,7 @@ glm::vec4 TextureData::getTexel( glm::uvec2 _texCoord )
}
#if defined(GL_DOUBLE)
else if ( mType == GL_DOUBLE ) {
GLdouble *data = (GLdouble *) (mData+texelOffset);
GLdouble *data = (GLdouble *) (mData.get() +texelOffset);
result.r = (float) data[0];
if ( getNumberOfChannels() > 1 ) result.g = (float) data[1];
if ( getNumberOfChannels() > 2 ) result.b = (float) data[2];
......@@ -226,7 +226,7 @@ void TextureData::setTexel( glm::uvec2 _texCoord, glm::vec4 _color )
texelOffset += _texCoord.x * getNumberOfChannels()*getGLTypeSize(mType);
if ( mType == GL_BYTE ) {
GLbyte *data = (GLbyte *) (mData+texelOffset);
GLbyte *data = (GLbyte *) (mData.get() +texelOffset);
glm::ivec4 color = glm::ivec4( _color * glm::vec4(128.0f) );
color = glm::clamp( color, glm::ivec4(-128), glm::ivec4(127) );
......@@ -237,7 +237,7 @@ void TextureData::setTexel( glm::uvec2 _texCoord, glm::vec4 _color )
if ( getNumberOfChannels() > 3 ) data[3] = color.a;
} else if ( mType == GL_UNSIGNED_BYTE ) {
GLubyte *data = (GLubyte *) (mData+texelOffset);
GLubyte *data = (GLubyte *) (mData.get() +texelOffset);
glm::ivec4 color = glm::ivec4( _color * glm::vec4(255.0f) );
color = glm::clamp( color, glm::ivec4(0), glm::ivec4(255) );
......@@ -248,7 +248,7 @@ void TextureData::setTexel( glm::uvec2 _texCoord, glm::vec4 _color )
if ( getNumberOfChannels() > 3 ) data[3] = color.a;
} else if ( mType == GL_SHORT ) {
GLshort *data = (GLshort *) (mData+texelOffset);
GLshort *data = (GLshort *) (mData.get() +texelOffset);
glm::ivec4 color = glm::ivec4( _color * glm::vec4(32768.0f) );
color = glm::clamp( color, glm::ivec4(-32768), glm::ivec4(32767) );
......@@ -259,7 +259,7 @@ void TextureData::setTexel( glm::uvec2 _texCoord, glm::vec4 _color )
if ( getNumberOfChannels() > 3 ) data[3] = color.a;
} else if ( mType == GL_UNSIGNED_SHORT ) {
GLushort *data = (GLushort *) (mData+texelOffset);
GLushort *data = (GLushort *) (mData.get() +texelOffset);
glm::ivec4 color = glm::ivec4( _color * glm::vec4(65535.0f) );
color = glm::clamp( color, glm::ivec4(0), glm::ivec4(65535) );
......@@ -270,7 +270,7 @@ void TextureData::setTexel( glm::uvec2 _texCoord, glm::vec4 _color )
if ( getNumberOfChannels() > 3 ) data[3] = color.a;
} else if ( mType == GL_INT ) {
GLint *data = (GLint *) (mData+texelOffset);
GLint *data = (GLint *) (mData.get() +texelOffset);
glm::ivec4 color = glm::ivec4( _color * glm::vec4(2147483648.0f) );
color = glm::clamp( color, glm::ivec4(std::numeric_limits<int>::min()), glm::ivec4(2147483647) );
......@@ -281,7 +281,7 @@ void TextureData::setTexel( glm::uvec2 _texCoord, glm::vec4 _color )
if ( getNumberOfChannels() > 3 ) data[3] = color.a;
} else if ( mType == GL_UNSIGNED_INT ) {
GLuint *data = (GLuint *) (mData+texelOffset);
GLuint *data = (GLuint *) (mData.get() +texelOffset);
glm::ivec4 color = glm::ivec4( _color * glm::vec4(4294967295.0f) );
color = glm::clamp( color, glm::ivec4(0), glm::ivec4(4294967295) );
......@@ -292,7 +292,7 @@ void TextureData::setTexel( glm::uvec2 _texCoord, glm::vec4 _color )
if ( getNumberOfChannels() > 3 ) data[3] = color.a;
} else if ( mType == GL_FLOAT ) {
GLfloat *data = (GLfloat *) (mData+texelOffset);
GLfloat *data = (GLfloat *) (mData.get() +texelOffset);
data[0] = _color.r;
if ( getNumberOfChannels() > 1 ) data[1] = _color.g;
......@@ -302,7 +302,7 @@ void TextureData::setTexel( glm::uvec2 _texCoord, glm::vec4 _color )
}
#if defined(GL_DOUBLE)
else if ( mType == GL_DOUBLE ) {
GLdouble *data = (GLdouble *) (mData+texelOffset);
GLdouble *data = (GLdouble *) (mData.get() +texelOffset);
data[0] = (float) _color.r;
if ( getNumberOfChannels() > 1 ) data[1] = (float) _color.g;
......
......@@ -33,13 +33,14 @@ private:
FMOD::Channel* playSound(Sound* sound);
void release(FMOD::Sound* sound);
void setListenerProperties();
public:
CONSTRUCT_SYSTEM(AudioSystem), m_nextId(1), m_isActive(false) {}
void update();
void setListener(Entity listener) { m_listener = listener; }
void setListener(Entity listener) { m_listener = listener; setListenerProperties(); }
std::shared_ptr<Sound> createSound(std::string filename, SoundMode mode);
bool startup() override;
......
......@@ -6,5 +6,6 @@
using namespace ACGL::OpenGL;
struct Material {
SharedShaderProgram prog;
};
\ No newline at end of file
......@@ -15,8 +15,6 @@ private:
WindowSystem *m_window;
Stack<DrawCall> m_submittedDrawCalls;
void draw(const DrawCall &drawCall, glm::mat4 view, glm::mat4 proj);
Entity m_mainCamera;
public:
CONSTRUCT_SYSTEM(RendererSystem), m_submittedDrawCalls(kilobytes(4)) {}
......@@ -25,7 +23,9 @@ public:
void shutdown() override;
void setMainCamera(Entity cam) { m_mainCamera = cam; }
void submit(DrawCall drawCall);
inline void submit(DrawCall drawCall) {
m_submittedDrawCalls.push(drawCall);
}
void frame(double interp);
};
\ No newline at end of file
......@@ -14,6 +14,12 @@ private:
WindowSystem* m_window;
RendererSystem* m_renderer;
SharedTextureData m_fontTextureData;
SharedTexture2D m_fontTexture;
SharedVertexArrayObject m_imguiVao;
SharedShaderProgram m_imguiProg;
bool m_mousePressed[3];
float m_mouseWheel;
......@@ -21,6 +27,8 @@ private:
void renderDrawLists(ImDrawData* drawData);
void prepareRender();
void setupGLObjects(ImGuiIO& io);
public:
CONSTRUCT_SYSTEM(UISystem) {}
......
......@@ -43,7 +43,7 @@ FMOD::Channel* AudioSystem::playSound(Sound* sound) {
return resultChannel;
}
void AudioSystem::update() {
void AudioSystem::setListenerProperties() {
if (m_listener.valid()) {
auto transform = m_listener.component<Transform>();
if (transform.valid()) {
......@@ -53,7 +53,7 @@ void AudioSystem::update() {
FMOD_VECTOR fmodPos = { pos.x, pos.y, pos.z };
FMOD_VECTOR fmodVel = { 0, 0, 0 };
auto up = rot * glm::vec4(0, 1, 0, 0);
auto up = rot * glm::vec4(0, 1, 0, 0);
auto forward = rot * glm::vec4(0, 0, -1, 0);
FMOD_VECTOR fmodUp = { up.x, up.y, up.z };
......@@ -62,13 +62,17 @@ void AudioSystem::update() {
m_fmodSystem->set3DListenerAttributes(0, &fmodPos, &fmodVel, &fmodForward, &fmodUp);
}
}
}
void AudioSystem::update() {
setListenerProperties();
auto drawableEntities = m_scene->entities_with_components<SoundSource, Transform>();
auto soundSources = m_scene->entities_with_components<SoundSource, Transform>();
SoundSource::Handle soundSource;
Transform::Handle transform;
for (auto e : drawableEntities) {
for (auto e : soundSources) {
e.unpack<SoundSource, Transform>(soundSource, transform);
FMOD::Sound* currentSound;
auto result = soundSource->m_channel->getCurrentSound(&currentSound);
......@@ -76,7 +80,6 @@ void AudioSystem::update() {
continue;
}
;
FMOD_VECTOR fmodPos = { transform->position.x, transform->position.y, transform->position.z };
FMOD_VECTOR fmodVel = { 0, 0, 0 };
soundSource->m_channel->set3DAttributes(&fmodPos, &fmodVel);
......
#pragma once
#include <engine/core/SettingsSystem.hpp>
#include <ACGL/ACGL.hh>
#include <ACGL/Base/Settings.hh>
......
......@@ -15,18 +15,7 @@ bool RendererSystem::startup() {
return true;
}
void RendererSystem::submit(DrawCall drawCall) {
m_submittedDrawCalls.push(drawCall);
}
void RendererSystem::draw(const DrawCall &drawCall, glm::mat4 view, glm::mat4 proj) {
drawCall.geometry.vao->bind();
drawCall.material.prog->use();
glm::mat4 viewMatrix = view * drawCall.transform;
drawCall.material.prog->setUniform("uViewMatrix", viewMatrix);
drawCall.material.prog->setUniform("uProjectionMatrix", proj );
drawCall.geometry.vao->render();
}
void RendererSystem::frame(double interp) {
glClearColor(0.2, 0.2, 0.2, 1.0);
......@@ -51,10 +40,15 @@ void RendererSystem::frame(double interp) {
auto windowSize = m_window->getSize();
auto projectioMatrix = glm::perspectiveFov<float>(cam->fov, windowSize.x, windowSize.y, cam->near, cam->far) *
glm::inverse(glm::translate<float>(trans->position) * trans->rotation);
auto viewMatrix = glm::mat4();
for (size_t i = 0; i < m_submittedDrawCalls.size(); i++) {
draw(m_submittedDrawCalls[i], viewMatrix, projectioMatrix);
auto drawCall = m_submittedDrawCalls[i];
drawCall.geometry.vao->bind();
drawCall.material.prog->use();
glm::mat4 viewMatrix = drawCall.transform;
drawCall.material.prog->setUniform("uViewMatrix", viewMatrix);
drawCall.material.prog->setUniform("uProjectionMatrix", projectioMatrix);
drawCall.geometry.vao->render();
}
openGLCriticalError();
......
......@@ -2,6 +2,14 @@
#include <engine/events/KeyboardEvent.hpp>
#include <engine/events/MouseEvent.hpp>
#include <SDL_syswm.h>
#include <ACGL/OpenGL/Creator/Texture2DCreator.hh>
#include <ACGL/OpenGL/Creator/ShaderProgramCreator.hh>
#include <ACGL/OpenGL/Creator/VertexArrayObjectCreator.hh>
#include <ACGL/OpenGL/Objects.hh>
using namespace ACGL::OpenGL;
using namespace ACGL::Base;
bool UISystem::startup() {
RESOLVE_DEPENDENCY(m_events);
......@@ -47,10 +55,12 @@ bool UISystem::startup() {
io.ImeWindowHandle = wmInfo.info.win.window;
#endif
/*m_events->subscribe<"StartFrame"_sh>(
setupGLObjects(io);
m_events->subscribe<"StartFrame"_sh>(
[this](const SEvent<"StartFrame"_sh> &_) { newFrame(); });
m_events->subscribe<"PrepareDraw"_sh>(
[this](const SEvent<"PrepareDraw"_sh> &_) { prepareRender(); });*/
[this](const SEvent<"PrepareDraw"_sh> &_) { prepareRender(); });
m_events->subscribe<KeyboardEvent>([&io](const KeyboardEvent &e) {
if (e.originalEvent.type == SDL_KEYDOWN ||
......@@ -85,6 +95,45 @@ bool UISystem::startup() {
return true;
}
void UISystem::setupGLObjects(ImGuiIO& io) {
m_imguiVao = std::make_shared<VertexArrayObject>(GL_TRIANGLES);
SharedArrayBuffer buffer = std::make_shared<ArrayBuffer>();
#define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT))
buffer->defineAttributeWithOffset("Position", GL_FLOAT, 2, OFFSETOF(ImDrawVert, pos));
buffer->defineAttributeWithOffset("UV", GL_FLOAT, 2, OFFSETOF(ImDrawVert, uv));
buffer->defineAttributeWithOffset("Color", GL_UNSIGNED_BYTE, 4, OFFSETOF(ImDrawVert, col));
#undef OFFSETOF
m_imguiVao->attachAllAttributes(buffer);
m_imguiProg = ShaderProgramCreator("ImGui").attributeLocations(m_imguiVao->getAttributeLocations()).create();
// Build texture atlas
unsigned char* pixels;
int width, height;
// Load as RGBA 32-bits for OpenGL3 demo because it is more likely to be compatible with user's existing shader.
io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
m_fontTextureData = std::make_shared<TextureData>();
// No need to delete pixels since ClearTexData below does that.
// Just needed to upload it to a texture
m_fontTextureData->setData(pixels, [](GLubyte* data) {});
m_fontTextureData->setFormat(GL_RGBA);
m_fontTextureData->setWidth(width);
m_fontTextureData->setHeight(height);
m_fontTexture = std::make_shared<Texture2D>(GL_RGBA);
m_fontTexture->setImageData(m_fontTextureData);
// Store our identifier
io.Fonts->TexID = (void *)(intptr_t)m_fontTexture->getObjectName();
// Cleanup (don't clear the input data if you want to append new fonts later)
io.Fonts->ClearInputData();
io.Fonts->ClearTexData();
}
void UISystem::newFrame() {
ImGuiIO &io = ImGui::GetIO();
......@@ -97,8 +146,6 @@ void UISystem::newFrame() {
io.DeltaTime = (float)(1.0f / 60.0f);
// Setup inputs
// (we already got mouse wheel, keyboard keys & characters from glfw callbacks
// polled in glfwPollEvents())
int mx, my;
Uint32 mouseMask = SDL_GetMouseState(&mx, &my);
if (SDL_GetWindowFlags(m_window->getWindowHandle()) & SDL_WINDOW_MOUSE_FOCUS) {
......@@ -130,10 +177,12 @@ void UISystem::newFrame() {
ImGui::NewFrame();
}
void UISystem::renderDrawLists(ImDrawData *drawData) {}
void UISystem::renderDrawLists(ImDrawData *drawData) {
void UISystem::prepareRender() {
}
void UISystem::prepareRender() {
ImGui::ShowMetricsWindow();
ImGui::Render();
renderDrawLists(ImGui::GetDrawData());
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment