#include <Imagine/Graphics.h>
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
using namespace std;
void windows() {
cout << "Testing Window functions!" << endl;
#ifdef IMAGINE_OPENGL
#endif
#ifdef IMAGINE_OPENGL
string names[]={"2D a","2D b","3D"};
WindowType types[]={WINDOW_2D,WINDOW_2D,WINDOW_3D};
#else
string names[]={"2D a","2D b"};
#endif
cout << "Click in red tab" << endl;
cout << "Click" << endl;
#ifdef IMAGINE_OPENGL
#endif
}
void draw2D() {
cout << "Testing 2D functions!" << endl;
int x[]={100,200,170},y[]={200,250,220};
int xy[]={120,30,200,60,160,80};
for (int i=0;i<200;i+=5)
for (int i=0;i<200;i+=5)
cout << "Please click" << endl;
}
void events2() {
int w=300, h=50;
bool b = false;
do {
case EVT_NONE:
cout << "No event" << endl;
break;
case EVT_MOTION: {
if(P1.x()<=x && x<P1.x()+w && P1.y()<=y && y<P1.y()+h) {
}
else if(P2.
x()<=x && x<P2.
x()+w && P2.
y()<=y && y<P2.
y()+h) {
} else {
}
break;
}
case EVT_KEY_ON:
cout << "Use your mouse!"<< endl;
break;
case EVT_BUT_ON: {
if(P1.x()<=x && x<P1.x()+w && P1.y()<=y && y<P1.y()+h) {
cout << "Starting a new Tetris game !!" << endl;
b = true;
}
break;
}
default: break;
}
}
while ((ev.
type!=EVT_KEY_ON || ev.
key!=
'q' )&& !b);
cout << "Use your arrow keys. Press 'q' to quit." << endl;
int x=50, y=200, r=5;
int xt=x, yt=y;
while(true) {
if(k == 'q')
break;
bool update = true;
switch(k){
case KEY_UP: y-=5; break;
case KEY_DOWN: y+=5; break;
case KEY_LEFT: x-=5; break;
case KEY_RIGHT:x+=5; break;
default: update=false; break;
}
if(update) {
xt = x; yt = y;
}
}
}
void events() {
cout << "Testing event functions!" << endl;
cout << "Please click in first tab" << endl;
cout << "button clicked: " << button << endl;
cout << "click again in any tab" << endl;
cout << "button clicked: " << button << endl;
int sw;
cout << "click again in any tab" << endl;
cout << "button clicked: " << button << endl;
int x,y;
cout << "click again in first tab" << endl;
cout << "button clicked: " << button << " pos: " << x << ',' << y << endl;
cout << "click again in first tab" << endl;
cout << "button clicked: " << button << " pos: " << p << endl;
cout << "click again in any tab" << endl;
cout << "button clicked: " << button << " pos: " << x << ',' << y
<< " subWin: " << sw << endl;
cout << "click again" << endl;
cout << "button clicked: " << button << " pos: " << p
<< " subWin: " << sw << endl;
do {
cout << "Please press UP arrow in first tab" << endl;
cout << "Press key, check modifier keys let you proceed" << endl;
cout << "Press key, check modifier keys do *not* let you proceed" << endl;
cout << "Please press some key in any tab (modifier keys ignored)" << endl;
cout << "Please press some key in any tab (modifier keys OK)" << endl;
cout << "Key: " << key << " subWin: " << sw << endl;
for (int i=0;i<4;i++) {
cout << i << endl;
}
cout << "Generate events, press 'q' key done" << endl;
do {
case EVT_NONE:
cout << "No event for 500ms" << endl;
break;
case EVT_MOTION:
cout << "Motion" << endl;
break;
case EVT_KEY_ON:
cout <<
"Key " << ev.
key <<
" pressed"<< endl;
break;
case EVT_KEY_OFF:
cout <<
"Key " << ev.
key <<
" released"<< endl;
break;
case EVT_BUT_ON:
cout <<
"Button " << ev.
button <<
" pressed"<< endl;
break;
case EVT_BUT_OFF:
cout <<
"Button " << ev.
button <<
" released"<< endl;
break;
}
cout <<
" Mouse=[" << ev.
pix.
x() <<
','<<ev.
pix.
y() <<
"] ,State="<< ev.
state <<
",Win=" << ev.
win <<
",SubWin=" << ev.
subWin << endl;
}
while (ev.
type!=EVT_KEY_ON || ev.
key!=
'q');
cout << "Please click" << endl;
}
void transparency() {
cout << "Testing image transparency" << endl;
int W, H;
byte *r1, *g1, *b1;
int w,h,w1,h1;
byte *R,*G,*B, *A;
byte *r,*g,*b, *a;
cout << "Transparent image (R,G,B,A) from PNG" << endl;
cout << "Two opaque images" << endl;
cout << "Tranparent image (RGBA) from PNG" << endl;
byte *rgba;
cout << "First Mask" << endl;
cout << "Image saved" << endl;
else
cout << "Image is *not* saved -> something is wrong" << endl;
int Aw, Ah;
cout << "out.png loaded" << endl;
cout << "Transparent image saved and reloaded on black background" << endl;
delete [] AC;
cout << "2nd Mask for AlphaColor" << endl;
delete[] C;
delete[] R; delete[] G; delete[] B; delete[] A;
delete[] r; delete[] g; delete[] b; delete[] a;
byte* RGBA1;
cout << "3rd mask for RGBA" << endl;
delete[] r1; delete[] g1; delete[] b1;
delete[] rgba;
delete[] RGBA;
delete[] RGBA1;
byte *ri, *gi, *bi, *ai;
int wi, hi;
for(int i = 0; i < 50; i++)
{
for(int j=0; j<wi*hi; j++)
{
ai[j] = i;
}
}
delete[] C1; delete[] aC;
delete[] ri; delete[] gi; delete[] bi; delete[] ai;
}
void bitmaps() {
cout << "Testing bitmap functions!" << endl;
byte r[256*256],g[256*256],b[256*256];
for (int j=0;j<256;j++) {
for (int i=0;i<256;i++) {
r[i+256*j]=i;
g[i+256*j]=255-i;
b[i+256*j]=(j<128)?255:0;
}
}
for (int j=0;j<256;j++)
for (int i=0;i<256;i++)
cols[i+256*j]=
Color(i,(2*j)%256,(i+j)%256);
delete [] cols;
byte* rgb=new byte[3*256*256];
for (int j=0;j<256;j++)
for (int i=0;i<256;i++) {
rgb[3*(i+256*j)]=i/2;
rgb[3*(i+256*j)+1]=255-j/2;
rgb[3*(i+256*j)+2]=128;
}
delete [] rgb;
byte*
grey=
new byte[256*256];
for (int j=0;j<256;j++)
for (int i=0;i<256;i++)
grey[i+256*j]=byte(128+127*sin((i+j)/10.));
int w,h;
byte *R,*G,*B;
delete[]R;
delete[]G;
delete[]B;
delete[]C;
delete[]RGB;
byte *I;
delete[]I;
delete[]R;
delete[]G;
delete[]B;
delete[]C;
delete[]RGB;
cout << "Color at (10,10): " << c1 << endl;
cout << "Color at (10,10): " << c2 << endl;
byte r1[100*50],g1[100*50],b1[100*50];
byte rgb1[100*50*3];
#ifdef IMAGINE_OPENGL
cout << "Click on cone window to capture" << endl;
delete[]R;
delete[]G;
delete[]B;
delete[]C;
delete[]RGB;
#if 0
float *Z;
captureZBuffer(Z,w,h);
I=new byte[w*h];
float mn=1e10,mx=-1e10;
for (int i=0;i<w*h;i++) {
mn=min(mn,Z[i]);
mx=max(mx,Z[i]);
}
for (int i=0;i<w*h;i++)
I[i]=byte(255*(Z[i]-mn)/(mx-mn));
delete[]Z;
delete[]I;
#endif
#endif
const int sz=128;
byte r2[sz*sz],g2[sz*sz],b2[sz*sz];
for (int y=0;y<sz;y++) {
for (int x=0;x<sz;x++) {
r2[x+sz*y]=x;
g2[x+sz*y]=y;
b2[x+sz*y]=(x+y);
}
}
for (int y=0;y<64;y++)
for (int x=0;x<64;x++)
cols2[x+64*y]=
Color((x+y)%256,0,0);
byte rgb2[3*64*64];
for (int y=0;y<64;y++)
for (int x=0;x<64;x++) {
rgb2[3*(x+64*y)]=x;
rgb2[3*(x+64*y)+1]=y;
rgb2[3*(x+64*y)+2]=x+y;
}
byte grey2[64*64];
for (int y=0;y<64;y++)
for (int x=0;x<64;x++)
grey2[(x+64*y)]=x+2*y;
for (int x=0;x<sz;x++) {
}
for (int i=0;i<1024;i+=8) {
}
cout << "Please click" << endl;
#ifdef IMAGINE_OPENGL
#endif
}
#ifdef IMAGINE_OPENGL
void draw3D() {
cout << "Testing 3D functions!" << endl;
DoublePoint3 dP[8]={
DoublePoint3(-1,-1,-1),
DoublePoint3(1,-1,-1),
DoublePoint3(1,1,-1),
DoublePoint3(-1,1,-1),
DoublePoint3(-1,-1,1),
DoublePoint3(1,-1,1),
DoublePoint3(1,1,1),
DoublePoint3(-1,1,1)
};
Triangle dT[12]={
Triangle(0,2,1),
Triangle(2,0,3),
Triangle(4,5,6),
Triangle(6,7,4),
Triangle(0,1,5),
Triangle(5,4,0),
Triangle(1,2,6),
Triangle(6,5,1),
Triangle(2,3,7),
Triangle(7,6,2),
Triangle(3,0,4),
Triangle(4,7,3)
};
FloatPoint3 fP[8]={
FloatPoint3(3,-1,-1),
FloatPoint3(5,-1,-1),
FloatPoint3(5,1,-1),
FloatPoint3(3,1,-1),
FloatPoint3(3,-1,1),
FloatPoint3(5,-1,1),
FloatPoint3(5,1,1),
FloatPoint3(3,1,1)
};
Triangle fT[12]={
Triangle(0,2,1),
Triangle(2,0,3),
Triangle(4,5,6),
Triangle(6,7,4),
Triangle(0,1,5),
Triangle(5,4,0),
Triangle(1,2,6),
Triangle(6,5,1),
Triangle(2,3,7),
Triangle(7,6,2),
Triangle(3,0,4),
Triangle(4,7,3)
};
cout << "Two cubes" << endl;
cout << "Play with navigation (use SHIFT + mouse and kb)" << endl;
cout << "Then click..." << endl;
int n=10;
for (int j=0;j<n;j++)
for (int i=0;i<n;i++)
P[i+n*j]=
DoublePoint3(
double(i)/n,
double(j)/n,.2f*cos(
double(i)*6/n)*sin(
double(j)*6/n));
for (int j=0;j<n-1;j++) {
for (int i=0;i<n-1;i++) {
T[2*(i+(n-1)*j)]=
Triangle(i+n*j,i+1+n*j,i+n*(j+1));
T[2*(i+(n-1)*j)+1]=
Triangle(i+1+n*j,i+1+n*(j+1),i+n*(j+1));
}
}
S=
Mesh(P,n*n,T,2*(n-1)*(n-1),0,0,CONSTANT_COLOR,FLAT_SHADING);
cout << "Flat shading, computed normals" << endl;
const float theta=0.05f;
const float rot[3*3] = {
cos(theta), sin(theta), 0,
-sin(theta), cos(theta), 0,
0, 0, 1};
cout << "Change light direction" << endl;
while(true) {
break;
dir = rotz*dir;
}
S=
Mesh(P,n*n,T,2*(n-1)*(n-1),0,0,CONSTANT_COLOR,SMOOTH_SHADING);
cout << "Smooth shading, computed normals" << endl;
for (int j=0;j<n;j++) {
for (int i=0;i<n;i++) {
double fx=-6*.2*sin(double(i)*6/n)*sin(double(j)*6/n);
double fy=6*.2*cos(double(i)*6/n)*cos(double(j)*6/n);
}
}
S=
Mesh(P,n*n,T,2*(n-1)*(n-1),0,0,CONSTANT_COLOR,SMOOTH_SHADING,N);
cout << "Smooth shading, true normals" << endl;
S=
Mesh(P,n*n,T,2*(n-1)*(n-1),0,0,FACE_COLOR,SMOOTH_SHADING);
for (int i=0;i<2*(n-1)*(n-1);i++)
delete[] cols;
cout << "Random triangle colors" << endl;
S=
Mesh(P,n*n,T,2*(n-1)*(n-1),0,0,VERTEX_COLOR,SMOOTH_SHADING);
for (int i=0;i<n*n;i++)
delete[] cols;
cout << "Random vertex colors" << endl;
float *vals=new float[2*(n-1)*(n-1)];
S=
Mesh(P,n*n,T,2*(n-1)*(n-1),0,0,FACE_VALUE,SMOOTH_SHADING);
for (int i=0;i<2*(n-1)*(n-1);i++)
vals[i]=float(i)/(2*(n-1)*(n-1));
delete[] vals;
cout << "Face value coloring" << endl;
S=
Mesh(P,n*n,T,2*(n-1)*(n-1),0,0,VERTEX_VALUE,SMOOTH_SHADING);
vals=new float[n*n];
for (int i=0;i<n*n;i++)
vals[i]=float(i)/(n*n);
delete[] vals;
cout << "Vertex value coloring" << endl;
cout << "Vertex value coloring with custom LUT" << endl;
cout << "Vertex value coloring with default LUT" << endl;
#if 0
MeshClipping cut;
cut.clipOn();
S=VtkMesh(P,n*n,T,2*(n-1)*(n-1),CONSTANT_COLOR,PHONG_SHADING);
cut.showClippedOn();
cut.setClipOpacity(.3);
#endif
delete[] P;
delete[] T;
delete[] N;
S=
Mesh(P3,3,&T3,1,0,0,CONSTANT_COLOR);
cout << "Triangle" << endl;
cout << "Bigger triangle" << endl;
DoublePoint3 P4[]={
DoublePoint3(0,0,0),
DoublePoint3(1,0,0),
DoublePoint3(0,1,0),
DoublePoint3(1,1,1)};
cout << "Two triangles" << endl;
#if 0
int id,but;
cout << "Use shift and mouse to pick a triangle" << endl;
pickMeshCell(M,id,but,-1);
cout << "Use shift and mouse to pick a vertex" << endl;
pickMeshPoint(M,id,but,-1);
#endif
DoublePoint3 PTSCL[]={
DoublePoint3(0,0,2),
DoublePoint3(1,0,2),
DoublePoint3(0,1,2),
DoublePoint3(1,1,2),
DoublePoint3(0,0,1),
DoublePoint3(1,0,1),
DoublePoint3(0,1,1),
DoublePoint3(1,1,1)
};
Mesh Cb=
Mesh::Cube(
DoublePoint3(0,-2,0),
DoubleVector3(.1,0,0),
DoubleVector3(.1,.2,0),
DoubleVector3(0,.1,.3));
#if 0
for (int i=0;i<50;i++) {
Ar.landMarkTransform(src,dest,2,false,false,true);
}
Ar.resetPosition();
setAmbient(.2);
double zoom;
getCamera(pos,dir,up,zoom);
for (int i=0;i<50;i++) {
zoom*=1.05;
}
reInitCamera();
#endif
}
#endif
int main(int argc, char** argv) {
typedef void (*FUNC)();
std::vector< pair<string,FUNC> > tests;
#define ADD_TEST(func) \
tests.push_back(make_pair(string(#func), func));
ADD_TEST(windows);
ADD_TEST(transparency);
ADD_TEST(draw2D);
ADD_TEST(events);
ADD_TEST(bitmaps);
ADD_TEST(events2);
#ifdef IMAGINE_OPENGL
ADD_TEST(draw3D);
#endif
std::vector< pair<string,FUNC> >::const_iterator it;
if(argc<=1)
for(it=tests.begin(); it!=tests.end(); ++it) {
cout << "---" << it->first << "---" << std::endl;
it->second();
}
else {
bool fail=false;
for(int i=1; i<argc; i++) {
for(it=tests.begin(); it!=tests.end(); ++it) {
if(it->first == argv[i]) {
cout << "---" << it->first << "---" << std::endl;
it->second();
break;
}
}
if(it==tests.end()) {
std::cout << "Unknown test " << argv[i] << endl;
fail = true;
}
}
if(fail) {
cout << "--- Available tests:" << endl;
for(it=tests.begin(); it!=tests.end(); ++it)
cout << it->first << ' ';
cout << endl;
}
}
return 0;
}