forked from shks/ofkTools
/
ofkMatrixHelper.cpp
executable file
·153 lines (109 loc) · 3.71 KB
/
ofkMatrixHelper.cpp
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "ofkMatrixHelper.h"
ofVec3f ofkMatrixHelper::getUnProjectionPoint(float ofScreenPosX, float ofScreenPosY, const GLdouble *model, const GLdouble *proj, const GLint *viewPort)
{
ofVec3f res;
// ---------------- SETTING MATRIX ---------------- //
glViewport(viewPort[0], viewPort[1], viewPort[2], viewPort[3]);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
#ifdef _WIN32
glLoadMatrixd(proj);
#else
{
float mat[16];
for(int i = 0 ;i < 16; i++)
{
mat[i] = proj[i];
}
glLoadMatrixf(mat);
}
#endif
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
#ifdef _WIN32
glLoadMatrixd(model);
#else
{
float mat[16];
for(int i = 0 ;i < 16; i++)
{
mat[i] = model[i];
}
glLoadMatrixf(mat);
}
#endif
// ---------------- DRAW and pickup points --------------- //
//Enable Depth
glEnable(GL_DEPTH_TEST);
//DEpth Buffer Clear
glClear(GL_DEPTH_BUFFER_BIT);
//Once Render one Plane to Draw Depth Buffer
ofEnableAlphaBlending();
ofSetColor(255, 255, 255, 0);
ofFill();
ofRect(-10000, -10000, 20000, 20000);
double objX;
double objY;
double objZ;
float z;
//#if defined WIN32 || defined TARGET_OS_MAC
#if defined WIN32
glReadPixels(ofScreenPosX, viewPort[3] - ofScreenPosY ,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&z);
gluUnProject(ofScreenPosX, viewPort[3] - ofScreenPosY ,z, model, proj, viewPort,&objX,&objY,&objZ);
#elif defined TARGET_OS_X
glReadPixels(ofScreenPosX, viewPort[3] - ofScreenPosY ,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&z);
gluUnProject(ofScreenPosX, viewPort[3] - ofScreenPosY ,z,model, proj, viewPort,&objX,&objY,&objZ);
#elif TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
cout << "gluUnProject and GL_DEPTH_COMPONENT is not implemented in iOS openGLES 1.0" << endl;
z = 0.0;
#endif
res.x = objX;
res.y = objY;
res.z = 0.0;
//no no we need to modify this
glClear(GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
return res;
}
ofVec3f ofkMatrixHelper::getUnProjectionPoint(float ofScreenPosX, float ofScreenPosY, ofMatrix4x4 modelView, ofMatrix4x4 projection, float viewWidth, float viewHeight )
{
GLdouble modelMAT[16];
GLdouble projectionMAT[16];
GLint view[4];
for(int i = 0 ; i < 16 ; i++)
{
modelMAT[i] = modelView.getPtr()[i];
projectionMAT[i] = projection.getPtr()[i];
}
view[0] = 0;
view[1] = 0;
view[2] = viewWidth;
view[3] = viewHeight;
return getUnProjectionPoint(ofScreenPosX, ofScreenPosY, modelMAT, projectionMAT, view);
}
ofVec3f ofkMatrixHelper::getProjectionPoint(ofVec3f pos, const GLdouble *model, const GLdouble *proj, const GLint *view)
{
double winX, winY, winZ;//ウィンドウ座標格納用
gluProject(pos.x,pos.y,pos.z,model,proj,view,&winX,&winY,&winZ); //座標変換の計算
return ofVec3f((float)winX, (float)winY, (float)winZ);
}
ofVec3f ofkMatrixHelper::getProjectionPoint(ofVec3f pos, const ofMatrix4x4 modelView, ofMatrix4x4 projection, const int *_view)
{
GLdouble modelMAT[16];
GLdouble projectionMAT[16];
GLint view[4];
for(int i = 0 ; i < 16 ; i++)
{
modelMAT[i] = modelView.getPtr()[i];
projectionMAT[i] = projection.getPtr()[i];
}
view[0] = _view[0];
view[1] = _view[1];
view[2] = _view[2];
view[3] = _view[3];
return getProjectionPoint(pos,modelMAT, projectionMAT,view);
}