Skip to content

Instantly share code, notes, and snippets.

@marty1885
Created March 15, 2016 14:33
Show Gist options
  • Save marty1885/6b68690b6cb16e59f58b to your computer and use it in GitHub Desktop.
Save marty1885/6b68690b6cb16e59f58b to your computer and use it in GitHub Desktop.
#if defined(_WIN32) || defined(_WIN64) || defined(__WIN32__) \
|| defined(__TOS_WIN__) || defined(__WINDOWS__)
/* Compiling for Windows */
#ifndef __WINDOWS__
#define __WINDOWS__
#endif
#include <windows.h>
#endif/* Predefined Windows macros */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <Window.h>
#include <quaternion.h>
#include <Incarnate.h>
#include <acclerator.h>
using namespace Incarnate;
#include <iostream>
using namespace std;
Quatf Rotor = Quatf(1.0);
Camera cam;
Scene scene;
class MainWindow : public SDLWindow
{
public:
MainWindow(int sampleNum, int bounceDepth);
void mouseDownEvent(Window::Button button);
void resizeEvent(int width, int height);
void keyDownEvent(Window::Key key);
void terminate();
protected:
void initScene();
Renderer* raytracer;
Acclerator* acclerator;
};
void MainWindow::initScene()
{
float4 bottomWallVert[] = {float4(5.0f,-5.0f,-5.0f,0.0f),float4(-5.0f,-5.0f,-5.0f,0.0f),
float4(-5.0f,-5.0f,5.0f,0.0f),float4(5.0f,-5.0f,5.0f,0.0f)};
float4 emit[] = {float4(0,0,0,0),float4(0,0,0,0),float4(0,0,0,0),float4(0,0,0,0)};
float4 colorWhite []= {float4(0.75,0.75,0.75,0),float4(0.75,0.75,0.75,0),float4(0.75,0.75,0.75,0),float4(0.75,0.75,0.75,0)};
int bottomWall[] = {0,1,2,2,3,0};
scene.addMesh(bottomWallVert,colorWhite,emit,4,bottomWall,6);
float4 leftWallVert[] = {float4(-5.0f,-5.0f,-5.0f,0.0f),float4(-5.0f,-5.0f,5.0f,0.0f),
float4(-5.0f,5.0f,5.0f,0.0f),float4(-5.0f,5.0f,-5.0f,0.0f)};
float4 colorRed[] = {float4(0.75,0.25,0.25,0),float4(0.75,0.25,0.25,0),float4(0.75,0.25,0.25,0),float4(0.75,0.25,0.25,0)};
int leftWall[] = {0,1,2,2,3,0};
scene.addMesh(leftWallVert,colorRed,emit,4,leftWall,6);
float4 rightWallVert[] = {float4(5.0f,-5.0f,-5.0f,0.0f),float4(5.0f,-5.0f,5.0f,0.0f),
float4(5.0f,5.0f,5.0f,0.0f),float4(5.0f,5.0f,-5.0f,0.0f)};
float4 colorGreen[] = {float4(0.25,0.25,0.75,0),float4(0.25,0.25,0.75,0),float4(0.25,0.25,0.75,0),float4(0.25,0.25,0.75,0)};
int rightWall[] = {0,1,2,2,3,0};
scene.addMesh(rightWallVert,colorGreen,emit,4,rightWall,6);
float4 backWallVert[] = {float4(-5.0f,-5.0f,-5.0f,0.0f),float4(5.0f,-5.0f,-5.0f,0.0f),float4(5.0f,5.0f,-5.0f,0.0f),float4(-5.0f,5.0f,-5.0f,0.0f)};
int backWall[] = {0,1,2,2,3,0};
scene.addMesh(backWallVert,colorWhite,emit,4,backWall,6);
float4 topWallVert[] = {float4(5.0f,5.0f,-5.0f,0.0f),float4(-5.0f,5.0f,-5.0f,0.0f),
float4(-5.0f,5.0f,5.0f,0.0f),float4(5.0f,5.0f,5.0f,0.0f)};
int topWall[] = {0,1,2,2,3,0};
scene.addMesh(topWallVert,colorWhite,emit,4,topWall,6);
float emitScaler = 9.0f;
float4 lightCoord[] = {float4(-2.0f,4.99f,-2.0f,0.0f),float4(-2.0f,4.99f,2.0f,0.0f),float4(2.0f,4.99f,2.0f,0.0f),float4(2.0f,4.99f,-2.0f,0.0f)};
int lightIndex[] = {0,1,2,2,3,0};
float4 lightReflect[] = {float4(0.0f,0.0f,0.0f,0.0f),float4(0.0f,0.0f,0.0f,0.0f),float4(0.0f,0.0f,0.0f,0.0f),float4(0.0f,0.0f,0.0f,0.0f)};
float4 lightEmit[] = {float4(1.0f,1.0f,1.0f,1.0f)*emitScaler,float4(1.0f,1.0f,1.0f,1.0f)*emitScaler,
float4(1.0f,1.0f,1.0f,1.0f)*emitScaler,float4(1.0f,1.0f,1.0f,1.0f)*emitScaler};
scene.addMesh(lightCoord,lightReflect,lightEmit,4,lightIndex,6);
}
void MainWindow::mouseDownEvent(Window::Button button)
{
int width = getWidth(), height = getHeight();
int u0 = width/2, v0 = height/2;
// Rotate
if (button == Window::Button::Button_Right)
{
// Get mouse position
int x,y;
getCursorPos(&x, &y);
double u = x, v = y;
// Compute new orientation
Quatf rotateVector = (u-u0) * Quatf(cam.up) + (v-v0) * Quatf(cam.right);
Quatf rotor = exp(2e-4 * rotateVector);
cam.direction = rotor * cam.direction * ~rotor;
cam.right = normalize(cross(cam.direction, cam.up));
//cout << cam.right << "\n";
//cout << cam.up << "\n";
//cout << cam.direction << "\n";
}
scene.setCamera(cam);
raytracer->render(&scene);
generateRenderTexture(raytracer->getBuffer(),width,height);
}
void MainWindow::resizeEvent(int width, int height)
{
raytracer->createBuffer(width,height);
cam.setSize(width,height);
scene.setCamera(cam);
raytracer->render(&scene);
generateRenderTexture(raytracer->getBuffer(),width,height);
}
void MainWindow::keyDownEvent(Window::Key key)
{
if (key == Window::Key_Escape)
exit(0);
else if(key == Window::Key_A)
cam.position += cam.direction*0.1f;
else if(key == Window::Key_Z)
cam.position -= cam.direction*0.1f;
else if(key == Window::Key_Right)
cam.position += cam.right*0.1f;
else if(key == Window::Key_Left)
cam.position -= cam.right*0.1f;
else if(key == Window::Key_Up)
cam.position += cam.up*0.1f;
else if(key == Window::Key_Down)
cam.position -= cam.up*0.1f;
scene.setCamera(cam);
raytracer->render(&scene);
int width = getWidth(), height = getHeight();
generateRenderTexture(raytracer->getBuffer(),width,height);
}
MainWindow::MainWindow(int sampleNum, int bounceDepth)
{
createWindow("SimpleRay 5 CPU - cornellBox",1200,800);
// setup camera and scene
cam.lookAt(float4(2.0f,2.0f,10.0f,0.0f),float4(0.0f,0.0f,0.0f,0.0f),float4(0.0f,1.0f,0.0f,0.0f));
cam.setSize(1200,800);
scene.setCamera(cam);
initScene();
// init Raytracer
raytracer = new Renderer;
acclerator = new BVHAcclerator;
if(raytracer->init() == 0)
{
raytracer->terminate();
return ;
}
raytracer->setAcclerator(acclerator);
raytracer->createBuffer(1200,800);
// Raytrace
raytracer->setSampleNum(sampleNum);
raytracer->setBounceDepth(bounceDepth);
raytracer->render(&scene);
// Display
generateRenderTexture(raytracer->getBuffer(),1200,800);
}
void MainWindow::terminate()
{
raytracer->terminate();
delete raytracer;
WindowBase::terminate();
}
int main(int argc, char* argv[])
{
// get arguments
// TODO:
// not a very smart argument parser. FIX ME
int sampleNum(0);
int bounceDepth(0);
if (argc < 5)
{
std::cout << "Usage: ./cornellBox -s <sample_num_int> -b <bounce_depth_int>\n";
exit(0);
} else {
for (int i=1; i<argc; i++)
{
if ( i+1 != argc)
{
if (strcmp(argv[i], "-s") == 0)
{
sampleNum = std::stoi(argv[i+1]);
}
if (strcmp(argv[i], "-b") == 0)
{
bounceDepth = std::stoi(argv[i+1]);
}
}
}
}
// init Window
MainWindow* displayWindow = new MainWindow(sampleNum, bounceDepth);
displayWindow->loop();
// Terminate
displayWindow->terminate();
delete displayWindow;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment