From b03381600f56dbcf6aa6e2a211b721e555c6d2e1 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 8 Mar 2020 17:47:14 +0100 Subject: Factor out cube drawing --- examples/cube_view.h | 82 ++++++++++++++++++++++++++++++++++++++++++++++ examples/demo_utils.h | 5 +++ examples/pugl_embed_demo.c | 65 ++++-------------------------------- 3 files changed, 93 insertions(+), 59 deletions(-) create mode 100644 examples/cube_view.h (limited to 'examples') diff --git a/examples/cube_view.h b/examples/cube_view.h new file mode 100644 index 0000000..fb32595 --- /dev/null +++ b/examples/cube_view.h @@ -0,0 +1,82 @@ +/* + Copyright 2012-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#define GL_SILENCE_DEPRECATION 1 + +#include "demo_utils.h" + +#include "pugl/gl.h" + +static inline void +reshapeCube(const int width, const int height) +{ + const float aspect = (float)width / (float)height; + + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LESS); + glClearColor(0.2f, 0.2f, 0.2f, 1.0f); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glViewport(0, 0, width, height); + + float projection[16]; + perspective(projection, 1.8f, aspect, 1.0f, 100.0f); + glLoadMatrixf(projection); +} + +static inline void +displayCube(PuglView* const view, + const double distance, + const double xAngle, + const double yAngle, + const bool entered) +{ + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0.0f, 0.0f, (float)distance * -1.0f); + glRotatef((float)xAngle, 0.0f, 1.0f, 0.0f); + glRotatef((float)yAngle, 1.0f, 0.0f, 0.0f); + + const float bg = entered ? 0.2f : 0.1f; + glClearColor(bg, bg, bg, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + if (puglHasFocus(view)) { + // Draw cube surfaces + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_COLOR_ARRAY); + glVertexPointer(3, GL_FLOAT, 0, cubeStripVertices); + glColorPointer(3, GL_FLOAT, 0, cubeStripVertices); + glDrawArrays(GL_TRIANGLE_STRIP, 0, 14); + glDisableClientState(GL_COLOR_ARRAY); + glDisableClientState(GL_VERTEX_ARRAY); + + glColor3f(0.0f, 0.0f, 0.0f); + } else { + glColor3f(1.0f, 1.0f, 1.0f); + } + + // Draw cube wireframe + glEnableClientState(GL_VERTEX_ARRAY); + glVertexPointer(3, GL_FLOAT, 0, cubeFrontLineLoop); + glDrawArrays(GL_LINE_LOOP, 0, 4); + glVertexPointer(3, GL_FLOAT, 0, cubeBackLineLoop); + glDrawArrays(GL_LINE_LOOP, 0, 4); + glVertexPointer(3, GL_FLOAT, 0, cubeSideLines); + glDrawArrays(GL_LINES, 0, 8); + glDisableClientState(GL_VERTEX_ARRAY); +} diff --git a/examples/demo_utils.h b/examples/demo_utils.h index 94be68e..9a1cb7a 100644 --- a/examples/demo_utils.h +++ b/examples/demo_utils.h @@ -14,6 +14,9 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#ifndef PUGL_DEMO_UTILS_H +#define PUGL_DEMO_UTILS_H + #include "pugl/pugl.h" #include @@ -168,3 +171,5 @@ puglPrintFps(const PuglWorld* world, *framesDrawn = 0; } } + +#endif // PUGL_DEMO_UTILS_H diff --git a/examples/pugl_embed_demo.c b/examples/pugl_embed_demo.c index 43d3d4e..f222773 100644 --- a/examples/pugl_embed_demo.c +++ b/examples/pugl_embed_demo.c @@ -20,6 +20,7 @@ #define GL_SILENCE_DEPRECATION 1 +#include "cube_view.h" #include "demo_utils.h" #include "test/test_utils.h" @@ -73,26 +74,6 @@ getChildFrame(const PuglRect parentFrame) return childFrame; } -static void -onReshape(PuglView* view, int width, int height) -{ - (void)view; - - const float aspect = (float)width / (float)height; - - glEnable(GL_DEPTH_TEST); - glDepthFunc(GL_LESS); - glClearColor(0.2f, 0.2f, 0.2f, 1.0f); - - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glViewport(0, 0, width, height); - - float projection[16]; - perspective(projection, 1.8f, aspect, 1.0f, 100.0f); - glLoadMatrixf(projection); -} - static void onDisplay(PuglView* view) { @@ -101,44 +82,12 @@ onDisplay(PuglView* view) const double thisTime = puglGetTime(app->world); if (app->continuous) { const double dTime = thisTime - app->lastDrawTime; + app->xAngle = fmod(app->xAngle + dTime * 100.0, 360.0); app->yAngle = fmod(app->yAngle + dTime * 100.0, 360.0); } - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - glTranslatef(0.0f, 0.0f, app->dist * -1); - glRotatef((float)app->xAngle, 0.0f, 1.0f, 0.0f); - glRotatef((float)app->yAngle, 1.0f, 0.0f, 0.0f); - - const float bg = app->mouseEntered ? 0.2f : 0.1f; - glClearColor(bg, bg, bg, 1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - if (puglHasFocus(app->child)) { - // Draw cube surfaces - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_COLOR_ARRAY); - glVertexPointer(3, GL_FLOAT, 0, cubeStripVertices); - glColorPointer(3, GL_FLOAT, 0, cubeStripVertices); - glDrawArrays(GL_TRIANGLE_STRIP, 0, 14); - glDisableClientState(GL_COLOR_ARRAY); - glDisableClientState(GL_VERTEX_ARRAY); - - glColor3f(0.0f, 0.0f, 0.0f); - } else { - glColor3f(1.0f, 1.0f, 1.0f); - } - - // Draw cube wireframe - glEnableClientState(GL_VERTEX_ARRAY); - glVertexPointer(3, GL_FLOAT, 0, cubeFrontLineLoop); - glDrawArrays(GL_LINE_LOOP, 0, 4); - glVertexPointer(3, GL_FLOAT, 0, cubeBackLineLoop); - glDrawArrays(GL_LINE_LOOP, 0, 4); - glVertexPointer(3, GL_FLOAT, 0, cubeSideLines); - glDrawArrays(GL_LINES, 0, 8); - glDisableClientState(GL_VERTEX_ARRAY); + displayCube(view, app->dist, app->xAngle, app->yAngle, app->mouseEntered); app->lastDrawTime = thisTime; ++app->framesDrawn; @@ -214,10 +163,7 @@ onParentEvent(PuglView* view, const PuglEvent* event) switch (event->type) { case PUGL_CONFIGURE: - onReshape(view, - (int)event->configure.width, - (int)event->configure.height); - + reshapeCube((int)event->configure.width, (int)event->configure.height); puglSetFrame(app->child, getChildFrame(parentFrame)); break; case PUGL_EXPOSE: @@ -262,7 +208,7 @@ onEvent(PuglView* view, const PuglEvent* event) switch (event->type) { case PUGL_CONFIGURE: - onReshape(view, (int)event->configure.width, (int)event->configure.height); + reshapeCube((int)event->configure.width, (int)event->configure.height); break; case PUGL_EXPOSE: onDisplay(view); @@ -302,6 +248,7 @@ int main(int argc, char** argv) { PuglTestApp app = {0}; + app.dist = 10; const PuglTestOptions opts = puglParseTestOptions(&argc, &argv); -- cgit v1.2.1