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

Reflective Sphere

Analytical reflections in OpenGL

/ Source / Pattern.cpp

#include <windows.h>
#include <math.h>

#include "Pattern.h"



//---- GLOBAL DATA -----------------------------------------------------------//

int pattern_sin[256];



//---- Pattern_Startup -------------------------------------------------------//

void Pattern_Startup()
{
  int i;

  for(i=0; i<256; i++) pattern_sin[i] = (int)(128+127*sin(i*3.1415926/128));
}



//---- Pattern_Display -------------------------------------------------------//

void Pattern_Display(HDC dc, PATTERN_RGB *rgb)
{
  BITMAPINFO bmi;

  bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  bmi.bmiHeader.biWidth = 256;
  bmi.bmiHeader.biHeight = -256;
  bmi.bmiHeader.biPlanes = 1;
  bmi.bmiHeader.biBitCount = 24;
  bmi.bmiHeader.biCompression = BI_RGB;
  bmi.bmiHeader.biSizeImage = 0;
  bmi.bmiHeader.biXPelsPerMeter = 1000;
  bmi.bmiHeader.biYPelsPerMeter = 1000;
  bmi.bmiHeader.biClrUsed = 0;
  bmi.bmiHeader.biClrImportant = 0;

  int w = 1280, h = 1024, x, y;

  for(y = 0; y < h; y += 256)
    for(x = 0; x < w; x += 256)

  StretchDIBits(
    dc,
    x, y, 256, 256,
    0, 0, 256, 256,
    (LPVOID)rgb->buf,
    (LPBITMAPINFO)&bmi,
    DIB_RGB_COLORS, SRCCOPY
  );

}



//---- Pattern_Create_A ------------------------------------------------------//

PATTERN_A *Pattern_Create_A()
{
  return (PATTERN_A *)malloc(sizeof(PATTERN_A));
}



//---- Pattern_Create_RGB ----------------------------------------------------//

PATTERN_RGB *Pattern_Create_RGB()
{
  return (PATTERN_RGB *)malloc(sizeof(PATTERN_RGB));
}



//---- Pattern_Fn_A_Plasma1 --------------------------------------------------//

void Pattern_Fn_A_Plasma1(PATTERN_A *a, int time)
{
  int i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<256; i++) {
      a->buf[j][i] = (
        pattern_sin[(time*3 + i*5 + j*4)&255] +
        pattern_sin[(time*6 + i*2 + j*5)&255] +
        pattern_sin[(time*5 + i*1 + j*3)&255] +
        pattern_sin[(time*2 + i*3 + j*1)&255]
      ) >> 2;
    }
  }

}



//---- Pattern_Fn_A_Plasma2 --------------------------------------------------//

void Pattern_Fn_A_Plasma2(PATTERN_A *a, int time)
{
  int i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<256; i++) {
      a->buf[j][i] = (
        pattern_sin[(time*4 + i*1 + j*1)&255] +
        pattern_sin[(time*3 - i*1 - j*3)&255] +
        pattern_sin[(time*1 - i*3 + j*2)&255] +
        pattern_sin[(time*2 - i*2 - j*1)&255]
      ) >> 2;
    }
  }

}



//---- Pattern_Fn_A_Plasma3 --------------------------------------------------//

void Pattern_Fn_A_Plasma3(PATTERN_A *a, int time)
{
  int i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<256; i++) {
      a->buf[j][i] = (
        pattern_sin[(time*1 - i*7 - j*3)&255] +
        pattern_sin[(time*1 - i*6 + j*6)&255] +
        pattern_sin[(time*1 + i*4 + j*3)&255] +
        pattern_sin[(time*1 + i*5 - j*2)&255]
      ) >> 2;
    }
  }

}



//---- Pattern_Fn_RGB_A ------------------------------------------------------//

void Pattern_Fn_RGB_A(PATTERN_RGB *rgb, PATTERN_A *a)
{
  int i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<256; i++) {
      rgb->buf[j][i][0] = a->buf[j][i];
      rgb->buf[j][i][1] = a->buf[j][i];
      rgb->buf[j][i][2] = a->buf[j][i];
    }
  }

}



//---- Pattern_Fn_RGB_A3 -----------------------------------------------------//

void Pattern_Fn_RGB_A3(PATTERN_RGB *rgb, PATTERN_A *a1, PATTERN_A *a2, PATTERN_A *a3)
{
  int i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<256; i++) {
      rgb->buf[j][i][0] = a1->buf[j][i];
      rgb->buf[j][i][1] = a2->buf[j][i];
      rgb->buf[j][i][2] = a3->buf[j][i];
    }
  }

}



//---- Pattern_Fn_RGB2_A2_Lookup --------------------------------------------//

void Pattern_Fn_RGB2_A2_Lookup(PATTERN_RGB *rgb1, PATTERN_RGB *rgb2, PATTERN_A *a1, PATTERN_A *a2)
{
  int i, j;
  int x, y;

  for(j=0; j<256; j++) {
    for(i=0; i<256; i++) {
      x = a1->buf[j][i];
      y = a2->buf[j][i];
      rgb1->buf[j][i][0] = rgb2->buf[y][x][0];
      rgb1->buf[j][i][1] = rgb2->buf[y][x][1];
      rgb1->buf[j][i][2] = rgb2->buf[y][x][2];
    }
  }

}



//---- Pattern_Fn_A2_A2_Lookup ----------------------------------------------//

void Pattern_Fn_A2_A2_Lookup(PATTERN_A *aa1, PATTERN_A *aa2, PATTERN_A *a1, PATTERN_A *a2)
{
  int i, j;
  int x, y;

  for(j=0; j<256; j++) {
    for(i=0; i<256; i++) {
      x = a1->buf[j][i];
      y = a2->buf[j][i];
      aa1->buf[j][i] = aa2->buf[y][x];
    }
  }

}



//---- Pattern_Fn_A_Checkers ------------------------------------------------//

void Pattern_Fn_A_Checkers(PATTERN_A *a)
{
  int i, j;
  int x, y;
  int c;

  for(j=0; j<256; j++) {
    for(i=0; i<256; i++) {
      //if((i<128 && j<128) || (i>=128 && j>=128)) c = max(i, j); else c = min(i, j);
      c = i^j;
      a->buf[j][i] = c;
    }
  }

}



//---- Pattern_Fn_RGB_Checkers ----------------------------------------------//

void Pattern_Fn_RGB_Checkers(PATTERN_RGB *rgb)
{
  int i, j;
  int x, y;
  int c;

  for(j=0; j<256; j++) {
    for(i=0; i<256; i++) {
//      c=i^j;
//      if((i<128 && j<128) || (i>=128 && j>=128)) c = 255-max(i, j); else c = min(i, j);
      if(i<128) c = j; else c = j-128;
      if(j<128) c -= i;

      rgb->buf[j][i][0] = c;
      rgb->buf[j][i][1] = c;
      rgb->buf[j][i][2] = c;
    }
  }

}



//---- END OF FILE -----------------------------------------------------------//
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