Skip to content

Commit

Permalink
Tests|libgui: Updating test_glsandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Sep 1, 2019
1 parent d71d0c1 commit de95716
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 94 deletions.
12 changes: 6 additions & 6 deletions doomsday/tests/test_glsandbox/CMakeLists.txt
Expand Up @@ -2,24 +2,24 @@ cmake_minimum_required (VERSION 3.1)
project (DE_TEST_GLSANDBOX)
include (../TestConfig.cmake)

find_package (Qt5 COMPONENTS Gui Widgets)
find_package (DengAppfw)
#find_package (Qt5 COMPONENTS Gui Widgets)
#find_package (DengGui REQUIRED)

list (APPEND DE_REQUIRED_PACKAGES net.dengine.stdlib)

deng_add_package (net.dengine.test.glsandbox)

deng_add_application (test_glsandbox
main.cpp
testwindow.cpp
testwindow.h
glsandbox.qrc
)

target_link_libraries (test_glsandbox PRIVATE Deng::libappfw)
target_link_libraries (test_glsandbox PRIVATE Deng::libgui SDL2 SDL2main)

if (APPLE)
set_property (TARGET test_glsandbox PROPERTY OUTPUT_NAME test_glsandbox)
deng_install_bundle_deps (test_glsandbox
Deng::libcore Deng::libgui Deng::libappfw Deng::libshell
Deng::libcore Deng::libgui Deng::libshell
)
deng_install_deployqt (test_glsandbox)
endif ()
5 changes: 0 additions & 5 deletions doomsday/tests/test_glsandbox/glsandbox.qrc

This file was deleted.

18 changes: 7 additions & 11 deletions doomsday/tests/test_glsandbox/main.cpp
Expand Up @@ -17,22 +17,19 @@
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include "testwindow.h"

#include <de/GuiApp>
#include <de/LogBuffer>
#include <QTimer>
#include <QDebug>

#include "testwindow.h"
#include <SDL2/SDL_main.h>

using namespace de;

int main(int argc, char **argv)
DE_EXTERN_C int main(int argc, char **argv)
{
try
{
GuiApp::setDefaultOpenGLFormat();

GuiApp app(argc, argv);
GuiApp app(makeList(argc, argv));
app.addInitPackage("net.dengine.test.glsandbox");
app.initSubsystems(App::DisablePlugins);

Expand All @@ -43,9 +40,8 @@ int main(int argc, char **argv)
}
catch (Error const &err)
{
qWarning() << err.asText();
err.warnPlainText();
}

qDebug("Exiting main()...");
debug("Exiting main()...");
return 0;
}
Binary file removed doomsday/tests/test_glsandbox/testpic.png
Binary file not shown.
131 changes: 63 additions & 68 deletions doomsday/tests/test_glsandbox/testwindow.cpp
Expand Up @@ -19,10 +19,6 @@

#include "testwindow.h"

#include <QMessageBox>
#include <QPainter>
#include <QToolBar>

#include <de/AtlasTexture>
#include <de/Drawable>
#include <de/FileSystem>
Expand All @@ -36,60 +32,59 @@
#include <de/ImageBank>
#include <de/ModelDrawable>

#include <SDL2/SDL_messagebox.h>

using namespace de;

DE_PIMPL(TestWindow),
DE_OBSERVES(GLWindow, Init),
DE_OBSERVES(GLWindow, Resize),
DE_OBSERVES(Clock, TimeChange),
DE_OBSERVES(Bank, Load)
namespace dgl = de::gl;

DE_PIMPL(TestWindow)
, DE_OBSERVES(GLWindow, Init)
, DE_OBSERVES(GLWindow, Resize)
, DE_OBSERVES(Clock, TimeChange)
, DE_OBSERVES(Bank, Load)
{
enum Mode
{
TestRenderToTexture,
TestDynamicAtlas,
TestModel
};
enum Mode { TestRenderToTexture, TestDynamicAtlas, TestModel };

Mode mode;
Mode mode;
ImageBank imageBank;
Drawable ob;
Drawable atlasOb;
Mat4f modelMatrix;
Mat4f projMatrix;
Drawable ob;
Drawable atlasOb;
Mat4f modelMatrix;
Mat4f projMatrix;
GLUniform uMvpMatrix;
GLUniform uColor;
GLUniform uTime;
GLUniform uTex;
GLTexture frameTex;
GLTexture testpic;

ModelDrawable model;
ModelDrawable::Animator modelAnim;
QScopedPointer<AtlasTexture> modelAtlas;
std::unique_ptr<AtlasTexture> modelAtlas;
GLUniform uModelTex;
GLProgram modelProgram;
QScopedPointer<AtlasTexture> atlas;
QScopedPointer<GLFramebuffer> frameTarget;

std::unique_ptr<AtlasTexture> atlas;
std::unique_ptr<GLFramebuffer> frameTarget;
Time startedAt;
Time lastAtlasAdditionAt;
bool eraseAtlas;

typedef GLBufferT<Vertex3TexRgba> VertexBuf;
typedef GLBufferT<Vertex2Tex> Vertex2Buf;

Impl(Public *i)
: Base(i),
mode (TestRenderToTexture),
//imageBank (0),
uMvpMatrix("uMvpMatrix", GLUniform::Mat4),
uColor ("uColor", GLUniform::Vec4),
uTime ("uTime", GLUniform::Float),
uTex ("uTex", GLUniform::Sampler2D),
modelAnim (model),
uModelTex ("uTex", GLUniform::Sampler2D),
atlas (AtlasTexture::newWithRowAllocator(Atlas::AllowDefragment |
Atlas::BackingStore |
Atlas::WrapBordersInBackingStore))
Impl(Public * i)
: Base(i)
, mode(TestRenderToTexture)
, uMvpMatrix("uMvpMatrix", GLUniform::Mat4)
, uColor("uColor", GLUniform::Vec4)
, uTime("uTime", GLUniform::Float)
, uTex("uTex", GLUniform::Sampler2D)
, modelAnim(model)
, uModelTex("uTex", GLUniform::Sampler2D)
, atlas(AtlasTexture::newWithRowAllocator(Atlas::AllowDefragment | Atlas::BackingStore |
Atlas::WrapBordersInBackingStore))
{
// Use this as the main window.
setMain(i);
Expand All @@ -101,7 +96,7 @@ DE_OBSERVES(Bank, Load)
uColor = Vec4f(.5f, .75f, .5f, 1);
atlas->setTotalSize(Vec2ui(256, 256));
atlas->setBorderSize(2);
atlas->setMagFilter(gl::Nearest);
atlas->setMagFilter(dgl::Nearest);

imageBank.add("rtt.cube", "/packs/net.dengine.test.glsandbox/testpic.png");
//imageBank.loadAll();
Expand Down Expand Up @@ -129,9 +124,11 @@ DE_OBSERVES(Bank, Load)
}
catch (Error const &er)
{
qWarning() << er.asText();
er.warnPlainText();

QMessageBox::critical(nullptr, "GL Init Error", er.asText());
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "GL Init Error",
er.asText().c_str(), nullptr);
// QMessageBox::critical(nullptr, "GL Init Error", er.asText());
exit(1);
}
}
Expand All @@ -141,17 +138,17 @@ DE_OBSERVES(Bank, Load)
// Set up the default state.
GLState &st = GLState::current();
st.setBlend(true);
st.setBlendFunc(gl::SrcAlpha, gl::OneMinusSrcAlpha);
//st.setCull(gl::Back);
st.setBlendFunc(dgl::SrcAlpha, dgl::OneMinusSrcAlpha);
//st.setCull(dgl::Back);
st.setDepthTest(true);

// Textures.
testpic.setAutoGenMips(true);
imageBank.load("rtt.cube");
//testpic.setImage(imageBank.image("rtt/cube"));
//testpic.setImage(QImage(":/images/testpic.png"));
testpic.setWrapT(gl::RepeatMirrored);
testpic.setMinFilter(gl::Linear, gl::MipLinear);
testpic.setWrapT(dgl::RepeatMirrored);
testpic.setMinFilter(dgl::Linear, dgl::MipLinear);
uTex = testpic;

// Prepare the custom target.
Expand All @@ -173,15 +170,15 @@ DE_OBSERVES(Bank, Load)
{ Vec3f(-1, 1, 1), Vec2f(1, 0), Vec4f(0, 0, 1, 1) }
};

buf->setVertices(verts, 8, gl::Static);
buf->setVertices(verts, 8, dgl::Static);

GLBuffer::Indices idx;
idx << 0 << 4 << 3 << 7 << 2 << 6 << 1 << 5 << 0 << 4
<< 4 << 0
<< 0 << 3 << 1 << 2
<< 2 << 7
<< 7 << 4 << 6 << 5;
buf->setIndices(gl::TriangleStrip, idx, gl::Static);
buf->setIndices(dgl::TriangleStrip, idx, dgl::Static);

ob.program().build(
ByteRefArray::fromCStr(
Expand Down Expand Up @@ -228,7 +225,7 @@ DE_OBSERVES(Bank, Load)
{ Vec2f(100, 100), Vec2f(1, 1) },
{ Vec2f(0, 100), Vec2f(0, 1) }
};
buf2->setVertices(gl::TriangleFan, verts2, 4, gl::Static);
buf2->setVertices(dgl::TriangleFan, verts2, 4, dgl::Static);
atlasOb.addBuffer(buf2);

atlasOb.program().build(
Expand Down Expand Up @@ -490,25 +487,23 @@ DE_OBSERVES(Bank, Load)
}

#if 1
if ((qrand() % 10) <= 5 && !atlas->isEmpty())
if ((rand() % 10) <= 5 && !atlas->isEmpty())
{
// Randomly remove one of the allocations.
QList<Id> ids;
foreach (Id const &id, atlas->allImages()) ids << id;
Id chosen = ids[qrand() % ids.size()];
List<Id> ids;
for (const Id &id : atlas->allImages()) ids << id;
Id chosen = ids[rand() % ids.size()];
atlas->release(chosen);

LOG_DEBUG("Removed ") << chosen;
}
#endif

// Generate a random image.
QSize imgSize(10 + qrand() % 40, 10 + 10 * (qrand() % 2));
QImage img(imgSize, QImage::Format_ARGB32);
QPainter painter(&img);
painter.fillRect(img.rect(), QColor(qrand() % 256, qrand() % 256, qrand() % 256));
painter.setPen(Qt::white);
painter.drawRect(img.rect().adjusted(0, 0, -1, -1));
Image::Size imgSize(10 + rand() % 40, 10 + 10 * (rand() % 2));
Image img(imgSize, Image::RGBA_8888);
img.fill(Image::makeColor(rand() % 256, rand() % 256, rand() % 256));
img.drawRect(img.rect() /*.adjusted(0, 0, -1, -1)*/, Image::Color(255, 255, 255, 255));

Id id = atlas->alloc(img);
LOG_DEBUG("Allocated ") << id;
Expand All @@ -524,22 +519,20 @@ DE_OBSERVES(Bank, Load)

TestWindow::TestWindow() : d(new Impl(this))
{
qsrand(Time().asDateTime().toTime_t());

setTitle("libgui GL Sandbox");
setMinimumSize(QSize(640, 480));
setMinimumSize(Size(640, 480));

QToolBar *tools = new QToolBar(tr("Tests"));
tools->addAction("RTT", this, SLOT(testRenderToTexture()));
tools->addAction("Atlas", this, SLOT(testDynamicAtlas()));
tools->addAction("Model", this, SLOT(testModel()));
tools->addSeparator();
tools->addAction("MD2", this, SLOT(loadMD2Model()));
tools->addAction("MD5", this, SLOT(loadMD5Model()));
// QToolBar *tools = new QToolBar(tr("Tests"));
// tools->addAction("RTT", this, SLOT(testRenderToTexture()));
// tools->addAction("Atlas", this, SLOT(testDynamicAtlas()));
// tools->addAction("Model", this, SLOT(testModel()));
// tools->addSeparator();
// tools->addAction("MD2", this, SLOT(loadMD2Model()));
// tools->addAction("MD5", this, SLOT(loadMD5Model()));

tools->show();
// tools->show();

tools->setGeometry(25, 75, tools->width(), tools->height());
// tools->setGeometry(25, 75, tools->width(), tools->height());
}

void TestWindow::draw()
Expand All @@ -549,6 +542,7 @@ void TestWindow::draw()
LIBGUI_ASSERT_GL_OK();
}

/*
void TestWindow::keyPressEvent(QKeyEvent *ev)
{
if (ev->modifiers() & Qt::ControlModifier)
Expand Down Expand Up @@ -582,6 +576,7 @@ void TestWindow::keyPressEvent(QKeyEvent *ev)
GLWindow::keyPressEvent(ev);
}
*/

void TestWindow::testRenderToTexture()
{
Expand Down
5 changes: 1 addition & 4 deletions doomsday/tests/test_glsandbox/testwindow.h
Expand Up @@ -24,15 +24,12 @@

class TestWindow : public de::GLWindow
{
Q_OBJECT

public:
TestWindow();

void draw() override;
void keyPressEvent(QKeyEvent *ev) override;
// void keyPressEvent(QKeyEvent *ev) override;

public slots:
void testRenderToTexture();
void testDynamicAtlas();
void testModel();
Expand Down

0 comments on commit de95716

Please sign in to comment.