#include #include #include "rtsCamera.h" #include "rtsSourceCode.h" #include "rts_glShaderProgram.h" using namespace std; #include class rts_glTrueEyes { static int glut_window; static rts_glShaderProgram shader; static string source_file; static rtsCamera camera; static int mouse_x; static int mouse_y; static float camera_pos[3]; static float volume_size[3]; static rts_glTextureMap volume; static bool zooming; static void DrawCube() { float sx = volume_size[0]/2; float sy = volume_size[1]/2; float sz = volume_size[2]/2; float tx = volume_size[0]/2; float ty = volume_size[1]/2; float tz = volume_size[2]/2; glBegin(GL_QUADS); glTexCoord3f(-tx, -ty, -tz); glVertex3f(-sx, -sy, -sz); glTexCoord3f(-tx, ty, -tz); glVertex3f(-sx, sy, -sz); glTexCoord3f(tx, ty, -tz); glVertex3f(sx, sy, -sz); glTexCoord3f(tx, -ty, -tz); glVertex3f(sx, -sy, -sz); glTexCoord3f(-tx, -ty, tz); glVertex3f(-sx, -sy, sz); glTexCoord3f(-tx, ty, tz); glVertex3f(-sx, sy, sz); glTexCoord3f(tx, ty, tz); glVertex3f(sx, sy, sz); glTexCoord3f(tx, -ty, tz); glVertex3f(sx, -sy, sz); glTexCoord3f(-tx, -ty, -tz); glVertex3f(-sx, -sy, -sz); glTexCoord3f(-tx, -ty, tz); glVertex3f(-sx, -sy, sz); glTexCoord3f(tx, -ty, tz); glVertex3f(sx, -sy, sz); glTexCoord3f(tx, -ty, -tz); glVertex3f(sx, -sy, -sz); glTexCoord3f(-tx, ty, -tz); glVertex3f(-sx, sy, -sz); glTexCoord3f(-tx, ty, tz); glVertex3f(-sx, sy, sz); glTexCoord3f(tx, ty, tz); glVertex3f(sx, sy, sz); glTexCoord3f(tx, ty, -tz); glVertex3f(sx, sy, -sz); glTexCoord3f(-tx, -ty, -tz); glVertex3f(-sx, -sy, -sz); glTexCoord3f(-tx, -ty, tz); glVertex3f(-sx, -sy, sz); glTexCoord3f(-tx, ty, tz); glVertex3f(-sx, sy, sz); glTexCoord3f(-tx, ty, -tz); glVertex3f(-sx, sy, -sz); glTexCoord3f(tx, -ty, -tz); glVertex3f(sx, -sy, -sz); glTexCoord3f(tx, -ty, tz); glVertex3f(sx, -sy, sz); glTexCoord3f(tx, ty, tz); glVertex3f(sx, sy, sz); glTexCoord3f(tx, ty, -tz); glVertex3f(sx, sy, -sz); glEnd(); } static void DisplayFunction() { //set up the viewport and projection glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0, 0, glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT)); float aspect_ratio = (float)glutGet(GLUT_WINDOW_WIDTH)/(float)glutGet(GLUT_WINDOW_HEIGHT); gluPerspective(camera.getFOV(), aspect_ratio, 0.01, 10); //clear the screen glClear(GL_COLOR_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT); //set up the camera glMatrixMode(GL_MODELVIEW); glLoadIdentity(); point3D pos = camera.getPosition(); vector3D up = camera.getUp(); gluLookAt(pos.x, pos.y, pos.z, 0, 0, 0, up.x, up.y, up.z); //draw a cube glColor3f(1.0, 0.0, 0.0); shader.UpdateGlobalUniforms(); shader.BeginProgram(); DrawCube(); shader.EndProgram(); glutSwapBuffers(); } static void IdleFunction() { glutPostRedisplay(); } static void MouseDrag(int x, int y) { int dx = x - mouse_x; int dy = y - mouse_y; if(zooming) { camera.Zoom(dy); } else { camera.OrbitFocus(-dx*0.01, dy*0.01); point3D pos = camera.getPosition(); camera_pos[0] = pos.x; camera_pos[1] = pos.y; camera_pos[2] = pos.z; } mouse_x = x; mouse_y = y; glutPostRedisplay(); } static void MouseMove(int x, int y) { mouse_x = x; mouse_y = y; } static void KeyPress(unsigned char key, int x, int y) { //reload the shader if(key == 'r' || key == 'R') { UpdateShader(); } } static void MousePress(int button, int state, int x, int y) { if(state == GLUT_DOWN && button == GLUT_MIDDLE_BUTTON) zooming = true; else zooming = false; } static void UpdateShader() { //load the source code shader.Clean(); shader.AttachShader(GL_FRAGMENT_SHADER, source_file.c_str()); //compile the shader shader.Compile(); shader.PrintLog(); shader.Link(); shader.PrintLog(); if(!shader.linked) { cout<<"Error linking shader."<