Exemplo n.º 1
0
//
//  main.m
//  GC3AssignmentTwo
//
//  Created by Meraj Patel on 11/8/2013.
//  Copyright (c) 2013 Meraj Patel. All rights reserved.
//

#include <stdlib.h>
#include <GLUT/glut.h>
#include "MathLibrary.h"
#include "PhysicsEngine.h"
#include "Particle.h"
#include "Texture.h"
#include <math.h>
#include "ray.h"
#include "Texture.h"
#include "Points.h"

ray newRay;
bool hit1, hit2;
double transparentWall1 = 1;
double transparentWall2 = 1;
int orientation[3] = {0,1,0};
bool flip1, flip2;
float moveY = -2;
float angY = 9*sin(1.05);//roation around Y
double camera[3] = {0,9,9};//declares camera at position
double bounceY = 0;
int x = -1;
PhysicsEngine game;
Texture textureObeject;
bool cameraParticlePosition = false;
bool startStop = true;
bool lightswitch = true;
bool gameOver = false;

void Get3DPos(int x, int y, float winz, GLdouble point[3])
{
    GLint viewport[4];
    GLdouble modelview[16];
    GLdouble projection[16];

    //get the matrices
    glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
    glGetDoublev( GL_PROJECTION_MATRIX, projection );
    glGetIntegerv( GL_VIEWPORT, viewport );

    //"un-project" the 2D point giving the 3D position corresponding to the provided depth (winz)
    gluUnProject( (float)x, (float)(viewport[3]-y), winz, modelview, projection, viewport, &point[0], &point[1], &point[2]);

}

///* rayCast - takes a mouse x,y, coordinate, and casts a ray through that point
// *   for subsequent intersection tests with objects.
// */
void rayCast(float x, float y)
{
    bool groundPlane = true;//check if hit on plane
    GLdouble pNear[3];//depth for z
    GLdouble pFar[3]; //depth for z
    float inter[3];//stores object intersection
    //count through number of objects to perform tests on each one
    //count through number of planes of each object to perform test on each one
    for (int count1 = 0; count1 < game.ActiveObjects.size(); count1++) {
        for(int count2 = 0; count2 < 6; count2++) {

            Get3DPos(x, y, 0.0, pNear);
            Get3DPos(x, y, 1.0, pFar);

            //store ray orgin
            newRay.org[0] = camera[0];
            newRay.org[1] = camera[1];
            newRay.org[2] = camera[2];

            //ray direction is the vector (pFar - pNear)
            newRay.dir[0] = pFar[0] - pNear[0];
            newRay.dir[1] = pFar[1] - pNear[1];
            newRay.dir[2] = pFar[2] - pNear[2];

            newRay.normalizeDirection();

            groundPlane = newRay.rayPlaneTest(count1, count2, game.ActiveObjects);
            //update the position of the object to the intersection point
            if ( groundPlane == true) {
                //check if object is hit between min and max bounds
                if ((game.ActiveObjects.at(count1).min + game.ActiveObjects.at(count1).translateX < newRay.inter[0] && newRay.inter[0] < game.ActiveObjects.at(count1).max + game.ActiveObjects.at(count1).translateX && game.ActiveObjects.at(count1).min + game.ActiveObjects.at(count1).translateZ < newRay.inter[2] && newRay.inter[2] < game.ActiveObjects.at(count1).max + game.ActiveObjects.at(count1).translateZ && inter[1] < game.ActiveObjects.at(count1).max + game.ActiveObjects.at(count1).translateY && game.ActiveObjects.at(count1).min + game.ActiveObjects.at(count1).translateY < newRay.inter[1])) {

                    game.ActiveObjects.at(count1).hit = true;
                    //check to see right click to delete object
                    break;
                }
                //return false hit else wise
                else {
                    game.ActiveObjects.at(count1).hit = false;
                }
            }
        }   //break out of cycle of object
        if(game.ActiveObjects.at(count1).hit == true) {
            for(int z = 0; z < game.ActiveObjects.size(); z++) {
                if(z != count1) {
                    game.ActiveObjects.at(z).hit = false;
                }
            }
            break;
        }
    }
}