#include "im.h" #include "im_format_avi.h" #include "ChessSimulation.h" #include "ChessVideo.h" #include "filters.h" #include #include #include using namespace std; ImageTexture texture01("wood01.bmp", (real)0.8, (real)0.8, (real)0.1); ImageTexture texture02("wood02.bmp", (real)0.8, (real)0.8, (real)0.1); Material whiteMaterial(240, 230, 230, (real)1.0, (real)0.8, (real)0.2); Material blackMaterial(80, 60, 60, (real)1.0, (real)0.8, (real)0.3); Material angelMaterial(210, 210, 250, (real)1.0, (real)0.3, (real)0.2); Vector3DW xe(1, 0, 0); Vector3DW ye(0, 1, 0); Vector3DW ze(0, 0, 1); vector Pawns; Knight wknight01(Vector3DW(-14, 10, (real)0.1), xe, ye, ze, &whiteMaterial); Knight wknight02(Vector3DW(-14, -10, (real)0.1), xe, ye, ze, &whiteMaterial); Bishop wbishop01(Vector3DW(-14, 6, (real)0.1), xe, ye, ze, &whiteMaterial); Bishop wbishop02(Vector3DW(-14, -6, (real)0.1), xe, ye, ze, &whiteMaterial); Rook wrook01(Vector3DW(-14, 14, (real)0.1), xe, ye, ze, &whiteMaterial); Rook wrook02(Vector3DW(-14, -14, (real)0.1), xe, ye, ze, &whiteMaterial); Queen wqueen01(Vector3DW(-14, 2, (real)0.1), xe, ye, ze, &whiteMaterial); King wking01(Vector3DW(-14, -2, (real)0.1), xe, ye, ze, &whiteMaterial); Knight bknight01(Vector3DW(14, 10, (real)0.1), xe, ye, ze, &blackMaterial); Knight bknight02(Vector3DW(14, -10, (real)0.1), xe, ye, ze, &blackMaterial); Bishop bbishop01(Vector3DW(14, 6, (real)0.1), xe, ye, ze, &blackMaterial); Bishop bbishop02(Vector3DW(14, -6, (real)0.1), xe, ye, ze, &blackMaterial); Rook brook01(Vector3DW(14, 14, (real)0.1), xe, ye, ze, &blackMaterial); Rook brook02(Vector3DW(14, -14, (real)0.1), xe, ye, ze, &blackMaterial); Queen bqueen01(Vector3DW(14, 2, (real)0.1), xe, ye, ze, &blackMaterial); King bking01(Vector3DW(14, -2, (real)0.1), xe, ye, ze, &blackMaterial); ChessBoard board(Vector3DW(0, 0, 0), Vector3DW(1, 0, 0), Vector3DW(0, 1, 0), Vector3DW(0, 0, 1), (real)2.0, (real)2.0, (real)0.2, &texture01, &texture02); vector moves; Color backColor(0, 0, 0); Light ambientLight((real)0.3, (real)0.3, (real)0.3); Scene ChessVideoScene(backColor, ambientLight); real gradeCamera01 = 0; real gradeCamera02 = 0; real gradeCamera03 = 0; Camera camera01(&ChessVideoScene, Vector3DW(0, 0, 1), Vector3DW(0, 0, 0), Vector3DW(0, 0, 1), (real)(M_PI/6.0), (real)2.0, (real)20.0); Camera camera02(&ChessVideoScene, Vector3DW(0, 0, 1), Vector3DW(0, 0, 0), Vector3DW(0, 0, 1), (real)(M_PI/3.0), (real)2.0, (real)20.0); Camera camera03(&ChessVideoScene, Vector3DW(0, 0, 1), Vector3DW(0, 0, 0), Vector3DW(0, 0, 1), (real)(M_PI/4.0), (real)2.0, (real)20.0); float frames_per_second = 30; void clearBoard() { wknight01.setPosition(Vector3DW(-14, 10, (real)0.1)); wknight02.setPosition(Vector3DW(-14, -10, (real)0.1)); wbishop01.setPosition(Vector3DW(-14, 6, (real)0.1)); wbishop02.setPosition(Vector3DW(-14, -6, (real)0.1)); wrook01.setPosition(Vector3DW(-14, 14, (real)0.1)); wrook02.setPosition(Vector3DW(-14, -14, (real)0.1)); wqueen01.setPosition(Vector3DW(-14, 2, (real)0.1)); wking01.setPosition(Vector3DW(-14, -2, (real)0.1)); bknight01.setPosition(Vector3DW(14, 10, (real)0.1)); bknight02.setPosition(Vector3DW(14, -10, (real)0.1)); bbishop01.setPosition(Vector3DW(14, 6, (real)0.1)); bbishop02.setPosition(Vector3DW(14, -6, (real)0.1)); brook01.setPosition(Vector3DW(14, 14, (real)0.1)); brook02.setPosition(Vector3DW(14, -14, (real)0.1)); bqueen01.setPosition(Vector3DW(14, 2, (real)0.1)); bking01.setPosition(Vector3DW(14, -2, (real)0.1)); } void clearCameras() { camera01.setEye(Vector3DW(0, 0, 1)); camera01.setAt(Vector3DW(0, 0, 0)); camera02.setEye(Vector3DW(0, 0, 1)); camera02.setAt(Vector3DW(0, 0, 0)); camera03.setEye(Vector3DW(0, 0, 1)); camera03.setAt(Vector3DW(0, 0, 0)); } void squareLocation(string square, real& x, real&y, real& z) { if(square=="") { x = 0; y = 0; z = 100; return; } x = 0; y = 0; z = 0.5; switch(square[0]) { case 'a': y = (real)14; break; case 'b': y = (real)10; break; case 'c': y = (real)6; break; case 'd': y = (real)2; break; case 'e': y = (real)-2; break; case 'f': y = (real)-6; break; case 'g': y = (real)-10; break; case 'h': y = (real)-14; break; } switch(square[1]) { case '1': x = (real)-14; break; case '2': x = (real)-10; break; case '3': x = (real)-6; break; case '4': x = (real)-2; break; case '5': x = (real)2; break; case '6': x = (real)6; break; case '7': x = (real)10; break; case '8': x = (real)14; break; } } void readGame(vector& moves) { /* string squares[120] = {"d2", "d4", "g8", "f6", "g1", "f3", "d7", "d5", "c2", "c4", "e7", "e6", "b1", "c3", "f8", "e7", "e2", "e3", "e8", "g8", "h8", "f8", "d1", "c2", "b8", "d7", "c4", "d5", "e6", "d5", "f1", "d3", "c7", "c5", "d4", "c5", "d7", "c5", "e1", "g1", "h1", "f1", "c5", "d3", "c2", "d3", "c8", "g4", "f3", "e5", "g4", "h5", "c1", "d2", "f8", "e8", "f2", "f4", "e7", "c5", "c3", "a4", "c5", "d6", "e5", "f3", "d8", "d7", "a4", "c3", "h5", "g6", "d3", "b5", "d7", "e7", "c3", "d5", "f6", "d5", "b5", "d5", "a8", "d8", "d5", "b3", "d6", "c5", "f1", "e1", "d8", "d3", "b3", "d1", "g6", "h5", "d1", "e2", "e7", "d6", "a1", "d1", "h5", "f3", "g2", "f3", "e8", "d8", "b2", "b4", "c5", "b6", "d1", "c1", "d3", "d2", "e2", "c4", "d6", "g6"}; */ string squares[14] = {"e2", "e4", "e7", "e5", "f1", "c4", "f8", "c5", "d1", "h5", "d7", "d6", "h5", "f7"}; /* string squares[16] = {"e2", "e4", "e7", "e5", "d2", "d4", "e5", "d4", "c2", "c3", "d4", "c3", "f1", "c4", "c3", "b2"}; */ moves.clear(); for(int i=0; i<14; i++) moves.push_back(squares[i]); } void generateImage(real ex, real ey, real ez, real ax, real ay, real az, real distEyes, real redFactor, real greenFactor, real blueFactor, int width, int height, bool makeVideo ) { if(width<=0) return; if(height<=0) return; Image image[5]; for(int i=0; i<5; i++) image[i] = imgCreate(width, height); for(i=0; i<8; i++) { Pawns.push_back(new Pawn(Vector3DW(10, -14+i*4, (real)0.1), xe, ye, ze, &blackMaterial)); Pawns.push_back(new Pawn(Vector3DW(-10, -14+i*4, (real)0.1), xe, ye, ze, &whiteMaterial)); } clearBoard(); clearCameras(); ChessVideoScene.clear(); readGame(moves); Light light01(Vector3DW(-10, 20, 30), (real)0.8, (real)0.8, (real)0.8); Light light02(Vector3DW(10, -20, 20), (real)0.8, (real)0.8, (real)0.8); ChessVideoScene.addLight(&light01); ChessVideoScene.addLight(&light02); Vector3DW eye(ex, ey, ez); Vector3DW at(ax, ay, az); Vector3DW up(0, 0, 1); real nearp = 2; real farp = 20; real fovy = (real)(3.1416/6.0); Camera camera(&ChessVideoScene, eye, at, up, fovy, nearp, farp); StereoscopicCamera stereoscopicCamera(&ChessVideoScene, eye, distEyes, at, up, fovy, nearp, farp); stereoscopicCamera.setColorFactors(redFactor, greenFactor, blueFactor); ChessSimulation simulation; if(!makeVideo) { for(i=0; i<60; i++) simulation.Tick(); // camera.rayTrace(imgGetBuffer(image[0]), width, height); stereoscopicCamera.rayTrace(imgGetBuffer(image[0]), width, height); // gaussiano1(imgGetBuffer(image[0]), imgGetBuffer(image[1]), width, height); // camera.zBuffer(imgGetBuffer(image[0]), width, height); writeRGBtoBMP("chess_image.bmp", width, height, imgGetBuffer(image[0])); } else { imFormatRegisterAVI(); int error; imFile* ifile = imFileNew("chess_video.avi", "AVI", &error); imFileSetInfo(ifile, "CUSTOM"); imFileSetAttribute(ifile, "FPS", IM_FLOAT, 1, &frames_per_second); int i=0; real sx, sy, sz; Vector3DW eaux = eye; Vector3DW aaux = at; // Camera* bestCamera = &camera01; while(true) { simulation.Tick(); if(simulation.getMessageBoard().getMessage("ENDGAME") == "Yes") break; error = imFileWriteImageInfo(ifile, width, height, IM_RGB | IM_PACKED, IM_BYTE); // sx = (real)45*(real)cos((2.0*3.1416*i)/(real)300); // sy = (real)45*(real)sin((2.0*3.1416*i)/(real)300); sx = (real)(-80 + i) * (real)0.7; sy = (real)0; sz = (real)10; eaux.setX(sx); eaux.setY(sy); eaux.setZ(sz); stereoscopicCamera.setMidEye(eaux); // aaux.setX((real)-10000 * sx); // aaux.setY((real)-10000 * sy); aaux.setX((real)10.0); aaux.setY((real)0.0); aaux.setZ((real)0.0); stereoscopicCamera.setAt(aaux); // camera.setEye(eaux); /* bestCamera = &camera01; if(gradeCamera02 > gradeCamera01) { bestCamera = &camera02; if(gradeCamera03 > gradeCamera02) bestCamera = &camera03; } else if(gradeCamera03 > gradeCamera01) bestCamera = &camera03; bestCamera->rayTrace(imgGetBuffer(image[0]), width, height); */ stereoscopicCamera.rayTrace(imgGetBuffer(image[0]), width, height); error = imFileWriteImageData(ifile, imgGetBuffer(image[0])); // printf("Frame %d saved...\n", i+1); i++; if(i>120) break; } imFileClose(ifile); } for(i=0; i