[CGSG] SVN Commit r423 - user/max/vis
max at metalab.at
max at metalab.at
Sun Apr 20 05:20:12 CEST 2008
Author: max
Date: 2008-04-20 05:20:12 +0200 (Sun, 20 Apr 2008)
New Revision: 423
Modified:
user/max/vis/spirograph.cpp
Log:
implmented class spirograph
needs containerclass holding all curves
needs libconfig
bug in drawing-function?
Modified: user/max/vis/spirograph.cpp
===================================================================
--- user/max/vis/spirograph.cpp 2008-04-20 02:45:36 UTC (rev 422)
+++ user/max/vis/spirograph.cpp 2008-04-20 03:20:12 UTC (rev 423)
@@ -19,15 +19,16 @@
// spirograph values
GLfloat angle1 = 0.0;
-GLfloat angleVelocity1 = 0.3;
-//GLfloat angleVelocity1 = 1;
-GLfloat distance1 = 13;
+GLfloat angleVelocity1 = 0.18;
+GLfloat distance1 = 8;
+
GLfloat angle2 = 0.0;
-GLfloat angleVelocity2 = 0.17;
-GLfloat distance2 = 11;
+GLfloat angleVelocity2 = 0.42;
+GLfloat distance2 = 12;
+
GLfloat angle3 = 0.0;
-GLfloat angleVelocity3 = 0.23;
-GLfloat distance3 = 7;
+GLfloat angleVelocity3 = 0.86;
+GLfloat distance3 = 5;
const int PATH_LENGTH = 5000;
GLfloat path[PATH_LENGTH*3];
@@ -40,6 +41,153 @@
//GLUint vboId;
//glGenBuffersARB(1,&vboId);
+class spirograph {
+ GLfloat a1,a2,a3,av1,av2,av3,d1,d2,d3;
+ GLfloat path[PATH_LENGTH*4];
+ GLfloat pathBuffer1[16], pathBuffer2[16], pathBuffer3[16];
+ int pathIndex;
+
+ public:
+ spirograph(GLfloat, GLfloat, GLfloat, GLfloat, GLfloat, GLfloat);
+ ~spirograph();
+ void draw();
+ private:
+ void drawPath();
+ void drawPathPart(GLfloat, GLfloat, GLfloat);
+ GLfloat drawPathPartYModifier(GLfloat, GLfloat, GLfloat );
+};
+
+spirograph::spirograph(GLfloat av1, GLfloat d1, GLfloat av2, GLfloat d2, GLfloat av3, GLfloat d3){
+ this->a1 = 0;
+ this->av1 = av1;
+ this->d1 = d1;
+ this->a2 = 0;
+ this->av2 = av2;
+ this->d2 = d2;
+ this->a3 = 0;
+ this->av3 = av3;
+ this->d3 = d3;
+
+ pathIndex = 0;
+ // TODO prefill path
+
+}
+
+spirograph::~spirograph(){}
+
+inline GLfloat spirograph::drawPathPartYModifier(GLfloat myY, GLfloat sourceFrame, GLfloat loopPos){
+// return (myY+sin(sourceFrame)*cos(loopPos))*(PATH_LENGTH-loopPos)/PATH_LENGTH;
+ return myY+myY*sin(frameCounter/100.0)*((PATH_LENGTH-loopPos)/PATH_LENGTH);
+}
+
+void spirograph::drawPathPart(GLfloat dX, GLfloat dY, GLfloat dZ){
+ glBegin(GL_LINE_STRIP);
+ for(int i = 0; i<PATH_LENGTH; i++){
+ int transPos = (i+pathIndex+1)%PATH_LENGTH;
+ glVertex3f(
+ path[transPos*4]-dX,
+ drawPathPartYModifier(path[transPos*4+1],path[transPos*4+3],transPos)-dY,
+ path[transPos*4+2]-dZ
+ );
+ }
+ glEnd();
+
+ glBegin(GL_LINE_STRIP);
+ for(int i = 0; i<PATH_LENGTH; i++){
+ int transPos = (i+pathIndex+1)%PATH_LENGTH;
+ glVertex3f(
+ path[transPos*4]+dX,
+ drawPathPartYModifier(path[transPos*4+1],path[transPos*4+3],transPos)+dY,
+ path[transPos*4+2]+dZ
+ );
+ }
+ glEnd();
+
+}
+
+void spirograph::drawPath(){
+ glPushMatrix();
+ glLoadIdentity();
+ glColor4f(0,0,10,1);
+ drawPathPart(0,0,0);
+
+ glColor4f(0.1,0.1,0.9,0.5);
+ drawPathPart(0.0,0.0,0.025);
+ drawPathPart(0.0,0.025,0.0);
+
+ glColor4f(0.5,0.5,0.6,0.5);
+ drawPathPart(0.0,0.0,0.05);
+ drawPathPart(0.0,0.05,0.0);
+
+ glColor4f(0.8,0.8,0.2,0.1);
+ drawPathPart(0.0,0.0,0.075);
+ drawPathPart(0.0,0.075,0.0);
+
+ glPopMatrix();
+
+}
+
+void spirograph::draw()
+{
+ // get new position
+ glPushMatrix();
+ glRotatef(a1,0,1,0);
+ glTranslatef(d1,0,0);
+ glGetFloatv(GL_MODELVIEW_MATRIX, pathBuffer1);
+
+ glPushMatrix();
+ glRotatef(a2,1,0,0);
+ glTranslatef(0,0,d2);
+ glGetFloatv(GL_MODELVIEW_MATRIX, pathBuffer2);
+
+ glPushMatrix();
+ glRotatef(a3,0,0,1);
+ glTranslatef(d3,0,0);
+ glGetFloatv(GL_MODELVIEW_MATRIX, pathBuffer3);
+
+ // put position into path
+ path[pathIndex*4] = pathBuffer3[12];
+ path[pathIndex*4+1] = pathBuffer3[13];
+ path[pathIndex*4+2] = pathBuffer3[14];
+ path[pathIndex*4+3] = (float)frameCounter;
+
+ // draw path
+ if(frameCounter > PATH_LENGTH){
+ drawPath();
+ }else{
+ glLoadIdentity();
+ }
+
+// glLoadIdentity();
+
+ // draw arm
+ glBegin(GL_LINES);
+ glVertex3f(pathBuffer3[12],pathBuffer3[13],pathBuffer3[14]);
+ glPopMatrix();
+ glVertex3f(pathBuffer2[12],pathBuffer2[13],pathBuffer2[14]);
+ glEnd();
+
+ glBegin(GL_LINES);
+ glVertex3f(pathBuffer2[12],pathBuffer2[13],pathBuffer2[14]);
+ glPopMatrix();
+ glVertex3f(pathBuffer1[12],pathBuffer1[13],pathBuffer1[14]);
+ glEnd();
+
+ glBegin(GL_LINES);
+ glVertex3f(pathBuffer1[12],pathBuffer1[13],pathBuffer1[14]);
+ glPopMatrix();
+ glVertex3f(0,0,0);
+ glEnd();
+
+ a1 = fmod((a1+av1),360);
+ a2 = fmod((a2+av2),360);
+ a3 = fmod((a3+av3),360);
+
+ pathIndex = (++pathIndex)%PATH_LENGTH;
+
+// glPopMatrix();
+}
+
void drawAxes()
{
// material vom planeten
@@ -61,7 +209,7 @@
{
// return sin((myY+(float)frameCounter/10)/10)*cos((float)frameCounter/100);
return myY+(myY*sin(frameCounter/100.0f)*(PATH_LENGTH-loopPos)/PATH_LENGTH);
- return myY;
+// return myY;
}
void drawPathPart(GLfloat dX, GLfloat dY, GLfloat dZ)
@@ -92,17 +240,17 @@
glColor4f(0.1,0.9,0.1,0.5);
drawPathPart(0,0,0.025);
drawPathPart(0,0.025,0);
- drawPathPart(0.025,0,0);
+// drawPathPart(0.025,0,0);
glColor4f(0.5,0.6,0.5,0.3);
drawPathPart(0,0,0.05);
drawPathPart(0,0.05,0);
- drawPathPart(0.05,0,0);
+// drawPathPart(0.05,0,0);
glColor4f(0.8,0.2,0.8,0.1);
drawPathPart(0,0,0.075);
drawPathPart(0,0.075,0);
- drawPathPart(0.075,0,0);
+// drawPathPart(0.075,0,0);
glPopMatrix();
@@ -142,7 +290,7 @@
fcStart = SDL_GetTicks();
}
-void myDisplay()
+void myDisplay(spirograph &curve1)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
@@ -151,7 +299,9 @@
glColor4f(0,1.0,0,1.0);
// drawAxes();
-
+
+ curve1.draw();
+
// move to new position
glPushMatrix();
glRotatef(angle1,0,1,0);
@@ -175,6 +325,7 @@
if(frameCounter > PATH_LENGTH){
drawPath();
}else{
+ // ...??? but it works
glLoadIdentity();
}
@@ -219,8 +370,8 @@
int main(int argc, char** argv)
{
- int width = 1300;
- int height = 800;
+ int width = 1400;
+ int height = 1050;
// initialize SDL
if( SDL_Init(SDL_INIT_VIDEO)<0 ){
@@ -228,8 +379,8 @@
return -1;
}
-// if( !SDL_SetVideoMode(width, height, 32, SDL_OPENGL|SDL_FULLSCREEN) ){
- if( !SDL_SetVideoMode(width, height, 32, SDL_OPENGL) ){
+ if( !SDL_SetVideoMode(width, height, 32, SDL_OPENGL|SDL_FULLSCREEN) ){
+// if( !SDL_SetVideoMode(width, height, 32, SDL_OPENGL) ){
fprintf(stderr, "Unable to set SDL video mode: %s\n", SDL_GetError());
SDL_Quit();
return -1;
@@ -240,7 +391,9 @@
// initialize opengl
myInit(width,height);
-
+
+ spirograph curve1(0.05,10,0.25,8,0.32,5);
+
// #####################
// main application loop
// #####################
@@ -250,7 +403,7 @@
//if (frameCounter>5000){done=true;}
- myDisplay();
+ myDisplay(curve1);
SDL_Event event;
More information about the CGSG
mailing list