Skip to content

Commit

Permalink
Client: Load shader definitions, automatic GuiWidget initialization
Browse files Browse the repository at this point in the history
GuiRootWidget also owns a large atlas for all UI images.
  • Loading branch information
skyjake committed May 16, 2013
1 parent 33d240a commit 143c1ee
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 3 deletions.
60 changes: 60 additions & 0 deletions doomsday/client/data/shaders.dei
@@ -0,0 +1,60 @@
# Doomsday's core set of shaders
#
# In each "shader" block, there can be:
# - path: path to both the .vsh and .fsh files (omit extension:
# "shaders/test" => shaders/test.vsh, shaders/test.fsh)
# - path.vertex: path to the vertex shader file
# - path.fragment: path to the fragment shader file
# - vertex: source of the vertex shader
# - fragment: source of the fragment shader

group generic {
# Simple shader with untextured vertices. There is an additional constant
# color applied to all vertices. Uses a combined model-view-projection
# matrix.
shader color {
vertex = "
uniform highp mat4 uMvpMatrix;
uniform highp vec4 uColor;
attribute highp vec4 aVertex;
attribute highp vec4 aColor;
varying highp vec4 vColor;

void main(void) {
gl_Position = uMvpMatrix * aVertex;
vColor = uColor * aColor;
}"
fragment = "
varying highp vec4 vColor;

void main(void) {
gl_FragColor = vColor;
}"
}

# Simple shader with one texture plus a color per vertex. Uses a
# combined model-view-projection matrix.
shader tex_color {
vertex = "
uniform highp mat4 uMvpMatrix;
attribute highp vec4 aVertex;
attribute highp vec2 aUV;
attribute highp vec4 aColor;
varying highp vec2 vUV;
varying highp vec4 vColor;

void main(void) {
gl_Position = uMvpMatrix * aVertex;
vUV = aUV;
vColor = aColor;
}"
fragment = "
uniform sampler2D uTex;
varying highp vec2 vUV;
varying highp vec4 vColor;

void main(void) {
gl_FragColor = vColor * texture2D(uTex, vUV);
}"
}
}
2 changes: 2 additions & 0 deletions doomsday/client/include/clientapp.h
Expand Up @@ -20,6 +20,7 @@
#define CLIENTAPP_H

#include <de/GuiApp>
#include <de/GLShaderBank>
#include "network/serverlink.h"
#include "ui/inputsystem.h"
#include "ui/windowsystem.h"
Expand Down Expand Up @@ -48,6 +49,7 @@ class ClientApp : public de::GuiApp
static InputSystem &inputSystem();
static WindowSystem &windowSystem();
static WidgetActions &widgetActions();
static de::GLShaderBank &glShaderBank();

private:
DENG2_PRIVATE(d)
Expand Down
8 changes: 8 additions & 0 deletions doomsday/client/include/ui/widgets/guirootwidget.h
Expand Up @@ -20,6 +20,8 @@
#define CLIENT_GUIROOTWIDGET_H

#include <de/RootWidget>
#include <de/AtlasTexture>
#include <de/GLShaderBank>

class ClientWindow;

Expand All @@ -44,6 +46,12 @@ class GuiRootWidget : public de::RootWidget
*/
ClientWindow &window();

de::AtlasTexture &atlas();
de::GLShaderBank &shaders();

// Events.
void update();

private:
DENG2_PRIVATE(d)
};
Expand Down
3 changes: 3 additions & 0 deletions doomsday/client/include/ui/widgets/guiwidget.h
Expand Up @@ -51,6 +51,9 @@ class GuiWidget : public de::Widget

void deleteLater();

// Events.
void update();

private:
DENG2_PRIVATE(d)
};
Expand Down
15 changes: 15 additions & 0 deletions doomsday/client/src/clientapp.cpp
Expand Up @@ -72,6 +72,7 @@ DENG2_PIMPL(ClientApp)
std::auto_ptr<WidgetActions> widgetActions;
WindowSystem *winSys;
ServerLink *svLink;
GLShaderBank shaderBank;

Instance(Public *i)
: Base(i),
Expand Down Expand Up @@ -160,6 +161,15 @@ void ClientApp::initialize()
}
#endif

// Load all the shader program definitions.
FS::FoundFiles found;
fileSystem().findAll("shaders.dei", found);
DENG2_FOR_EACH(FS::FoundFiles, i, found)
{
LOG_MSG("Loading shader definitions from %s") << (*i)->description();
d->shaderBank.addFromInfo(**i);
}

// Create the window system.
d->winSys = new WindowSystem;
addSystem(*d->winSys);
Expand Down Expand Up @@ -237,3 +247,8 @@ WidgetActions &ClientApp::widgetActions()
{
return *app().d->widgetActions.get();
}

GLShaderBank &ClientApp::glShaderBank()
{
return app().d->shaderBank;
}
35 changes: 34 additions & 1 deletion doomsday/client/src/ui/widgets/guirootwidget.cpp
Expand Up @@ -17,14 +17,23 @@
*/

#include "ui/widgets/guirootwidget.h"
#include "ui/clientwindow.h"
#include "clientapp.h"

#include <de/AtlasTexture>
#include <de/GLTexture>

using namespace de;

DENG2_PIMPL(GuiRootWidget)
{
ClientWindow *window;
QScopedPointer<AtlasTexture> atlas; ///< Shared atlas for most UI graphics/text.

Instance(Public *i, ClientWindow *win) : Base(i), window(win)
Instance(Public *i, ClientWindow *win)
: Base(i),
window(win),
atlas(0)
{}
};

Expand All @@ -42,3 +51,27 @@ ClientWindow &GuiRootWidget::window()
DENG2_ASSERT(d->window != 0);
return *d->window;
}

AtlasTexture &GuiRootWidget::atlas()
{
if(d->atlas.isNull())
{
d->atlas.reset(AtlasTexture::newWithRowAllocator(
Atlas::BackingStore | Atlas::AllowDefragment,
GLTexture::maximumSize()));
}
return *d->atlas;
}

GLShaderBank &GuiRootWidget::shaders()
{
return ClientApp::glShaderBank();
}

void GuiRootWidget::update()
{
// Allow GL operations.
window().canvas().makeCurrent();

RootWidget::update();
}
13 changes: 11 additions & 2 deletions doomsday/client/src/ui/widgets/guiwidget.cpp
Expand Up @@ -26,9 +26,9 @@ using namespace de;
DENG2_PIMPL(GuiWidget)
{
RuleRectangle rule;
bool needInit;

Instance(Public *i) : Base(i)
{}
Instance(Public *i) : Base(i), needInit(true) {}
};

GuiWidget::GuiWidget(String const &name) : Widget(name), d(new Instance(this))
Expand Down Expand Up @@ -63,3 +63,12 @@ void GuiWidget::deleteLater()
{
Garbage_TrashInstance(this, deleteGuiWidget);
}

void GuiWidget::update()
{
if(d->needInit)
{
initialize();
d->needInit = false;
}
}

0 comments on commit 143c1ee

Please sign in to comment.