add sparkle explosions

This commit is contained in:
gmueller
2011-01-05 23:02:10 +01:00
parent b19f44ec32
commit a02ad6bd34
106 changed files with 19753 additions and 88 deletions

View File

@@ -0,0 +1,326 @@
//////////////////////////////////////////////////////////////////////////////////
// SPARK particle engine //
// Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com //
// //
// This software is provided 'as-is', without any express or implied //
// warranty. In no event will the authors be held liable for any damages //
// arising from the use of this software. //
// //
// Permission is granted to anyone to use this software for any purpose, //
// including commercial applications, and to alter it and redistribute it //
// freely, subject to the following restrictions: //
// //
// 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 //
// in a product, an acknowledgment in the product documentation would be //
// appreciated but is not required. //
// 2. Altered source versions must be plainly marked as such, and must not be //
// misrepresented as being the original software. //
// 3. This notice may not be removed or altered from any source distribution. //
//////////////////////////////////////////////////////////////////////////////////
#include "RenderingAPIs/OpenGL/SPK_GLExtHandler.h"
#if defined(__APPLE__) || defined(macintosh)
// Necessary includes to get GL functions pointers from MAC
#include <mach-o/dyld.h>
#include <stdlib.h>
#include <string.h>
#endif
namespace SPK
{
namespace GL
{
GLExtHandler::GLExtension GLExtHandler::pointSpriteGLExt = UNCHECKED;
GLExtHandler::GLExtension GLExtHandler::pointParameterGLExt = UNCHECKED;
GLExtHandler::GLExtension GLExtHandler::texture3DGLExt = UNCHECKED;
GLExtHandler::GLExtension GLExtHandler::shaderGLExt = UNCHECKED;
GLExtHandler::SPK_PFNGLPOINTPARAMETERFPROC GLExtHandler::SPK_glPointParameterf = NULL;
GLExtHandler::SPK_PFNGLPOINTPARAMETERFVPROC GLExtHandler::SPK_glPointParameterfv = NULL;
GLExtHandler::SPK_PFNGLTEXIMAGE3DPROC GLExtHandler::SPK_glTexImage3D = NULL;
GLExtHandler::SPK_PFNGLCREATESHADERPROC GLExtHandler::SPK_glCreateShader = NULL;
GLExtHandler::SPK_PFNGLDELETESHADERPROC GLExtHandler::SPK_glDeleteShader = NULL;
GLExtHandler::SPK_PFNGLSHADERSOURCEPROC GLExtHandler::SPK_glShaderSource = NULL;
GLExtHandler::SPK_PFNGLCOMPILESHADERPROC GLExtHandler::SPK_glCompileShader = NULL;
GLExtHandler::SPK_PFNGLCREATEPROGRAMPROC GLExtHandler::SPK_glCreateProgram = NULL;
GLExtHandler::SPK_PFNGLDELETEPROGRAMPROC GLExtHandler::SPK_glDeleteProgram = NULL;
GLExtHandler::SPK_PFNGLATTACHSHADERPROC GLExtHandler::SPK_glAttachShader = NULL;
GLExtHandler::SPK_PFNGLDETACHSHADERPROC GLExtHandler::SPK_glDetachShader = NULL;
GLExtHandler::SPK_PFNGLLINKPROGRAMPROC GLExtHandler::SPK_glLinkProgram = NULL;
GLExtHandler::SPK_PFNGLUSEPROGRAMPROC GLExtHandler::SPK_glUseProgram = NULL;
const float GLExtHandler::QUADRATIC_SCREEN[3] = {1.0f,0.0f,0.0f};
float GLExtHandler::pixelPerUnit = 1024.0f;
const float GLExtHandler::POINT_SIZE_CURRENT = 32.0f;
const float GLExtHandler::POINT_SIZE_MIN = 1.0f;
const float GLExtHandler::POINT_SIZE_MAX = 1024.0f;
#ifdef SPK_NO_GLEXT
bool GLExtHandler::loadGLExtPointSprite()
{
pointSpriteGLExt = UNSUPPORTED;
return false;
}
bool GLExtHandler::loadGLExtPointParameter()
{
pointParameterGLExt = UNSUPPORTED;
return false;
}
bool GLExtHandler::loadGLExtTexture3D()
{
texture3DGLExt = UNSUPPORTED;
return false;
}
bool GLExtHandler::loadGLExtShader()
{
shaderGLExt = UNSUPPORTED;
return false;
}
#else
bool GLExtHandler::loadGLExtPointSprite()
{
if (pointSpriteGLExt == UNCHECKED)
{
pointSpriteGLExt = UNSUPPORTED;
std::string version = reinterpret_cast<const char*>(glGetString(GL_VERSION));
// openGL 2.0 and higher
if (int(version[0] - '0') >= 2)
pointSpriteGLExt = SUPPORTED;
else
{
std::string extensions = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
if ((extensions.find("GL_ARB_point_sprites") != std::string::npos)
||(extensions.find("GL_NV_point_sprites") != std::string::npos))
pointSpriteGLExt = SUPPORTED;
}
}
return pointSpriteGLExt == SUPPORTED;
}
bool GLExtHandler::loadGLExtPointParameter()
{
if (pointParameterGLExt == UNCHECKED)
{
pointParameterGLExt = UNSUPPORTED;
std::string version = reinterpret_cast<const char*>(glGetString(GL_VERSION));
std::string extensions = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
// openGL 1.4 and higher
if (((int(version[0] - '0') == 1)&&(int(version[2] - '0') >= 4))
||(int(version[0] - '0') > 1))
{
SPK_glPointParameterf = (SPK_PFNGLPOINTPARAMETERFPROC)glGetProcAddress("glPointParameterf");
SPK_glPointParameterfv = (SPK_PFNGLPOINTPARAMETERFVPROC)glGetProcAddress("glPointParameterfv");
}
// point parameter ARB
if (((SPK_glPointParameterf == NULL)||(SPK_glPointParameterfv == NULL))
&&(extensions.find("GL_ARB_point_parameters") != std::string::npos))
{
SPK_glPointParameterf = (SPK_PFNGLPOINTPARAMETERFPROC)glGetProcAddress("glPointParameterfARB");
SPK_glPointParameterfv = (SPK_PFNGLPOINTPARAMETERFVPROC)glGetProcAddress("glPointParameterfvARB");
}
// point parameter EXT
if (((SPK_glPointParameterf == NULL)||(SPK_glPointParameterfv == NULL))
&&(extensions.find("GL_EXT_point_parameters") != std::string::npos))
{
SPK_glPointParameterf = (SPK_PFNGLPOINTPARAMETERFPROC)glGetProcAddress("glPointParameterfEXT");
SPK_glPointParameterfv = (SPK_PFNGLPOINTPARAMETERFVPROC)glGetProcAddress("glPointParameterfvEXT");
}
// point parameter SGIS
if (((SPK_glPointParameterf == NULL)||(SPK_glPointParameterfv == NULL))
&&(extensions.find("GL_SGIS_point_parameters") != std::string::npos))
{
SPK_glPointParameterf = (SPK_PFNGLPOINTPARAMETERFPROC)glGetProcAddress("glPointParameterfSGIS");
SPK_glPointParameterfv = (SPK_PFNGLPOINTPARAMETERFVPROC)glGetProcAddress("glPointParameterfvSGIS");
}
if ((SPK_glPointParameterf != NULL)&&(SPK_glPointParameterfv != NULL))
pointParameterGLExt = SUPPORTED;
}
return pointParameterGLExt == SUPPORTED;
}
bool GLExtHandler::loadGLExtTexture3D()
{
if (texture3DGLExt == UNCHECKED)
{
texture3DGLExt = UNSUPPORTED;
std::string version = reinterpret_cast<const char*>(glGetString(GL_VERSION));
std::string extensions = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
// openGL 1.2 and higher
if (((int(version[0] - '0') == 1)&&(int(version[2] - '0') >= 4))
||(int(version[0] - '0') > 1))
SPK_glTexImage3D = (SPK_PFNGLTEXIMAGE3DPROC)glGetProcAddress("glTexImage3D");
// texture 3D EXT
if ((SPK_glTexImage3D == NULL)&&(extensions.find("GL_EXT_texture3D") != std::string::npos))
SPK_glTexImage3D = (SPK_PFNGLTEXIMAGE3DPROC)glGetProcAddress("glTexImage3DEXT");
if (SPK_glTexImage3D != NULL)
texture3DGLExt = SUPPORTED;
}
return texture3DGLExt == SUPPORTED;
}
bool GLExtHandler::loadGLExtShader()
{
if (shaderGLExt == UNCHECKED)
{
shaderGLExt = UNSUPPORTED;
std::string version = reinterpret_cast<const char*>(glGetString(GL_VERSION));
// openGL 2.0 and higher
if (int(version[0] - '0') >= 2)
{
bool ok = true;
if (ok && ((SPK_glCreateShader = (SPK_PFNGLCREATESHADERPROC)glGetProcAddress("glCreateShader")) == NULL)) ok = false;
if (ok && ((SPK_glDeleteShader = (SPK_PFNGLDELETESHADERPROC)glGetProcAddress("glDeleteShader")) == NULL)) ok = false;
if (ok && ((SPK_glShaderSource = (SPK_PFNGLSHADERSOURCEPROC)glGetProcAddress("glShaderSource")) == NULL)) ok = false;
if (ok && ((SPK_glCompileShader = (SPK_PFNGLCOMPILESHADERPROC)glGetProcAddress("glCompileShader")) == NULL)) ok = false;
if (ok && ((SPK_glCreateProgram = (SPK_PFNGLCREATEPROGRAMPROC)glGetProcAddress("glCreateProgram")) == NULL)) ok = false;
if (ok && ((SPK_glDeleteProgram = (SPK_PFNGLDELETEPROGRAMPROC)glGetProcAddress("glDeleteProgram")) == NULL)) ok = false;
if (ok && ((SPK_glAttachShader = (SPK_PFNGLATTACHSHADERPROC)glGetProcAddress("glAttachShader")) == NULL)) ok = false;
if (ok && ((SPK_glDetachShader = (SPK_PFNGLDETACHSHADERPROC)glGetProcAddress("glDetachShader")) == NULL)) ok = false;
if (ok && ((SPK_glLinkProgram = (SPK_PFNGLLINKPROGRAMPROC)glGetProcAddress("glLinkProgram")) == NULL)) ok = false;
if (ok && ((SPK_glUseProgram = (SPK_PFNGLUSEPROGRAMPROC)glGetProcAddress("glUseProgram")) == NULL)) ok = false;
if (ok) shaderGLExt = SUPPORTED;
}
if (shaderGLExt == UNSUPPORTED)
{
std::string extensions = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
if ((extensions.find("GL_ARB_shading_language_100") != std::string::npos)&&
(extensions.find("GL_ARB_shader_objects") != std::string::npos)&&
(extensions.find("GL_ARB_vertex_shader") != std::string::npos)&&
(extensions.find("GL_ARB_fragment_shader") != std::string::npos))
{
bool ok = true;
if (ok && ((SPK_glCreateShader = (SPK_PFNGLCREATESHADERPROC)glGetProcAddress("glCreateShaderObjectARB")) == NULL)) ok = false;
if (ok && ((SPK_glDeleteShader = SPK_glDeleteProgram = (SPK_PFNGLDELETESHADERPROC)glGetProcAddress("glDeleteObjectARB")) == NULL)) ok = false;
if (ok && ((SPK_glShaderSource = (SPK_PFNGLSHADERSOURCEPROC)glGetProcAddress("glShaderSourceARB")) == NULL)) ok = false;
if (ok && ((SPK_glCompileShader = (SPK_PFNGLCOMPILESHADERPROC)glGetProcAddress("glCompileShaderARB")) == NULL)) ok = false;
if (ok && ((SPK_glCreateProgram = (SPK_PFNGLCREATEPROGRAMPROC)glGetProcAddress("glCreateProgramObjectARB")) == NULL)) ok = false;
if (ok && ((SPK_glAttachShader = (SPK_PFNGLATTACHSHADERPROC)glGetProcAddress("glAttachObjectARB")) == NULL)) ok = false;
if (ok && ((SPK_glDetachShader = (SPK_PFNGLDETACHSHADERPROC)glGetProcAddress("glDetachObjectARB")) == NULL)) ok = false;
if (ok && ((SPK_glLinkProgram = (SPK_PFNGLLINKPROGRAMPROC)glGetProcAddress("glLinkProgramARB")) == NULL)) ok = false;
if (ok && ((SPK_glUseProgram = (SPK_PFNGLUSEPROGRAMPROC)glGetProcAddress("glUseProgramObjectARB")) == NULL)) ok = false;
if (ok) shaderGLExt = SUPPORTED;
}
}
}
return shaderGLExt == SUPPORTED;
}
#endif
#if defined(__APPLE__) || defined(macintosh)
void* GLExtHandler::SPK_NSGLGetProcAddress(const char* name)
{
NSSymbol symbol;
char *symbolName;
symbolName = (char*)malloc (strlen (name) + 2);
strcpy(symbolName + 1, name);
symbolName[0] = '_';
symbol = NULL;
if (NSIsSymbolNameDefined (symbolName))
symbol = NSLookupAndBindSymbol (symbolName);
free (symbolName);
return symbol ? NSAddressOfSymbol (symbol) : NULL;
}
#endif
void GLExtHandler::setPixelPerUnit(float fovy,int screenHeight)
{
// the pixel per unit is computed for a distance from the camera of screenHeight
pixelPerUnit = screenHeight / (2.0f * tan(fovy * 0.5f));
}
void GLExtHandler::glTexImage3D(GLenum target,
GLint level,
GLenum internalFormat,
GLsizei width,
GLsizei height,
GLsizei depth,
GLint border,
GLenum format,
GLenum type,
const GLvoid* pixels)
{
if (loadGLExtTexture3D())
SPK_glTexImage3D(target,level,internalFormat,width,height,depth,border,format,type,pixels);
}
GLExtHandler::GLExtension GLExtHandler::getPointSpriteGLExt()
{
return pointSpriteGLExt;
}
GLExtHandler::GLExtension GLExtHandler::getPointParameterGLExt()
{
return pointParameterGLExt;
}
void GLExtHandler::enablePointParameterGLExt(float size,bool distance)
{
// derived size = size * sqrt(1 / (A + B * distance + C * distance<63>))
if (distance)
{
const float sqrtC = POINT_SIZE_CURRENT / (size * pixelPerUnit);
const float QUADRATIC_WORLD[3] = {0.0f,0.0f,sqrtC * sqrtC}; // A = 0; B = 0; C = (POINT_SIZE_CURRENT / (size * pixelPerUnit))<29>
SPK_glPointParameterfv(GL_POINT_DISTANCE_ATTENUATION,QUADRATIC_WORLD);
}
else
{
const float sqrtA = POINT_SIZE_CURRENT / size;
const float QUADRATIC_WORLD[3] = {sqrtA * sqrtA,0.0f,0.0f}; // A = (POINT_SIZE_CURRENT / size)<29>; B = 0; C = 0
SPK_glPointParameterfv(GL_POINT_DISTANCE_ATTENUATION,QUADRATIC_WORLD);
}
glPointSize(POINT_SIZE_CURRENT);
SPK_glPointParameterf(GL_POINT_SIZE_MIN,POINT_SIZE_MIN);
SPK_glPointParameterf(GL_POINT_SIZE_MAX,POINT_SIZE_MAX);
}
void GLExtHandler::disablePointParameterGLExt()
{
SPK_glPointParameterfv(GL_POINT_DISTANCE_ATTENUATION,QUADRATIC_SCREEN);
}
GLExtHandler::GLExtension GLExtHandler::getTexture3DGLExt()
{
return texture3DGLExt;
}
GLExtHandler::GLExtension GLExtHandler::getShaderGLExt()
{
return shaderGLExt;
}
}}

View File

@@ -0,0 +1,107 @@
//////////////////////////////////////////////////////////////////////////////////
// SPARK particle engine //
// Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com //
// //
// This software is provided 'as-is', without any express or implied //
// warranty. In no event will the authors be held liable for any damages //
// arising from the use of this software. //
// //
// Permission is granted to anyone to use this software for any purpose, //
// including commercial applications, and to alter it and redistribute it //
// freely, subject to the following restrictions: //
// //
// 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 //
// in a product, an acknowledgment in the product documentation would be //
// appreciated but is not required. //
// 2. Altered source versions must be plainly marked as such, and must not be //
// misrepresented as being the original software. //
// 3. This notice may not be removed or altered from any source distribution. //
//////////////////////////////////////////////////////////////////////////////////
#include "RenderingAPIs/OpenGL/SPK_GLLineRenderer.h"
#include "Core/SPK_Particle.h"
#include "Core/SPK_Group.h"
#include "Core/SPK_ArrayBuffer.h"
namespace SPK
{
namespace GL
{
const std::string GLLineRenderer::GPU_BUFFER_NAME("SPK_GLLineRenderer_GPU");
float* GLLineRenderer::gpuBuffer = NULL;
float* GLLineRenderer::gpuIterator = NULL;
GLLineRenderer::GLLineRenderer(float length,float width) :
GLRenderer(),
LineRendererInterface(length,width)
{}
bool GLLineRenderer::checkBuffers(const Group& group)
{
FloatBuffer* fBuffer;
if ((fBuffer = dynamic_cast<FloatBuffer*>(group.getBuffer(GPU_BUFFER_NAME))) == NULL)
return false;
gpuIterator = gpuBuffer = fBuffer->getData();
return true;
}
void GLLineRenderer::createBuffers(const Group& group)
{
FloatBuffer* fBuffer = dynamic_cast<FloatBuffer*>(group.createBuffer(GPU_BUFFER_NAME,FloatBufferCreator(14),0,false));
gpuIterator = gpuBuffer = fBuffer->getData();
}
void GLLineRenderer::destroyBuffers(const Group& group)
{
group.destroyBuffer(GPU_BUFFER_NAME);
}
void GLLineRenderer::render(const Group& group)
{
if (!prepareBuffers(group))
return;
initBlending();
initRenderingHints();
glLineWidth(width);
glDisable(GL_TEXTURE_2D);
glShadeModel(GL_FLAT);
for (size_t i = 0; i < group.getNbParticles(); ++i)
{
const Particle& particle = group.getParticle(i);
*(gpuIterator++) = particle.position().x;
*(gpuIterator++) = particle.position().y;
*(gpuIterator++) = particle.position().z;
gpuIterator += 4; // skips the first vertex color data as GL_FLAT was forced
*(gpuIterator++) = particle.position().x + particle.velocity().x * length;
*(gpuIterator++) = particle.position().y + particle.velocity().y * length;
*(gpuIterator++) = particle.position().z + particle.velocity().z * length;
*(gpuIterator++) = particle.getR();
*(gpuIterator++) = particle.getG();
*(gpuIterator++) = particle.getB();
*(gpuIterator++) = particle.getParamCurrentValue(PARAM_ALPHA);
}
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
// interleaves vertex and color data
glVertexPointer(3,GL_FLOAT,7 * sizeof(float),gpuBuffer);
glColorPointer(4,GL_FLOAT,7 * sizeof(float),gpuBuffer + 3);
glDrawArrays(GL_LINES,0,group.getNbParticles() << 1);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}
}}

View File

@@ -0,0 +1,204 @@
//////////////////////////////////////////////////////////////////////////////////
// SPARK particle engine //
// Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com //
// //
// This software is provided 'as-is', without any express or implied //
// warranty. In no event will the authors be held liable for any damages //
// arising from the use of this software. //
// //
// Permission is granted to anyone to use this software for any purpose, //
// including commercial applications, and to alter it and redistribute it //
// freely, subject to the following restrictions: //
// //
// 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 //
// in a product, an acknowledgment in the product documentation would be //
// appreciated but is not required. //
// 2. Altered source versions must be plainly marked as such, and must not be //
// misrepresented as being the original software. //
// 3. This notice may not be removed or altered from any source distribution. //
//////////////////////////////////////////////////////////////////////////////////
#include "RenderingAPIs/OpenGL/SPK_GLLineTrailRenderer.h"
#include "Core/SPK_Particle.h"
#include "Core/SPK_Group.h"
#include "Core/SPK_ArrayBuffer.h"
namespace SPK
{
namespace GL
{
const std::string GLLineTrailRenderer::VERTEX_BUFFER_NAME("SPK_GLLineTrailRenderer_Vertex");
const std::string GLLineTrailRenderer::COLOR_BUFFER_NAME("SPK_GLLineTrailRenderer_Color");
const std::string GLLineTrailRenderer::VALUE_BUFFER_NAME("SPK_GLLineTrailRenderer_Value");
float* GLLineTrailRenderer::vertexBuffer = NULL;
float* GLLineTrailRenderer::vertexIterator = NULL;
float* GLLineTrailRenderer::colorBuffer = NULL;
float* GLLineTrailRenderer::colorIterator = NULL;
float* GLLineTrailRenderer::valueBuffer = NULL;
float* GLLineTrailRenderer::valueIterator = NULL;
GLLineTrailRenderer::GLLineTrailRenderer() :
GLRenderer(),
nbSamples(8),
width(1.0f),
duration(1.0f),
degeneratedR(0.0f),
degeneratedG(0.0f),
degeneratedB(0.0f),
degeneratedA(0.0f)
{
enableBlending(true);
}
void GLLineTrailRenderer::setDegeneratedLines(float r,float g,float b,float a)
{
degeneratedR = r;
degeneratedG = g;
degeneratedB = b;
degeneratedA = a;
}
bool GLLineTrailRenderer::checkBuffers(const Group& group)
{
FloatBuffer* fVertexBuffer;
FloatBuffer* fColorBuffer;
FloatBuffer* fValueBuffer;
if ((fVertexBuffer = dynamic_cast<FloatBuffer*>(group.getBuffer(VERTEX_BUFFER_NAME,nbSamples))) == NULL)
return false;
if ((fColorBuffer = dynamic_cast<FloatBuffer*>(group.getBuffer(COLOR_BUFFER_NAME,nbSamples))) == NULL)
return false;
if ((fValueBuffer = dynamic_cast<FloatBuffer*>(group.getBuffer(VALUE_BUFFER_NAME,nbSamples))) == NULL)
return false;
vertexIterator = vertexBuffer = fVertexBuffer->getData();
colorIterator = colorBuffer = fColorBuffer->getData();
valueIterator = valueBuffer = fValueBuffer->getData();
return true;
}
void GLLineTrailRenderer::createBuffers(const Group& group)
{
FloatBuffer* fVertexBuffer = dynamic_cast<FloatBuffer*>(group.createBuffer(VERTEX_BUFFER_NAME,FloatBufferCreator((nbSamples + 2) * 3),nbSamples,true));
FloatBuffer* fColorBuffer = dynamic_cast<FloatBuffer*>(group.createBuffer(COLOR_BUFFER_NAME,FloatBufferCreator((nbSamples + 2) << 2),nbSamples,true));
FloatBuffer* fValueBuffer = dynamic_cast<FloatBuffer*>(group.createBuffer(VALUE_BUFFER_NAME,FloatBufferCreator(nbSamples),nbSamples,true));
vertexIterator = vertexBuffer = fVertexBuffer->getData();
colorIterator = colorBuffer = fColorBuffer->getData();
valueIterator = valueBuffer = fValueBuffer->getData();
// Fills the buffers with correct values
for (size_t i = 0; i < group.getNbParticles(); ++i)
{
const Particle& particle = group.getParticle(i);
init(particle,particle.getAge());
}
// Resets the iterators at the beginning after the init
vertexIterator = vertexBuffer;
colorIterator = colorBuffer;
valueIterator = valueBuffer;
}
void GLLineTrailRenderer::destroyBuffers(const Group& group)
{
group.destroyBuffer(VERTEX_BUFFER_NAME);
group.destroyBuffer(COLOR_BUFFER_NAME);
group.destroyBuffer(VALUE_BUFFER_NAME);
}
void GLLineTrailRenderer::init(const Group& group)
{
if (!prepareBuffers(group))
return;
for (size_t i = 0; i < group.getNbParticles(); ++i)
{
const Particle& particle = group.getParticle(i);
init(particle,particle.getAge());
}
}
void GLLineTrailRenderer::render(const Group& group)
{
if (!prepareBuffers(group))
return;
initBlending();
initRenderingHints();
// Inits lines' parameters
glLineWidth(width);
glDisable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
for (size_t i = 0; i < group.getNbParticles(); ++i)
{
const Particle& particle = group.getParticle(i);
float age = particle.getAge();
float oldAge = *valueIterator;
if ((age == 0.0f)||(age < *valueIterator)) // If the particle is new, buffers for it are reinitialized
init(particle,0.0f);
else
{
if (age - *(valueIterator + 1) >= duration / (nbSamples - 1)) // shifts the data by one
{
memmove(vertexIterator + 6,vertexIterator + 3,(nbSamples - 1) * 3 * sizeof(float));
memmove(colorIterator + 8,colorIterator + 4,((nbSamples - 1) << 2) * sizeof(float));
memmove(valueIterator + 1,valueIterator,(nbSamples - 1) * sizeof(float));
// post degenerated vertex copy
memcpy(vertexIterator + (nbSamples + 1) * 3,vertexIterator + nbSamples * 3,3 * sizeof(float));
}
// Updates the current sample
const Vector3D& pos = particle.position();
*(vertexIterator++) = pos.x;
*(vertexIterator++) = pos.y;
*(vertexIterator++) = pos.z;
memcpy(vertexIterator,vertexIterator - 3,3 * sizeof(float));
vertexIterator += (nbSamples + 1) * 3;
colorIterator += 4; // skips post degenerated vertex color
*(colorIterator++) = particle.getR();
*(colorIterator++) = particle.getG();
*(colorIterator++) = particle.getB();
*(colorIterator++) = particle.getParamCurrentValue(PARAM_ALPHA);
colorIterator += 3;
*(valueIterator++) = age;
//valueIterator += nbSamples;
// Updates alpha
for (size_t i = 0; i < nbSamples - 1; ++i)
{
float ratio = (age - oldAge) / (duration - age + *valueIterator);
if (ratio > 0.0f)
*colorIterator *= ratio < 1.0f ? 1.0f - ratio : 0.0f;
colorIterator += 4;
++valueIterator;
}
++colorIterator;
}
}
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(4,GL_FLOAT,0,colorBuffer);
glVertexPointer(3,GL_FLOAT,0,vertexBuffer);
glDrawArrays(GL_LINE_STRIP,0,group.getNbParticles() * (nbSamples + 2));
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}
}}

View File

@@ -0,0 +1,104 @@
//////////////////////////////////////////////////////////////////////////////////
// SPARK particle engine //
// Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com //
// //
// This software is provided 'as-is', without any express or implied //
// warranty. In no event will the authors be held liable for any damages //
// arising from the use of this software. //
// //
// Permission is granted to anyone to use this software for any purpose, //
// including commercial applications, and to alter it and redistribute it //
// freely, subject to the following restrictions: //
// //
// 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 //
// in a product, an acknowledgment in the product documentation would be //
// appreciated but is not required. //
// 2. Altered source versions must be plainly marked as such, and must not be //
// misrepresented as being the original software. //
// 3. This notice may not be removed or altered from any source distribution. //
//////////////////////////////////////////////////////////////////////////////////
#include "RenderingAPIs/OpenGL/SPK_GLPointRenderer.h"
#include "Core/SPK_Particle.h"
#include "Core/SPK_Group.h"
namespace SPK
{
namespace GL
{
GLPointRenderer::GLPointRenderer(float size) :
GLRenderer(),
PointRendererInterface(POINT_SQUARE,size),
GLExtHandler(),
textureIndex(0),
worldSize(false)
{}
bool GLPointRenderer::setType(PointType type)
{
if ((type == POINT_SPRITE)&&(!loadGLExtPointSprite()))
return false;
this->type = type;
return true;
}
bool GLPointRenderer::enableWorldSize(bool worldSizeEnabled)
{
worldSize = ((worldSizeEnabled)&&(loadGLExtPointParameter()));
return worldSize;
}
void GLPointRenderer::render(const Group& group)
{
initBlending();
initRenderingHints();
switch(type)
{
case POINT_SQUARE :
glDisable(GL_TEXTURE_2D);
glDisable(GL_POINT_SMOOTH);
if (getPointSpriteGLExt() == SUPPORTED)
disablePointSpriteGLExt();
break;
case POINT_SPRITE :
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,textureIndex);
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,getTextureBlending());
enablePointSpriteGLExt();
break;
case POINT_CIRCLE :
glDisable(GL_TEXTURE_2D);
glEnable(GL_POINT_SMOOTH);
if (getPointSpriteGLExt() == SUPPORTED)
disablePointSpriteGLExt();
break;
}
if (worldSize)
enablePointParameterGLExt(size,true);
else
{
glPointSize(size);
if (getPointParameterGLExt() == SUPPORTED)
disablePointParameterGLExt();
}
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(group.getModel()->isEnabled(PARAM_ALPHA) ? 4 : 3,GL_FLOAT,group.getParamStride(),group.getParamAddress(PARAM_RED));
glVertexPointer(3,GL_FLOAT,group.getPositionStride(),group.getPositionAddress());
glDrawArrays(GL_POINTS,0,group.getNbParticles());
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}
}}

View File

@@ -0,0 +1,279 @@
//////////////////////////////////////////////////////////////////////////////////
// SPARK particle engine //
// Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com //
// //
// This software is provided 'as-is', without any express or implied //
// warranty. In no event will the authors be held liable for any damages //
// arising from the use of this software. //
// //
// Permission is granted to anyone to use this software for any purpose, //
// including commercial applications, and to alter it and redistribute it //
// freely, subject to the following restrictions: //
// //
// 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 //
// in a product, an acknowledgment in the product documentation would be //
// appreciated but is not required. //
// 2. Altered source versions must be plainly marked as such, and must not be //
// misrepresented as being the original software. //
// 3. This notice may not be removed or altered from any source distribution. //
//////////////////////////////////////////////////////////////////////////////////
#include "RenderingAPIs/OpenGL/SPK_GLQuadRenderer.h"
#include "Core/SPK_Particle.h"
#include "Core/SPK_Group.h"
#include "Core/SPK_ArrayBuffer.h"
namespace SPK
{
namespace GL
{
const std::string GLQuadRenderer::GPU_BUFFER_NAME("SPK_GLQuadRenderer_GPU");
const std::string GLQuadRenderer::TEXTURE_BUFFER_NAME("SPK_GLQuadRenderer_Texture");
float* GLQuadRenderer::gpuBuffer = NULL;
float* GLQuadRenderer::gpuIterator = NULL;
float* GLQuadRenderer::textureBuffer = NULL;
float* GLQuadRenderer::textureIterator = NULL;
void (GLQuadRenderer::*GLQuadRenderer::renderParticle)(const Particle&) const = NULL;
GLQuadRenderer::GLQuadRenderer(float scaleX,float scaleY) :
GLRenderer(),
QuadRendererInterface(scaleX,scaleY),
Oriented3DRendererInterface(),
GLExtHandler(),
textureIndex(0)
{}
bool GLQuadRenderer::checkBuffers(const Group& group)
{
FloatBuffer* fGpuBuffer;
if ((fGpuBuffer = dynamic_cast<FloatBuffer*>(group.getBuffer(GPU_BUFFER_NAME))) == NULL)
return false;
if (texturingMode != TEXTURE_NONE)
{
FloatBuffer* fTextureBuffer;
if ((fTextureBuffer = dynamic_cast<FloatBuffer*>(group.getBuffer(TEXTURE_BUFFER_NAME,texturingMode))) == NULL)
textureBuffer = createTextureBuffer(group);
textureIterator = textureBuffer = fTextureBuffer->getData();
}
gpuIterator = gpuBuffer = fGpuBuffer->getData();
return true;
}
void GLQuadRenderer::createBuffers(const Group& group)
{
FloatBuffer* fBuffer = dynamic_cast<FloatBuffer*>(group.createBuffer(GPU_BUFFER_NAME,FloatBufferCreator(28),0,false));
gpuIterator = gpuBuffer = fBuffer->getData();
if (texturingMode != TEXTURE_NONE)
textureIterator = textureBuffer = createTextureBuffer(group);
}
void GLQuadRenderer::destroyBuffers(const Group& group)
{
group.destroyBuffer(GPU_BUFFER_NAME);
group.destroyBuffer(TEXTURE_BUFFER_NAME);
}
float* GLQuadRenderer::createTextureBuffer(const Group& group) const
{
FloatBuffer* fbuffer = NULL;
switch(texturingMode)
{
case TEXTURE_2D :
fbuffer = dynamic_cast<FloatBuffer*>(group.createBuffer(TEXTURE_BUFFER_NAME,FloatBufferCreator(8),TEXTURE_2D,false));
if (!group.getModel()->isEnabled(PARAM_TEXTURE_INDEX))
{
float t[8] = {1.0f,0.0f,0.0f,0.0f,0.0f,1.0f,1.0f,1.0f};
for (size_t i = 0; i < group.getParticles().getNbReserved() << 3; ++i)
fbuffer->getData()[i] = t[i & 7];
}
break;
case TEXTURE_3D :
fbuffer = dynamic_cast<FloatBuffer*>(group.createBuffer(TEXTURE_BUFFER_NAME,FloatBufferCreator(12),TEXTURE_3D,false));
float t[12] = {1.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,1.0f,0.0f,1.0f,1.0f,0.0f};
for (size_t i = 0; i < group.getParticles().getNbReserved() * 12; ++i)
fbuffer->getData()[i] = t[i % 12];
break;
}
return fbuffer->getData();
}
bool GLQuadRenderer::setTexturingMode(TexturingMode mode)
{
if ((mode == TEXTURE_3D)&&(!loadGLExtTexture3D()))
return false;
texturingMode = mode;
return true;
}
void GLQuadRenderer::render(const Group& group)
{
if (!prepareBuffers(group))
return;
float oldModelView[16];
for (int i = 0; i < 16; ++i)
oldModelView[i] = modelView[i];
glGetFloatv(GL_MODELVIEW_MATRIX,modelView);
for (int i = 0; i < 16; ++i)
if (oldModelView[i] != modelView[i])
{
invertModelView();
break;
}
initBlending();
initRenderingHints();
glShadeModel(GL_FLAT);
switch(texturingMode)
{
case TEXTURE_2D :
if (getTexture3DGLExt() == SUPPORTED)
glDisable(GL_TEXTURE_3D);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,textureIndex);
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,getTextureBlending());
if (!group.getModel()->isEnabled(PARAM_TEXTURE_INDEX))
{
if (!group.getModel()->isEnabled(PARAM_ANGLE))
renderParticle = &GLQuadRenderer::render2D;
else
renderParticle = &GLQuadRenderer::render2DRot;
}
else
{
if (!group.getModel()->isEnabled(PARAM_ANGLE))
renderParticle = &GLQuadRenderer::render2DAtlas;
else
renderParticle = &GLQuadRenderer::render2DAtlasRot;
}
break;
case TEXTURE_3D :
glDisable(GL_TEXTURE_2D);
glEnable(GL_TEXTURE_3D);
glBindTexture(GL_TEXTURE_3D,textureIndex);
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,getTextureBlending());
if (!group.getModel()->isEnabled(PARAM_ANGLE))
renderParticle = &GLQuadRenderer::render3D;
else
renderParticle = &GLQuadRenderer::render3DRot;
break;
case TEXTURE_NONE :
glDisable(GL_TEXTURE_2D);
if (getTexture3DGLExt() == SUPPORTED)
glDisable(GL_TEXTURE_3D);
if (!group.getModel()->isEnabled(PARAM_ANGLE))
renderParticle = &GLQuadRenderer::render2D;
else
renderParticle = &GLQuadRenderer::render2DRot;
break;
}
bool globalOrientation = precomputeOrientation3D(
group,
Vector3D(-invModelView[8],-invModelView[9],-invModelView[10]),
Vector3D(invModelView[4],invModelView[5],invModelView[6]),
Vector3D(invModelView[12],invModelView[13],invModelView[14]));
if (globalOrientation)
{
computeGlobalOrientation3D();
for (size_t i = 0; i < group.getNbParticles(); ++i)
(this->*renderParticle)(group.getParticle(i));
}
else
{
for (size_t i = 0; i < group.getNbParticles(); ++i)
{
const Particle& particle = group.getParticle(i);
computeSingleOrientation3D(particle);
(this->*renderParticle)(particle);
}
}
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
if (texturingMode == TEXTURE_2D)
{
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2,GL_FLOAT,0,textureBuffer);
}
else if (texturingMode == TEXTURE_3D)
{
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(3,GL_FLOAT,0,textureBuffer);
}
// interleaves vertex and color data
glVertexPointer(3,GL_FLOAT,7 * sizeof(float),gpuBuffer);
glColorPointer(4,GL_FLOAT,7 * sizeof(float),gpuBuffer + 3);
glDrawArrays(GL_QUADS,0,group.getNbParticles() << 2);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
if (texturingMode != TEXTURE_NONE)
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
void GLQuadRenderer::render2D(const Particle& particle) const
{
scaleQuadVectors(particle,scaleX,scaleY);
GLCallColorAndVertex(particle);
}
void GLQuadRenderer::render2DRot(const Particle& particle) const
{
rotateAndScaleQuadVectors(particle,scaleX,scaleY);
GLCallColorAndVertex(particle);
}
void GLQuadRenderer::render3D(const Particle& particle) const
{
scaleQuadVectors(particle,scaleX,scaleY);
GLCallColorAndVertex(particle);
GLCallTexture3D(particle);
}
void GLQuadRenderer::render3DRot(const Particle& particle) const
{
rotateAndScaleQuadVectors(particle,scaleX,scaleY);
GLCallColorAndVertex(particle);
GLCallTexture3D(particle);
}
void GLQuadRenderer::render2DAtlas(const Particle& particle) const
{
scaleQuadVectors(particle,scaleX,scaleY);
GLCallColorAndVertex(particle);
GLCallTexture2DAtlas(particle);
}
void GLQuadRenderer::render2DAtlasRot(const Particle& particle) const
{
rotateAndScaleQuadVectors(particle,scaleX,scaleY);
GLCallColorAndVertex(particle);
GLCallTexture2DAtlas(particle);
}
}}

View File

@@ -0,0 +1,80 @@
//////////////////////////////////////////////////////////////////////////////////
// SPARK particle engine //
// Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com //
// //
// This software is provided 'as-is', without any express or implied //
// warranty. In no event will the authors be held liable for any damages //
// arising from the use of this software. //
// //
// Permission is granted to anyone to use this software for any purpose, //
// including commercial applications, and to alter it and redistribute it //
// freely, subject to the following restrictions: //
// //
// 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 //
// in a product, an acknowledgment in the product documentation would be //
// appreciated but is not required. //
// 2. Altered source versions must be plainly marked as such, and must not be //
// misrepresented as being the original software. //
// 3. This notice may not be removed or altered from any source distribution. //
//////////////////////////////////////////////////////////////////////////////////
#include "RenderingAPIs/OpenGL/SPK_GLRenderer.h"
namespace SPK
{
namespace GL
{
GLRenderer::GLRenderer() :
Renderer(),
blendingEnabled(false),
srcBlending(GL_SRC_ALPHA),
destBlending(GL_ONE_MINUS_SRC_ALPHA),
textureBlending(GL_MODULATE)
{}
GLRenderer::~GLRenderer() {}
void GLRenderer::setBlending(BlendingMode blendMode)
{
switch(blendMode)
{
case BLENDING_NONE :
srcBlending = GL_ONE;
destBlending = GL_ZERO;
blendingEnabled = false;
break;
case BLENDING_ADD :
srcBlending = GL_SRC_ALPHA;
destBlending = GL_ONE;
blendingEnabled = true;
break;
case BLENDING_ALPHA :
srcBlending = GL_SRC_ALPHA;
destBlending = GL_ONE_MINUS_SRC_ALPHA;
blendingEnabled = true;
break;
}
}
void GLRenderer::saveGLStates()
{
glPushAttrib(GL_POINT_BIT |
GL_LINE_BIT |
GL_ENABLE_BIT |
GL_COLOR_BUFFER_BIT |
GL_CURRENT_BIT |
GL_TEXTURE_BIT |
GL_DEPTH_BUFFER_BIT |
GL_LIGHTING_BIT |
GL_POLYGON_BIT);
}
void GLRenderer::restoreGLStates()
{
glPopAttrib();
}
}}