|
Small stuff
|
||||
A little something
[ 3D-fonts ]
[ Casteljau's algorithm ]
[ Torus and sphere ] 3D-fonts
This program draws the letter above. Except
some updates in the near future.
Casteljau's algorithmThis smal Java applet animates the Casteljau's algorithm with four control points, thus making up a Bezier curve. Source code here.For more information about this algorithm, see the pages of Børre Stenseth. (in Norwegian) Torus and sphereThe sphere moves inside the torus.
Abstract from the source code of display(): float x;
float y;
float z;
float R = 0.3f; //inner radius of torus
float r = 0.07f; //radius of torus (r*2 = thicknes)
float rings = 80.0f;
float sides = 12.0f;
float tmp;
double pi_2_s;
double pi_2_t;
float step_s = 1.0f / rings;
float step_t = 1.0f / sides;
gl.glLineWidth( 3.0f );
gl.glColor3f(0.2f, 0.1f, 0.7f);
gl.glBegin( GL.GL_LINE_STRIP );
for(double s = 0.0f; s < 1.0; s += step_s) //rings
{
for(double t = 0.0f; t < 1.0; t += step_t) //divsions in each ring
{
pi_2_t = PI_2 * t;
//(step_s*t) makes s spiral out of each ring
//to remove the gap between beginning and end of two nabouring rings
pi_2_s = PI_2 * (s + (step_s*t));
tmp = R + r*(float)Math.cos( pi_2_t );
x = tmp*(float)Math.cos( pi_2_s );
y = tmp*(float)Math.sin( pi_2_s );
z = r*(float)Math.sin( pi_2_t );
gl.glVertex3f( x, y, z );
}
}
gl.glEnd();
//draw shpere moving inside the torus
GLUQuadric glpQ = new GLUQuadric();
gl.glRotatef( sphereAngle, 0.0f, 0.0f, 1.0f );
gl.glTranslatef(R, 0.0f , 0.0f );
gl.glColor3f(1.0f, 1.0f, 0.1f);
glu.gluSphere(glpQ,r*0.3f,20,20);
glu.gluDeleteQuadric(glpQ);
//move the sphere
sphereAngle += 3.0f;
A watchThis is a watch my father gave some years ago. :)And of course it shows your local time.
The source code also features a moving mechanism controlled by the keyboard and mouse (not optimized yet, thought). To navigate, use these keys:
Rubik's Cube
First try
My first, none abstract, object in OpenGL.
private final double sqrt2 = Math.sqrt( 2.0 );
private final double sqrt2_div2 = sqrt2/2;
private final float sqrt2_div2f = (float)sqrt2_div2;
(...)
float r = 2.0f;
gl.glRotatef( 90.0f, 0.0f, 0.0f, 1.0f );
gl.glColor3f(1.0f, 0.0f, 0.0f);
glu.sphere( glpQ, r, 2, 2 ); //draw ground sphere
for(int i = 0; i < 4; i++)
{
gl.glPushMatrix();
gl.glRotatef( -45.0f + (float)(90*i), 1.0f, 0.0f, 0.0f ); //rotate to mid (instaed of corner)
gl.glTranslatef( 0.0f, r*sqrt2_div2f, 0.0f ); //trans to edge
gl.glRotatef( -90.0f, 0.0f, 0.0f, 1.0f ); //rotate around edge
gl.glTranslatef( 0.0f, r*sqrt2_div2f, 0.0f ); //trans to mid
gl.glRotatef( 45.0f, 1.0f, 0.0f, 0.0f ); //rotate to get square instead of diamond
gl.glColor3f(1.0f, 0.0f, 1.0f); //set color
glu.sphere( glpQ, r, 2, 2 ); //draw quadrat
gl.glPopMatrix(); //go back to ground sphere
}
|