Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Some examples of equivalence of OpenGL math libraries glm (C++) and kazmath (C)
--- p6.cpp 2014-03-28 22:55:37.000000000 -0500
+++ p6.c 2014-03-28 23:02:28.000000000 -0500
@@ -7,9 +7,8 @@
#define PNG_DEBUG 3
#include <png.h>
-#include <glm/glm.hpp>
-#include <glm/gtc/matrix_transform.hpp>
-#include <glm/gtc/type_ptr.hpp>
+#include <kazmath/kazmath.h>
+#define M_PI 3.14159265358979323846f
#ifndef nullptr
#define nullptr NULL
@@ -18,7 +17,7 @@
// example of print_matrix usage:
//
-// print_matrix(glm::value_ptr(model));
+// print_matrix(&model.mat[0]);
//
void print_matrix(const float * p_matrix)
{
@@ -316,21 +315,24 @@
free(image);
glUniform1i(glGetUniformLocation(shaderProgram, "texPuppy"), 1);
- glm::mat4 identity;
- glm::mat4 model = glm::rotate(identity, 90.0f, glm::vec3(0.0f, 0.0f, 1.0f));
+ kmMat4 model;
+ kmMat4Identity(&model);
+ kmMat4RotationZ(&model, 90.0f * M_PI / 180.0f);
GLint uniModel = glGetUniformLocation(shaderProgram, "model");
- glUniformMatrix4fv(uniModel, 1, GL_FALSE, glm::value_ptr(model));
+ glUniformMatrix4fv(uniModel, 1, GL_FALSE, &model.mat[0]);
- glm::mat4 view = glm::lookAt(
- glm::vec3(1.2f, 1.2f, 1.2f),
- glm::vec3(0.0f, 0.0f, 0.0f),
- glm::vec3(0.0f, 0.0f, 1.0f));
+ kmMat4 view;
+ kmVec3 p_eye = { 1.2f, 1.2f, 1.2f };
+ kmVec3 p_ctr = { 0.0f, 0.0f, 0.0f };
+ kmVec3 p_up = { 0.0f, 0.0f, 1.0f };
+ kmMat4LookAt(&view, &p_eye, &p_ctr, &p_up);
GLint uniView = glGetUniformLocation(shaderProgram, "view");
- glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view));
+ glUniformMatrix4fv(uniView, 1, GL_FALSE, &view.mat[0]);
- glm::mat4 proj = glm::perspective(45.0f, 1280.0f / 720.0f, 1.0f, 10.0f);
+ kmMat4 proj;
+ kmMat4PerspectiveProjection(&proj, 45.0f, 1280.0f / 720.0f, 1.0f, 10.0f);
GLint uniProj = glGetUniformLocation(shaderProgram, "proj");
- glUniformMatrix4fv(uniProj, 1, GL_FALSE, glm::value_ptr(proj));
+ glUniformMatrix4fv(uniProj, 1, GL_FALSE, &proj.mat[0]);
glEnable(GL_DEPTH_TEST);
//glEnable(GL_STENCIL_TEST);
@@ -343,6 +345,8 @@
//glStencilFunc(GL_EQUAL, 1, 0xFF);
//glStencilMask(0x00);
+ kmMat4 tmp;
+
GLint uniColor = glGetUniformLocation(shaderProgram, "overrideColor");
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
@@ -351,9 +355,9 @@
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- model = glm::rotate(identity, (float)clock() / (float)CLOCKS_PER_SEC * 90.0f,
- glm::vec3(0.0f, 0.0f, 1.0f));
- glUniformMatrix4fv(uniModel, 1, GL_FALSE, glm::value_ptr(model));
+ kmMat4Identity(&model);
+ kmMat4RotationZ(&model, (float)clock() / (float)CLOCKS_PER_SEC * 90.0f * M_PI / 180.0f);
+ glUniformMatrix4fv(uniModel, 1, GL_FALSE, &model.mat[0]);
//glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glDrawArrays(GL_TRIANGLES, 0, 36);
@@ -373,8 +377,11 @@
glStencilMask(0x00);
glDepthMask(GL_TRUE);
- model = glm::scale(glm::translate(model, glm::vec3(0, 0, -1)), glm::vec3(1, 1, -1));
- glUniformMatrix4fv(uniModel, 1, GL_FALSE, glm::value_ptr(model));
+ kmMat4Translation(&tmp, 0.0f, 0.0f, -1.0f);
+ kmMat4Multiply(&model, &model, &tmp);
+ kmMat4Scaling(&tmp, 1.0f, 1.0f, -1.0f);
+ kmMat4Multiply(&model, &model, &tmp);
+ glUniformMatrix4fv(uniModel, 1, GL_FALSE, &model.mat[0]);
glUniform3f(uniColor, 0.3f, 0.3f, 0.3f);
glDrawArrays(GL_TRIANGLES, 0, 36);
glUniform3f(uniColor, 1.0f, 1.0f, 1.0f);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment