summaryrefslogtreecommitdiff
path: root/rayhit/src/testApp.cpp
blob: 750287ce4337847ba3bc6641ae5badcbbbc79389 (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){
	ofVec3f centre=ofVec3f(ofGetWidth()/2,ofGetHeight(),0);
	ofVec3f normal=ofVec3f(0,0,1);
	ray=ofRay();
	plane=ofPlane(centre,normal);
	
	projector=ofProjector(ofGetWidth(),ofGetHeight());
	
	pos=ofVec3f(ofGetWidth()/2,ofGetHeight()/2,0);
}

//--------------------------------------------------------------
void testApp::update(){
	


}

//--------------------------------------------------------------
void testApp::draw(){
	ofBackground(0,0,0);
	plane.draw();
	
	float gap=ofGetHeight()-ofGetWidth();
	
	ofPushMatrix();

	ofTranslate(0,ofGetHeight(),0);
	ofRotate(cam_angle,1,0,0);
	ofTranslate(0,-ofGetHeight(),0);

	for (float i=0;i<ofGetWidth();i+=ofGetWidth()/10) {
        glBegin(GL_LINES);
            glVertex3f(i,gap,0);
            glVertex3f(i,ofGetHeight(),0);
        glEnd();
        glBegin(GL_LINES);
            glVertex3f(0,i+gap,0);
            glVertex3f(ofGetWidth(),i+gap,0);
        glEnd();
	}
	
	ofPopMatrix();
	
	ofSphere(pos.x,pos.y,pos.z,20);

}

//--------------------------------------------------------------
void testApp::keyPressed(int key){

	switch (key){
		case 'a':
			cam_angle+=1;
			break;
		case 'z':
			cam_angle-=1;
			break;
	}
	plane.setNormal(ofVec3f(0,sin(cam_angle),cos(cam_angle)));
}

//--------------------------------------------------------------
void testApp::keyReleased(int key){

}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
	
}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
	ray=projector.castPixel(x,(ofGetHeight()-y));
	bool hit = plane.intersect(ray,pos);
	pos=ofVec3f(pos.x*2,pos.y*2,pos.z*2);
	if (hit) printf("ray:%i,%i hit plane:%f,%f,%f\n",x,y,pos.x,pos.y,pos.z);
	else printf("ray:%i,%i missed plane\n",x,y);
	
}

//--------------------------------------------------------------
void testApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){

}