266 lines
5.9 KiB
C
266 lines
5.9 KiB
C
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <sys/stat.h>
|
|
#include <math.h>
|
|
#include <string.h>
|
|
#include "glslUtil.h"
|
|
|
|
/**************************************************************************
|
|
* Helper Functions *
|
|
*************************************************************************/
|
|
static char *loadFile(const char *fname)
|
|
{
|
|
struct stat st;
|
|
if (stat(fname, &st) != 0)
|
|
return NULL;
|
|
if (st.st_size <= 0)
|
|
return NULL;
|
|
char * buff = malloc(st.st_size + 1);
|
|
FILE *fil = fopen(fname, "r");
|
|
fread(buff, st.st_size, 1, fil);
|
|
fclose(fil);
|
|
buff[st.st_size] = '\0';
|
|
return buff;
|
|
}
|
|
|
|
void guMatrixLoadIdentity(guMatrix4x4 *m)
|
|
{
|
|
int i, j;
|
|
for (i = 0; i < 4; i++)
|
|
{
|
|
for (j = 0; j < 4; j++)
|
|
{
|
|
(*m)[i][j] = (i == j) ? 1.0 : 0.0;
|
|
}
|
|
}
|
|
}
|
|
|
|
void guMatrixMult(guMatrix4x4 *m, guMatrix4x4 *a, guMatrix4x4 *b)
|
|
{
|
|
guMatrix4x4 tmp;
|
|
int i, j, p;
|
|
if (m == a)
|
|
{
|
|
memcpy(&tmp, a, sizeof(tmp));
|
|
a = &tmp;
|
|
}
|
|
for (i = 0; i < 4; i++)
|
|
{
|
|
for (j = 0; j < 4; j++)
|
|
{
|
|
GLfloat v = 0.0;
|
|
for (p = 0; p < 4; p++)
|
|
{
|
|
v += (*a)[i][p] * (*b)[p][j];
|
|
}
|
|
(*m)[i][j] = v;
|
|
}
|
|
}
|
|
}
|
|
|
|
void guMatrixTranslate(guMatrix4x4 *m, GLfloat x, GLfloat y, GLfloat z)
|
|
{
|
|
int i;
|
|
for (i = 0; i < 4; i++)
|
|
{
|
|
(*m)[i][3] += (*m)[i][0] * x + (*m)[i][1] * y + (*m)[i][2] * z;
|
|
}
|
|
}
|
|
|
|
void guMatrixScale(guMatrix4x4 *m, GLfloat x, GLfloat y, GLfloat z)
|
|
{
|
|
int i;
|
|
for (i = 0; i < 4; i++)
|
|
{
|
|
GLfloat *base = &(*m)[i][0];
|
|
base[0] *= x;
|
|
base[1] *= y;
|
|
base[2] *= z;
|
|
}
|
|
}
|
|
|
|
void guMatrixRotate(guMatrix4x4 *m, GLfloat angle,
|
|
GLfloat x, GLfloat y, GLfloat z)
|
|
{
|
|
GLfloat lx = x, ly = y, lz = z, p;
|
|
p = lx*lx + ly*ly + lz*lz;
|
|
if (p != 1.0)
|
|
{
|
|
GLfloat scale = sqrt(p);
|
|
lx /= scale;
|
|
ly /= scale;
|
|
lz /= scale;
|
|
}
|
|
GLfloat c = cos(angle);
|
|
GLfloat s = sin(angle);
|
|
guMatrix4x4 mult;
|
|
GLfloat oc = 1 - c;
|
|
mult[0][0] = lx * lx * oc + c;
|
|
mult[0][1] = lx * ly * oc - lz * s;
|
|
mult[0][2] = lx * lz * oc + ly * s;
|
|
mult[0][3] = 0.0;
|
|
mult[1][0] = ly * lx * oc + lz * s;
|
|
mult[1][1] = ly * ly * oc + c;
|
|
mult[1][2] = ly * lz * oc - lx * s;
|
|
mult[1][3] = 0.0;
|
|
mult[2][0] = lx * lz * oc - ly * s;
|
|
mult[2][1] = ly * lz * oc + lx * s;
|
|
mult[2][2] = lz * lz * oc + c;
|
|
mult[2][3] = 0.0;
|
|
mult[3][0] = 0.0;
|
|
mult[3][1] = 0.0;
|
|
mult[3][2] = 0.0;
|
|
mult[3][3] = 1.0;
|
|
guMatrixMult(m, m, &mult);
|
|
}
|
|
|
|
void guMatrixFrustum(guMatrix4x4 *m,
|
|
GLfloat left, GLfloat right,
|
|
GLfloat bottom, GLfloat top,
|
|
GLfloat near, GLfloat far)
|
|
{
|
|
int i, j;
|
|
guMatrix4x4 mult;
|
|
for (i = 0; i < 4; i++)
|
|
{
|
|
for (j = 0; j < 4; j++)
|
|
{
|
|
mult[i][j] = 0.0;
|
|
}
|
|
}
|
|
GLfloat rl = right - left;
|
|
GLfloat tb = top - bottom;
|
|
GLfloat fn = far - near;
|
|
mult[0][0] = 2 * near / rl;
|
|
mult[0][2] = (right + left) / rl;
|
|
mult[1][1] = 2 * near / tb;
|
|
mult[1][2] = (top + bottom) / tb;
|
|
mult[2][2] = - (far + near) / fn;
|
|
mult[2][3] = - 2 * far * near / fn;
|
|
mult[3][2] = -1.0;
|
|
guMatrixMult(m, m, &mult);
|
|
}
|
|
|
|
void guPerspective(guMatrix4x4 *m, GLfloat fovy, GLfloat aspect,
|
|
GLfloat near, GLfloat far)
|
|
{
|
|
int i, j;
|
|
guMatrix4x4 mult;
|
|
for (i = 0; i < 4; i++)
|
|
{
|
|
for (j = 0; j < 4; j++)
|
|
{
|
|
mult[i][j] = 0.0;
|
|
}
|
|
}
|
|
GLfloat f = 1.0 / tan(fovy / 2.0);
|
|
GLfloat nf = near - far;
|
|
mult[0][0] = f / aspect;
|
|
mult[1][1] = f;
|
|
mult[2][2] = (far + near) / nf;
|
|
mult[2][3] = (2 * far * near) / nf;
|
|
mult[3][2] = -1.0;
|
|
guMatrixMult(m, m, &mult);
|
|
}
|
|
|
|
void guOrtho(guMatrix4x4 *m,
|
|
GLfloat left, GLfloat right,
|
|
GLfloat bottom, GLfloat top,
|
|
GLfloat near, GLfloat far)
|
|
{
|
|
int i, j;
|
|
guMatrix4x4 mult;
|
|
for (i = 0; i < 4; i++)
|
|
{
|
|
for (j = 0; j < 4; j++)
|
|
{
|
|
mult[i][j] = 0.0;
|
|
}
|
|
}
|
|
GLfloat rl = right - left;
|
|
GLfloat tb = top - bottom;
|
|
GLfloat fn = far - near;
|
|
mult[0][0] = 2 / rl;
|
|
mult[0][3] = - (right + left) / rl;
|
|
mult[1][1] = 2 / tb;
|
|
mult[1][3] = - (top + bottom) / tb;
|
|
mult[2][2] = -2 / fn;
|
|
mult[2][3] = - (far + near) / fn;
|
|
mult[3][3] = 1.0;
|
|
}
|
|
|
|
char *guGetShaderLog(GLuint id)
|
|
{
|
|
GLint log_length;
|
|
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &log_length);
|
|
if (log_length > 0)
|
|
{
|
|
char *log = malloc(sizeof(char) * log_length);
|
|
glGetShaderInfoLog(id, log_length, &log_length, log);
|
|
return log;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
char *guGetProgramLog(GLuint id)
|
|
{
|
|
GLint log_length;
|
|
glGetProgramiv(id, GL_INFO_LOG_LENGTH, &log_length);
|
|
if (log_length > 0)
|
|
{
|
|
char *log = malloc(sizeof(char) * log_length);
|
|
glGetProgramInfoLog(id, log_length, &log_length, log);
|
|
return log;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
GLuint guMakeShaderFromFile(GLenum shaderType, const char *fname)
|
|
{
|
|
char *source;
|
|
source = loadFile(fname);
|
|
if (source == NULL)
|
|
{
|
|
fprintf(stderr, "Error reading file '%s'\n", fname);
|
|
return 0;
|
|
}
|
|
GLuint id = guMakeShader(shaderType, source);
|
|
free(source);
|
|
return id;
|
|
}
|
|
|
|
GLuint guMakeShader(GLenum shaderType, const char *source)
|
|
{
|
|
GLuint id;
|
|
GLint status;
|
|
|
|
id = glCreateShader(shaderType);
|
|
if (id <= 0)
|
|
{
|
|
fprintf(stderr, "Error creating shader object\n");
|
|
goto out;
|
|
}
|
|
|
|
glShaderSource(id, 1, &source, NULL);
|
|
|
|
glCompileShader(id);
|
|
|
|
glGetShaderiv(id, GL_COMPILE_STATUS, &status);
|
|
if (status != GL_TRUE)
|
|
{
|
|
fprintf(stderr, "Error compiling shader\n");
|
|
char *log = guGetShaderLog(id);
|
|
fprintf(stderr, "Shader Log:\n%s\n", log);
|
|
free(log);
|
|
goto cleanup_shader;
|
|
}
|
|
|
|
return id;
|
|
|
|
cleanup_shader:
|
|
glDeleteShader(id);
|
|
out:
|
|
return 0;
|
|
}
|