-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathofApp.cpp
More file actions
115 lines (65 loc) · 2.31 KB
/
ofApp.cpp
File metadata and controls
115 lines (65 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "ofApp.h"
// Example by Jordi Puig
//--------------------------------------------------------------
void ofApp::setup(){
// let the axis oclude
ofEnableDepthTest();
// set attributes to our displayed plane
planePrimitive.setPosition(0 ,0, 0);
planePrimitive.setScale(1,2,1);
//set the rotation speeds
rotationSpeedX = 0.33;
rotationSpeedY = 0.2;
// get the attributes from the plane primitive
// and apply them to the intersection plane
plane.setFrom(planePrimitive);
}
//--------------------------------------------------------------
void ofApp::update(){
// the mouse position on screen coordinates
screenMouse = ofVec3f(ofGetMouseX(),ofGetMouseY(),0);
// the mouse position on world coordinates
worldMouse = camera.screenToWorld(ofVec3f(screenMouse.x, screenMouse.y, 0.0f));
// a point right in front of the mouse (used to get mouse direction)
worldMouseEnd = camera.screenToWorld(ofVec3f(screenMouse.x, screenMouse.y, 1.0f));
// a vector representing the mouse direction (from camera to infinity?)
worldMouseTransmissionVector = worldMouseEnd - worldMouse;
// set attributes to the ray
mouseRay.s = worldMouse;
mouseRay.t = worldMouseTransmissionVector;
// rotate the plane
rotation.x += rotationSpeedX;
rotation.y += rotationSpeedY;
planePrimitive.setOrientation(rotation);
// update intersection plane
plane.setFrom(planePrimitive);
// check for intersection
// all the good stuff is done here!
doesIntersect = plane.intersect(mouseRay, intersectionPosition);
}
//--------------------------------------------------------------
void ofApp::draw(){
ofBackgroundGradient(100, 50);
camera.begin();
ofDrawGrid(1000.0f, 1.0f);
// this will be drawn as a dot at your mouse cursor
// because it's a line going away from the camera
// under your mouse
mouseRay.draw();
plane.draw();
ofSetColor(0, 250, 250, 100);
planePrimitive.draw(OF_MESH_FILL);
planePrimitive.drawAxes(100);
camera.end();
drawLabels();
}
void ofApp::drawLabels(){
string label;
int y = ofGetHeight() - 70;
label = doesIntersect ? "hits" : "misses";
label += + " at world position " + ofToString(intersectionPosition);
ofDrawBitmapStringHighlight(label, 40, y);
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
}