Permalink
Browse files

chugl/gluck stuff

  • Loading branch information...
1 parent 833b8dd commit 061fbd173ecdd35b20fcf3ce1ab30fdbf7969c6d @spencersalazar committed Dec 20, 2012
Showing with 225 additions and 9 deletions.
  1. +121 −0 GlucK2/CircularBuffer.h
  2. +56 −9 GlucK2/GlucK2.cpp
  3. +28 −0 GlucK2/chugl-test.ck
  4. +20 −0 GlucK2/def.h
View
121 GlucK2/CircularBuffer.h
@@ -0,0 +1,121 @@
+/*******************************************************************************
+
+ CircularBuffer
+
+ Author: Spencer Salazar - spencer@ccrma.stanford.edu
+
+ Notes: This class is defined entirely in this header file, as thats the only
+ sane way to implement a C++ template class.
+
+ ******************************************************************************/
+
+
+#ifndef __CIRCULAR_BUFFER_H__
+#define __CIRCULAR_BUFFER_H__
+
+
+#include <sys/types.h>
+#include "def.h"
+
+
+template<typename T>
+class CircularBuffer
+{
+public:
+
+ CircularBuffer(size_t numElements) :
+ m_read(0),
+ m_write(0),
+ m_numElements(numElements+1) // need 1 element to pad
+ {
+ m_elements = new T[m_numElements];
+ }
+
+ ~CircularBuffer()
+ {
+ SAFE_DELETE_V(m_elements);
+ }
+
+ // put one element
+ // returns number of elements successfully put
+ inline size_t put(T element)
+ {
+ if((m_write + 1)%m_numElements == m_read)
+ {
+ // no space
+ return 0;
+ }
+
+ m_elements[m_write] = element;
+
+ m_write = (m_write+1)%m_numElements;
+
+ return 1;
+ }
+
+ // get one element
+ // returns number of elements successfully got
+ inline size_t get(T &element)
+ {
+ if(m_read == m_write)
+ {
+ // nothing to get
+ return 0;
+ }
+
+ element = m_elements[m_read];
+
+ m_read = (m_read+1)%m_numElements;
+
+ return 1;
+ }
+
+ // peek at element i without removing it
+ // i = 1 would correspond to the least recently put element;
+ // i = -1 would correspond to the most recent
+ // WARNING: doesn't sanity check that i is a valid index
+ // returns number of elements successfully peeked
+ inline size_t peek(T &element, size_t i)
+ {
+ if(i == 0)
+ return 0;
+
+ if(i > 0)
+ element = m_elements[(m_read+(i-1))%m_numElements];
+ if(i < 0)
+ element = m_elements[(m_write-(i+1))%m_numElements];
+
+ return 1;
+ }
+
+ inline void clear() { m_write = m_read; }
+
+ // return maximum number of elements that can be held
+ inline size_t maxElements() { return m_numElements-1; }
+
+ // return if buffer is full
+ inline bool atMaximum() { return (m_write + 1)%m_numElements == m_read; }
+
+ // return number of valid elements in the buffer
+ inline size_t numElements()
+ {
+ if(m_read == m_write)
+ return 0;
+ else if(m_read < m_write)
+ return m_write - m_read;
+ else
+ return m_numElements - m_read + m_write;
+ }
+
+private:
+
+ T * m_elements;
+
+ size_t m_read, m_write;
+ const size_t m_numElements;
+
+};
+
+
+#endif // __CIRCULAR_BUFFER_H__
+
View
65 GlucK2/GlucK2.cpp
@@ -31,6 +31,7 @@ using namespace std;
#include "Geometry.h"
+
Chuck_DL_MainThreadHook * g_hook = NULL;
GLsizei g_width = 1024;
@@ -66,32 +67,51 @@ namespace ChuGL
g_gmutex.release();
}
+ GLvertex3f setPosition(GLvertex3f p)
+ {
+ m_nextPosition = p;
+ return p;
+ }
+
+ GLcolor4f setColor(GLcolor4f c)
+ {
+ m_nextColor = c;
+ return c;
+ }
+
+ protected:
GLvertex3f m_position;
GLcolor4f m_color;
private:
list<GGen *> m_children;
+ GLcolor4f m_savedColor;
+ GLvertex3f m_nextPosition;
+ GLcolor4f m_nextColor;
};
GGen::GGen()
{
- m_position = GLvertex3f(0, 0, 0);
- m_color = GLcolor4f(0, 0, 0, 1);
+ m_position = m_nextPosition = GLvertex3f(0, 0, 0);
+ m_color = m_nextColor = GLcolor4f(0, 0, 0, 1);
}
GGen::~GGen() { }
void GGen::update(float dt, float t)
{
-
+ m_position = m_nextPosition;
+ m_color = m_nextColor;
}
void GGen::state()
{
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
+ glGetFloatv(GL_CURRENT_COLOR, (float *) &m_savedColor);
+
glTranslatef(m_position.x, m_position.y, m_position.z);
glColor4f(m_color.r, m_color.g, m_color.b, m_color.a);
}
@@ -103,7 +123,10 @@ namespace ChuGL
void GGen::unstate()
{
+ glMatrixMode(GL_MODELVIEW);
glPopMatrix();
+
+ glColor4fv((const float *) &m_savedColor);
}
void GGen::system_update(float dt, float t)
@@ -138,17 +161,39 @@ namespace ChuGL
class GGen_Line : public GGen
{
public:
+ virtual void update(float dt, float t)
+ {
+ GGen::update(dt, t);
+
+ m_endpoint = m_nextEndpoint;
+ }
+
+ virtual void state()
+ {
+ GGen::state();
+
+ glTranslatef(m_endpoint.x, m_endpoint.y, m_endpoint.z);
+ }
+
virtual void geometry()
{
glBegin(GL_LINES);
glVertex3f(0, 0, 0);
- glVertex3f(m_endpoint.x-m_position.x,
- m_endpoint.y-m_position.y,
- m_endpoint.z-m_position.z);
+ glVertex3f(m_position.x-m_endpoint.x,
+ m_position.y-m_endpoint.y,
+ m_position.z-m_endpoint.z);
glEnd();
}
+ GLvertex3f setEndpoint(GLvertex3f e)
+ {
+ m_nextEndpoint = e;
+ return m_nextEndpoint;
+ }
+
+ protected:
GLvertex3f m_endpoint;
+ GLvertex3f m_nextEndpoint;
};
}
@@ -463,6 +508,8 @@ CK_DLL_CHUCK(ggen_chuck)
ChuGL::GGen * to = (ChuGL::GGen *) OBJ_MEMBER_INT(SELF, ggen_data_offset);
ChuGL::GGen * from = (ChuGL::GGen *) OBJ_MEMBER_INT(FROM, ggen_data_offset);
+ FROM->add_ref();
+
to->add(from);
return TRUE;
@@ -477,7 +524,7 @@ CK_DLL_MFUN(ggen_position)
float y = GET_NEXT_FLOAT(ARGS);
float z = GET_NEXT_FLOAT(ARGS);
- gg->m_position = GLvertex3f(x, y, z);
+ gg->setPosition(GLvertex3f(x, y, z));
}
@@ -490,7 +537,7 @@ CK_DLL_MFUN(ggen_color)
float b = GET_NEXT_FLOAT(ARGS);
float a = GET_NEXT_FLOAT(ARGS);
- gg->m_color = GLcolor4f(r, g, b, a);
+ gg->setColor(GLcolor4f(r, g, b, a));
}
@@ -528,7 +575,7 @@ CK_DLL_MFUN(gline_endpoint)
float y = GET_NEXT_FLOAT(ARGS);
float z = GET_NEXT_FLOAT(ARGS);
- gl->m_endpoint = GLvertex3f(x, y, z);
+ gl->setEndpoint(GLvertex3f(x, y, z));
}
View
28 GlucK2/chugl-test.ck
@@ -0,0 +1,28 @@
+
+GLine g => GGen.fbd();
+g.position(0, 0, 0);
+g.color(1, 1, 1, 1);
+
+0.25 => float freq;
+1 => float tick;
+
+while(true)
+{
+ now / second => float phase;
+ g.endpoint(Math.sin(2*pi*phase*freq), Math.cos(2*pi*phase*freq), 0);
+
+ if(phase > tick)
+ {
+ 1 +=> tick;
+ GLine c => g;
+ Math.random2f(0,2*pi) => float theta;
+ 0.2 => float radius;
+ c.position(0, 0, 0);
+ c.color(1, 0.25, 0.33, 1);
+ c.endpoint(radius*Math.cos(theta), radius*Math.sin(theta), 0);
+ }
+
+ 2::ms => now;
+}
+
+
View
20 GlucK2/def.h
@@ -0,0 +1,20 @@
+/*******************************************************************************
+
+ def.h
+
+ author: Spencer Salazar - spencer@ccrma.stanford.edu
+
+ ******************************************************************************/
+
+
+#ifndef __DEF_H__
+#define __DEF_H__
+
+
+#define SAFE_DELETE(x) do{ if(x!=0x0){ delete x; x = 0x0; } } while(0)
+#define SAFE_DELETE_V(x) do{ if(x!=0x0){ delete[] x; x = 0x0; } } while(0)
+
+
+#endif // __DEF_H__
+
+

0 comments on commit 061fbd1

Please sign in to comment.