* Merged in my latest changes into SVN repository.

* Arranged to avoid reading/writing textures at same time.
* Removed depth and stencil buffers from FBO's.
* Eliminated 1-component FBO that caused grief for FBO completeness test.
* Added back missing cubeShaderNoTexture.vert
This commit is contained in:
sjbaker
2006-09-20 00:44:41 +00:00
parent 22fdf584b8
commit dad2cb634e
12 changed files with 884 additions and 803 deletions

View File

@@ -1,5 +1,10 @@
(Add new stuff at the top!) (Add new stuff at the top!)
* Merged in my latest changes into SVN repository.
* Arranged to avoid reading/writing textures at same time.
* Removed depth and stencil buffers from FBO's.
* Eliminated 1-component FBO that caused grief for FBO completeness test.
* Added back missing cubeShaderNoTexture.vert
------------------- FIRST ADDITION TO SUBVERSION --------------------- ------------------- FIRST ADDITION TO SUBVERSION ---------------------
GPUphysics-0.4 GPUphysics-0.4

View File

@@ -1,63 +1,63 @@
I have added some command line options to hopefully I have added some command line options to hopefully
help with our debugging problems. help with our debugging problems.
1) Run without any shaders at all: 1) Run without any shaders at all:
GPU_physics_demo -s GPU_physics_demo -s
This should produce a grey screen with a neat grid of randomly This should produce a grey screen with a neat grid of randomly
coloured cubes that are sitting completely motionless. If this coloured cubes that are sitting completely motionless. If this
doesn't work then render-to-texture and shaders are not the doesn't work then render-to-texture and shaders are not the
problem and we have some very basic OpenGL problem to worry problem and we have some very basic OpenGL problem to worry
about. about.
2) Run with just one shader - but no render-to-texture: 2) Run with just one shader - but no render-to-texture:
GPU_physics_demo -p GPU_physics_demo -p
Just like '-s', this should produce a grey screen with a neat Just like '-s', this should produce a grey screen with a neat
grid of randomly coloured cubes that are sitting completely grid of randomly coloured cubes that are sitting completely
motionless. This time, the vertex shader is reading the motionless. This time, the vertex shader is reading the
positioning information from a texturemap. If this positioning information from a texturemap. If this
doesn't work then render-to-texture isn't the problem but doesn't work then render-to-texture isn't the problem but
something is amiss in shader-land. something is amiss in shader-land.
There are several possibilities - the nastiest of which might There are several possibilities - the nastiest of which might
be that your graphics card/driver doesn't support floating point be that your graphics card/driver doesn't support floating point
textures. (This is pretty much 'Game Over' for you because textures. (This is pretty much 'Game Over' for you because
without that, doing physics in the GPU is going to be without that, doing physics in the GPU is going to be
virtually impossible). virtually impossible).
3) Run without forces being applied: 3) Run without forces being applied:
GPU_physics -f GPU_physics -f
This sets the cubes off moving at constant speed (each cube This sets the cubes off moving at constant speed (each cube
going at a different speed and spinning). going at a different speed and spinning).
Run without collision against the ground: Run without collision against the ground:
GPU_physics -c GPU_physics -c
The cubes move under gravity - but don't interact with the The cubes move under gravity - but don't interact with the
notional ground plane so the eventually fall off the bottom notional ground plane so the eventually fall off the bottom
of the screen.. of the screen..
If either (3) or (4) fails but (1) and (2) worked then the If either (3) or (4) fails but (1) and (2) worked then the
problem is probably something to do with render-to-texture. problem is probably something to do with render-to-texture.
This is the most likely scenario. This is the most likely scenario.
4) Run without vertex texturing: 4) Run without vertex texturing:
GPU_physics_demo -v GPU_physics_demo -v
On hardware that doesn't support vertex texturing, this flag On hardware that doesn't support vertex texturing, this flag
is turned on by default (and things run about 5x slower!) is turned on by default (and things run about 5x slower!)
Use this flag to force the software to run without vertex Use this flag to force the software to run without vertex
texturing on hardware that does actually support it. texturing on hardware that does actually support it.
You can use this flag in conjunction with any of the others. You can use this flag in conjunction with any of the others.

View File

@@ -1,101 +1,113 @@
/**********************\ /**********************\
* * * *
* Determine OS type * * Determine OS type *
* * * *
\**********************/ \**********************/
#if defined(__CYGWIN__) #if defined(__CYGWIN__)
#define GPUP_WIN32 1 #define GPUP_WIN32 1
#define GPUP_CYGWIN 1 /* Windoze AND Cygwin. */ #define GPUP_CYGWIN 1 /* Windoze AND Cygwin. */
#elif defined(_WIN32) || defined(__WIN32__) || defined(_MSC_VER) #elif defined(_WIN32) || defined(__WIN32__) || defined(_MSC_VER)
#define GPUP_WIN32 1 #define GPUP_WIN32 1
#define GPUP_MSVC 1 /* Windoze AND MSVC. */ #define GPUP_MSVC 1 /* Windoze AND MSVC. */
#elif defined(__BEOS__) #elif defined(__BEOS__)
#define GPUP_BEOS 1 #define GPUP_BEOS 1
#elif defined( macintosh ) #elif defined( macintosh )
#define GPUP_MACINTOSH 1 #define GPUP_MACINTOSH 1
#elif defined(__APPLE__) #elif defined(__APPLE__)
#define GPUP_MAC_OSX 1 #define GPUP_MAC_OSX 1
#elif defined(__linux__) #elif defined(__linux__)
#define GPUP_LINUX 1 #define GPUP_LINUX 1
#elif defined(__sgi) #elif defined(__sgi)
#define GPUP_IRIX 1 #define GPUP_IRIX 1
#elif defined(_AIX) #elif defined(_AIX)
#define GPUP_AIX 1 #define GPUP_AIX 1
#elif defined(SOLARIS) || defined(sun) #elif defined(SOLARIS) || defined(sun)
#define GPUP_SOLARIS 1 #define GPUP_SOLARIS 1
#elif defined(hpux) #elif defined(hpux)
#define GPUP_HPUX 1 #define GPUP_HPUX 1
#elif (defined(__unix__) || defined(unix)) && !defined(USG) #elif (defined(__unix__) || defined(unix)) && !defined(USG)
#define GPUP_BSD 1 #define GPUP_BSD 1
#endif #endif
#if defined(BORLANDBUILDER) #if defined(BORLANDBUILDER)
#define GPUP_BB 1 #define GPUP_BB 1
#endif #endif
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
/* /*
Add specialised includes/defines... Add specialised includes/defines...
*/ */
#ifdef GPUP_WIN32 #ifdef GPUP_WIN32
#include <windows.h> #include <windows.h>
#include <mmsystem.h> #include <mmsystem.h>
#include <regstr.h> #include <regstr.h>
#define GPUP_WGL 1 #define GPUP_WGL 1
#endif #endif
#ifdef GPUP_CYGWIN #ifdef GPUP_CYGWIN
#include <unistd.h> #include <unistd.h>
#define GPUP_WGL 1 #define GPUP_WGL 1
#endif #endif
#ifdef GPUP_BEOS #ifdef GPUP_BEOS
#include <be/kernel/image.h> #include <be/kernel/image.h>
#define GPUP_GLX 1 #define GPUP_GLX 1
#endif #endif
#ifdef GPUP_MACINTOSH #ifdef GPUP_MACINTOSH
#include <CodeFragments.h> #include <CodeFragments.h>
#include <unistd.h> #include <unistd.h>
#define GPUP_AGL 1 #define GPUP_AGL 1
#endif #endif
#ifdef GPUP_MAC_OSX #ifdef GPUP_MAC_OSX
#include <unistd.h> #include <unistd.h>
#define GPUP_CGL 1 #define GPUP_CGL 1
#endif #endif
#if defined(GPUP_LINUX) || defined(GPUP_BSD) || defined(GPUP_IRIX) || defined(GPUP_SOLARIS) || defined(GPUP_AIX) #if defined(GPUP_LINUX) || defined(GPUP_BSD) || defined(GPUP_IRIX) || defined(GPUP_SOLARIS) || defined(GPUP_AIX)
#include <unistd.h> #include <unistd.h>
#include <dlfcn.h> #include <dlfcn.h>
#include <fcntl.h> #include <fcntl.h>
#define GPUP_GLX 1 #define GPUP_GLX 1
#endif #endif
#if defined(GPUP_BSD) #if defined(GPUP_BSD)
#include <sys/param.h> #include <sys/param.h>
#define GPUP_GLX 1 #define GPUP_GLX 1
#endif #endif
#include <assert.h> #include <assert.h>
#include <limits.h> #include <limits.h>
#include <math.h> #include <math.h>
#include <float.h> #include <float.h>
#include <errno.h> #include <errno.h>
#include <GL/glew.h> #include <GL/glew.h>
#if defined(GPUP_MAC_OSX) && !defined (VMDMESA) #if defined(GPUP_MAC_OSX) && !defined (VMDMESA)
#include <OpenGL/gl.h> #include <OpenGL/gl.h>
#include <OpenGL/glu.h> #include <OpenGL/glu.h>
#include <GLUT/glut.h> #include <GLUT/glut.h>
#else #else
#include <GL/glut.h> #include <GL/glut.h>
#include <GL/gl.h> #include <GL/gl.h>
#endif #endif
#ifdef DISABLE_GL_ERROR_CHECKS
inline void showGLerror ( const char * ) {}
#else
inline void showGLerror ( const char *msg )
{
GLenum err ;
while ( (err = glGetError()) != GL_NO_ERROR )
fprintf ( stderr, "%s: OpenGL Error - %s\n", msg, gluErrorString ( err ) ) ;
}
#endif

View File

@@ -73,7 +73,7 @@ static FrameBufferObject *rotation ;
static FrameBufferObject *velocity ; static FrameBufferObject *velocity ;
static FrameBufferObject *rotvelocity ; static FrameBufferObject *rotvelocity ;
static FrameBufferObject *force ; static FrameBufferObject *force ;
static FrameBufferObject *mass ; static FrameBufferObject *massXX ;
static FrameBufferObject *old ; static FrameBufferObject *old ;
#define TEX_SIZE 128 #define TEX_SIZE 128
@@ -139,7 +139,7 @@ void initMotionTextures ()
velocity = NULL ; velocity = NULL ;
rotvelocity = NULL ; rotvelocity = NULL ;
force = NULL ; force = NULL ;
mass = NULL ; massXX = NULL ;
} }
else else
{ {
@@ -148,13 +148,13 @@ void initMotionTextures ()
if ( debugOpt == DRAW_WITHOUT_FORCES ) if ( debugOpt == DRAW_WITHOUT_FORCES )
{ {
force = NULL ; force = NULL ;
mass = NULL ; massXX = NULL ;
} }
else else
{ {
force = new FrameBufferObject ( TEX_SIZE, TEX_SIZE, 3, FBO_FLOAT ) ; force = new FrameBufferObject ( TEX_SIZE, TEX_SIZE, 3, FBO_FLOAT ) ;
mass = new FrameBufferObject ( TEX_SIZE, TEX_SIZE, 1, FBO_FLOAT ) ; massXX = new FrameBufferObject ( TEX_SIZE, TEX_SIZE, 3, FBO_FLOAT ) ;
} }
} }
@@ -164,14 +164,14 @@ void initMotionTextures ()
float *velocityData ; float *velocityData ;
float *rotvelocityData ; float *rotvelocityData ;
float *forceData ; float *forceData ;
float *massData ; float *massXXData ;
if ( debugOpt == DRAW_WITHOUT_PHYSICS ) if ( debugOpt == DRAW_WITHOUT_PHYSICS )
{ {
velocityData = NULL ; velocityData = NULL ;
rotvelocityData = NULL ; rotvelocityData = NULL ;
forceData = NULL ; forceData = NULL ;
massData = NULL ; massXXData = NULL ;
} }
else else
{ {
@@ -181,12 +181,12 @@ void initMotionTextures ()
if ( debugOpt == DRAW_WITHOUT_FORCES ) if ( debugOpt == DRAW_WITHOUT_FORCES )
{ {
forceData = NULL ; forceData = NULL ;
massData = NULL ; massXXData= NULL ;
} }
else else
{ {
forceData = new float [ TEX_SIZE * TEX_SIZE * 3 ] ; forceData = new float [ TEX_SIZE * TEX_SIZE * 3 ] ;
massData = new float [ TEX_SIZE * TEX_SIZE ] ; massXXData = new float [ TEX_SIZE * TEX_SIZE * 3 ] ;
} }
} }
@@ -229,7 +229,9 @@ void initMotionTextures ()
forceData [ (i*TEX_SIZE + j) * 3 + 2 ] = 0.0f ; forceData [ (i*TEX_SIZE + j) * 3 + 2 ] = 0.0f ;
/* One kg in weight each */ /* One kg in weight each */
massData [ i*TEX_SIZE + j ] = 1.0f ; massXXData [ (i*TEX_SIZE + j) * 3 + 0 ] = 1.0f ;
massXXData [ (i*TEX_SIZE + j) * 3 + 1 ] = 0.0f ; /* Unused */
massXXData [ (i*TEX_SIZE + j) * 3 + 2 ] = 0.0f ; /* Unused */
} }
} }
} }
@@ -248,7 +250,7 @@ void initMotionTextures ()
if ( debugOpt != DRAW_WITHOUT_FORCES ) if ( debugOpt != DRAW_WITHOUT_FORCES )
{ {
force -> fillTexture ( forceData ) ; force -> fillTexture ( forceData ) ;
mass -> fillTexture ( massData ) ; massXX -> fillTexture ( massXXData ) ;
} }
} }
} }
@@ -278,13 +280,13 @@ void initPhysicsShaders ()
"uniform vec4 g_dt ;" "uniform vec4 g_dt ;"
"uniform sampler2D old_velocity ;" "uniform sampler2D old_velocity ;"
"uniform sampler2D force ;" "uniform sampler2D force ;"
"uniform sampler2D mass ;" "uniform sampler2D massXX ;"
"void main() {" "void main() {"
" gl_FragColor = vec4 (" " gl_FragColor = vec4 ("
" texture2D ( old_velocity, gl_TexCoord[0].st ).xyz +" " texture2D ( old_velocity, gl_TexCoord[0].st ).xyz +"
" g_dt.w * ( g_dt.xyz +" " g_dt.w * ( g_dt.xyz +"
" texture2D ( force , gl_TexCoord[0].st ).xyz /" " texture2D ( force , gl_TexCoord[0].st ).xyz /"
" texture2D ( mass , gl_TexCoord[0].st ).x)," " texture2D ( massXX , gl_TexCoord[0].st ).x),"
" 1.0 ) ; }", " 1.0 ) ; }",
"VelocityGenerator Frag Shader" ) ; "VelocityGenerator Frag Shader" ) ;
assert ( velocityGenerator -> compiledOK () ) ; assert ( velocityGenerator -> compiledOK () ) ;
@@ -578,7 +580,7 @@ void display ( void )
velocityGenerator -> use () ; velocityGenerator -> use () ;
velocityGenerator -> applyTexture ( "old_velocity", old , 0 ) ; velocityGenerator -> applyTexture ( "old_velocity", old , 0 ) ;
velocityGenerator -> applyTexture ( "force" , force , 1 ) ; velocityGenerator -> applyTexture ( "force" , force , 1 ) ;
velocityGenerator -> applyTexture ( "mass" , mass , 2 ) ; velocityGenerator -> applyTexture ( "massXX" , massXX , 2 ) ;
velocityGenerator -> setUniform4f ( "g_dt", 0.0f, -9.8f, 0.0f, 0.016f ) ; velocityGenerator -> setUniform4f ( "g_dt", 0.0f, -9.8f, 0.0f, 0.016f ) ;
velocity -> paint () ; velocity -> paint () ;
} }

View File

@@ -1,25 +1,25 @@
/* GPUphysics -- A library and demo program to assist in running physics /* GPUphysics -- A library and demo program to assist in running physics
simulations on a GPU. simulations on a GPU.
Copyright (C) 2006 Stephen J. Baker Copyright (C) 2006 Stephen J. Baker
This software is provided 'as-is', without any express or implied This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages
arising from the use of this software. arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions: freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not 1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be in a product, an acknowledgment in the product documentation would be
appreciated but is not required. appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be 2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software. misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution. 3. This notice may not be removed or altered from any source distribution.
Steve Baker sjbaker1@airmail.net Steve Baker sjbaker1@airmail.net
*/ */

View File

@@ -1,106 +1,106 @@
Author : Steve Baker (sjbaker1@airmail.net) Author : Steve Baker (sjbaker1@airmail.net)
Build using: 'make' Build using: 'make'
Run using: 'GPU_physics_demo' Run using: 'GPU_physics_demo'
Requires : A pretty modern graphics card - nVidia 6800 or better. Requires : A pretty modern graphics card - nVidia 6800 or better.
Not tested on ATI hardware. Not tested on ATI hardware.
License : zLib - see file: 'LICENSE' License : zLib - see file: 'LICENSE'
---------------------------------------------------------- ----------------------------------------------------------
Demonstrates 16,384 cubes moving under the influence of Demonstrates 16,384 cubes moving under the influence of
gravity and one other force - all with potentially different gravity and one other force - all with potentially different
masses, velocities, applied forces, etc. The CPU is not masses, velocities, applied forces, etc. The CPU is not
involved in any of the calculations - or even in applying involved in any of the calculations - or even in applying
those calculations to the graphics when rendering the cubes. those calculations to the graphics when rendering the cubes.
---------------------------------------------------------- ----------------------------------------------------------
C++ Sources: C++ Sources:
shaderSupport.cxx -- A wrapper layer for OpenGL/GLSL shaders. shaderSupport.cxx -- A wrapper layer for OpenGL/GLSL shaders.
fboSupport.cxx -- A wrapper layer for render-to-texture tricks. fboSupport.cxx -- A wrapper layer for render-to-texture tricks.
GPU_physics_demo.cxx -- The application code. GPU_physics_demo.cxx -- The application code.
---------------------------------------------------------- ----------------------------------------------------------
GLSLShader Sources used in the Demo: GLSLShader Sources used in the Demo:
cubeShader.vert cubeShader.vert
cubeShaderNoVertexTexture.vert cubeShaderNoVertexTexture.vert
cubeShader.frag cubeShader.frag
---------------------------------------------------------- ----------------------------------------------------------
The objective of this library is to provide a basis for the The objective of this library is to provide a basis for the
the Bullet physics engine to: the Bullet physics engine to:
* Compile shader source code into a 'class GLSL_ShaderPair' object. * Compile shader source code into a 'class GLSL_ShaderPair' object.
* Allocate an NxM texture that you can render into. (class FrameBufferObject) * Allocate an NxM texture that you can render into. (class FrameBufferObject)
* Populate a specified texture with floating point data. * Populate a specified texture with floating point data.
* Run a specified shader on a specified set of textures - leaving the * Run a specified shader on a specified set of textures - leaving the
results in another specified texture. results in another specified texture.
* Demonstrate that this texture can be applied directly into the * Demonstrate that this texture can be applied directly into the
graphics code without any CPU intervention whatever. graphics code without any CPU intervention whatever.
------------------------------------------------------------- -------------------------------------------------------------
Step 1: In initialisation code: Step 1: In initialisation code:
* Declare some number of FrameBufferObjects. These are texture * Declare some number of FrameBufferObjects. These are texture
maps that you can render to that represent the 'variables' maps that you can render to that represent the 'variables'
in the massively parallel calculations: in the massively parallel calculations:
eg: eg:
position = new FrameBufferObject ( 512, 512, 4, FBO_FLOAT ) ; position = new FrameBufferObject ( 512, 512, 4, FBO_FLOAT ) ;
* Declare some number of GLSL_ShaderPair objects: * Declare some number of GLSL_ShaderPair objects:
/* GLSL code read from disk */ /* GLSL code read from disk */
teapotShaders = new GLSL_ShaderPair ( teapotShaders = new GLSL_ShaderPair (
"TeapotShader", "shader.vert", "shader.frag" ) ; "TeapotShader", "shader.vert", "shader.frag" ) ;
/* ...or...Inline GLSL code */ /* ...or...Inline GLSL code */
textureShaders = new GLSL_ShaderPair ( textureShaders = new GLSL_ShaderPair (
"TextureShader", "TextureShader",
"void main() { gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; }", "void main() { gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; }",
"Render2Texture Vert Shader", "Render2Texture Vert Shader",
"void main() { gl_FragColor = vec4 ( 1, 1, 0, 1 ) ; }", "void main() { gl_FragColor = vec4 ( 1, 1, 0, 1 ) ; }",
"Render2Texture Frag Shader" ) ) "Render2Texture Frag Shader" ) )
* Check that they compiled OK: * Check that they compiled OK:
assert ( textureShaders -> compiledOK () ) ; assert ( textureShaders -> compiledOK () ) ;
assert ( teapotShaders -> compiledOK () ) ; assert ( teapotShaders -> compiledOK () ) ;
Step 2: In the main loop: Step 2: In the main loop:
* Select one shader pair to use to do the calculations: * Select one shader pair to use to do the calculations:
teapotShaders -> use () ; teapotShaders -> use () ;
* Set any 'uniform' parameters that the shader might need to be * Set any 'uniform' parameters that the shader might need to be
the same for all of the objects being calculated: the same for all of the objects being calculated:
teapotShaders -> setUniform3f ( "gravity", 0, -9.8f,0 ) ; teapotShaders -> setUniform3f ( "gravity", 0, -9.8f,0 ) ;
teapotShaders -> setUniform1f ( "delta_T", 0.016f ) ; teapotShaders -> setUniform1f ( "delta_T", 0.016f ) ;
* Assign slot numbers to any FBO/Textures that the shader uses as inputs * Assign slot numbers to any FBO/Textures that the shader uses as inputs
and bind them to uniforms in the shader code: and bind them to uniforms in the shader code:
/* Texture 'slots' 0 up to about 15 */ /* Texture 'slots' 0 up to about 15 */
teapotShaders -> applyTexture ( "velocity" , velocityFBO, 0 ) ; teapotShaders -> applyTexture ( "velocity" , velocityFBO, 0 ) ;
teapotShaders -> applyTexture ( "accelleration", accnFBO, 1 ) ; teapotShaders -> applyTexture ( "accelleration", accnFBO, 1 ) ;
* Choose a target FBO/Texture as the place to store the results and * Choose a target FBO/Texture as the place to store the results and
render a polygon of suitable size to perform the calculations: render a polygon of suitable size to perform the calculations:
position -> paint () ; position -> paint () ;
* Restore the frame buffer to normal so we can go back to using the GPU * Restore the frame buffer to normal so we can go back to using the GPU
to do graphics: to do graphics:
restoreFrameBuffer () ; restoreFrameBuffer () ;
Step 3: Draw stuff! Step 3: Draw stuff!

View File

@@ -1,6 +1,6 @@
void main() void main()
{ {
gl_FragColor = gl_Color ; gl_FragColor = gl_Color ;
} }

View File

@@ -1,42 +1,42 @@
/* Use this for rendering the little cubes. */ /* Use this for rendering the little cubes. */
/* Translation & rotation passed in texture maps */ /* Translation & rotation passed in texture maps */
uniform sampler2D position ; uniform sampler2D position ;
uniform sampler2D rotation ; uniform sampler2D rotation ;
/* gl_Color is just the vertex colour */ /* gl_Color is just the vertex colour */
/* gl_MultiTexCoord0 selects where within the texture we /* gl_MultiTexCoord0 selects where within the texture we
find this cubes location */ find this cubes location */
void main() void main()
{ {
vec4 pos = vec4 ( texture2D ( position, gl_MultiTexCoord0.st ).xyz, 1.0 ) ; vec4 pos = vec4 ( texture2D ( position, gl_MultiTexCoord0.st ).xyz, 1.0 ) ;
vec3 rot = texture2D ( rotation, gl_MultiTexCoord0.st ).xyz ; vec3 rot = texture2D ( rotation, gl_MultiTexCoord0.st ).xyz ;
/* Build a rotation matrix */ /* Build a rotation matrix */
float sh = sin ( rot.x ) ; float ch = cos ( rot.x ) ; float sh = sin ( rot.x ) ; float ch = cos ( rot.x ) ;
float sp = sin ( rot.y ) ; float cp = cos ( rot.y ) ; float sp = sin ( rot.y ) ; float cp = cos ( rot.y ) ;
float sr = sin ( rot.z ) ; float cr = cos ( rot.z ) ; float sr = sin ( rot.z ) ; float cr = cos ( rot.z ) ;
mat4 mat = mat4 ( ch * cr - sh * sr * sp, mat4 mat = mat4 ( ch * cr - sh * sr * sp,
-sh * cp, -sh * cp,
sr * ch + sh * cr * sp, sr * ch + sh * cr * sp,
0, 0,
cr * sh + sr * sp * ch, cr * sh + sr * sp * ch,
ch * cp, ch * cp,
sr * sh - cr * sp * ch, sr * sh - cr * sp * ch,
0, 0,
-sr * cp, -sr * cp,
sp, sp,
cr * cp, cr * cp,
0, 0,
0.0, 0.0, 0.0, 1.0 ) ; 0.0, 0.0, 0.0, 1.0 ) ;
gl_FrontColor = gl_Color ; gl_FrontColor = gl_Color ;
pos.xyz += (mat * gl_Vertex).xyz ; pos.xyz += (mat * gl_Vertex).xyz ;
gl_Position = gl_ModelViewProjectionMatrix * pos ; gl_Position = gl_ModelViewProjectionMatrix * pos ;
} }

View File

@@ -0,0 +1,12 @@
/*
Use this for rendering the little cubes when
there is no vertex shader texture support.
*/
void main()
{
gl_FrontColor = gl_Color ;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex ;
}

View File

@@ -46,8 +46,34 @@ static void checkFrameBufferStatus ()
fprintf ( stderr, "ERROR: Unsupported FBO setup.\n" ) ; fprintf ( stderr, "ERROR: Unsupported FBO setup.\n" ) ;
exit ( 1 ) ; exit ( 1 ) ;
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT : case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT :
fprintf ( stderr, "WARNING: Incomplete FBO setup.\n" ) ; fprintf ( stderr, "WARNING: Incomplete FBO attachment.\n" ) ;
break ;
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT :
fprintf ( stderr, "WARNING: Incomplete FBO - missing attachment.\n" ) ;
break ;
#ifdef GL_FRAMEBUFFER_INCOMPLETE_DUPLICATE_ATTACHMENT_EXT
case GL_FRAMEBUFFER_INCOMPLETE_DUPLICATE_ATTACHMENT_EXT:
fprintf ( stderr, "WARNING: Incomplete FBO - duplicate attachment.\n" ) ;
break ;
#endif
case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT :
fprintf ( stderr, "WARNING: Incomplete FBO - improper dimensions.\n" ) ;
break ;
case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT :
fprintf ( stderr, "WARNING: Incomplete FBO - improper formats.\n" ) ;
break ;
case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT :
fprintf ( stderr, "WARNING: Incomplete FBO draw buffer.\n" ) ;
break ;
case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT :
fprintf ( stderr, "WARNING: Incomplete FBO read buffer.\n" ) ;
break ; break ;
default : default :
@@ -145,38 +171,61 @@ FrameBufferObject::FrameBufferObject ( int _width ,
} }
glGenTextures ( 1, & textureHandle ) ; glGenTextures ( 1, & textureHandle ) ;
glBindTexture ( GL_TEXTURE_2D, textureHandle ) ; glBindTexture ( GL_TEXTURE_2D, textureHandle ) ;
glTexParameterf ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ) ; glTexParameterf ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ) ;
glTexParameterf ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ) ; glTexParameterf ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ) ;
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ) ;
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ) ;
fillTexture ( (void *) NULL ) ; fillTexture ( (void *) NULL ) ;
glGenFramebuffersEXT ( 1, & fboHandle ) ; glGenFramebuffersEXT ( 1, & fboHandle ) ;
glGenRenderbuffersEXT ( 1, & depth_rb ) ;
glBindFramebufferEXT ( GL_FRAMEBUFFER_EXT, fboHandle ) ; glBindFramebufferEXT ( GL_FRAMEBUFFER_EXT, fboHandle ) ;
glFramebufferTexture2DEXT ( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, glFramebufferTexture2DEXT ( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
GL_TEXTURE_2D, textureHandle, 0 ) ; GL_TEXTURE_2D, textureHandle, 0 ) ;
// initialize depth renderbuffer #ifdef NEED_DEPTH_BUFFER
static GLuint depth_rb = 0 ;
glBindRenderbufferEXT ( GL_RENDERBUFFER_EXT, depth_rb ) ; if ( depth_rb == 0 )
glRenderbufferStorageEXT ( GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, {
width, height ) ; glGenRenderbuffersEXT ( 1, & depth_rb ) ;
glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, glBindRenderbufferEXT ( GL_RENDERBUFFER_EXT, depth_rb ) ;
GL_RENDERBUFFER_EXT, depth_rb ) ; glRenderbufferStorageEXT ( GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24,
width, height ) ;
glFramebufferRenderbufferEXT ( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
GL_RENDERBUFFER_EXT, depth_rb ) ;
}
else
glFramebufferRenderbufferEXT ( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
GL_RENDERBUFFER_EXT, depth_rb ) ;
#else
glDisable ( GL_DEPTH_TEST ) ;
glDepthMask ( 0 ) ;
glFramebufferRenderbufferEXT ( GL_FRAMEBUFFER_EXT , GL_DEPTH_ATTACHMENT_EXT,
GL_RENDERBUFFER_EXT, 0 ) ;
#endif
#ifdef NEED_STENCIL_BUFFER #ifdef NEED_STENCIL_BUFFER
static GLuint stencil_rb ; static GLuint stencil_rb = 0 ;
glGenRenderbuffersEXT ( 1, & stencil_rb ) ;
// initialize stencil renderbuffer if ( stencil_rb == 0 )
glBindRenderbufferEXT ( GL_RENDERBUFFER_EXT, stencil_rb ) ; {
glRenderbufferStorageEXT ( GL_RENDERBUFFER_EXT, GL_STENCIL_INDEX, width, height ) ; glGenRenderbuffersEXT ( 1, & stencil_rb ) ;
glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, glBindRenderbufferEXT ( GL_RENDERBUFFER_EXT, stencil_rb ) ;
GL_RENDERBUFFER_EXT, stencil_rb ) ; glRenderbufferStorageEXT ( GL_RENDERBUFFER_EXT, GL_STENCIL_INDEX,
width, height ) ;
glFramebufferRenderbufferEXT ( GL_FRAMEBUFFER_EXT , GL_STENCIL_ATTACHMENT_EXT,
GL_RENDERBUFFER_EXT, stencil_rb ) ;
}
else
glFramebufferRenderbufferEXT ( GL_FRAMEBUFFER_EXT , GL_STENCIL_ATTACHMENT_EXT,
GL_RENDERBUFFER_EXT, stencil_rb ) ;
#else #else
glDisable ( GL_STENCIL_TEST ) ; glDisable ( GL_STENCIL_TEST ) ;
glStencilMask ( 0 ) ;
glFramebufferRenderbufferEXT ( GL_FRAMEBUFFER_EXT , GL_STENCIL_ATTACHMENT_EXT,
GL_RENDERBUFFER_EXT, 0 ) ;
#endif #endif
// Check framebuffer completeness at the end of initialization. // Check framebuffer completeness at the end of initialization.
@@ -278,6 +327,7 @@ void FrameBufferObject::paint ()
glDisable ( GL_CULL_FACE ) ; glDisable ( GL_CULL_FACE ) ;
glDisable ( GL_BLEND ) ; glDisable ( GL_BLEND ) ;
float s_min = 0.5f / (float) width ; float s_min = 0.5f / (float) width ;
float s_max = (((float) width) - 0.5f) / (float) width ; float s_max = (((float) width) - 0.5f) / (float) width ;
float t_min = 0.5f / (float) height ; float t_min = 0.5f / (float) height ;

View File

@@ -3,7 +3,9 @@ GLuint makeTextureTarget ( GLuint textureHandle ) ;
void renderTo2DTexture ( GLuint fboHandle ) ; void renderTo2DTexture ( GLuint fboHandle ) ;
void renderToFrameBuffer () ; void renderToFrameBuffer () ;
//#define NEED_STENCIL_BUFFER 1 // #define NEED_STENCIL_BUFFER 1
// #define NEED_DEPTH_BUFFER 1
enum fboDataType enum fboDataType
{ {
@@ -44,11 +46,6 @@ class FrameBufferObject
GLuint textureHandle ; GLuint textureHandle ;
GLuint fboHandle ; GLuint fboHandle ;
GLuint depth_rb ;
#ifdef NEED_STENCIL_BUFFER
GLuint stencil_rb ;
#endif
void fillTexture ( void *data ) ; void fillTexture ( void *data ) ;
void fetchTexture ( void *data ) ; void fetchTexture ( void *data ) ;
@@ -61,6 +58,12 @@ public:
void makeDestination () void makeDestination ()
{ {
#ifndef NEED_DEPTH_BUFFER
glDepthMask ( 0 ) ;
#endif
#ifndef NEED_STENCIL_BUFFER
glStencilMask ( 0 ) ;
#endif
glBindFramebufferEXT ( GL_FRAMEBUFFER_EXT, fboHandle ) ; glBindFramebufferEXT ( GL_FRAMEBUFFER_EXT, fboHandle ) ;
} }
@@ -83,5 +86,11 @@ public:
inline void restoreFrameBuffer () inline void restoreFrameBuffer ()
{ {
glBindFramebufferEXT ( GL_FRAMEBUFFER_EXT, 0 ) ; glBindFramebufferEXT ( GL_FRAMEBUFFER_EXT, 0 ) ;
#ifndef NEED_DEPTH_BUFFER
glDepthMask ( 1 ) ;
#endif
#ifndef NEED_STENCIL_BUFFER
glStencilMask ( 1 ) ;
#endif
} }

View File

@@ -1,420 +1,411 @@
#include "GPU_physics.h" #include "GPU_physics.h"
#include "shaderSupport.h" #include "shaderSupport.h"
#include "fboSupport.h" #include "fboSupport.h"
static void showGLerror () #define DEFAULT_VERT_SHADER \
{ "void main()" \
GLenum err ; "{" \
" gl_TexCoord[0] = gl_MultiTexCoord0 ;" \
while ( (err = glGetError()) != GL_NO_ERROR ) " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex ;" \
fprintf ( stderr, "OpenGL Error: %s\n", gluErrorString ( err ) ) ; "}"
}
class FrameBufferObject ;
#define DEFAULT_VERT_SHADER \
"void main()" \ class GLSL_Shader
"{" \ {
" gl_TexCoord[0] = gl_MultiTexCoord0 ;" \ GLenum type ;
" gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex ;" \ GLhandleARB handle ;
"}" char *name ;
char *source ;
class FrameBufferObject ;
public:
class GLSL_Shader GLSL_Shader ()
{ {
GLenum type ; type = (GLenum) 0 ;
GLhandleARB handle ; handle = 0 ;
char *name ; name = NULL ;
char *source ; source = NULL ;
}
public:
~GLSL_Shader ()
GLSL_Shader () {
{ delete [] name ;
type = (GLenum) 0 ; delete [] source ;
handle = 0 ; }
name = NULL ;
source = NULL ; bool compileFile ( const char *fname , GLenum type ) ;
} bool compileString ( const char *source, GLenum type,
const char *name ) ;
~GLSL_Shader ()
{ GLenum getType () { return type ; }
delete [] name ; GLhandleARB getHandle () { return handle ; }
delete [] source ; } ;
}
bool compileFile ( const char *fname , GLenum type ) ;
bool compileString ( const char *source, GLenum type, static char *readShaderText ( const char *fname )
const char *name ) ; {
GLenum getType () { return type ; }
GLhandleARB getHandle () { return handle ; }
} ; FILE *fd = fopen ( fname, "r" ) ;
int size = 0;
/* File operations denied? ok, just close and return failure */
static char *readShaderText ( const char *fname ) if (fseek(fd, 0, SEEK_END) || (size = ftell(fd)) == EOF || fseek(fd, 0, SEEK_SET))
{ {
printf("Error: cannot get filesize from %s\n", fname);
exit (1);
}
FILE *fd = fopen ( fname, "r" ) ;
char *shader = new char [ size + 1 ] ;
int size = 0;
/* File operations denied? ok, just close and return failure */ if ( fd == NULL )
if (fseek(fd, 0, SEEK_END) || (size = ftell(fd)) == EOF || fseek(fd, 0, SEEK_SET)) {
{ fprintf ( stderr, "Cannot read shader '%s'\n", fname ) ;
printf("Error: cannot get filesize from %s\n", fname); exit ( 1 ) ;
exit (1); }
}
int count = fread ( shader, 1, size, fd ) ;
char *shader = new char [ size + 1 ] ;
shader [ count ] = '\0' ;
if ( fd == NULL )
{ fclose ( fd ) ;
fprintf ( stderr, "Cannot read shader '%s'\n", fname ) ;
exit ( 1 ) ; return shader ;
} }
int count = fread ( shader, 1, size, fd ) ;
/*****************************\
shader [ count ] = '\0' ; * *
* Error handling stuff *
fclose ( fd ) ; * *
\*****************************/
return shader ;
}
static void showShaderInfo ( const char *which,
GLhandleARB handle, const char *name )
/*****************************\ {
* * int len = 0 ;
* Error handling stuff *
* * showGLerror ( "showShaderInfo_0" ) ;
\*****************************/ glGetObjectParameterivARB ( handle, GL_OBJECT_INFO_LOG_LENGTH_ARB, (GLint*) &len ) ;
showGLerror ( "showShaderInfo_1" ) ;
static void showShaderInfo ( const char *which, if ( len > 0 )
GLhandleARB handle, const char *name ) {
{ int trueLen ;
int len = 0 ; char *s = new char [ len ] ;
showGLerror () ; glGetInfoLogARB ( handle, len, (GLint*)&trueLen, s ) ;
glGetObjectParameterivARB ( handle, GL_OBJECT_INFO_LOG_LENGTH_ARB, (GLint*) &len ) ;
showGLerror () ; if ( trueLen > 0 && s [ 0 ] != '\0' )
fprintf ( stderr, "%s:%s - \n%s\n", which, name, s ) ;
if ( len > 0 )
{ delete [] s ;
int trueLen ; }
char *s = new char [ len ] ; }
glGetInfoLogARB ( handle, len, (GLint*)&trueLen, s ) ;
/************************************\
if ( trueLen > 0 && s [ 0 ] != '\0' ) * *
fprintf ( stderr, "%s:%s - \n%s\n", which, name, s ) ; * Single Shader Stuff *
* *
delete [] s ; \************************************/
}
}
bool GLSL_Shader::compileFile ( const char *fname, GLenum _type )
/************************************\ {
* * return compileString ( readShaderText ( fname ), _type, fname ) ;
* Single Shader Stuff * }
* *
\************************************/
bool GLSL_Shader::compileString ( const char *_source,
GLenum _type,
const char *_name )
bool GLSL_Shader::compileFile ( const char *fname, GLenum _type ) {
{ delete [] name ;
return compileString ( readShaderText ( fname ), _type, fname ) ; delete [] source ;
}
type = _type ;
name = strdup ( _name ) ;
bool GLSL_Shader::compileString ( const char *_source, source = strdup ( _source ) ;
GLenum _type,
const char *_name ) GLint stat ;
{
delete [] name ; handle = glCreateShaderObjectARB ( type ) ;
delete [] source ;
glShaderSourceARB ( handle, 1, (const GLcharARB **) & source, NULL);
type = _type ; glCompileShaderARB ( handle ) ;
name = strdup ( _name ) ; glGetObjectParameterivARB ( handle, GL_OBJECT_COMPILE_STATUS_ARB, & stat ) ;
source = strdup ( _source ) ; showShaderInfo ( "Compiling", handle, name ) ;
GLint stat ; if ( ! stat )
{
handle = glCreateShaderObjectARB ( type ) ; fprintf ( stderr, "Failed to compile shader '%s'.\n", name ) ;
return false ;
glShaderSourceARB ( handle, 1, (const GLcharARB **) & source, NULL); }
glCompileShaderARB ( handle ) ;
glGetObjectParameterivARB ( handle, GL_OBJECT_COMPILE_STATUS_ARB, & stat ) ; return true ;
showShaderInfo ( "Compiling", handle, name ) ; }
if ( ! stat )
{
fprintf ( stderr, "Failed to compile shader '%s'.\n", name ) ; /************************************\
return false ; * *
} * Shader Pair Stuff *
* *
return true ; \************************************/
}
GLint GLSL_ShaderPair::getUniformLocation ( const char *uni_name )
/************************************\ {
* * assert ( success ) ;
* Shader Pair Stuff *
* * GLint loc = glGetUniformLocationARB ( handle, uni_name ) ;
\************************************/
if ( loc == -1 )
fprintf ( stderr, "No such uniform as '%s' or"
" '%s' is unused in shader pair '%s'.\n", uni_name,
GLint GLSL_ShaderPair::getUniformLocation ( const char *uni_name ) uni_name,
{ name ) ;
assert ( success ) ; else
showGLerror ( "GLSL_ShaderPair::getUniformLocation" ) ;
GLint loc = glGetUniformLocationARB ( handle, uni_name ) ;
return loc ;
if ( loc == -1 ) }
fprintf ( stderr, "No such uniform as '%s' or"
" '%s' is unused in shader pair '%s'.\n", uni_name,
uni_name,
name ) ; void GLSL_ShaderPair::showActiveUniforms ( FILE *fd )
else {
showGLerror () ; GLint maxlen = 0 ;
GLint maxattrs = 0 ;
return loc ;
} if ( fd == NULL ) fd = stderr ;
glGetObjectParameterivARB ( handle,
GL_OBJECT_ACTIVE_UNIFORMS_ARB,
void GLSL_ShaderPair::showActiveUniforms ( FILE *fd ) &maxattrs ) ;
{
GLint maxlen = 0 ; if ( maxattrs == 0 )
GLint maxattrs = 0 ; {
fprintf ( fd, "No Active Uniforms.\n" ) ;
if ( fd == NULL ) fd = stderr ; return ;
}
glGetObjectParameterivARB ( handle,
GL_OBJECT_ACTIVE_UNIFORMS_ARB, glGetObjectParameterivARB ( handle,
&maxattrs ) ; GL_OBJECT_ACTIVE_UNIFORM_MAX_LENGTH_ARB,
&maxlen ) ;
if ( maxattrs == 0 )
{ char *name = new char [ maxlen+1 ] ;
fprintf ( fd, "No Active Uniforms.\n" ) ;
return ; fprintf ( fd, "Active Uniforms:\n" ) ;
}
for ( int i = 0 ; i < maxattrs ; i++ )
glGetObjectParameterivARB ( handle, {
GL_OBJECT_ACTIVE_UNIFORM_MAX_LENGTH_ARB, GLsizei len ;
&maxlen ) ; GLint size ;
GLenum vartype ;
char *name = new char [ maxlen+1 ] ; char *vartypename ;
GLint location ;
fprintf ( fd, "Active Uniforms:\n" ) ;
glGetActiveUniformARB ( handle, i,
for ( int i = 0 ; i < maxattrs ; i++ ) maxlen+1, &len, &size, &vartype, name ) ;
{
GLsizei len ; location = glGetUniformLocationARB ( handle, name ) ;
GLint size ;
GLenum vartype ; switch ( vartype )
char *vartypename ; {
GLint location ; case GL_FLOAT : vartypename = "float " ; break ;
case GL_FLOAT_VEC2_ARB : vartypename = "vec2 " ; break ;
glGetActiveUniformARB ( handle, i, case GL_FLOAT_VEC3_ARB : vartypename = "vec3 " ; break ;
maxlen+1, &len, &size, &vartype, name ) ; case GL_FLOAT_VEC4_ARB : vartypename = "vec4 " ; break ;
case GL_INT : vartypename = "int " ; break ;
location = glGetUniformLocationARB ( handle, name ) ; case GL_INT_VEC2_ARB : vartypename = "intvec2 " ; break ;
case GL_INT_VEC3_ARB : vartypename = "intvec3 " ; break ;
switch ( vartype ) case GL_INT_VEC4_ARB : vartypename = "intvec4 " ; break ;
{ case GL_BOOL : vartypename = "bool " ; break ;
case GL_FLOAT : vartypename = "float " ; break ; case GL_BOOL_VEC2_ARB : vartypename = "boolvec2 " ; break ;
case GL_FLOAT_VEC2_ARB : vartypename = "vec2 " ; break ; case GL_BOOL_VEC3_ARB : vartypename = "boolvec3 " ; break ;
case GL_FLOAT_VEC3_ARB : vartypename = "vec3 " ; break ; case GL_BOOL_VEC4_ARB : vartypename = "boolvec4 " ; break ;
case GL_FLOAT_VEC4_ARB : vartypename = "vec4 " ; break ; case GL_FLOAT_MAT2_ARB : vartypename = "mat2 " ; break ;
case GL_INT : vartypename = "int " ; break ; case GL_FLOAT_MAT3_ARB : vartypename = "mat3 " ; break ;
case GL_INT_VEC2_ARB : vartypename = "intvec2 " ; break ; case GL_FLOAT_MAT4_ARB : vartypename = "mat4 " ; break ;
case GL_INT_VEC3_ARB : vartypename = "intvec3 " ; break ; case GL_SAMPLER_1D_ARB : vartypename = "sampler1D" ; break ;
case GL_INT_VEC4_ARB : vartypename = "intvec4 " ; break ; case GL_SAMPLER_2D_ARB : vartypename = "sampler2D" ; break ;
case GL_BOOL : vartypename = "bool " ; break ; case GL_SAMPLER_3D_ARB : vartypename = "sampler3D" ; break ;
case GL_BOOL_VEC2_ARB : vartypename = "boolvec2 " ; break ; default : vartypename = "?????????" ; break ;
case GL_BOOL_VEC3_ARB : vartypename = "boolvec3 " ; break ; }
case GL_BOOL_VEC4_ARB : vartypename = "boolvec4 " ; break ;
case GL_FLOAT_MAT2_ARB : vartypename = "mat2 " ; break ; if ( size == 1 )
case GL_FLOAT_MAT3_ARB : vartypename = "mat3 " ; break ; fprintf ( fd, "%2d) %s %s ; // @%d\n", i,
case GL_FLOAT_MAT4_ARB : vartypename = "mat4 " ; break ; vartypename, name, location ) ;
case GL_SAMPLER_1D_ARB : vartypename = "sampler1D" ; break ; else
case GL_SAMPLER_2D_ARB : vartypename = "sampler2D" ; break ; fprintf ( fd, "%2d) %s %s [ %d ] ; // @%d\n", i,
case GL_SAMPLER_3D_ARB : vartypename = "sampler3D" ; break ; vartypename, name, size, location ) ;
default : vartypename = "?????????" ; break ; }
}
fprintf ( fd, "\n" ) ;
if ( size == 1 ) delete name ;
fprintf ( fd, "%2d) %s %s ; // @%d\n", i, }
vartypename, name, location ) ;
else
fprintf ( fd, "%2d) %s %s [ %d ] ; // @%d\n", i, void GLSL_ShaderPair::showActiveAttribs ( FILE *fd )
vartypename, name, size, location ) ; {
} if ( fd == NULL ) fd = stderr ;
fprintf ( fd, "\n" ) ; GLint maxlen = 0 ;
delete name ; GLint maxattrs = 0 ;
}
glGetObjectParameterivARB ( handle,
GL_OBJECT_ACTIVE_ATTRIBUTES_ARB,
void GLSL_ShaderPair::showActiveAttribs ( FILE *fd ) &maxattrs ) ;
{
if ( fd == NULL ) fd = stderr ; if ( maxattrs == 0 )
{
GLint maxlen = 0 ; fprintf ( fd, "No Active Attributes.\n" ) ;
GLint maxattrs = 0 ; return ;
}
glGetObjectParameterivARB ( handle,
GL_OBJECT_ACTIVE_ATTRIBUTES_ARB, glGetObjectParameterivARB ( handle,
&maxattrs ) ; GL_OBJECT_ACTIVE_ATTRIBUTE_MAX_LENGTH_ARB,
&maxlen ) ;
if ( maxattrs == 0 )
{ char *name = new char [ maxlen+1 ] ;
fprintf ( fd, "No Active Attributes.\n" ) ;
return ; fprintf ( fd, "Active Attributes:\n" ) ;
}
for ( int i = 0 ; i < maxattrs ; i++ )
glGetObjectParameterivARB ( handle, {
GL_OBJECT_ACTIVE_ATTRIBUTE_MAX_LENGTH_ARB, GLsizei len ;
&maxlen ) ; GLint size ;
GLenum vartype ;
char *name = new char [ maxlen+1 ] ; char *vartypename ;
GLint location ;
fprintf ( fd, "Active Attributes:\n" ) ;
glGetActiveAttribARB ( handle, i, maxlen+1, &len, &size, &vartype, name ) ;
for ( int i = 0 ; i < maxattrs ; i++ )
{ location = glGetAttribLocationARB ( handle, name ) ;
GLsizei len ;
GLint size ; switch ( vartype )
GLenum vartype ; {
char *vartypename ; case GL_FLOAT : vartypename = "float" ; break ;
GLint location ; case GL_FLOAT_VEC2_ARB : vartypename = "vec2 " ; break ;
case GL_FLOAT_VEC3_ARB : vartypename = "vec3 " ; break ;
glGetActiveAttribARB ( handle, i, maxlen+1, &len, &size, &vartype, name ) ; case GL_FLOAT_VEC4_ARB : vartypename = "vec4 " ; break ;
case GL_FLOAT_MAT2_ARB : vartypename = "mat2 " ; break ;
location = glGetAttribLocationARB ( handle, name ) ; case GL_FLOAT_MAT3_ARB : vartypename = "mat3 " ; break ;
case GL_FLOAT_MAT4_ARB : vartypename = "mat4 " ; break ;
switch ( vartype ) default : vartypename = "???? " ; break ;
{ }
case GL_FLOAT : vartypename = "float" ; break ;
case GL_FLOAT_VEC2_ARB : vartypename = "vec2 " ; break ; if ( size == 1 )
case GL_FLOAT_VEC3_ARB : vartypename = "vec3 " ; break ; fprintf ( fd, "%2d) %s %s ; // @%d\n", i,
case GL_FLOAT_VEC4_ARB : vartypename = "vec4 " ; break ; vartypename, name, location ) ;
case GL_FLOAT_MAT2_ARB : vartypename = "mat2 " ; break ; else
case GL_FLOAT_MAT3_ARB : vartypename = "mat3 " ; break ; fprintf ( fd, "%2d) %s %s [ %d ] ; // @%d\n", i,
case GL_FLOAT_MAT4_ARB : vartypename = "mat4 " ; break ; vartypename, name, size, location ) ;
default : vartypename = "???? " ; break ; }
}
fprintf ( fd, "\n" ) ;
if ( size == 1 ) delete name ;
fprintf ( fd, "%2d) %s %s ; // @%d\n", i, }
vartypename, name, location ) ;
else
fprintf ( fd, "%2d) %s %s [ %d ] ; // @%d\n", i,
vartypename, name, size, location ) ; bool GLSL_ShaderPair::link ()
} {
GLint stat ;
fprintf ( fd, "\n" ) ;
delete name ; handle = glCreateProgramObjectARB () ;
}
glAttachObjectARB ( handle, vertShader -> getHandle () ) ;
glAttachObjectARB ( handle, fragShader -> getHandle () ) ;
glLinkProgramARB ( handle ) ;
bool GLSL_ShaderPair::link () glGetObjectParameterivARB ( handle, GL_OBJECT_LINK_STATUS_ARB, &stat ) ;
{ showShaderInfo ( "Linking", handle, name ) ;
GLint stat ; glValidateProgramARB ( handle ) ;
showShaderInfo ( "Validate", handle, name ) ;
handle = glCreateProgramObjectARB () ;
if ( ! stat )
glAttachObjectARB ( handle, vertShader -> getHandle () ) ; {
glAttachObjectARB ( handle, fragShader -> getHandle () ) ; fprintf ( stderr, "Failed to link shader.\n" ) ;
glLinkProgramARB ( handle ) ; return false ;
glGetObjectParameterivARB ( handle, GL_OBJECT_LINK_STATUS_ARB, &stat ) ; }
showShaderInfo ( "Linking", handle, name ) ;
glValidateProgramARB ( handle ) ; return true ;
showShaderInfo ( "Validate", handle, name ) ; }
if ( ! stat )
{ GLSL_ShaderPair::GLSL_ShaderPair ( const char *_name,
fprintf ( stderr, "Failed to link shader.\n" ) ; const char *vertFname,
return false ; const char *fragFname )
} {
name = strdup ( _name ) ;
return true ; handle = 0 ;
}
vertShader = new GLSL_Shader () ;
fragShader = new GLSL_Shader () ;
GLSL_ShaderPair::GLSL_ShaderPair ( const char *_name,
const char *vertFname, bool res1 = ( vertFname == NULL ) ?
const char *fragFname ) vertShader -> compileString ( DEFAULT_VERT_SHADER,
{ GL_VERTEX_SHADER_ARB,
name = strdup ( _name ) ; "Default Vertex Shader" ) :
handle = 0 ; vertShader -> compileFile ( vertFname, GL_VERTEX_SHADER_ARB ) ;
vertShader = new GLSL_Shader () ; bool res2 = fragShader -> compileFile ( fragFname, GL_FRAGMENT_SHADER_ARB ) ;
fragShader = new GLSL_Shader () ;
success = ( res1 && res2 && link () ) ;
bool res1 = ( vertFname == NULL ) ? }
vertShader -> compileString ( DEFAULT_VERT_SHADER,
GL_VERTEX_SHADER_ARB,
"Default Vertex Shader" ) : GLSL_ShaderPair::GLSL_ShaderPair ( const char *_name,
vertShader -> compileFile ( vertFname, GL_VERTEX_SHADER_ARB ) ; const char *vertSource,
const char *vertName,
bool res2 = fragShader -> compileFile ( fragFname, GL_FRAGMENT_SHADER_ARB ) ; const char *fragSource,
const char *fragName )
success = ( res1 && res2 && link () ) ; {
} name = strdup ( _name ) ;
handle = 0 ;
GLSL_ShaderPair::GLSL_ShaderPair ( const char *_name, vertShader = new GLSL_Shader () ;
const char *vertSource, fragShader = new GLSL_Shader () ;
const char *vertName,
const char *fragSource, bool res1 = ( vertSource == NULL ) ?
const char *fragName ) vertShader -> compileString ( DEFAULT_VERT_SHADER,
{ GL_VERTEX_SHADER_ARB,
name = strdup ( _name ) ; "Default Vertex Shader" ) :
handle = 0 ; vertShader -> compileString ( vertSource,
GL_VERTEX_SHADER_ARB,
vertShader = new GLSL_Shader () ; vertName );
fragShader = new GLSL_Shader () ;
bool res2 = fragShader -> compileString ( fragSource,
bool res1 = ( vertSource == NULL ) ? GL_FRAGMENT_SHADER_ARB,
vertShader -> compileString ( DEFAULT_VERT_SHADER, fragName );
GL_VERTEX_SHADER_ARB,
"Default Vertex Shader" ) : success = ( res1 && res2 && link () ) ;
vertShader -> compileString ( vertSource, }
GL_VERTEX_SHADER_ARB,
vertName );
GLSL_ShaderPair::~GLSL_ShaderPair ()
bool res2 = fragShader -> compileString ( fragSource, {
GL_FRAGMENT_SHADER_ARB, delete [] name ;
fragName ); delete vertShader ;
delete fragShader ;
success = ( res1 && res2 && link () ) ; }
}
void GLSL_ShaderPair::applyTexture ( const char *uniformName,
GLSL_ShaderPair::~GLSL_ShaderPair () FrameBufferObject *fbo,
{ int slot )
delete [] name ; {
delete vertShader ; fbo -> use ( slot ) ;
delete fragShader ; glUniform1iARB ( getUniformLocation ( uniformName ), slot ) ;
} }
void GLSL_ShaderPair::applyTexture ( const char *uniformName,
FrameBufferObject *fbo,
int slot )
{
fbo -> use ( slot ) ;
glUniform1iARB ( getUniformLocation ( uniformName ), slot ) ;
}