Commit 75219f04 authored by Kevin Dang's avatar Kevin Dang

2 changes:

-1. changed the erase line function so that it removes last added line instead of clearing all lines
-2. added code for ray casting
parent 3e582ca2
No preview for this file type
......@@ -20,8 +20,10 @@ bool ofApp::intersectPlane(const ofVec3f &n, const ofVec3f &p0, const ofVec3f &l
return false;
}
/*** Simon's Help End ***/
//--------------------------------------------------------------
void ofApp::camRotate(){
int windowcenterx = ofGetWindowWidth() / 2;
......@@ -74,10 +76,16 @@ void ofApp::setup(){
ofEnableAlphaBlending();
LightDrawing.setup();
userInterface.setup();
lightPos.set(0,0,planeDist);
boxTestPosition.set(100,0,0);
boxTestDimensions.set(40,40,40);
}
//--------------------------------------------------------------
......@@ -138,7 +146,7 @@ void ofApp::update(){
if(isMouseHeldDown == true && userInterface.lightCluster == true){
LightDrawing.mousePressed(pos);
}
//User Camera Movements
if (ofGetKeyPressed('w') == true) {
......@@ -161,15 +169,110 @@ void ofApp::update(){
cam.boom(1.4);
}
//Updates lightdrawing
if(userInterface.lightCluster == true)
LightDrawing.update(pos);
LightDrawing.update(pos);
//Setting LineWidth
LightDrawing.standardSize = userInterface.standardSize;
/*** !ATTEMPT AT RAY CASTING! ***/
// following steps 0 - 4 from: http://antongerdelan.net/opengl/raycasting.html
// step 0: 2d viewport coordinates
// this is in the viewport coordinate system
mouseCoordinatesForRayCasting.set(ofGetMouseX(),ofGetMouseY());
// step 1: 3d normalised device coordinates
// we transform it into 3D normalised coordinates
xTest = (2.0f*mouseCoordinatesForRayCasting.x)/ofGetWindowWidth()-1.0f;
yTest = 1.0f-(2.0f*mouseCoordinatesForRayCasting.y)/ofGetWindowHeight();
zTest = 1.0f;
theRayTest.set(xTest,yTest,zTest);
// step 2: 4d homogeneous clip coordinates
// we make our ray point forward by putting the z value as a negative number
rayClipTest.set(theRayTest.x, theRayTest.y, -1.0, 1.0);
// step 3: 4d Eye (Camera) Coordinates
// the camera space/VIEW space
// so usually, you go from view --> projection space
// but we're going to get there from doing the inverse
// i.e. projection --> view
inverseProjectionMatrixTest = cam.getProjectionMatrix().getInverse();
rayEyeTest.set(rayClipTest.x,rayClipTest.y,rayClipTest.z,rayClipTest.w);
theRayClipMatrix = {
rayEyeTest.x, 0,0,0,
rayEyeTest.y, 0,0,0,
rayEyeTest.z, 0,0,0,
rayEyeTest.w, 0,0,0
};
multiplicationOfInverseMatrixAndRayClipMatrix.makeFromMultiplicationOf(inverseProjectionMatrixTest, theRayClipMatrix);
// getTranspose in order to retrieve correct row of value
transposedTheRayClipMatrix = multiplicationOfInverseMatrixAndRayClipMatrix.getTransposedOf(multiplicationOfInverseMatrixAndRayClipMatrix);
rowOneOfTransposedMatrix = transposedTheRayClipMatrix.getRowAsVec4f(0);
// the final ofVec4f value in step 3: ray_eye = vec4 (ray_eye.xy,-1.0,0.0);
// we manually set the z and w values here
theFinalRayEyeofVec4f.set(rowOneOfTransposedMatrix.x,rowOneOfTransposedMatrix.y,-1.0,0.0);
// step 4:
// world space
inverseViewMatrix = cam.getModelViewMatrix().getInverse();
newRayEyeMatrix = {
theFinalRayEyeofVec4f.x,0,0,0,
theFinalRayEyeofVec4f.y,0,0,0,
theFinalRayEyeofVec4f.z,0,0,0,
0,0,0,0
};
multiplicationOfInverseViewAndnewRayEyeMatrix.makeFromMultiplicationOf(inverseViewMatrix, newRayEyeMatrix);
transposedOfInverseViewxNewRayEyeMatrix = multiplicationOfInverseViewAndnewRayEyeMatrix.getTransposedOf(multiplicationOfInverseViewAndnewRayEyeMatrix);
rowOneOfTransposedMultiplicationResult = transposedOfInverseViewxNewRayEyeMatrix.getRowAsVec4f(0);
rayForWorldCoordinates.set(rowOneOfTransposedMultiplicationResult.x,rowOneOfTransposedMultiplicationResult.y,rowOneOfTransposedMultiplicationResult.z);
rayForWorldCoordinates.normalize();
// std::cout << rayForWorldCoordinates << std::endl;
std::cout << "" << std::endl;
/*** !ATTEMPT AT RAY CASTING ENDS! ***/
if (intersectionWithBoundaryBox(rayForWorldCoordinates, boxTestPosition, boxTestDimensions)) {
std::cout << "detected" << std::endl;
} else {
std::cout << "no collision" << std::endl;
}
}
......@@ -177,19 +280,27 @@ void ofApp::update(){
void ofApp::draw(){
ofBackground(45);
ofEnableDepthTest();
//Lighting
light.enable();
light.setPosition(0, 0, planeDist);
// light.setPosition(0, 0, planeDist);
light.setPosition(lightPos.x, lightPos.y, lightPos.z);
light.setAmbientColor(ofFloatColor(userInterface.a1, userInterface.a2, userInterface.a3, 1.0));
light.setDiffuseColor(ofFloatColor(userInterface.d1, userInterface.d2, userInterface.d3));
light.setSpecularColor(ofFloatColor(1.f, 1.f, 1.f));
cam.begin();
ofDrawSphere(0, 0, planeDist, 20); //this is our reference sphere
ofFill();
ofDrawSphere(lightPos.x, lightPos.y, lightPos.z, 20); //this is our reference sphere
ofPushMatrix();
ofTranslate(0,0,0);
ofNoFill();
ofDrawBox(boxTestPosition.x,boxTestPosition.y,boxTestPosition.z,boxTestDimensions.x,boxTestDimensions.y,boxTestDimensions.z);
ofPopMatrix();
//ofDrawLine(cam.getPosition(), cam.getPosition() + ray * cam.getFarClip()); //notice we can't see this line at all !!!
......@@ -204,7 +315,16 @@ void ofApp::draw(){
LightDrawing.draw();
LightDrawing.bubbledraw();
cam.end();
ofDisableDepthTest();
......@@ -220,6 +340,20 @@ void ofApp::draw(){
ofSetColor(255);
ofDrawBitmapString("world coordinate: " + ofToString(pos.x, 2) + "," + ofToString(pos.y, 2) + "," + ofToString(pos.z, 2), 20, 20);
ofDrawBitmapString("player coordinate: " + ofToString(cam.getX(), 2) + "," + ofToString(cam.getY(), 2) + "," + ofToString(cam.getZ(), 2), 20, 40);
}
//--------------------------------------------------------------
......@@ -231,15 +365,20 @@ void ofApp::keyPressed(int key){
}
mouseMode = !mouseMode;
}
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
if (key == 'r') {
LightDrawing.clearAllLines();
LightDrawing.clearPreviousLine();
}
}
//--------------------------------------------------------------
......@@ -263,7 +402,7 @@ void ofApp::mousePressed(int x, int y, int button){
if(mouseMode == false && userInterface.standard == true){
LightDrawing.mouseRPressed();
}
......@@ -275,7 +414,7 @@ void ofApp::mouseReleased(int x, int y, int button){
if(mouseMode == false){
isMouseHeldDown = false;
if(userInterface.lightCluster == true)
LightDrawing.mouseReleased(pos);
LightDrawing.mouseReleased(pos);
}
......
......@@ -3,32 +3,35 @@
#include "ofMain.h"
#include "lightDrawing.h"
#include "Gui.h"
#include "worldObjects.h"
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void camRotate();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void mouseEntered(int x, int y);
void mouseExited(int x, int y);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
public:
void setup();
void update();
void draw();
void camRotate();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void mouseEntered(int x, int y);
void mouseExited(int x, int y);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
bool intersectPlane(const ofVec3f &n, const ofVec3f &p0, const ofVec3f &l0, const ofVec3f &l, float &t);
ofCamera cam;
ofVec3f pos;
......@@ -50,6 +53,63 @@ class ofApp : public ofBaseApp{
ofLight light;
Gui userInterface;
ofVec3f lightPos;
ofVec3f boxTestPosition;
ofVec3f boxTestDimensions;
// make sure X and Y is in-between 1 and -1 in z axis
// this is for step 0:
ofVec2f mouseCoordinatesForRayCasting;
// this is for step 1:
ofVec3f theRayTest;
float xTest, yTest, zTest;
// this is for step 2:
ofVec4f rayClipTest;
// this is for step 3:
ofVec4f rayEyeTest;
ofMatrix4x4 inverseProjectionMatrixTest;
ofMatrix4x4 theRayClipMatrix;
ofMatrix4x4 multiplicationOfInverseMatrixAndRayClipMatrix;
ofMatrix4x4 transposedTheRayClipMatrix;
ofVec4f rowOneOfTransposedMatrix;
ofVec4f theFinalRayEyeofVec4f;
// this is for step 4:
ofVec3f rayForWorldCoordinates;
ofMatrix4x4 inverseViewMatrix;
ofMatrix4x4 newRayEyeMatrix;
ofMatrix4x4 multiplicationOfInverseViewAndnewRayEyeMatrix;
ofMatrix4x4 transposedOfInverseViewxNewRayEyeMatrix;
ofVec4f rowOneOfTransposedMultiplicationResult;
bool intersectionWithBoundaryBox(const ofVec3f worldSpace, const ofVec3f boxPosition, const ofVec3f boxWHD) {
if ((worldSpace.x > boxPosition.x && worldSpace.x < boxWHD.x) && (worldSpace.y > boxPosition.y && worldSpace.y < boxWHD.y)) {
return true;
} else {
return false;
}
};
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment