Commit e2efa549 authored by David Llewellyn-Jones's avatar David Llewellyn-Jones

Added shader C functions to Functy.

parent a21ef3b5
......@@ -2,7 +2,7 @@
# Process this file with autoconf to produce a configure script.
AC_PREREQ(2.61)
AC_INIT([Functy], [0.22], [david-web02@flypig.co.uk])
AC_INIT([Functy], [0.23], [david-web02@flypig.co.uk])
AC_CONFIG_AUX_DIR([config])
AM_INIT_AUTOMAKE([-Wall -Werror])
AC_CONFIG_SRCDIR([src/functy.c])
......@@ -13,12 +13,15 @@ AC_PROG_CC
AM_PROG_CC_C_O
# Checks for libraries.
PKG_CHECK_MODULES([FUNKTY], [gtk+-2.0 libglade-2.0])
PKG_CHECK_MODULES([FUNCTY], [gtk+-2.0 libglade-2.0 gdk-2.0])
# Checks for header files.
AC_HEADER_DIRENT
AC_HEADER_STDC
AC_CHECK_HEADERS([stdlib.h string.h sys/timeb.h])
AC_CHECK_HEADERS([stdlib.h string.h sys/timeb.h GLee.h])
# Checks for compiler characteristics
AC_C_BIGENDIAN
# Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_STDBOOL
......
bin_PROGRAMS = ../functy
___functy_SOURCES = function.c function.h function_private.h functy.c functy.h textures.c textures.h utils.c utils.h vis.c vis.h filesave.c filesave.h cartesian.c cartesian.h spherical.c spherical.h
___functy_SOURCES = function.c function.h function_private.h functy.c functy.h textures.c textures.h utils.c utils.h vis.c vis.h filesave.c filesave.h cartesian.c cartesian.h spherical.c spherical.h shader.c shader.h
___functy_LDADD = -lsymbolic -lgtkglext-x11-1.0 -lgdkglext-x11-1.0 -lGLU -lglut -lXmu -lXt -lSM -lICE -lgtk-x11-2.0 -lpangox-1.0 -lX11 -lgdk-x11-2.0 -latk-1.0 -lpangoft2-1.0 -lgdk_pixbuf-2.0 -lm -lpangocairo-1.0 -lgio-2.0 -lcairo -lpango-1.0 -lfreetype -lz -lfontconfig -lgobject-2.0 -lgmodule-2.0 @FUNKTY_LIBS@
___functy_LDADD = -lsymbolic -lgtkglext-x11-1.0 -lgdkglext-x11-1.0 -lGLU -lglut -lXmu -lXt -lSM -lICE -lgtk-x11-2.0 -lpangox-1.0 -lX11 -lgdk-x11-2.0 -latk-1.0 -lpangoft2-1.0 -lgdk_pixbuf-2.0 -lm -lpangocairo-1.0 -lgio-2.0 -lcairo -lpango-1.0 -lfreetype -lz -lfontconfig -lgobject-2.0 -lgmodule-2.0 -lglee @FUNCTY_LIBS@
___functy_CPPFLAGS = -DFUNCTYDIR=\"$(datadir)/@PACKAGE@\" -Wall -I/usr/include/gtkglext-1.0 -I/usr/lib/gtkglext-1.0/include -I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include -I/usr/include/pango-1.0 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng12 -I/usr/include/atk-1.0 @FUNKTY_CFLAGS@
___functy_CPPFLAGS = -DFUNCTYDIR=\"$(datadir)/@PACKAGE@\" -Wall -I/usr/include/gtkglext-1.0 -I/usr/lib/gtkglext-1.0/include -I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include -I/usr/include/pango-1.0 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng12 -I/usr/include/atk-1.0 @FUNCTY_CFLAGS@
#dist_pkgdata_DATA = ../ui/Functy.glade
......@@ -220,8 +220,8 @@ void CartesianPopulateVerticesNoColour (FuncPersist * psFuncData) {
double fYFunc;
double fZFunc;
double fZScreen;
double x;
double y;
//double x;
//double y;
Vector3 vX;
Vector3 vY;
Vector3 vN;
......@@ -245,8 +245,8 @@ void CartesianPopulateVerticesNoColour (FuncPersist * psFuncData) {
for (fY = 0.0; fY < fEnd; fY += psFuncData->fAccuracy) {
fYFunc = (fY * psFuncData->fYWidth) + psFuncData->fYMin;
x = fXFunc;
y = fYFunc;
//x = fXFunc;
//y = fYFunc;
if (psCartesianData->psVariableX) {
SetVariable (psCartesianData->psVariableX, fXFunc);
......@@ -293,8 +293,8 @@ void CartesianPopulateVerticesColour (FuncPersist * psFuncData) {
double fYFunc;
double fZFunc;
double fZScreen;
double x;
double y;
//double x;
//double y;
Vector3 vX;
Vector3 vY;
Vector3 vN;
......@@ -319,8 +319,8 @@ void CartesianPopulateVerticesColour (FuncPersist * psFuncData) {
for (fY = 0.0; fY < fEnd; fY += psFuncData->fAccuracy) {
fYFunc = (fY * psFuncData->fYWidth) + psFuncData->fYMin;
x = fXFunc;
y = fYFunc;
//x = fXFunc;
//y = fYFunc;
if (psCartesianData->psVariableX) {
SetVariable (psCartesianData->psVariableX, fXFunc);
......
......@@ -236,9 +236,9 @@ static gboolean ExposeEvent (GtkWidget * psWidget, GdkEventExpose * psEvent, gpo
static gboolean ButtonPressEvent (GtkWidget * psWidget, GdkEventButton * psEvent, gpointer * psData) {
FunctyPersist * psFunctyData = (FunctyPersist * )psData;
gboolean boMoving;
//gboolean boMoving;
boMoving = GetMoving (psFunctyData->psVisData);
//boMoving = GetMoving (psFunctyData->psVisData);
Mouse (psEvent->button, psEvent->type, psEvent->x, psEvent->y, psFunctyData->psVisData);
......
/*******************************************************************
* Shader.c
* Manage the OpenGL GLSL shaders
*
* David Llewellyn-Jones
* http://www.flypig.co.uk
*
* December 2011
*******************************************************************
*/
/* Includes */
#include "shader.h"
/* Defines */
/* Enums */
/* Structures */
struct _ShaderPersist {
GLuint uVertex;
GLuint uFragment;
GLuint uProgram;
};
/* Function prototypes */
void LinkShader (GLuint uShader, ShaderPersist * psShaderData);
/* Function defininitions */
ShaderPersist * NewShaderPersist () {
ShaderPersist * psShaderData;
GLenum eError;
psShaderData = (ShaderPersist *)calloc (1, sizeof (ShaderPersist));
psShaderData->uProgram = glCreateProgram ();
eError = glGetError ();
if (eError != 0) {
fprintf (stderr, "OpenGL program creation failed. Error %d.\n", eError);
}
psShaderData->uVertex = 0u;
psShaderData->uFragment = 0u;
return psShaderData;
}
void DeleteShaderPersist (ShaderPersist * psShaderData) {
if (psShaderData) {
if (psShaderData->uVertex != 0) {
glDeleteShader (psShaderData->uVertex);
if (psShaderData->uProgram != 0u) {
glDetachShader (psShaderData->uProgram, psShaderData->uVertex);
}
}
if (psShaderData->uFragment != 0) {
glDeleteShader (psShaderData->uFragment);
if (psShaderData->uProgram != 0u) {
glDetachShader (psShaderData->uProgram, psShaderData->uFragment);
}
}
if (psShaderData->uProgram != 0u) {
glDeleteProgram (psShaderData->uProgram);
}
free (psShaderData);
psShaderData = NULL;
}
}
char const * LoadTextFile (char const * const szFilename) {
FILE * hFile;
char * szMemory;
int nLength;
int nRead;
szMemory = NULL;
hFile = fopen (szFilename, "r");
if (hFile) {
fseek (hFile, 0, SEEK_END);
nLength = ftell (hFile);
szMemory = (char *)malloc (nLength + 1);
szMemory[nLength] = 0;
fseek (hFile, 0, SEEK_SET);
nRead = fread (szMemory, 1, nLength, hFile);
if (nRead < nLength) {
fprintf (stderr, "Shader file incompletely read.\n");
}
fclose (hFile);
}
return szMemory;
}
void ReleaseTextFile (char const * szMemory) {
free ((void *)szMemory);
}
void LoadVertexShader (char const * const szFilename, ShaderPersist * psShaderData) {
char const * szShader;
GLint nStatus;
GLint nLogsize;
char * szLog;
GLenum eError;
szShader = LoadTextFile (szFilename);
if (szShader) {
psShaderData->uVertex = glCreateShader (GL_VERTEX_SHADER);
glShaderSource (psShaderData->uVertex, 1, & szShader, NULL);
glCompileShader (psShaderData->uVertex);
glGetShaderiv (psShaderData->uVertex, GL_COMPILE_STATUS, & nStatus);
if (nStatus != GL_TRUE) {
eError = glGetError ();
fprintf (stderr, "Vertex shader failed to compile. Error %d.\n", eError);
}
glGetShaderiv (psShaderData->uVertex, GL_INFO_LOG_LENGTH, & nLogsize);
if (nLogsize > 1) {
szLog = (char *)malloc (nLogsize + 1);
glGetShaderInfoLog (psShaderData->uVertex, nLogsize + 1, NULL, szLog);
szLog[nLogsize] = 0;
printf ("%s\n", szLog);
free (szLog);
szLog = NULL;
}
ReleaseTextFile (szShader);
szShader = NULL;
LinkShader (psShaderData->uVertex, psShaderData);
}
else {
fprintf (stderr, "Vertex shader file could not be read.\n");
}
}
void LoadFragmentShader (char const * const szFilename, ShaderPersist * psShaderData) {
char const * szShader;
GLint nStatus;
GLint nLogsize;
char * szLog;
GLenum eError;
szShader = LoadTextFile (szFilename);
if (szShader) {
psShaderData->uFragment = glCreateShader (GL_FRAGMENT_SHADER);
glShaderSource (psShaderData->uFragment, 1, & szShader, NULL);
glCompileShader (psShaderData->uFragment);
glGetShaderiv (psShaderData->uFragment, GL_COMPILE_STATUS, & nStatus);
if (nStatus != GL_TRUE) {
eError = glGetError ();
fprintf (stderr, "Fragment shader failed to compile. Error %d.\n", eError);
}
glGetShaderiv (psShaderData->uFragment, GL_INFO_LOG_LENGTH, & nLogsize);
if (nLogsize > 1) {
szLog = (char *)malloc (nLogsize + 1);
glGetShaderInfoLog (psShaderData->uFragment, nLogsize + 1, NULL, szLog);
szLog[nLogsize] = 0;
printf ("%s\n", szLog);
free (szLog);
szLog = NULL;
}
ReleaseTextFile (szShader);
szShader = NULL;
LinkShader (psShaderData->uFragment, psShaderData);
}
else {
fprintf (stderr, "Fragment shader file could not be read.\n");
}
}
void LinkShader (GLuint uShader, ShaderPersist * psShaderData) {
GLint nStatus;
GLint nLogsize;
char * szLog;
GLenum eError;
glAttachShader (psShaderData->uProgram, uShader);
glLinkProgram (psShaderData->uProgram);
glGetProgramiv (psShaderData->uProgram, GL_LINK_STATUS, & nStatus);
if (nStatus != GL_TRUE) {
eError = glGetError ();
fprintf (stderr, "Shader failed to link. Error %d.\n", eError);
}
glGetProgramiv (psShaderData->uProgram, GL_INFO_LOG_LENGTH, & nLogsize);
if (nLogsize > 1) {
szLog = (char *)malloc (nLogsize + 1);
glGetProgramInfoLog (psShaderData->uProgram, nLogsize + 1, NULL, szLog);
szLog[nLogsize] = 0;
printf ("%s\n", szLog);
free (szLog);
szLog = NULL;
}
}
void ActivateShader (ShaderPersist * psShaderData) {
if (psShaderData->uProgram != 0u) {
if ((psShaderData->uVertex != 0) || (psShaderData->uFragment != 0)) {
glUseProgram (psShaderData->uProgram);
}
}
}
void DeactivateShader (ShaderPersist * psShaderData) {
glUseProgram (0u);
}
/*******************************************************************
* Shader.c
* Manage the OpenGL GLSL shaders
*
* David Llewellyn-Jones
* http://www.flypig.co.uk
*
* December 2011
*******************************************************************
*/
#ifndef SHADER_H
#define SHADER_H
/* Includes */
#include "utils.h"
/* Defines */
/* Enums */
/* Structures */
typedef struct _ShaderPersist ShaderPersist;
/* Function prototypes */
ShaderPersist * NewShaderPersist ();
void DeleteShaderPersist (ShaderPersist * psShaderData);
void LoadVertexShader (char const * const szFilename, ShaderPersist * psShaderData);
void LoadFragmentShader (char const * const szFilename, ShaderPersist * psShaderData);
void ActivateShader (ShaderPersist * psShaderData);
void DeactivateShader (ShaderPersist * psShaderData);
#endif /* CELTIC_H */
......@@ -8,34 +8,35 @@
// Spring 2008
///////////////////////////////////////////////////////////////////
#ifndef UTILS_H
#define UTILS_H
#ifndef UTILS_H
#define UTILS_H
///////////////////////////////////////////////////////////////////
// Includes
#define _USE_MATH_DEFINES
#include <math.h>
#ifndef M_TWOPI
#define M_TWOPI (M_PI * 2.0)
#include <math.h>
#ifndef M_TWOPI
#define M_TWOPI (M_PI * 2.0)
#endif
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#endif
#include <glib.h>
#include <GLee.h>
#include <glib.h>
#include <GL/glut.h>
#include <gtk/gtk.h>
#include <stdbool.h>
#include <gtk/gtk.h>
#include <stdbool.h>
///////////////////////////////////////////////////////////////////
// Defines
///////////////////////////////////////////////////////////////////
// Structures and enumerations
typedef struct _Vector3 {
GLfloat fX;
GLfloat fY;
......@@ -64,16 +65,16 @@ float absf (float fValue);
Vector3 Normal (Vector3 * v1, Vector3 * v2);
void Normalise (Vector3 * v1);
void Normalise3f (float * pfX, float * pfY, float * pfZ);
float Length (Vector3 * v1);
float Length (Vector3 * v1);
Matrix3 Invert (Matrix3 * m1);
float Determinant (Matrix3 * m1);
float DotProdAngle (float fX1, float fY1, float fX2, float fY2);
Vector3 MultMatrixVector (Matrix3 * m1, Vector3 * v1);
void PrintMatrix (Matrix3 * m1);
void PrintVector (Vector3 * v1);
///////////////////////////////////////////////////////////////////
// Function definitions
#endif /* UTILS_H */
#endif /* UTILS_H */
......@@ -30,6 +30,7 @@
#include "vis.h"
#include "textures.h"
#include "spherical.h"
#include "shader.h"
///////////////////////////////////////////////////////////////////
// Defines
......@@ -166,6 +167,7 @@ static GLfloat gGridColour[] = { 1.0, 1.0, 1.0, 1.0 };
///////////////////////////////////////////////////////////////////
// Function prototypes
void Render (VisPersist * psVisData);
void RenderTextInSpace (char const * szText, GLdouble fX, GLdouble fY, GLdouble fZ);
void DrawTextOverlay (VisPersist * psVisData);
void ChangeView (float fTheta, float fPhi, float fPsi, float fRadius, VisPersist * psVisData);
......@@ -498,7 +500,7 @@ void RenderTextInSpace (char const * szText, GLdouble fX, GLdouble fY, GLdouble
glEnable (GL_LIGHTING);
}
void Display (VisPersist * psVisData) {
void Render (VisPersist * psVisData) {
glLoadIdentity ();
gluLookAt ((psVisData->fViewRadius) * psVisData->fX, (psVisData->fViewRadius) * psVisData->fY,
(psVisData->fViewRadius) * psVisData->fZ, 0.0, 0.0, 0.0, psVisData->fXn, psVisData->fYn, psVisData->fZn);
......@@ -525,16 +527,16 @@ void Display (VisPersist * psVisData) {
}
void DrawAxes (VisPersist * psVisData) {
int nXSize;
int nYSize;
int nZSize;
//int nXSize;
//int nYSize;
//int nZSize;
int nOctant;
glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
nXSize = (AXIS_XSIZE / SQUARE_NUM);
nYSize = (AXIS_YSIZE / SQUARE_NUM);
nZSize = (AXIS_ZSIZE / SQUARE_NUM);
//nXSize = (AXIS_XSIZE / SQUARE_NUM);
//nYSize = (AXIS_YSIZE / SQUARE_NUM);
//nZSize = (AXIS_ZSIZE / SQUARE_NUM);
glMaterialfv (GL_FRONT, GL_SPECULAR, gBoardSpecular);
glMaterialfv (GL_FRONT, GL_SHININESS, gBoardShininess);
......@@ -711,7 +713,7 @@ void DrawFloor (VisPersist * psVisData) {
void DrawNumbersX (double fY, double fZ, VisPersist * psVisData) {
double fX;
double fXFirst;
double fXMin;
//double fXMin;
double fXMax;
double fXGrid;
char szNumber[NUMBER_MAX];
......@@ -719,7 +721,7 @@ void DrawNumbersX (double fY, double fZ, VisPersist * psVisData) {
fNumber = ceil (psVisData->fXMin / psVisData->fXGrid) * psVisData->fXGrid;
fXMin = -AXIS_XHSIZE;
//fXMin = -AXIS_XHSIZE;
fXGrid = ((psVisData->fXGrid / psVisData->fXWidth) * AXIS_XSIZE);
fXMax = AXIS_XHSIZE;
......@@ -738,7 +740,7 @@ void DrawNumbersX (double fY, double fZ, VisPersist * psVisData) {
void DrawNumbersY (double fX, double fZ, VisPersist * psVisData) {
double fY;
double fYFirst;
double fYMin;
//double fYMin;
double fYMax;
double fYGrid;
char szNumber[NUMBER_MAX];
......@@ -746,7 +748,7 @@ void DrawNumbersY (double fX, double fZ, VisPersist * psVisData) {
fNumber = ceil (psVisData->fYMin / psVisData->fYGrid) * psVisData->fYGrid;
fYMin = -AXIS_YHSIZE;
//fYMin = -AXIS_YHSIZE;
fYGrid = ((psVisData->fYGrid / psVisData->fYWidth) * AXIS_YSIZE);
fYMax = AXIS_YHSIZE;
......@@ -765,7 +767,7 @@ void DrawNumbersY (double fX, double fZ, VisPersist * psVisData) {
void DrawNumbersZ (double fX, double fY, VisPersist * psVisData) {
double fZ;
double fZFirst;
double fZMin;
//double fZMin;
double fZMax;
double fZGrid;
char szNumber[NUMBER_MAX];
......@@ -773,7 +775,7 @@ void DrawNumbersZ (double fX, double fY, VisPersist * psVisData) {
fNumber = ceil (psVisData->fZMin / psVisData->fZGrid) * psVisData->fZGrid;
fZMin = -AXIS_ZHSIZE;
//fZMin = -AXIS_ZHSIZE;
fZGrid = ((psVisData->fZGrid / psVisData->fZWidth) * AXIS_ZSIZE);
fZMax = AXIS_ZHSIZE;
......@@ -1179,7 +1181,7 @@ void RecentreGraph (FuncPersist * psFuncData) {
void DrawTextOverlay (VisPersist * psVisData) {
GString * szString;
int nStringWidth;
//int nStringWidth;
szString = g_string_new ("");
......@@ -1193,7 +1195,7 @@ void DrawTextOverlay (VisPersist * psVisData) {
g_string_printf (szString, "Hello!");
nStringWidth = BitmapStringWidth (GLUT_BITMAP_HELVETICA_18, szString->str);
//nStringWidth = BitmapStringWidth (GLUT_BITMAP_HELVETICA_18, szString->str);
//RenderBitmapString (psVisData->nScreenWidth - nStringWidth - 16.0, psVisData->nScreenHeight - 32, GLUT_BITMAP_HELVETICA_18, szString->str);
glEnable (GL_LIGHTING);
......@@ -1337,13 +1339,13 @@ bool PointTowards (float fXPos, float fYPos, float fZPos, float fRadius, VisPers
void Idle (VisPersist * psVisData) {
struct timeb sTime;
float fTimeDelta;
//float fTimeDelta;
psVisData->fPrevTime = psVisData->fCurrentTime;
ftime (& sTime);
psVisData->fCurrentTime = (double)(sTime.time) + (double)(sTime.millitm) / 1000.0;
if (psVisData->fCurrentTime < 0) psVisData->fCurrentTime = 0.0;
fTimeDelta = psVisData->fCurrentTime - psVisData->fPrevTime;
//fTimeDelta = psVisData->fCurrentTime - psVisData->fPrevTime;
KeyIdle (psVisData);
......@@ -1396,7 +1398,7 @@ void Redraw (VisPersist * psVisData) {
//glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glPushMatrix ();
Display (psVisData);
Render (psVisData);
DrawTextOverlay (psVisData);
glPopMatrix ();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment