utils

old (and slightly embarrassing) C utility scripts
Log | Files | Refs | README | LICENSE

utilopengl.h (2074B)


      1 #pragma once
      2 
      3 #include <GL/glut.h>
      4 #include <math.h>
      5 #include <stdlib.h>
      6 
      7 //setting 3d stuff
      8 
      9 void setObjectT(GLfloat* aF0, GLfloat aF1, GLfloat aF2, GLfloat aF3){
     10   aF0[3] = aF1;
     11   aF0[7] = aF2;
     12   aF0[11] = aF3;
     13 }
     14 
     15 void setObjectRx(GLfloat* aF0, GLfloat aF1){
     16   aF0[5] = cos(aF1);
     17   aF0[6] = -sin(aF1);
     18   aF0[9] = sin(aF1);
     19   aF0[10] = cos(aF1);
     20 }
     21 
     22 void setObjectRy(GLfloat* aF0, GLfloat aF1){
     23   aF0[0] = cos(aF1);
     24   aF0[2] = sin(aF1);
     25   aF0[8] = -sin(aF1);
     26   aF0[10] = cos(aF1);
     27 }
     28 
     29 void setObjectRz(GLfloat* aF0, GLfloat aF1){
     30   aF0[0] = cos(aF1);
     31   aF0[1] = -sin(aF1);
     32   aF0[4] = sin(aF1);
     33   aF0[5] = cos(aF1);
     34 }
     35 
     36 void scaleObject(GLfloat* aF0, GLfloat aF1){
     37   aF0[0] *= aF1;
     38   aF0[5] *= aF1;
     39   aF0[10] *= aF1;
     40 }
     41 
     42 void setObjectS(GLfloat* aF0, GLfloat aF1){
     43   aF0[0] = aF1;
     44   aF0[5] = aF1;
     45   aF0[10] = aF1;
     46 }
     47 
     48 //create zero matrix
     49 GLfloat* ugl_Z(int aI0){
     50   GLfloat* ret = malloc(sizeof(GLfloat)*aI0);
     51   for(int it0 = 0; it0 < aI0; it0++) ret[it0] = 0;
     52   return ret;
     53 }
     54 
     55 //create identity matrix
     56 GLfloat* ugl_I(int aI0){
     57   GLfloat* ret = malloc(sizeof(GLfloat)*aI0*aI0);
     58   for(int it0 = 0; it0 < aI0*aI0; it0++){
     59     if(it0%(aI0 + 1) == 0) ret[it0] = 1;
     60     else ret[it0] = 0;
     61   }
     62   return ret;
     63 }
     64 
     65 //create duplicate
     66 GLfloat* ugl_duplicate(GLfloat* aF0, int aI0){
     67   GLfloat* ret = malloc(sizeof(GLfloat)*aI0);
     68   for(int it0 = 0; it0 < aI0; it0++) ret[it0] = aF0[it0];
     69   return ret;
     70 }
     71 
     72 //some arithmetics
     73 GLfloat* ugl_mult(GLfloat* aF0, GLfloat* aF1, int aI0, int aI1, int aI2){
     74   //input array 0, input array 1, row 0, col 0/row1, col 1
     75   GLfloat* ret = ugl_Z(aI0*aI2);
     76   for(int it0 = 0; it0 < aI0*aI2; it0++){
     77     for(int it1 = 0; it1 < aI1; it1++) ret[it0] += aF0[(it0/4)*aI1 + it1]*aF1[it1*aI2 + (it0%4)];
     78   }
     79   return ret;
     80 }
     81 
     82 //create a single transformation matrix from a list of transformation
     83 //matrices
     84 GLfloat* ugl_T(GLfloat** aF0){
     85   GLfloat* ret = ugl_mult(aF0[1], aF0[0], 4, 4, 4);
     86   for(int it0 = 2; it0 < 5; it0++){
     87     GLfloat* temp = ugl_mult(aF0[it0], ret, 4, 4, 4);
     88     free(ret);
     89     ret = ugl_duplicate(temp, 16);
     90     free(temp);
     91   }
     92   return ret;
     93 }
     94 

Generated using stagit (https://codemadness.org/stagit.html)