[gpu] Convert matrix4x4 and trackball to C++ headers

Merge the .c and .h files into single .hh headers with
static inline functions.  Eliminates the C/C++ linkage issue
and simplifies the web demo build to a single em++ call.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 4.6 committed 2026-03-28 03:53:20 -06:00
1 parent 2f486f7447
commit 30e1a703f0
7 files changed
+92 -301

No files matched your search

+2 -4
View File
@@ -5,10 +5,8 @@
#include "demo-view.h"
extern "C" {
#include "trackball.h"
#include "matrix4x4.h"
}
#include "trackball.hh"
#include "matrix4x4.hh"
struct demo_view_t {
demo_glstate_t *st;
-104
View File
@@ -1,104 +0,0 @@
/*
* Copyright (c) 2009, Mozilla Corp
* Copyright (c) 2012, Google, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY <copyright holder> ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Based on sample code from the OpenGL(R) ES 2.0 Programming Guide, which carriers
* the following header:
*
* Book: OpenGL(R) ES 2.0 Programming Guide
* Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner
* ISBN-10: 0321502795
* ISBN-13: 9780321502797
* Publisher: Addison-Wesley Professional
* URLs: http://safari.informit.com/9780321563835
* http://www.opengles-book.com
*/
/*
* Ported from JavaScript to C by Behdad Esfahbod, 2012.
* Added MultMatrix. Converting from fixed-function OpenGL matrix
* operations to these functions should be as simple as renaming the
* 'gl' prefix to 'm4' and adding the matrix argument to the call.
*
* The C version lives at http://code.google.com/p/matrix4x4-c/
*/
/*
* A simple 4x4 matrix utility implementation
*/
#ifndef MATRIX4x4_H
#define MATRIX4x4_H
/* Copies other matrix into mat */
float *
m4Copy (float *mat, const float *other);
float *
m4Multiply (float *mat, const float *right);
float *
m4MultMatrix (float *mat, const float *left);
float
m4Get (float *mat, unsigned int row, unsigned int col);
float *
m4Scale (float *mat, float sx, float sy, float sz);
float *
m4Translate (float *mat, float tx, float ty, float tz);
float *
m4Rotate (float *mat, float angle, float x, float y, float z);
float *
m4Frustum (float *mat, float left, float right, float bottom, float top, float nearZ, float farZ);
float *
m4Perspective (float *mat, float fovy, float aspect, float nearZ, float farZ);
float *
m4Ortho (float *mat, float left, float right, float bottom, float top, float nearZ, float farZ);
/* In-place inversion */
float *
m4Invert (float *mat);
/* Puts the inverse of other matrix into mat */
float *
m4Inverse (float *mat, const float *other);
/* In-place transpose */
float *
m4Transpose (float *mat);
float *
m4LoadIdentity (float *mat);
#endif
+23 -16
View File
@@ -1,3 +1,9 @@
#ifndef MATRIX4X4_HH
#define MATRIX4X4_HH
#include <cmath>
#include <cstring>
/*
* Copyright (c) 2009, Mozilla Corp
* Copyright (c) 2012, Google, Inc.
@@ -48,15 +54,14 @@
* The C version lives at http://code.google.com/p/matrix4x4-c/
*/
#include "matrix4x4.h"
#include <math.h>
/*
* A simple 4x4 matrix utility implementation
*/
float *
static inline float *
m4LoadIdentity (float *mat) {
unsigned int i;
for (i = 0; i < 16; i++)
@@ -69,7 +74,7 @@ m4LoadIdentity (float *mat) {
}
/* Copies other matrix into mat */
float *
static inline float *
m4Copy (float *mat, const float *other) {
unsigned int i;
for (i = 0; i < 16; i++) {
@@ -78,7 +83,7 @@ m4Copy (float *mat, const float *other) {
return mat;
}
float *
static inline float *
m4Multiply (float *mat, const float *right) {
float tmp[16];
unsigned int i;
@@ -112,18 +117,18 @@ m4Multiply (float *mat, const float *right) {
return m4Copy (mat, tmp);
}
float
static inline float
m4Get (float *mat, unsigned int row, unsigned int col) {
return mat[4*row+col];
}
float *
static inline float *
m4MultMatrix (float *mat, const float *left) {
float tmp[16];
return m4Copy (mat, m4Multiply (m4Copy (tmp, left), mat));
}
float *
static inline float *
m4Scale (float *mat, float sx, float sy, float sz) {
mat[0*4+0] *= sx;
mat[0*4+1] *= sx;
@@ -143,7 +148,7 @@ m4Scale (float *mat, float sx, float sy, float sz) {
return mat;
}
float *
static inline float *
m4Translate (float *mat, float tx, float ty, float tz) {
mat[3*4+0] += mat[0*4+0] * tx + mat[1*4+0] * ty + mat[2*4+0] * tz;
mat[3*4+1] += mat[0*4+1] * tx + mat[1*4+1] * ty + mat[2*4+1] * tz;
@@ -153,7 +158,7 @@ m4Translate (float *mat, float tx, float ty, float tz) {
return mat;
}
float *
static inline float *
m4Rotate (float *mat, float angle, float x, float y, float z) {
float mag = sqrt(x*x + y*y + z*z);
float sinAngle = sin(angle * M_PI / 180.0);
@@ -207,7 +212,7 @@ m4Rotate (float *mat, float angle, float x, float y, float z) {
return m4Copy (mat, m4Multiply (rotMat, mat));
}
float *
static inline float *
m4Frustum (float *mat, float left, float right, float bottom, float top, float nearZ, float farZ) {
float deltaX = right - left;
float deltaY = top - bottom;
@@ -238,7 +243,7 @@ m4Frustum (float *mat, float left, float right, float bottom, float top, float n
return m4Copy (mat, m4Multiply (frust, mat));
}
float *
static inline float *
m4Perspective (float *mat, float fovy, float aspect, float nearZ, float farZ) {
float frustumH = tan(fovy / 360.0 * M_PI) * nearZ;
float frustumW = frustumH * aspect;
@@ -246,7 +251,7 @@ m4Perspective (float *mat, float fovy, float aspect, float nearZ, float farZ) {
return m4Frustum(mat, -frustumW, frustumW, -frustumH, frustumH, nearZ, farZ);
}
float *
static inline float *
m4Ortho (float *mat, float left, float right, float bottom, float top, float nearZ, float farZ) {
float deltaX = right - left;
float deltaY = top - bottom;
@@ -270,7 +275,7 @@ m4Ortho (float *mat, float left, float right, float bottom, float top, float nea
}
/* In-place inversion */
float *
static inline float *
m4Invert (float *mat) {
float tmp_0 = m4Get(mat,2,2) * m4Get(mat,3,3);
float tmp_1 = m4Get(mat,3,2) * m4Get(mat,2,3);
@@ -360,7 +365,7 @@ m4Invert (float *mat) {
}
/* Puts the inverse of other matrix into mat */
float *
static inline float *
m4Inverse (float *mat, const float *other) {
m4Copy (mat, other);
m4Invert (mat);
@@ -368,7 +373,7 @@ m4Inverse (float *mat, const float *other) {
}
/* In-place transpose */
float *
static inline float *
m4Transpose (float *mat) {
float tmp = mat[0*4+1];
mat[0*4+1] = mat[1*4+0];
@@ -396,3 +401,5 @@ m4Transpose (float *mat) {
return mat;
}
#endif /* MATRIX4X4_HH */
+2 -4
View File
@@ -16,10 +16,8 @@ gpu_demo_sources = files(
'demo-vertex-glsl.hh',
'default-text.hh',
'default-font.hh',
'matrix4x4.h',
'matrix4x4.c',
'trackball.h',
'trackball.c',
'matrix4x4.hh',
'trackball.hh',
)
glew_dep = dependency('glew', required: false)
-109
View File
@@ -1,109 +0,0 @@
/*
* (c) Copyright 1993, 1994, Silicon Graphics, Inc.
* ALL RIGHTS RESERVED
* Permission to use, copy, modify, and distribute this software for
* any purpose and without fee is hereby granted, provided that the above
* copyright notice appear in all copies and that both the copyright notice
* and this permission notice appear in supporting documentation, and that
* the name of Silicon Graphics, Inc. not be used in advertising
* or publicity pertaining to distribution of the software without specific,
* written prior permission.
*
* THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
* AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
* GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
* SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
* KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
* LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
* THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
* POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
*
* US Government Users Restricted Rights
* Use, duplication, or disclosure by the Government is subject to
* restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
* (c)(1)(ii) of the Rights in Technical Data and Computer Software
* clause at DFARS 252.227-7013 and/or in similar or successor
* clauses in the FAR or the DOD or NASA FAR Supplement.
* Unpublished-- rights reserved under the copyright laws of the
* United States. Contractor/manufacturer is Silicon Graphics,
* Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
*
* OpenGL(TM) is a trademark of Silicon Graphics, Inc.
*/
/*
* trackball.h
* A virtual trackball implementation
* Written by Gavin Bell for Silicon Graphics, November 1988.
*/
void
vzero(float *v);
void
vset(float *v, float x, float y, float z);
void
vsub(const float *src1, const float *src2, float *dst);
void
vcopy(const float *v1, float *v2);
void
vcross(const float *v1, const float *v2, float *cross);
float
vlength(const float *v);
void
vscale(float *v, float div);
void
vnormal(float *v);
float
vdot(const float *v1, const float *v2);
void
vadd(const float *src1, const float *src2, float *dst);
/*
* Pass the x and y coordinates of the last and current positions of
* the mouse, scaled so they are from (-1.0 ... 1.0).
*
* The resulting rotation is returned as a quaternion rotation in the
* first paramater.
*/
void
trackball(float q[4], float p1x, float p1y, float p2x, float p2y);
/*
* Given two quaternions, add them together to get a third quaternion.
* Adding quaternions to get a compound rotation is analagous to adding
* translations to get a compound translation. When incrementally
* adding rotations, the first argument here should be the new
* rotation, the second and third the total rotation (which will be
* over-written with the resulting new total rotation).
*/
void
add_quats(float q1[4], float q2[4], float dest[4]);
/*
* A useful function, builds a rotation matrix in Matrix based on
* given quaternion.
*/
void
build_rotmatrix(float m[4][4], float q[4]);
/*
* This function computes a quaternion based on an axis (defined by
* the given vector) and an angle about which to rotate. The angle is
* expressed in radians. The result is put into the third argument.
*/
void
axis_to_quat(float a[3], float phi, float q[4]);
+59 -54
View File
@@ -1,4 +1,8 @@
#include <stdio.h>
#ifndef TRACKBALL_HH
#define TRACKBALL_HH
#include <cmath>
/*
* (c) Copyright 1993, 1994, Silicon Graphics, Inc.
* ALL RIGHTS RESERVED
@@ -53,8 +57,7 @@
#if defined(_WIN32)
#pragma warning (disable:4244) /* disable bogus conversion warnings */
#endif
#include <math.h>
#include "trackball.h"
/*
* This size should really be based on the distance from the center of
@@ -65,13 +68,13 @@
*/
#define TRACKBALLSIZE (0.5f)
/*
* Local function prototypes (not defined in trackball.h)
*/
static float tb_project_to_sphere(float, float, float);
static void normalize_quat(float [4]);
static inline float tb_project_to_sphere(float, float, float);
static inline void normalize_quat(float [4]);
void
/*
*/
static inline void
vzero(float *v)
{
v[0] = 0.0;
@@ -79,7 +82,7 @@ vzero(float *v)
v[2] = 0.0;
}
void
static inline void
vset(float *v, float x, float y, float z)
{
v[0] = x;
@@ -87,7 +90,7 @@ vset(float *v, float x, float y, float z)
v[2] = z;
}
void
static inline void
vsub(const float *src1, const float *src2, float *dst)
{
dst[0] = src1[0] - src2[0];
@@ -95,15 +98,15 @@ vsub(const float *src1, const float *src2, float *dst)
dst[2] = src1[2] - src2[2];
}
void
static inline void
vcopy(const float *v1, float *v2)
{
register int i;
int i;
for (i = 0 ; i < 3 ; i++)
v2[i] = v1[i];
}
void
static inline void
vcross(const float *v1, const float *v2, float *cross)
{
float temp[3];
@@ -114,13 +117,13 @@ vcross(const float *v1, const float *v2, float *cross)
vcopy(temp, cross);
}
float
static inline float
vlength(const float *v)
{
return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
}
void
static inline void
vscale(float *v, float div)
{
v[0] *= div;
@@ -128,19 +131,19 @@ vscale(float *v, float div)
v[2] *= div;
}
void
static inline void
vnormal(float *v)
{
vscale(v,1.0/vlength(v));
}
float
static inline float
vdot(const float *v1, const float *v2)
{
return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
}
void
static inline void
vadd(const float *src1, const float *src2, float *dst)
{
dst[0] = src1[0] + src2[0];
@@ -148,6 +151,37 @@ vadd(const float *src1, const float *src2, float *dst)
dst[2] = src1[2] + src2[2];
}
/*
* Given an axis and angle, compute quaternion.
*/
static inline void
axis_to_quat(float a[3], float phi, float q[4])
{
vcopy(a,q);
vnormal(q);
vscale(q,sin(phi/2.0));
q[3] = cos(phi/2.0);
}
/*
* Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
* if we are away from the center of the sphere.
*/
static inline float
tb_project_to_sphere(float r, float x, float y)
{
float d, t, z;
d = sqrt(x*x + y*y);
if (d < r * 0.70710678118654752440) { /* Inside sphere */
z = sqrt(r*r - d*d);
} else { /* On hyperbola */
t = r / 1.41421356237309504880;
z = t*t / d;
}
return z;
}
/*
* Ok, simulate a track-ball. Project the points onto the virtual
* trackball, then figure out the axis of rotation, which is the cross
@@ -160,7 +194,7 @@ vadd(const float *src1, const float *src2, float *dst)
* It is assumed that the arguments to this routine are in the range
* (-1.0 ... 1.0)
*/
void
static inline void
trackball(float q[4], float p1x, float p1y, float p2x, float p2y)
{
float a[3]; /* Axis of rotation */
@@ -203,37 +237,6 @@ trackball(float q[4], float p1x, float p1y, float p2x, float p2y)
axis_to_quat(a,phi,q);
}
/*
* Given an axis and angle, compute quaternion.
*/
void
axis_to_quat(float a[3], float phi, float q[4])
{
vcopy(a,q);
vnormal(q);
vscale(q,sin(phi/2.0));
q[3] = cos(phi/2.0);
}
/*
* Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
* if we are away from the center of the sphere.
*/
static float
tb_project_to_sphere(float r, float x, float y)
{
float d, t, z;
d = sqrt(x*x + y*y);
if (d < r * 0.70710678118654752440) { /* Inside sphere */
z = sqrt(r*r - d*d);
} else { /* On hyperbola */
t = r / 1.41421356237309504880;
z = t*t / d;
}
return z;
}
/*
* Given two rotations, e1 and e2, expressed as quaternion rotations,
* figure out the equivalent single rotation and stuff it into dest.
@@ -247,7 +250,7 @@ tb_project_to_sphere(float r, float x, float y)
#define RENORMCOUNT 97
void
static inline void
add_quats(float q1[4], float q2[4], float dest[4])
{
static int count=0;
@@ -297,7 +300,7 @@ printf("tf = %f %f %f %f\n", tf[0], tf[1], tf[2], tf[3]);
* - Pletinckx, D., Quaternion calculus as a basic tool in computer
* graphics, The Visual Computer 5, 2-13, 1989.
*/
static void
static inline void
normalize_quat(float q[4])
{
int i;
@@ -311,7 +314,7 @@ normalize_quat(float q[4])
* Build a rotation matrix, given a quaternion rotation.
*
*/
void
static inline void
build_rotmatrix(float m[4][4], float q[4])
{
m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
@@ -335,3 +338,5 @@ build_rotmatrix(float m[4][4], float q[4])
m[3][3] = 1.0;
}
#endif /* TRACKBALL_HH */
+6 -10
View File
@@ -10,16 +10,14 @@ OUTDIR="$(dirname "$0")/out"
mkdir -p "$OUTDIR"
COMMON_FLAGS="-O2 -I$SRCDIR/src -DHB_NO_MT -DHAVE_ROUND -DHB_GPU_ATLAS_2D -DHB_HAS_GPU"
# Compile C files
emcc $COMMON_FLAGS -c "$SRCDIR/util/gpu/matrix4x4.c" -o "$OUTDIR/matrix4x4.o"
emcc $COMMON_FLAGS -c "$SRCDIR/util/gpu/trackball.c" -o "$OUTDIR/trackball.o"
# Compile C++ files and link
em++ \
-std=c++17 \
$COMMON_FLAGS \
-O2 \
-I"$SRCDIR/src" \
-DHB_NO_MT \
-DHAVE_ROUND \
-DHB_GPU_ATLAS_2D \
-DHB_HAS_GPU \
"$SRCDIR/src/harfbuzz-world.cc" \
"$SRCDIR/util/gpu/demo-atlas.cc" \
"$SRCDIR/util/gpu/demo-buffer.cc" \
@@ -28,8 +26,6 @@ em++ \
"$SRCDIR/util/gpu/demo-shader.cc" \
"$SRCDIR/util/gpu/demo-view.cc" \
"$SRCDIR/util/gpu/web/hb-gpu-web.cc" \
"$OUTDIR/matrix4x4.o" \
"$OUTDIR/trackball.o" \
-sUSE_GLFW=3 \
-sUSE_WEBGL2=1 \
-sFULL_ES3=1 \