summaryrefslogtreecommitdiff
path: root/src/viewpoint.cpp
blob: efb6894c9ba5767df9561f176f351c44ee089480 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "viewpoint.h"
#define DEBUG 0

void viewpoint::setup(int ln,float w, float h, float x, float y) {
	lightNum=ln;
	window=ofRectangle(w,h,x,y);
	distortFactor=0.0;
	renderFBO.allocate(window.width,window.height,GL_RGB);

	//todo: load/save from xml
	fov=17.25;
	aspect=1.79;
	near=1;
	far=20;

	//camera.setParent(target);
	vars=new keyVar[8];

	//void set(char _keyInc,char _keyDec,float _val,float _speed,float _accel,float accelTime);
	vars[0].set('w','s',17.25,0.5,1.0,3.0);
	vars[1].set('g','d',0.0,10,1.0,3.0);
	vars[2].set('r','v',112,10,1.0,3.0);
	vars[3].set('t','c',0.0,10,1.0,3.0);
	vars[4].set('u','n',0.0,10,1.0,3.0);
	vars[5].set('j','h',0.0,10,1.0,3.0);
	vars[6].set('o','l',1000.0,10,1.0,3.0);
	vars[7].set('q','a',0,1,1.0,3.0);
	
	//camera.setPosition(0,0,vars[6].getVal());
	
	getLightPosition=new GLfloat[4];
	
	light.enable();
	light.setDirectional();
}
//--------------------------------------------------------------
void viewpoint::setLight(){
	//glLoadIdentity();
	//glPushMatrix();
	target.setPosition(vars[1].getVal(),vars[2].getVal(),vars[3].getVal());
	/*
	target.rotate(vars[4].getInc(),1,0,0);
	target.rotate(vars[5].getInc(),0,1,0);
	camera.setPosition(0,0,vars[6].getVal());
	 */
	
	camera.orbit(vars[5].getVal(), vars[4].getVal(), vars[6].getVal(), target);
	camera.lookAt(target,ofVec3f(0,1,0));	
	//glPopMatrix();
	//ofVec3f lp=camera.getGlobalPosition();
	light.setPosition(camera.getGlobalPosition());
	/*
	GLfloat lightPosition[] = {lp.x, lp.y, lp.z,1};
	GLfloat lightColour[] = {0.99, 0.99, 0.99, 0.5};
	
	glLightfv(lightNum, GL_POSITION, lightPosition);
	glLightfv(lightNum, GL_DIFFUSE, lightColour);
	glEnable(lightNum);
	glGetLightfv(lightNum,GL_POSITION, getLightPosition);
	 */
}
//--------------------------------------------------------------
void viewpoint::begin(){
	
	renderFBO.begin();
	ofClear(0,0,0);
	
	//ofPushView();
	
	camera.begin();
	camera.setFov(vars[0].getVal());
	
	
		
	}
//--------------------------------------------------------------
void viewpoint::end(){
	
	camera.end();
	//ofPopView();
	
	renderFBO.end();

	ofPushMatrix();
	
	bindTexture(renderFBO);

	//draw a grid
	//glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
	ofNoFill();

	ofSetLineWidth(1.0);
	//ofSetColor(I_fade1,I_fade1,I_fade1);
	int gridX=50;
	int gridY=50;
	int xStep=ofGetWidth()/2;
	int yStep=ofGetHeight()/2;
	ofTranslate(ofGetWidth()/2,ofGetHeight()/2);

	for (float i = -1; i < 1.001; i+=(2.0f/gridY)){

	    glBegin(GL_QUAD_STRIP);
	    ofPoint p0;
	    ofPoint p1;
		for (float j = -1; j < 1.001; j+=(2.0f/gridX)){
		    p0=distort(ofPoint(j,i-(2.0f/gridY)),vars[7].getVal());
		    p1=distort(ofPoint(j,i),vars[7].getVal());
		    glTexCoord2f((j+1)*0.5,((i-(2.0f/gridY))+1)*0.5);
		    glVertex3f(p0.x*xStep,p0.y*yStep,-0.1);
		    glTexCoord2f((j+1)*0.5,(i+1)*0.5);
		    glVertex3f(p1.x*xStep,p1.y*yStep,-0.1);
		}

	    glEnd();
	   //
	}

	ofFill();

	unbindTexture(renderFBO);

	ofPopMatrix();
	
	ofSetHexColor(0xFFFFFF);
    ofDrawBitmapString("camera: "+ofToString(camera.getX(), 2)+","+ofToString(camera.getY(), 2)+","+ofToString(camera.getZ(), 2), 10, ofGetHeight()-30);
	ofDrawBitmapString("light: "+ofToString(light.getX(), 2)+","+ofToString(light.getY(), 2)+","+ofToString(light.getZ(), 2), 10, ofGetHeight()-18);

}
//--------------------------------------------------------------
void viewpoint::keyPressed(int key){
    for (int i=0;i<8;i++) vars[i].keyPressed(key);
    if (DEBUG) printf("fov: %f distort: %f\n",vars[0].getVal(),vars[7].getVal());
 }
//--------------------------------------------------------------
void viewpoint::keyReleased(int key){
	for (int i=0;i<8;i++) vars[i].keyReleased(key);
}