-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmggl_vertexOps.cpp
63 lines (48 loc) · 1.82 KB
/
mggl_vertexOps.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
#include "vector.h"
#include "mggl_light.h"
#include "mggl_transforms.h"
#include "mggl_vertexOps.h"
Vector mggl_processVertex(const Vector& v, const Vector& n) {
Vector eyeV = mggl_modelViewTransform(v);
Vector eyeN = mggl_normalToEyeTransform(n);
Vector col = mggl_calcColor(eyeV,eyeN);
return col;
}
Vector mggl_modelViewTransform(const Vector& v) {
Matrix<float> mv = mggl_getModelViewMatrix();
Vector eyeV;
eyeV.x = mv[0][0]*(v.x)+mv[0][1]*(v.y)+mv[0][2]*(v.z)+mv[0][3]*(v.w);
eyeV.y = mv[1][0]*(v.x)+mv[1][1]*(v.y)+mv[1][2]*(v.z)+mv[1][3]*(v.w);
eyeV.z = mv[2][0]*(v.x)+mv[2][1]*(v.y)+mv[2][2]*(v.z)+mv[2][3]*(v.w);
eyeV.w = mv[3][0]*(v.x)+mv[3][1]*(v.y)+mv[3][2]*(v.z)+mv[3][3]*(v.w);
return eyeV;
}
Vector mggl_normalToEyeTransform(const Vector& v) {
Matrix<float> invMv = mggl_getInverseMVMatrix();
invMv.t(); Vector eyeN; eyeN.w = 0;
eyeN.x = invMv[0][0]*(v.x)+invMv[0][1]*(v.y)+invMv[0][2]*(v.z);
eyeN.y = invMv[1][0]*(v.x)+invMv[1][1]*(v.y)+invMv[1][2]*(v.z);
eyeN.z = invMv[2][0]*(v.x)+invMv[2][1]*(v.y)+invMv[2][2]*(v.z);
eyeN.normalize();
return eyeN;
}
Vector mggl_projectionTransform(const Vector& v) {
Vector projV;
Matrix<float> projMat = mggl_getProjectionMatrix();
projV.x = projMat[0][0]*(v.x)+projMat[0][1]*(v.y)+projMat[0][2]*(v.z)+projMat[0][3]*(v.w);
projV.y = projMat[1][0]*(v.x)+projMat[1][1]*(v.y)+projMat[1][2]*(v.z)+projMat[1][3]*(v.w);
projV.z = projMat[2][0]*(v.x)+projMat[2][1]*(v.y)+projMat[2][2]*(v.z)+projMat[2][3]*(v.w);
projV.w = projMat[3][0]*(v.x)+projMat[3][1]*(v.y)+projMat[3][2]*(v.z)+projMat[3][3]*(v.w);
//To NDC
double w = projV.w;
projV/=w;
return projV;
}
Vector mggl_viewportTransform(const Vector& v) {
Vector win=v;
Vector vp = mggl_getViewport();
//Here z is width, w is height
win.x=(v.x+1)*(vp.z/2.0)+vp.x;
win.y=(v.y+1)*(vp.w/2.0)+vp.y;
return win;
}