Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

chugl/gluck stuff

  • Loading branch information...
commit 061fbd173ecdd35b20fcf3ce1ab30fdbf7969c6d 1 parent 833b8dd
Spencer Salazar authored December 19, 2012
121  GlucK2/CircularBuffer.h
... ...
@@ -0,0 +1,121 @@
  1
+/*******************************************************************************
  2
+ 
  3
+ CircularBuffer
  4
+ 
  5
+ Author: Spencer Salazar - spencer@ccrma.stanford.edu
  6
+ 
  7
+ Notes: This class is defined entirely in this header file, as thats the only
  8
+ sane way to implement a C++ template class. 
  9
+ 
  10
+ ******************************************************************************/
  11
+
  12
+
  13
+#ifndef __CIRCULAR_BUFFER_H__
  14
+#define __CIRCULAR_BUFFER_H__
  15
+
  16
+
  17
+#include <sys/types.h>
  18
+#include "def.h"
  19
+
  20
+
  21
+template<typename T>
  22
+class CircularBuffer
  23
+{
  24
+public:
  25
+    
  26
+    CircularBuffer(size_t numElements) :
  27
+    m_read(0),
  28
+    m_write(0),
  29
+    m_numElements(numElements+1) // need 1 element to pad
  30
+    {
  31
+        m_elements = new T[m_numElements];
  32
+    }
  33
+    
  34
+    ~CircularBuffer()
  35
+    {
  36
+        SAFE_DELETE_V(m_elements);
  37
+    }
  38
+    
  39
+    // put one element
  40
+    // returns number of elements successfully put
  41
+    inline size_t put(T element)
  42
+    {
  43
+        if((m_write + 1)%m_numElements == m_read)
  44
+        {
  45
+            // no space
  46
+            return 0;
  47
+        }
  48
+        
  49
+        m_elements[m_write] = element;
  50
+        
  51
+        m_write = (m_write+1)%m_numElements;
  52
+        
  53
+        return 1;
  54
+    }
  55
+        
  56
+    // get one element
  57
+    // returns number of elements successfully got
  58
+    inline size_t get(T &element)
  59
+    {
  60
+        if(m_read == m_write)
  61
+        {
  62
+            // nothing to get
  63
+            return 0;
  64
+        }
  65
+        
  66
+        element = m_elements[m_read];
  67
+        
  68
+        m_read = (m_read+1)%m_numElements;
  69
+        
  70
+        return 1;
  71
+    }
  72
+    
  73
+    // peek at element i without removing it
  74
+    // i = 1 would correspond to the least recently put element;
  75
+    // i = -1 would correspond to the most recent
  76
+    // WARNING: doesn't sanity check that i is a valid index
  77
+    // returns number of elements successfully peeked
  78
+    inline size_t peek(T &element, size_t i)
  79
+    {
  80
+        if(i == 0)
  81
+            return 0;
  82
+        
  83
+        if(i > 0)
  84
+            element = m_elements[(m_read+(i-1))%m_numElements];
  85
+        if(i < 0)
  86
+            element = m_elements[(m_write-(i+1))%m_numElements];
  87
+        
  88
+        return 1;
  89
+    }
  90
+    
  91
+    inline void clear() { m_write = m_read; }
  92
+    
  93
+    // return maximum number of elements that can be held
  94
+    inline size_t maxElements() { return m_numElements-1; }
  95
+    
  96
+    // return if buffer is full
  97
+    inline bool atMaximum() { return (m_write + 1)%m_numElements == m_read; }
  98
+    
  99
+    // return number of valid elements in the buffer
  100
+    inline size_t numElements()
  101
+    {
  102
+        if(m_read == m_write)
  103
+            return 0;
  104
+        else if(m_read < m_write)
  105
+            return m_write - m_read;
  106
+        else
  107
+            return m_numElements - m_read + m_write;
  108
+    }
  109
+    
  110
+private:
  111
+    
  112
+    T * m_elements;
  113
+    
  114
+    size_t m_read, m_write;
  115
+    const size_t m_numElements;
  116
+    
  117
+};
  118
+
  119
+
  120
+#endif // __CIRCULAR_BUFFER_H__
  121
+
65  GlucK2/GlucK2.cpp
@@ -31,6 +31,7 @@ using namespace std;
31 31
 
32 32
 #include "Geometry.h"
33 33
 
  34
+
34 35
 Chuck_DL_MainThreadHook * g_hook = NULL;
35 36
 
36 37
 GLsizei g_width = 1024;
@@ -66,25 +67,42 @@ namespace ChuGL
66 67
             g_gmutex.release();
67 68
         }
68 69
         
  70
+        GLvertex3f setPosition(GLvertex3f p)
  71
+        {
  72
+            m_nextPosition = p;
  73
+            return p;
  74
+        }
  75
+        
  76
+        GLcolor4f setColor(GLcolor4f c)
  77
+        {
  78
+            m_nextColor = c;
  79
+            return c;
  80
+        }
  81
+        
  82
+    protected:
69 83
         GLvertex3f m_position;
70 84
         GLcolor4f m_color;
71 85
         
72 86
     private:
73 87
         list<GGen *> m_children;
  88
+        GLcolor4f m_savedColor;
74 89
         
  90
+        GLvertex3f m_nextPosition;
  91
+        GLcolor4f m_nextColor;
75 92
     };
76 93
 
77 94
     GGen::GGen()
78 95
     {
79  
-        m_position = GLvertex3f(0, 0, 0);
80  
-        m_color = GLcolor4f(0, 0, 0, 1);
  96
+        m_position = m_nextPosition = GLvertex3f(0, 0, 0);
  97
+        m_color = m_nextColor = GLcolor4f(0, 0, 0, 1);
81 98
     }
82 99
     
83 100
     GGen::~GGen() { }
84 101
     
85 102
     void GGen::update(float dt, float t)
86 103
     {
87  
-        
  104
+        m_position = m_nextPosition;
  105
+        m_color = m_nextColor;
88 106
     }
89 107
 
90 108
     void GGen::state()
@@ -92,6 +110,8 @@ namespace ChuGL
92 110
         glMatrixMode(GL_MODELVIEW);
93 111
         glPushMatrix();
94 112
         
  113
+        glGetFloatv(GL_CURRENT_COLOR, (float *) &m_savedColor);
  114
+        
95 115
         glTranslatef(m_position.x, m_position.y, m_position.z);
96 116
         glColor4f(m_color.r, m_color.g, m_color.b, m_color.a);
97 117
     }
@@ -103,7 +123,10 @@ namespace ChuGL
103 123
     
104 124
     void GGen::unstate()
105 125
     {
  126
+        glMatrixMode(GL_MODELVIEW);
106 127
         glPopMatrix();
  128
+        
  129
+        glColor4fv((const float *) &m_savedColor);
107 130
     }
108 131
     
109 132
     void GGen::system_update(float dt, float t)
@@ -138,17 +161,39 @@ namespace ChuGL
138 161
     class GGen_Line : public GGen
139 162
     {
140 163
     public:
  164
+        virtual void update(float dt, float t)
  165
+        {
  166
+            GGen::update(dt, t);
  167
+            
  168
+            m_endpoint = m_nextEndpoint;
  169
+        }
  170
+        
  171
+        virtual void state()
  172
+        {
  173
+            GGen::state();
  174
+            
  175
+            glTranslatef(m_endpoint.x, m_endpoint.y, m_endpoint.z);
  176
+        }
  177
+        
141 178
         virtual void geometry()
142 179
         {
143 180
             glBegin(GL_LINES);
144 181
             glVertex3f(0, 0, 0);
145  
-            glVertex3f(m_endpoint.x-m_position.x,
146  
-                       m_endpoint.y-m_position.y,
147  
-                       m_endpoint.z-m_position.z);
  182
+            glVertex3f(m_position.x-m_endpoint.x,
  183
+                       m_position.y-m_endpoint.y,
  184
+                       m_position.z-m_endpoint.z);
148 185
             glEnd();
149 186
         }
150 187
         
  188
+        GLvertex3f setEndpoint(GLvertex3f e)
  189
+        {
  190
+            m_nextEndpoint = e;
  191
+            return m_nextEndpoint;
  192
+        }
  193
+        
  194
+    protected:
151 195
         GLvertex3f m_endpoint;
  196
+        GLvertex3f m_nextEndpoint;
152 197
     };
153 198
 }
154 199
 
@@ -463,6 +508,8 @@ CK_DLL_CHUCK(ggen_chuck)
463 508
     ChuGL::GGen * to = (ChuGL::GGen *) OBJ_MEMBER_INT(SELF, ggen_data_offset);
464 509
     ChuGL::GGen * from = (ChuGL::GGen *) OBJ_MEMBER_INT(FROM, ggen_data_offset);
465 510
     
  511
+    FROM->add_ref();
  512
+    
466 513
     to->add(from);
467 514
     
468 515
     return TRUE;
@@ -477,7 +524,7 @@ CK_DLL_MFUN(ggen_position)
477 524
     float y = GET_NEXT_FLOAT(ARGS);
478 525
     float z = GET_NEXT_FLOAT(ARGS);
479 526
     
480  
-    gg->m_position = GLvertex3f(x, y, z);
  527
+    gg->setPosition(GLvertex3f(x, y, z));
481 528
 }
482 529
 
483 530
 
@@ -490,7 +537,7 @@ CK_DLL_MFUN(ggen_color)
490 537
     float b = GET_NEXT_FLOAT(ARGS);
491 538
     float a = GET_NEXT_FLOAT(ARGS);
492 539
     
493  
-    gg->m_color = GLcolor4f(r, g, b, a);
  540
+    gg->setColor(GLcolor4f(r, g, b, a));
494 541
 }
495 542
 
496 543
 
@@ -528,7 +575,7 @@ CK_DLL_MFUN(gline_endpoint)
528 575
     float y = GET_NEXT_FLOAT(ARGS);
529 576
     float z = GET_NEXT_FLOAT(ARGS);
530 577
     
531  
-    gl->m_endpoint = GLvertex3f(x, y, z);
  578
+    gl->setEndpoint(GLvertex3f(x, y, z));
532 579
 }
533 580
 
534 581
 
28  GlucK2/chugl-test.ck
... ...
@@ -0,0 +1,28 @@
  1
+
  2
+GLine g => GGen.fbd();
  3
+g.position(0, 0, 0);
  4
+g.color(1, 1, 1, 1);
  5
+
  6
+0.25 => float freq;
  7
+1 => float tick;
  8
+
  9
+while(true)
  10
+{
  11
+    now / second => float phase;
  12
+    g.endpoint(Math.sin(2*pi*phase*freq), Math.cos(2*pi*phase*freq), 0);
  13
+    
  14
+    if(phase > tick)
  15
+    {
  16
+        1 +=> tick;
  17
+        GLine c => g;
  18
+        Math.random2f(0,2*pi) => float theta;
  19
+        0.2 => float radius;
  20
+        c.position(0, 0, 0);
  21
+        c.color(1, 0.25, 0.33, 1);
  22
+        c.endpoint(radius*Math.cos(theta), radius*Math.sin(theta), 0);
  23
+    }
  24
+    
  25
+    2::ms => now;
  26
+}
  27
+
  28
+ 
20  GlucK2/def.h
... ...
@@ -0,0 +1,20 @@
  1
+/*******************************************************************************
  2
+ 
  3
+ def.h
  4
+ 
  5
+ author: Spencer Salazar - spencer@ccrma.stanford.edu
  6
+ 
  7
+ ******************************************************************************/
  8
+
  9
+
  10
+#ifndef __DEF_H__
  11
+#define __DEF_H__
  12
+
  13
+
  14
+#define SAFE_DELETE(x) do{ if(x!=0x0){ delete x; x = 0x0; } } while(0)
  15
+#define SAFE_DELETE_V(x) do{ if(x!=0x0){ delete[] x; x = 0x0; } } while(0)
  16
+
  17
+
  18
+#endif // __DEF_H__
  19
+
  20
+

0 notes on commit 061fbd1

Please sign in to comment.
Something went wrong with that request. Please try again.