#include <FFGL.h>
#include <FFGLLib.h>
#include <cstdlib>
#include <cmath>
#include <stdio.h>
#include "FFGLStaticSource.h"
#include "utilities.h"
#ifdef _WIN32
#include "../common/opengl/include/glut.h"
#include <windows.h>
#else
#include <OpenGL/glu.h>
#include <sys/time.h>
#endif
#ifdef __APPLE__
#include <Carbon.h>
#endif
#define FFPARAM_Mode (0)
bool hastime = false;
static CFFGLPluginInfo PluginInfo (
FFGLStaticSource::CreateInstance, "MSTS", "Static Generator", 1, 000, 1, 100, FF_SOURCE, "Static Generator", "by Matias Wilkman" );
char *vertexShaderCode =
"void main()"
"{"
" gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;"
" gl_FrontColor = gl_Color;"
"}";
char *fragmentShaderCode =
"float rand(vec2 co){"
" return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);"
"}"
"uniform float time;"
"uniform bool twotone;"
"uniform bool grayscale;"
"void main(void)"
"{"
" vec4 c_out = vec4(0.0, 1.0, 0.0, 1.0);"
" vec2 texCoord = gl_FragCoord.xy;"
" texCoord.s += 8.64*fract(texCoord.t + time);"
" texCoord.t += 4.57*fract(texCoord.s + time);"
" if (grayscale && !twotone)" " c_out.rgb = vec3(mod(time+rand(vec2(texCoord.s+2.34*time, texCoord.t+3.14*time)), 1.0));"
" if (!grayscale && !twotone)" " c_out.rgb = vec3(mod(time+rand(texCoord+vec2(time)), 1.0), mod(time+rand(texCoord+vec2(2.0*time)), 1.0), mod(time+rand(texCoord+vec2(3.0*time)), 1.0));"
" if (twotone)" " if (mod(time+rand(texCoord + vec2(time)), 1.0) > 0.5)"
" c_out.rgb = vec3(1.0);"
" else"
" c_out.rgb = vec3(0.0);"
" gl_FragColor = c_out;"
"}";
FFGLStaticSource::FFGLStaticSource()
:CFreeFrameGLPlugin(),
m_initResources(1)
{
SetMinInputs(0);
SetMaxInputs(0);
SetTimeSupported(true);
init_time(&t0);
#ifdef _WIN32
srand(GetTickCount());
#else
srand(time(NULL));
#endif
SetParamInfo(FFPARAM_Mode, "Mode", FF_TYPE_STANDARD, 0.5f);
m_mode = 0.5f;
}
DWORD FFGLStaticSource::SetTime(double time)
{
m_time = time;
hastime = true;
return FF_SUCCESS;
}
DWORD FFGLStaticSource::InitGL(const FFGLViewportStruct *vp)
{
m_extensions.Initialize();
if (m_extensions.ARB_shader_objects==0)
return FF_FAIL;
m_shader.SetExtensions(&m_extensions);
m_shader.Compile(vertexShaderCode,fragmentShaderCode);
m_shader.BindShader();
m_timeLocation = m_shader.FindUniform("time");
m_grayscaleLocation = m_shader.FindUniform("grayscale");
m_twotoneLocation = m_shader.FindUniform("twotone");
m_shader.UnbindShader();
return FF_SUCCESS;
}
DWORD FFGLStaticSource::DeInitGL()
{
m_shader.FreeGLResources();
return FF_SUCCESS;
}
DWORD FFGLStaticSource::ProcessOpenGL(ProcessOpenGLStruct *pGL)
{
if (!hastime)
{
update_time(&m_time, t0);
}
m_shader.BindShader();
float foo = (m_time+float(rand()%100));
m_extensions.glUniform1fARB(m_timeLocation, foo);
m_extensions.glUniform1iARB(m_grayscaleLocation, m_mode>0.33f?true:false);
m_extensions.glUniform1iARB(m_twotoneLocation, m_mode>0.66f?true:false);
glBegin(GL_QUADS);
glVertex2f(-1,-1);
glVertex2f(-1,1);
glVertex2f(1,1);
glVertex2f(1,-1);
glEnd();
m_shader.UnbindShader();
return FF_SUCCESS;
}
DWORD FFGLStaticSource::GetParameter(DWORD dwIndex)
{
DWORD dwRet;
switch (dwIndex) {
case FFPARAM_Mode:
*((float *)(unsigned)&dwRet) = m_mode;
return dwRet;
default:
return FF_FAIL;
}
}
char* FFGLStaticSource::GetParameterDisplay(DWORD dwIndex)
{
DWORD dwType = m_pPlugin->GetParamType(dwIndex);
DWORD dwValue = m_pPlugin->GetParameter(dwIndex);
if ((dwValue != FF_FAIL) && (dwType != FF_FAIL))
{
if (dwType == FF_TYPE_TEXT)
{
return (char *)dwValue;
}
else
{
switch (dwIndex) {
case FFPARAM_Mode:
if (m_mode < 0.33)
sprintf(m_Displayvalue, "%s", "Color");
else if (m_mode < 0.66)
sprintf(m_Displayvalue, "%s", "Grayscale");
else
sprintf(m_Displayvalue, "%s", "Two-tone");
break;
default:
return NULL;
}
return m_Displayvalue;
}
}
return NULL;
}
DWORD FFGLStaticSource::SetParameter(const SetParameterStruct* pParam)
{
if (pParam != NULL) {
switch (pParam->ParameterNumber) {
case FFPARAM_Mode:
m_mode = *((float *)&(pParam->NewParameterValue));
break;
default:
return FF_FAIL;
}
return FF_SUCCESS;
}
return FF_FAIL;
}