Untitled

                Never    
C/C++
       
#include<bits/stdc++.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>


#include <windows.h>
#include <glut.h>
#include "ray_tracer.h"
#include "bitmap_image.hpp"
using namespace std;


#define Rad 20.0
#define pi (2*acos(0.0))
#define VIEW_ANGLE 80.0

///ray tracer variables starts
vector <object*> objects;
vector <vector3> lights;
///ray tracer variables closed


double a,b;

double cameraHeight;
double cameraAngle;
int drawgrid;
int drawaxes;
double angle;
///ray tracer declarations
int imageWidth=768;
double windowHeight,windowWidth;

int recur_lvl;
///


//struct point CamPos;




void drawAxes()
{
	if(drawaxes==1)
	{
		glColor3f(1.0, 0.0, 0.0);
		glBegin(GL_LINES);{
			glVertex3f( 100,0,0);
			glVertex3f(-100,0,0);

            glColor3f(0.0, 1.0, 0.0);
			glVertex3f(0,-100,0);
			glVertex3f(0, 100,0);

            glColor3f(0.0, 0.0, 1.0);
			glVertex3f(0,0, 100);
			glVertex3f(0,0,-100);
		}glEnd();
	}
}

void captureHSR(){
    bitmap_image img(imageWidth,imageWidth);
    double plane_distance,du,dv;
    double angle_rad=pi*(VIEW_ANGLE/2.0)/180.0;
    plane_distance=(windowHeight/2.0)/tan(angle_rad);

    vector3 tLeft = CamPos + L*plane_distance - R*(windowWidth/2.0) + U*(windowHeight/2.0);
    du = windowWidth/(double)imageWidth;
    dv = windowHeight/(double)imageWidth;

    for(int i=0;i<imageWidth;i++){
        for(int j=0;j<imageWidth;j++){
            int closest=-1;
            double color[3];
            double min_t = 999999999;
            vector3 cornerVal;
            cornerVal=tLeft+R*j*du -U*i*dv;
            Ray *r=new Ray(CamPos,cornerVal-CamPos);

            for(int ind=0;ind<objects.size();ind++){
                double temp = objects[ind]->intersectObj(r,color,0);
                if(temp<=0)continue;

                if(temp<min_t){
                    min_t=temp;
                    closest=ind;
                }
            }
            if(closest!=-1){///the closest obj found
                double temp = objects[closest]->intersectObj(r,color,1);
                img.set_pixel(j,i,color[0]*255.0,color[1]*255.0,color[2]*255.0);
            }

        }
    }
    cout<<"at last"<<endl;
    img.save_image("Ray_Tracer_Output.bmp");
}


void lastSixMotions(vector3 *p,vector3 *q,double angle){
    double a=cos(angle);
	double b=sin(angle);

    p->x=p->x*a+q->x*b;
    p->y=p->y*a+q->y*b;
    p->z=p->z*a+q->z*b;
    //drawgrid=1-drawgrid;
    double m= sqrt(p->x*p->x+p->y*p->y+p->z*p->z);

    p->x/=m;
    p->y/=m;
    p->z/=m;

    q->x=q->x*a-p->x*b;
    q->y=q->y*a-p->y*b;
    q->z=q->z*a-p->z*b;

    double n= sqrt(q->x*q->x+q->y*q->y+q->z*q->z);

    q->x/=n;
    q->y/=n;
    q->z/=n;
}

void keyboardListener(unsigned char key, int x,int y){
	switch(key){
	    case '0':{
            captureHSR();
	    }

		case '1':{
		    lastSixMotions(&R,&L,pi/90.0);
			break;
        }
        case '2':{
            lastSixMotions(&R,&L,-pi/90.0);
			break;
        }
        case '3':{
            lastSixMotions(&L,&U,pi/90.0);
			break;
        }

        case '4':{
                lastSixMotions(&L,&U,-pi/90.0);
                break;
        }
        case '5':{
                lastSixMotions(&R,&U,-pi/90.0);
                break;
        }

        case '6':{
                lastSixMotions(&R,&U,pi/90.0);
                break;
        }

		default:
			break;
	}
}


void specialKeyListener(int key, int x,int y){
	switch(key){
		case GLUT_KEY_DOWN:		//down arrow key
			//cameraHeight -= 3.0;
			CamPos.x+=1.0;
			CamPos.y+=1.0;
			break;
		case GLUT_KEY_UP:		// up arrow key
			//cameraHeight += 3.0;
			CamPos.x-=1.0;
			CamPos.y-=1.0;
			break;

		case GLUT_KEY_RIGHT:
			//cameraAngle += 0.03;
			CamPos.x-=1.5;
			CamPos.y+=1.5;
			break;
		case GLUT_KEY_LEFT:
			//cameraAngle -= 0.03;
			CamPos.x+=1.5;
            CamPos.y-=1.5;
			break;

		case GLUT_KEY_PAGE_UP:
		    CamPos.z+=1.5;
		    //CamPos.z+=1.0;
			break;
		case GLUT_KEY_PAGE_DOWN:
			CamPos.z-=1.5;
			//CamPos.z-=1.0;
			break;

		case GLUT_KEY_INSERT:
			break;

		case GLUT_KEY_HOME:
		    //if(side>0)side-=0.5;
			break;
		case GLUT_KEY_END:
		    //if(side<H)side+=0.5;
			break;

		default:
			break;
	}
}


void mouseListener(int button, int state, int x, int y){	//x, y is the x-y of the screen (2D)
	switch(button){
		case GLUT_LEFT_BUTTON:
			if(state == GLUT_DOWN){		// 2 times?? in ONE click? -- solution is checking DOWN or UP
				drawaxes=1-drawaxes;
			}
			break;

		case GLUT_RIGHT_BUTTON:
			//........
			break;

		case GLUT_MIDDLE_BUTTON:
			//........
			break;

		default:
			break;
	}
}

void loadTestData(){
    recur_lvl=3;
    object *temp;
    vector3 center(0,0,10);
    double radius=10.0;
    temp=new sphere(center, radius); // Center(0,0,10), Radius 10
    temp->setColor(1,0,0);
    temp->setCoEfficients(0.4,0.2,0.2,0.2);
    temp->setShine(1);
    objects.push_back(temp);
    vector3 light1(-50,50,50);
    lights.push_back(light1);

    /*object *temp1;
    temp1=new Floor(1000, 20);
    temp1->setCoEfficients(0.4,0.2,0.2,0.2);
    temp1->setShine(1);
    objects.push_back(temp1);*/

}

void display(){
	//clear the display
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glClearColor(0,0,0,0);	//color black
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	/********************
	/ set-up camera here
	********************/
	//load the correct matrix -- MODEL-VIEW matrix
	glMatrixMode(GL_MODELVIEW);
	//initialize the matrix
	glLoadIdentity();
	//now give three info
	//1. where is the camera (viewer)?
	//2. where is the camera looking?
	//3. Which direction is the camera's UP direction?
	gluLookAt(CamPos.x,CamPos.y,CamPos.z,	CamPos.x+L.x,CamPos.y+L.y,CamPos.z+L.z,	U.x,U.y,U.z);
	//again select MODEL-VIEW
	glMatrixMode(GL_MODELVIEW);
	/****************************
	/ Add your objects from here
	****************************/
	//add objects
	drawAxes();
    for(int i=0;i<objects.size();i++){
        objects[i]->draw();
    }
    for(int i=0;i<lights.size();i++){
        glBegin(GL_POINTS);{ //starts drawing of points
            glVertex3f(lights[i].x,lights[i].y,lights[i].z);//upper-right corner
        }glEnd();
    }
	//ADD this line in the end --- if you use double buffer (i.e. GL_DOUBLE)
	glutSwapBuffers();
}


void animate(){
	angle+=0.05;
	//radius=H-side;
	//codes for any changes in Models, Camera
	glutPostRedisplay();
}

void init(){
	//codes for initialization
	CamPos.x = 100;
    CamPos.y = 100;
    CamPos.z = 50;

    U.x=0;
    U.y=0;
    U.z=1.0;


    L.x = -1/(double)sqrt(2.0);
    L.y = -1/(double)sqrt(2.0);
    L.z = 0;

    R.x=-1/(double)sqrt(2.0);
    R.y=1/(double)sqrt(2.0);
    R.z=0;

	drawgrid=0;
	drawaxes=1;
	cameraHeight=150.0;
	cameraAngle=1.0;
	angle=0;
	//side=H/2.0;
	//radius=H-side;

	//clear the screen
	glClearColor(0,0,0,0);

	/************************
	/ set-up projection here
	************************/
	//load the PROJECTION matrix
	glMatrixMode(GL_PROJECTION);

	//initialize the matrix
	glLoadIdentity();

	//give PERSPECTIVE parameters
	gluPerspective(80,	1,	1,	1000.0);
	//field of view in the Y (vertically)
	//aspect ratio that determines the field of view in the X direction (horizontally)
	//near distance
	//far distance
}

int main(int argc, char **argv){

    loadTestData();
	glutInit(&argc,argv);
	glutInitWindowSize(500, 500);
	glutInitWindowPosition(0, 0);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);	//Depth, Double buffer, RGB color

	glutCreateWindow("My OpenGL Program");

	init();

	glEnable(GL_DEPTH_TEST);	//enable Depth Testing

	glutDisplayFunc(display);	//display callback function
	glutIdleFunc(animate);		//what you want to do in the idle time (when no drawing is occuring)

	glutKeyboardFunc(keyboardListener);
	glutSpecialFunc(specialKeyListener);
	glutMouseFunc(mouseListener);

	glutMainLoop();		//The main loop of OpenGL

	return 0;
}

Raw Text