ManageScene.cpp 4.33 KB
/*This file manages the scene details, including:
	o) Transforming between world and texture coordinates
	o) Keeping track of the lights and camera
	o) Rendering the cube geometry for the volume
*/

#include "trueeyes.h"
#include "VolumeSourceStruct.h"
#include "GL/glut.h"

//camera used for rendering
//rtsCamera ViewCamera;

//list of rendering sources
vector<VolumeSource> SourceList;
int SelectedSource = 0;
int RenderSource = 0;
float StepSize = 0.01;
bool RenderSources = false;

//size of the volume
//vector3D<float> VolumeSize = vector3D<float>(1.0, 1.0, 1.0);
float gpVolSize[3] = {1.0, 1.0, 1.0};
float gpCropMin[3] = {0.0, 0.0, 0.0};
float gpCropMax[3] = {1.0, 1.0, 1.0};

void DrawCube()
{
	//This function draws the cube representing the bounds of the volume
	//A fragment shader is used to perform ray-casting

	//if there is no shader selected, exit
	if(SelectedShader < 0)
		return;

	

	//update all of the source data
	point3D<float> p;// = SourceList[RS].S.getPosition();
	//SourceList[RS].pos[0] = p.x*(1.0/gpVolSize[0])+0.5;
	//SourceList[RS].pos[1] = p.y*(1.0/gpVolSize[1])+0.5;
	//SourceList[RS].pos[2] = p.z*(1.0/gpVolSize[2])+0.5;

	for(int s=0; s<SourceList.size(); s++)
	{
		p = SourceList[s].S.getPosition();
		SourceList[s].pos[0] = p.x*(1.0/gpVolSize[0])+0.5;
		SourceList[s].pos[1] = p.y*(1.0/gpVolSize[1])+0.5;
		SourceList[s].pos[2] = p.z*(1.0/gpVolSize[2])+0.5;
	}

	vector3D<float> MinCrop(gpCropMin[0], gpCropMin[1], gpCropMin[2]);
	vector3D<float> MaxCrop(gpCropMax[0], gpCropMax[1], gpCropMax[2]);

	//compute the cube size
	vector3D<float> volS = vector3D<float>(gpVolSize[0], gpVolSize[1], gpVolSize[2]);
	vector3D<float> s = volS*0.5;

	vector3D<float> c_min = MinCrop.Times(volS) - s;
	vector3D<float> c_max = MaxCrop.Times(volS) - s;

	vector3D<float> t_min = MinCrop;
	vector3D<float> t_max = MaxCrop;

	//AttachShaderVariables();
	ShaderList[SelectedShader].glProgram.UpdateGlobalUniforms();
	ShaderList[SelectedShader].glProgram.BeginProgram();
	//glutSolidCube(1);
	glBegin(GL_QUADS);
		//Z-
		glTexCoord3f(t_min.x, t_min.y, t_min.z);
		glVertex3f(c_min.x, c_min.y, c_min.z);
		glTexCoord3f(t_min.x, t_max.y, t_min.z);
		glVertex3f(c_min.x, c_max.y, c_min.z);
		glTexCoord3f(t_max.x, t_max.y, t_min.z);
		glVertex3f(c_max.x, c_max.y, c_min.z);
		glTexCoord3f(t_max.x, t_min.y, t_min.z);
		glVertex3f(c_max.x, c_min.y, c_min.z);
		//Z+
		glTexCoord3f(t_min.x, t_min.y, t_max.z);
		glVertex3f(c_min.x, c_min.y, c_max.z);
		glTexCoord3f(t_max.x, t_min.y, t_max.z);
		glVertex3f(c_max.x, c_min.y, c_max.z);
		glTexCoord3f(t_max.x, t_max.y, t_max.z);
		glVertex3f(c_max.x, c_max.y, c_max.z);
		glTexCoord3f(t_min.x, t_max.y, t_max.z);
		glVertex3f(c_min.x, c_max.y, c_max.z);
		//X-
		glTexCoord3f(t_min.x, t_min.y, t_min.z);
		glVertex3f(c_min.x, c_min.y, c_min.z);
		glTexCoord3f(t_min.x, t_min.y, t_max.z);
		glVertex3f(c_min.x, c_min.y, c_max.z);
		glTexCoord3f(t_min.x, t_max.y, t_max.z);
		glVertex3f(c_min.x, c_max.y, c_max.z);
		glTexCoord3f(t_min.x, t_max.y, t_min.z);
		glVertex3f(c_min.x, c_max.y, c_min.z);
		//X+
		glTexCoord3f(t_max.x, t_min.y, t_min.z);
		glVertex3f(c_max.x, c_min.y, c_min.z);
		glTexCoord3f(t_max.x, t_max.y, t_min.z);
		glVertex3f(c_max.x, c_max.y, c_min.z);
		glTexCoord3f(t_max.x, t_max.y, t_max.z);
		glVertex3f(c_max.x, c_max.y, c_max.z);
		glTexCoord3f(t_max.x, t_min.y, t_max.z);
		glVertex3f(c_max.x, c_min.y, c_max.z);
		//Y-
		glTexCoord3f(t_min.x, t_min.y, t_min.z);
		glVertex3f(c_min.x, c_min.y, c_min.z);
		glTexCoord3f(t_max.x, t_min.y, t_min.z);
		glVertex3f(c_max.x, c_min.y, c_min.z);
		glTexCoord3f(t_max.x, t_min.y, t_max.z);
		glVertex3f(c_max.x, c_min.y, c_max.z);
		glTexCoord3f(t_min.x, t_min.y, t_max.z);
		glVertex3f(c_min.x, c_min.y, c_max.z);
		//Y+
		glTexCoord3f(t_min.x, t_max.y, t_min.z);
		glVertex3f(c_min.x, c_max.y, c_min.z);
		glTexCoord3f(t_min.x, t_max.y, t_max.z);
		glVertex3f(c_min.x, c_max.y, c_max.z);
		glTexCoord3f(t_max.x, t_max.y, t_max.z);
		glVertex3f(c_max.x, c_max.y, c_max.z);
		glTexCoord3f(t_max.x, t_max.y, t_min.z);
		glVertex3f(c_max.x, c_max.y, c_min.z);


	glEnd();
	ShaderList[SelectedShader].glProgram.EndProgram();
}
void DrawLights()
{
	
	glColor3f(1.0, 0.0, 0.0);
	//draw a sphere for each light
	for(int l=1; l<SourceList.size(); l++)
	{
		glPushMatrix();
		point3D<float> p = SourceList[l].S.getPosition();
		glTranslatef(p.x, p.y, p.z);
		glutSolidSphere(0.1, 10, 10);
		glPopMatrix();
	}

}