From fc023d0913e518dc4e96a2b804a99abe21935ff2 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 14 Sep 2016 17:04:02 +0800 Subject: Clean up pugl_test and remove use of GLU --- pugl_test.c | 131 ++++++++++++++++++++++++++++++++++++++++++------------------ wscript | 6 ++- 2 files changed, 96 insertions(+), 41 deletions(-) diff --git a/pugl_test.c b/pugl_test.c index b534162..942b725 100644 --- a/pugl_test.c +++ b/pugl_test.c @@ -1,5 +1,5 @@ /* - Copyright 2012 David Robillard + Copyright 2012-2016 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 @@ -18,73 +18,115 @@ @file pugl_test.c A simple Pugl test that creates a top-level window. */ +#include #include #include -#include "pugl/pugl.h" #include "pugl/gl.h" -#include "pugl/glu.h" +#include "pugl/pugl.h" static int quit = 0; static float xAngle = 0.0f; static float yAngle = 0.0f; static float dist = 10.0f; +static const float cubeVertices[] = { + -1.0f, -1.0f, -1.0f, + -1.0f, -1.0f, 1.0f, + -1.0f, 1.0f, 1.0f, + + 1.0f, 1.0f, -1.0f, + -1.0f, -1.0f, -1.0f, + -1.0f, 1.0f, -1.0f, + + 1.0f, -1.0f, 1.0f, + -1.0f, -1.0f, -1.0f, + 1.0f, -1.0f, -1.0f, + + 1.0f, 1.0f, -1.0f, + 1.0f, -1.0f, -1.0f, + -1.0f, -1.0f, -1.0f, + + -1.0f, -1.0f, -1.0f, + -1.0f, 1.0f, 1.0f, + -1.0f, 1.0f, -1.0f, + + 1.0f, -1.0f, 1.0f, + -1.0f, -1.0f, 1.0f, + -1.0f, -1.0f, -1.0f, + + -1.0f, 1.0f, 1.0f, + -1.0f, -1.0f, 1.0f, + 1.0f, -1.0f, 1.0f, + + 1.0f, 1.0f, 1.0f, + 1.0f, -1.0f, -1.0f, + 1.0f, 1.0f, -1.0f, + + 1.0f, -1.0f, -1.0f, + 1.0f, 1.0f, 1.0f, + 1.0f, -1.0f, 1.0f, + + 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, -1.0f, + -1.0f, 1.0f, -1.0f, + + 1.0f, 1.0f, 1.0f, + -1.0f, 1.0f, -1.0f, + -1.0f, 1.0f, 1.0f, + + 1.0f, 1.0f, 1.0f, + -1.0f, 1.0f, 1.0f, + 1.0f, -1.0f, 1.0f +}; + +/** Calculate a projection matrix for a given perspective. */ +static void +perspective(float* m, float fov, float aspect, float zNear, float zFar) +{ + const float h = tan(fov); + const float w = h / aspect; + const float depth = zNear - zFar; + const float q = (zFar + zNear) / depth; + const float qn = 2 * zFar * zNear / depth; + + m[0] = w; m[1] = 0; m[2] = 0; m[3] = 0; + m[4] = 0; m[5] = h; m[6] = 0; m[7] = 0; + m[8] = 0; m[9] = 0; m[10] = q; m[11] = -1; + m[12] = 0; m[13] = 0; m[14] = qn; m[15] = 0; +} + static void onReshape(PuglView* view, int width, int height) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0, 0, width, height); - gluPerspective(45.0f, width/(float)height, 1.0f, 10.0f); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); + float projection[16]; + perspective(projection, 1.8f, width / (float)height, 1.0, 100.0f); + glLoadMatrixf(projection); } static void onDisplay(PuglView* view) { - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glMatrixMode(GL_MODELVIEW); glLoadIdentity(); - glTranslatef(0.0f, 0.0f, dist * -1); glRotatef(xAngle, 0.0f, 1.0f, 0.0f); glRotatef(yAngle, 1.0f, 0.0f, 0.0f); - glBegin(GL_QUADS); - - glColor3f(0, 0, 0); glVertex3f(-1, -1, -1); - glColor3f(0, 0, 1); glVertex3f(-1, -1, 1); - glColor3f(0, 1, 1); glVertex3f(-1, 1, 1); - glColor3f(0, 1, 0); glVertex3f(-1, 1, -1); - - glColor3f(1, 0, 0); glVertex3f( 1, -1, -1); - glColor3f(1, 0, 1); glVertex3f( 1, -1, 1); - glColor3f(1, 1, 1); glVertex3f( 1, 1, 1); - glColor3f(1, 1, 0); glVertex3f( 1, 1, -1); - - glColor3f(0, 0, 0); glVertex3f(-1, -1, -1); - glColor3f(0, 0, 1); glVertex3f(-1, -1, 1); - glColor3f(1, 0, 1); glVertex3f( 1, -1, 1); - glColor3f(1, 0, 0); glVertex3f( 1, -1, -1); - - glColor3f(0, 1, 0); glVertex3f(-1, 1, -1); - glColor3f(0, 1, 1); glVertex3f(-1, 1, 1); - glColor3f(1, 1, 1); glVertex3f( 1, 1, 1); - glColor3f(1, 1, 0); glVertex3f( 1, 1, -1); - - glColor3f(0, 0, 0); glVertex3f(-1, -1, -1); - glColor3f(0, 1, 0); glVertex3f(-1, 1, -1); - glColor3f(1, 1, 0); glVertex3f( 1, 1, -1); - glColor3f(1, 0, 0); glVertex3f( 1, -1, -1); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_COLOR_ARRAY); - glColor3f(0, 0, 1); glVertex3f(-1, -1, 1); - glColor3f(0, 1, 1); glVertex3f(-1, 1, 1); - glColor3f(1, 1, 1); glVertex3f( 1, 1, 1); - glColor3f(1, 0, 1); glVertex3f( 1, -1, 1); + glVertexPointer(3, GL_FLOAT, 0, cubeVertices); + glColorPointer(3, GL_FLOAT, 0, cubeVertices); + glDrawArrays(GL_TRIANGLES, 0, 12 * 3); - glEnd(); + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_COLOR_ARRAY); } static void @@ -142,6 +184,9 @@ onScroll(PuglView* view, int x, int y, float dx, float dy) fprintf(stderr, "Scroll %d %d %f %f ", x, y, dx, dy); printModifiers(view); dist += dy / 4.0f; + if (dist < 10.0f) { + dist = 10.0f; + } puglPostRedisplay(view); } @@ -189,6 +234,14 @@ main(int argc, char** argv) puglSetCloseFunc(view, onClose); puglCreateWindow(view, "Pugl Test"); + + puglEnterContext(view); + glEnable(GL_MULTISAMPLE); + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LESS); + glClearColor(0.2f, 0.2f, 0.2f, 1.0f); + puglLeaveContext(view, false); + puglShowWindow(view); while (!quit) { diff --git a/wscript b/wscript index a65dc96..349ee14 100644 --- a/wscript +++ b/wscript @@ -105,7 +105,7 @@ def build(bld): if bld.env.TARGET_PLATFORM == 'win32': lang = 'cxx' lib_source = ['pugl/pugl_win.cpp'] - libs = ['opengl32', 'glu32', 'gdi32', 'user32'] + libs = ['opengl32', 'gdi32', 'user32'] defines = [] elif bld.env.TARGET_PLATFORM == 'darwin': lang = 'c' # Objective C, actually @@ -118,9 +118,11 @@ def build(bld): libs = ['X11'] defines = [] if bld.is_defined('HAVE_GL'): - libs += ['GL', 'GLU'] + libs += ['GL'] if bld.env['MSVC_COMPILER']: libflags = [] + else: + libs += ['m'] # Shared Library if bld.env['BUILD_SHARED']: -- cgit v1.2.3