Browse Source

Replace GLU with linmath.h in examples

Camilla Berglund 10 years ago
parent
commit
dd01dd7bef
7 changed files with 86 additions and 72 deletions
  1. 2 0
      README.md
  2. 4 5
      docs/compile.dox
  3. 1 1
      examples/CMakeLists.txt
  4. 16 34
      examples/boing.c
  5. 8 3
      examples/particles.c
  6. 47 26
      examples/splitview.c
  7. 8 3
      examples/wave.c

+ 2 - 0
README.md

@@ -63,6 +63,8 @@ GLFW bundles a number of dependencies in the `deps/` directory.
 ## Changelog
 
  - Changed minimum required CMake version to 2.8.12
+ - Replaced GLU with [linmath.h](https://github.com/datenwolf/linmath.h) in
+   example programs
  - Bugfix: Initialization failed on headless systems
  - Bugfix: The cached current context could get out of sync
  - [Win32] Renamed hybrid GPU override compile-time option to

+ 4 - 5
docs/compile.dox

@@ -90,11 +90,10 @@ Once you have Xcode installed, move on to @ref compile_generate.
 To compile GLFW for X11, you need to have the X11 and OpenGL header packages
 installed, as well as the basic development tools like GCC and make.  For
 example, on Ubuntu and other distributions based on Debian GNU/Linux, you need
-to install the `xorg-dev` and `libglu1-mesa-dev` packages.  The former pulls in
-all X.org header packages and the latter pulls in the Mesa OpenGL and GLU
-packages.  GLFW itself doesn't need or use GLU, but some of the examples do.
-Note that using header files and libraries from Mesa during compilation
-_will not_ tie your binaries to the Mesa implementation of OpenGL.
+to install the `xorg-dev` and `libgl1-mesa-dev` packages.  The former pulls in
+all X.org header packages and the latter pulls in the Mesa OpenGL development
+packages.  Note that using header files and libraries from Mesa during
+compilation _will not_ tie your binaries to the Mesa implementation of OpenGL.
 
 Once you have installed the necessary packages, move on to @ref
 compile_generate.

+ 1 - 1
examples/CMakeLists.txt

@@ -1,5 +1,5 @@
 
-link_libraries(glfw "${OPENGL_glu_LIBRARY}")
+link_libraries(glfw)
 
 if (BUILD_SHARED_LIBS)
     add_definitions(-DGLFW_DLL)

+ 16 - 34
examples/boing.c

@@ -36,9 +36,10 @@
 #include <stdlib.h>
 #include <math.h>
 
-#define GLFW_INCLUDE_GLU
 #include <GLFW/glfw3.h>
 
+#include <linmath.h>
+
 
 /*****************************************************************************
  * Various declarations and macros
@@ -167,28 +168,6 @@ void CrossProduct( vertex_t a, vertex_t b, vertex_t c, vertex_t *n )
    n->z = u1 * v2 - v1 * u2;
 }
 
-/*****************************************************************************
- * Calculate the angle to be passed to gluPerspective() so that a scene
- * is visible.  This function originates from the OpenGL Red Book.
- *
- * Parms   : size
- *           The size of the segment when the angle is intersected at "dist"
- *           (ie at the outermost edge of the angle of vision).
- *
- *           dist
- *           Distance from viewpoint to scene.
- *****************************************************************************/
-GLfloat PerspectiveAngle( GLfloat size,
-                          GLfloat dist )
-{
-   GLfloat radTheta, degTheta;
-
-   radTheta = 2.f * (GLfloat) atan2( size / 2.f, dist );
-   degTheta = (180.f * radTheta) / (GLfloat) M_PI;
-   return degTheta;
-}
-
-
 
 #define BOING_DEBUG 0
 
@@ -233,22 +212,25 @@ void display(void)
  *****************************************************************************/
 void reshape( GLFWwindow* window, int w, int h )
 {
+   mat4x4 projection, view;
+
    glViewport( 0, 0, (GLsizei)w, (GLsizei)h );
 
    glMatrixMode( GL_PROJECTION );
-   glLoadIdentity();
-
-   gluPerspective( PerspectiveAngle( RADIUS * 2, 200 ),
-                   (GLfloat)w / (GLfloat)h,
-                   1.0,
-                   VIEW_SCENE_DIST );
+   mat4x4_perspective( projection,
+                       2.f * (float) atan2( RADIUS, 200.f ),
+                       (float)w / (float)h,
+                       1.f, VIEW_SCENE_DIST );
+   glLoadMatrixf((const GLfloat*) projection);
 
    glMatrixMode( GL_MODELVIEW );
-   glLoadIdentity();
-
-   gluLookAt( 0.0, 0.0, VIEW_SCENE_DIST,/* eye */
-              0.0, 0.0, 0.0,            /* center of vision */
-              0.0, -1.0, 0.0 );         /* up vector */
+   {
+      vec3 eye = { 0.f, 0.f, VIEW_SCENE_DIST };
+      vec3 center = { 0.f, 0.f, 0.f };
+      vec3 up = { 0.f, -1.f, 0.f };
+      mat4x4_look_at( view, eye, center, up );
+   }
+   glLoadMatrixf((const GLfloat*) view);
 }
 
 void key_callback( GLFWwindow* window, int key, int scancode, int action, int mods )

+ 8 - 3
examples/particles.c

@@ -37,8 +37,8 @@
 
 #include <tinycthread.h>
 #include <getopt.h>
+#include <linmath.h>
 
-#define GLFW_INCLUDE_GLU
 #include <GLFW/glfw3.h>
 
 // Define tokens for GL_EXT_separate_specular_color if not already defined
@@ -785,17 +785,22 @@ static void draw_scene(GLFWwindow* window, double t)
     double xpos, ypos, zpos, angle_x, angle_y, angle_z;
     static double t_old = 0.0;
     float dt;
+    mat4x4 projection;
 
     // Calculate frame-to-frame delta time
     dt = (float) (t - t_old);
     t_old = t;
 
+    mat4x4_perspective(projection,
+                       65.f * (float) M_PI / 180.f,
+                       aspect_ratio,
+                       1.0, 60.0);
+
     glClearColor(0.1f, 0.1f, 0.1f, 1.f);
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
     glMatrixMode(GL_PROJECTION);
-    glLoadIdentity();
-    gluPerspective(65.0, aspect_ratio, 1.0, 60.0);
+    glLoadMatrixf((const GLfloat*) projection);
 
     // Setup camera
     glMatrixMode(GL_MODELVIEW);

+ 47 - 26
examples/splitview.c

@@ -10,7 +10,6 @@
 //  because I am not a friend of orthogonal projections)
 //========================================================================
 
-#define GLFW_INCLUDE_GLU
 #define GLFW_INCLUDE_GLEXT
 #include <GLFW/glfw3.h>
 
@@ -23,6 +22,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include <linmath.h>
+
 
 //========================================================================
 // Global variables
@@ -151,6 +152,7 @@ static void drawGrid(float scale, int steps)
 {
     int i;
     float x, y;
+    mat4x4 view;
 
     glPushMatrix();
 
@@ -159,10 +161,13 @@ static void drawGrid(float scale, int steps)
     glClear(GL_COLOR_BUFFER_BIT);
 
     // Setup modelview matrix (flat XY view)
-    glLoadIdentity();
-    gluLookAt(0.0, 0.0, 1.0,
-              0.0, 0.0, 0.0,
-              0.0, 1.0, 0.0);
+    {
+        vec3 eye = { 0.f, 0.f, 1.f };
+        vec3 center = { 0.f, 0.f, 0.f };
+        vec3 up = { 0.f, 1.f, 0.f };
+        mat4x4_look_at(view, eye, center, up);
+    }
+    glLoadMatrixf((const GLfloat*) view);
 
     // We don't want to update the Z-buffer
     glDepthMask(GL_FALSE);
@@ -211,13 +216,14 @@ static void drawAllViews(void)
     const GLfloat light_diffuse[4]  = {1.0f, 1.0f, 1.0f, 1.0f};
     const GLfloat light_specular[4] = {1.0f, 1.0f, 1.0f, 1.0f};
     const GLfloat light_ambient[4]  = {0.2f, 0.2f, 0.3f, 1.0f};
-    double aspect;
+    float aspect;
+    mat4x4 view, projection;
 
     // Calculate aspect of window
     if (height > 0)
-        aspect = (double) width / (double) height;
+        aspect = (float) width / (float) height;
     else
-        aspect = 1.0;
+        aspect = 1.f;
 
     // Clear screen
     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
@@ -249,10 +255,13 @@ static void drawAllViews(void)
     glViewport(0, height / 2, width / 2, height / 2);
     glScissor(0, height / 2, width / 2, height / 2);
     glMatrixMode(GL_MODELVIEW);
-    glLoadIdentity();
-    gluLookAt(0.0f, 10.0f, 1e-3f,   // Eye-position (above)
-              0.0f, 0.0f, 0.0f,     // View-point
-              0.0f, 1.0f, 0.0f);   // Up-vector
+    {
+        vec3 eye = { 0.f, 10.f, 1e-3f };
+        vec3 center = { 0.f, 0.f, 0.f };
+        vec3 up = { 0.f, 1.f, 0.f };
+        mat4x4_look_at( view, eye, center, up );
+    }
+    glLoadMatrixf((const GLfloat*) view);
     drawGrid(0.5, 12);
     drawScene();
 
@@ -260,10 +269,13 @@ static void drawAllViews(void)
     glViewport(0, 0, width / 2, height / 2);
     glScissor(0, 0, width / 2, height / 2);
     glMatrixMode(GL_MODELVIEW);
-    glLoadIdentity();
-    gluLookAt(0.0f, 0.0f, 10.0f,    // Eye-position (in front of)
-              0.0f, 0.0f, 0.0f,     // View-point
-              0.0f, 1.0f, 0.0f);   // Up-vector
+    {
+        vec3 eye = { 0.f, 0.f, 10.f };
+        vec3 center = { 0.f, 0.f, 0.f };
+        vec3 up = { 0.f, 1.f, 0.f };
+        mat4x4_look_at( view, eye, center, up );
+    }
+    glLoadMatrixf((const GLfloat*) view);
     drawGrid(0.5, 12);
     drawScene();
 
@@ -271,10 +283,13 @@ static void drawAllViews(void)
     glViewport(width / 2, 0, width / 2, height / 2);
     glScissor(width / 2, 0, width / 2, height / 2);
     glMatrixMode(GL_MODELVIEW);
-    glLoadIdentity();
-    gluLookAt(10.0f, 0.0f, 0.0f,    // Eye-position (to the right)
-              0.0f, 0.0f, 0.0f,     // View-point
-              0.0f, 1.0f, 0.0f);   // Up-vector
+    {
+        vec3 eye = { 10.f, 0.f, 0.f };
+        vec3 center = { 0.f, 0.f, 0.f };
+        vec3 up = { 0.f, 1.f, 0.f };
+        mat4x4_look_at( view, eye, center, up );
+    }
+    glLoadMatrixf((const GLfloat*) view);
     drawGrid(0.5, 12);
     drawScene();
 
@@ -294,17 +309,23 @@ static void drawAllViews(void)
 
     // Setup perspective projection matrix
     glMatrixMode(GL_PROJECTION);
-    glLoadIdentity();
-    gluPerspective(65.0f, aspect, 1.0f, 50.0f);
+    mat4x4_perspective(projection,
+                       65.f * (float) M_PI / 180.f,
+                       aspect,
+                       1.f, 50.f);
+    glLoadMatrixf((const GLfloat*) projection);
 
     // Upper right view (PERSPECTIVE VIEW)
     glViewport(width / 2, height / 2, width / 2, height / 2);
     glScissor(width / 2, height / 2, width / 2, height / 2);
     glMatrixMode(GL_MODELVIEW);
-    glLoadIdentity();
-    gluLookAt(3.0f, 1.5f, 3.0f,     // Eye-position
-              0.0f, 0.0f, 0.0f,     // View-point
-              0.0f, 1.0f, 0.0f);   // Up-vector
+    {
+        vec3 eye = { 3.f, 1.5f, 3.f };
+        vec3 center = { 0.f, 0.f, 0.f };
+        vec3 up = { 0.f, 1.f, 0.f };
+        mat4x4_look_at( view, eye, center, up );
+    }
+    glLoadMatrixf((const GLfloat*) view);
 
     // Configure and enable light source 1
     glLightfv(GL_LIGHT1, GL_POSITION, light_position);

+ 8 - 3
examples/wave.c

@@ -17,9 +17,10 @@
 #include <stdlib.h>
 #include <math.h>
 
-#define GLFW_INCLUDE_GLU
 #include <GLFW/glfw3.h>
 
+#include <linmath.h>
+
 // Maximum delta T to allow for differential calculations
 #define MAX_DELTA_T 0.01
 
@@ -363,6 +364,7 @@ void scroll_callback(GLFWwindow* window, double x, double y)
 void framebuffer_size_callback(GLFWwindow* window, int width, int height)
 {
     float ratio = 1.f;
+    mat4x4 projection;
 
     if (height > 0)
         ratio = (float) width / (float) height;
@@ -372,8 +374,11 @@ void framebuffer_size_callback(GLFWwindow* window, int width, int height)
 
     // Change to the projection matrix and set our viewing volume
     glMatrixMode(GL_PROJECTION);
-    glLoadIdentity();
-    gluPerspective(60.0, ratio, 1.0, 1024.0);
+    mat4x4_perspective(projection,
+                       60.f * (float) M_PI / 180.f,
+                       ratio,
+                       1.f, 1024.f);
+    glLoadMatrixf((const GLfloat*) projection);
 }