test.cpp

C++ source for Blob Function (blobs1), 196 lines. Raw file

#define WIN32_LEAN_AND_MEAN

#include <math.h>

#include	"qe_ogl.h"
#include "Pattern.h"
#include "mmsystem.h"

double r1=0, r2=0;
BOOL DrawMe = TRUE;

PATTERN_A   *a1, *a2, *a3, *a4, *a5;
PATTERN_RGB *rgb1, *rgb2, *rgb3, *rgb4, *rgb5;


//---- QUEUE

#define QUEUE_SIZE 1000

struct {
  int x, int y, int z;
} queue[QUEUE_SIZE];

int queue_start = 0;
int queue_num = 0;

void Queue_Init()
{
  queue_start = 0;
  queue_num = 0;
}

bool Queue_Push(int x, int y, int z)
{
  if(queue_num < QUEUE_SIZE)
  {
    int index = (queue_start + queue_num) % QUEUE_SIZE;
    queue[index].x = x;
    queue[index].y = y;
    queue[index].z = z;
    queue_num ++;
    return true;
  }
  return false;
}

bool queue_pop(int *x, int *y, int *z)
{
  if(queue_num > 0)
  {
    queue_num --;
    int index = (queue_start + queue_num) % QUEUE_SIZE;
    *x = queue[index].x;
    *y = queue[index].y;
    *z = queue[index].z;
    return true;
  }
  return false;
}



//---- STEPPING CUBES

double f(int x, int y, int z)
{
  double d = sqrt(x*x + y*y + z*z);
  if(d < 0.1) d = 0.1;
  d = 1.0/d;
  return d;
}

void oglcube(double x1, double y1, double z1, double x2, double y2, double z2, double r, double g, double b)
{
  double t = 7 + 2*sin(GetTickCount()*3.14159/16400);

  double dd = sqrt(x1*x1+y1*y1+z1*z1);
  double d = sin(t*dd);
//  if(d < 0) return;

  d += 1.3;

  r *= d; g *= d; b *= d;

  glColor3f(r, g, b);

  glBegin(GL_TRIANGLE_FAN);
    glVertex3f(x1, y1, z1);
    glVertex3f(x1, y1, z2);
    glVertex3f(x2, y1, z2);
    glVertex3f(x2, y1, z1);
  glEnd();

  glBegin(GL_TRIANGLE_FAN);
    glVertex3f(x2, y2, z1);
    glVertex3f(x2, y2, z2);
    glVertex3f(x1, y2, z2);
    glVertex3f(x1, y2, z1);
  glEnd();

  glColor3f(r*0.95, g*0.95, b*0.95);

  glBegin(GL_TRIANGLE_FAN);
    glVertex3f(x1, y2, z1);
    glVertex3f(x1, y2, z2);
    glVertex3f(x1, y1, z2);
    glVertex3f(x1, y1, z1);
  glEnd();

  glBegin(GL_TRIANGLE_FAN);
    glVertex3f(x2, y1, z1);
    glVertex3f(x2, y1, z2);
    glVertex3f(x2, y2, z2);
    glVertex3f(x2, y2, z1);
  glEnd();

  glColor3f(r*0.90, g*0.90, b*0.90);

  glBegin(GL_TRIANGLE_FAN);
    glVertex3f(x2, y1, z1);
    glVertex3f(x2, y2, z1);
    glVertex3f(x1, y2, z1);
    glVertex3f(x1, y1, z1);
  glEnd();

  glBegin(GL_TRIANGLE_FAN);
    glVertex3f(x1, y1, z2);
    glVertex3f(x1, y2, z2);
    glVertex3f(x2, y2, z2);
    glVertex3f(x2, y1, z2);
  glEnd();

}

double scale = 0.05;

void cube(int x, int y, int z)
{
  oglcube(x*scale, y*scale, z*scale, (x+1)*scale, (y+1)*scale, (z+1)*scale, 0.8, 0.4, 0.0);
}

void draw()
{
  int i, j, k;

  static int p = 0;
  p++;

  int w = 20;

  for(k = -w; k < w; k++)
    for(j = -w; j < w; j++)
      for(i = -w; i < w; i++)
      {
//        if( ((i+j+k+p)&31) < 1 ) cube(i, j, k);

        double t = sin(i*3.14159/17+p*3.14159/31 + 0.12451) +
                   sin(j*3.14159/16+p*3.14159/54 + 1.89214) +
                   sin(k*3.14159/15+p*3.14159/47 + 1.00298);
        if(t > 0 && t < 0.25) cube(i, j, k);
      }
}



//---- MAIN

void Initialise()
{
  Queue_Init();
}



void GameDraw()
{
  draw();
}



void GameMain()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	glTranslatef(0.f, 0.f, -3.f);

	glRotatef(-r2*0.2, 1.0, 0.0, 0.0);
	glRotatef(-r1*0.2, 0.0, 1.0, 0.0);

	GameDraw();
	SwapBuffers(gl_dc);
}