home dev games gallery work
Lewpen.com»Research & Development»3D Graphics»OpenGL»Reflective Sphere

Reflective Sphere

Analytical reflections in OpenGL

/ Source / common / 001 / cylinder.cpp

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <math.h>


#include "radialobject.h"

#include "interpolate.h"

#include "vector.h"


#include "cylinder.h"




  //  this will be a pure scope function (no params, everything coming from environment.



//---- CYLINDER RADIUS MAP FUNCTIONS

DRAWCYLINDER_GETRADIUS drawCylinder_getRadius = drawCylinder_getRadius_standard;



//---- CYLINDER POINT MAP FUNCTIONS

DRAWCYLINDER_GETPOINT drawCylinder_getPoint = drawCylinder_getPoint_standard;






int drawCylinder_segmentsLong   = 15*2;
int drawCylinder_segmentsAround = 27;

double drawCylinder_radius = 1.0;
double drawCylinder_length = 10.0;





void drawCylinder()
{
  int i, j;

  double around1, around2, along1, along2;

  double x1, y1, z1;
  double x2, y2, z2;
  double x3, y3, z3;
  double x4, y4, z4;

  along1 = 0.0;
  along2 = 0.0;

  for( j=0; j<drawCylinder_segmentsLong; j++ )
  {
    along1 = along2;
    along2 = (j+1.0) / (drawCylinder_segmentsLong+0.0);

    around1 = 0.0;
    around2 = 0.0;

    for( i=0; i<drawCylinder_segmentsAround; i++ )
    {
      around1 = around2;
      around2 = (i+1.0) / (drawCylinder_segmentsAround+0.0);

      drawCylinder_getPoint( around1, along1, x1, y1, z1 );
      drawCylinder_getPoint( around1, along2, x2, y2, z2 );
      drawCylinder_getPoint( around2, along2, x3, y3, z3 );
      drawCylinder_getPoint( around2, along1, x4, y4, z4 );

      glBegin( GL_TRIANGLE_FAN );
        glTexCoord2f( 0.0, 0.0 );  glVertex3f( x1, y1, z1 );
        glTexCoord2f( 1.0, 0.0 );  glVertex3f( x2, y2, z2 );
        glTexCoord2f( 1.0, 1.0 );  glVertex3f( x3, y3, z3 );
        glTexCoord2f( 0.0, 1.0 );  glVertex3f( x4, y4, z4 );
      glEnd();
    }
  }
}



//---- CYLINDER RADIUS FUNCTIONS

double drawCylinder_getRadius_standard( double around, double along )
{
  return drawCylinder_radius;
}



double drawCylinder_getRadius_animation001( double around, double along )
{
  return drawCylinder_radius * (1.0
           + 0.03*sin( 3.1415926*2.0*(around*1.0 + along*5.0 + time*0.4218) )
           + 0.09*sin( 3.1415926*2.0*(around*2.0 + along*3.0 + time*0.5161) )
           + 0.13*sin( 3.1415926*2.0*(around*4.0 + along*1.0 + time*0.7996) )
         );
}



void drawCylinder_getPoint_standard( double around, double along, double &x, double &y, double &z )
{
  x = drawCylinder_radius * sin( around * 2.0*3.1415926 );
  y = drawCylinder_radius * cos( around * 2.0*3.1415926 );
  z = drawCylinder_length * along;
}






//---- drawCylinder_getPoint_radiusMap

//  Uses the function specified by drawCylinder_getRadius to perturb the surface grid of the cylinder

void drawCylinder_getPoint_radiusMap( double around, double along, double &x, double &y, double &z )
{
  double r = drawCylinder_getRadius( around, along );

  x = r * sin( around * 2.0*3.1415926 );
  y = r * cos( around * 2.0*3.1415926 );
  z = drawCylinder_length * along;
}



//---- drawCylinder_getPoint_torus

//  Torus with height map

//  INPUTS:
//    around, along - proportions around and along the cylinder ( both 0.0 .. 1.0 )
//    &x, &y, &z    - references to vars to receive the 3d point

//  ENVIRONMENT:
//    drawCylinder_getRadius - the callback function to determine the radius of each point

//  Uses the function specified by drawCylinder_getRadius to perturb the surface grid of the cylinder
//  Plus wraps the cylinder into a donut shape

double drawCylinder_getPoint_torus_majorRadius = 3.0;

void drawCylinder_getPoint_torus( double around, double along, double &x, double &y, double &z )
{
  double r = drawCylinder_getRadius( around, along );

  //  (tx,y) is the position before the rotation
  double tx = drawCylinder_getPoint_torus_majorRadius + r * sin( around * 2.0*3.1415926 );
  y = r * cos( around * 2.0*3.1415926 );

  //  Rotate in a full circle over along = 0.0 .. 1.0
  x = cos( along * 2.0*3.1415926 ) * tx;
  z = sin( along * 2.0*3.1415926 ) * tx;
}
Related Articles

Procedural city block layout

Animated 4-dimensional objects mapped down to 3D

Simulating a cloth falling and slipping over objects

Windows C++ OpenGL example

Games

The Dodge Game
Flatspace

2-Player Games:

Quake 2D
Meteora

Puzzle Games:

Mini Tetris
Sudoku Solver

Development

3D Graphics:

3D Graphics Articles
WebGL Examples
OpenGL Examples
Flash 3D Engine
Java 3D Engine

Development:

Programming Articles
Game Development Examples

Work

Portfolio
Clients
Startups & Projects
Expertise

Links

CubeLogix Studios
PHP Charts & Graphs
Local Legends Football