void Pipeline::InitRotateTransform(Matrix4f& m) const { Matrix4f rx, ry, rz; const float x = ToRadian(m_rotateInfo.x); const float y = ToRadian(m_rotateInfo.y); const float z = ToRadian(m_rotateInfo.z); rx.m[0][0] = 1.0f; rx.m[0][1] = 0.0f ; rx.m[0][2] = 0.0f ; rx.m[0][3] = 0.0f; rx.m[1][0] = 0.0f; rx.m[1][1] = cosf(x); rx.m[1][2] = -sinf(x); rx.m[1][3] = 0.0f; rx.m[2][0] = 0.0f; rx.m[2][1] = sinf(x); rx.m[2][2] = cosf(x) ; rx.m[2][3] = 0.0f; rx.m[3][0] = 0.0f; rx.m[3][1] = 0.0f ; rx.m[3][2] = 0.0f ; rx.m[3][3] = 1.0f; ry.m[0][0] = cosf(y); ry.m[0][1] = 0.0f; ry.m[0][2] = -sinf(y); ry.m[0][3] = 0.0f; ry.m[1][0] = 0.0f ; ry.m[1][1] = 1.0f; ry.m[1][2] = 0.0f ; ry.m[1][3] = 0.0f; ry.m[2][0] = sinf(y); ry.m[2][1] = 0.0f; ry.m[2][2] = cosf(y) ; ry.m[2][3] = 0.0f; ry.m[3][0] = 0.0f ; ry.m[3][1] = 0.0f; ry.m[3][2] = 0.0f ; ry.m[3][3] = 1.0f; rz.m[0][0] = cosf(z); rz.m[0][1] = -sinf(z); rz.m[0][2] = 0.0f; rz.m[0][3] = 0.0f; rz.m[1][0] = sinf(z); rz.m[1][1] = cosf(z) ; rz.m[1][2] = 0.0f; rz.m[1][3] = 0.0f; rz.m[2][0] = 0.0f ; rz.m[2][1] = 0.0f ; rz.m[2][2] = 1.0f; rz.m[2][3] = 0.0f; rz.m[3][0] = 0.0f ; rz.m[3][1] = 0.0f ; rz.m[3][2] = 0.0f; rz.m[3][3] = 1.0f; m = rz * ry * rx; }
Vector3 FPMovement::movementFP(Vector3 dir) { Vector3 direction = Vector3(0,0,0); //D3DXVec3Transform(&start,&lightSource.dir,&rotateX) if(GetAsyncKeyState('W') & 0x8000) direction = Vector3(dir.x,0,dir.z); if(GetAsyncKeyState('A') & 0x8000) { Matrix r; RotateY(&r,ToRadian(-90.0f)); Vector4 d; D3DXVec3Transform(&d,&dir,&r); direction = Vector3(d.x,0,d.z); } if(GetAsyncKeyState('S') & 0x8000) direction = Vector3(dir.x,0,dir.z)*-1; if(GetAsyncKeyState('D') & 0x8000) { Matrix r; RotateY(&r,ToRadian(90.0f)); Vector4 d; D3DXVec3Transform(&d,&dir,&r); direction = Vector3(d.x,0,d.z); } if(direction != VectorZero) Normalize(&direction, &direction); return direction; }
void Node::getObjectRotation(Matrix4f& dst) { float angle; Matrix4f mX, mY, mZ; if(this->_rotation.getX()) { //rotate around X angle = ToRadian(_rotation.getX()); mX.m[1][1] = cos(angle); mX.m[1][2] = -sin(angle); mX.m[2][1] = sin(angle); mX.m[2][2] = cos(angle); } if(this->_rotation.getY()) { //rotate around y angle = ToRadian(_rotation.getY()); mY.m[0][0] = cos(angle); mY.m[0][2] = sin(angle); mY.m[2][0] = -sin(angle); mY.m[2][2] = cos(angle); } if(this->_rotation.getZ()) { //rotate around z angle = ToRadian(_rotation.getZ()); mZ.m[0][0] = cos(angle); mZ.m[0][1] = -sin(angle); mZ.m[1][0] = sin(angle); mZ.m[1][1] = cos(angle); } dst = mX * mY * mZ; }
void Vector3f::Rotate(float Angle, const Vector3f& Axis) { /*printf("\n"); printf("Angle %f\n", Angle); printf("Before %f %f %f\n", x, y, z);*/ const float SinHalfAngle = sinf(ToRadian(Angle/2)); const float CosHalfAngle = cosf(ToRadian(Angle/2)); //printf("sin %f cos %f\n", SinHalfAngle, CosHalfAngle); const float Rx = Axis.x * SinHalfAngle; const float Ry = Axis.y * SinHalfAngle; const float Rz = Axis.z * SinHalfAngle; const float Rw = CosHalfAngle; //printf("Rotation quaternion %f %f %f %f\n", Rx, Ry, Rz, Rw); Quaternion RotationQ(Rx, Ry, Rz, Rw); Quaternion ConjugateQ = RotationQ.Conjugate(); ConjugateQ.Normalize(); //printf("Conjugate %f %f %f %f\n", ConjugateQ.x, ConjugateQ.y, ConjugateQ.z, ConjugateQ.w); Quaternion W = RotationQ * (*this); // printf("Q * View: %f %f %f %f\n", W.x, W.y, W.z, W.w); W *= ConjugateQ; // printf("Q * View * Conjugate: %f %f %f %f\n", W.x, W.y, W.z, W.w); x = W.x; y = W.y; z = W.z; //printf("After %f %f %f\n", x, y, z); }
void Pipeline::InitRotateTrans(Matrix4f& mat) { Matrix4f rx,ry,rz; const float x = ToRadian(m_rotation.x); const float y = ToRadian(m_rotation.y); const float z = ToRadian(m_rotation.z); rx.mat[0][0] = 1.0f;rx.mat[0][1] = 0.0f;rx.mat[0][2] = 0.0f;rx.mat[0][3] = 0.0f; rx.mat[1][0] = 0.0f;rx.mat[1][1] = cosf(x);rx.mat[1][2] = -sinf(x);rx.mat[1][3] = 0.0f; rx.mat[2][0] = 0.0f;rx.mat[2][1] = sinf(x);rx.mat[2][2] = cosf(x);rx.mat[2][3] = 0.0f; rx.mat[3][0] = 0.0f;rx.mat[3][1] = 0.0f;rx.mat[3][2] = 0.0f;rx.mat[3][3] = 1.0f; ry.mat[0][0] = cosf(y);ry.mat[0][1] = 0.0f;ry.mat[0][2] = -sinf(y);ry.mat[0][3] = 0.0f; ry.mat[1][0] = 0.0f;ry.mat[1][1] = 1.0f;ry.mat[1][2] = 0.0f;ry.mat[1][3] = 0.0f; ry.mat[2][0] = sinf(y);ry.mat[2][1] = 0.0f;ry.mat[2][2] = cosf(y);ry.mat[2][3] = 0.0f; ry.mat[3][0] = 0.0f;ry.mat[3][1] = 0.0f;ry.mat[3][2] = 0.0f;ry.mat[3][3] = 1.0f; rz.mat[0][0] = cosf(z);rz.mat[0][1] = -sinf(z);rz.mat[0][2] = 0.0f;rz.mat[0][3] = 0.0f; rz.mat[1][0] = sinf(z);rz.mat[1][1] = cos(z);rz.mat[1][2] = 0.0f;rz.mat[1][3] = 0.0f; rz.mat[2][0] = 0.0f;rz.mat[2][1] = 0.0f;rz.mat[2][2] = 1.0f;rz.mat[2][3] = 0.0f; rz.mat[3][0] = 0.0f;rz.mat[3][1] = 0.0f;rz.mat[3][2] = 0.0f;rz.mat[3][3] = 1.0f; mat = rz * ry * rx; }
Matrix4f Camera::getRotationM() { Matrix4f xRot, yRot, zRot, m_identity, result; m_identity = getIdentity(); /* get the rotation radians about each axis */ const float xRads = ToRadian(xRotation); const float yRads = ToRadian(yRotation); const float zRads = ToRadian(zRotation); /* rotate around the x axis */ copyMatrix(xRot, m_identity); xRot.m[1][1] = cosf(xRads); xRot.m[1][2] = -sinf(xRads); xRot.m[2][1] = sinf(xRads); xRot.m[2][2] = cosf(xRads); /* rotate around the y axis */ copyMatrix(yRot, m_identity); yRot.m[0][0] = cosf(yRads); yRot.m[0][2] = -sinf(yRads); yRot.m[2][0] = sinf(yRads); yRot.m[2][2] = cosf(yRads); /* rotate around the z axis*/ copyMatrix(zRot, m_identity); zRot.m[0][0] = cosf(zRads); zRot.m[0][1] = -sinf(zRads); zRot.m[1][0] = sinf(zRads); zRot.m[1][1] = cosf(zRads); result = (yRot * xRot * zRot); return result; }
void Matrix4f::InitRotateTransform(float RotateX, float RotateY, float RotateZ) { Matrix4f rx, ry, rz; const float x = ToRadian(RotateX); const float y = ToRadian(RotateY); const float z = ToRadian(RotateZ); rx.m[0][0] = 1.0f; rx.m[0][1] = 0.0f ; rx.m[0][2] = 0.0f ; rx.m[0][3] = 0.0f; rx.m[1][0] = 0.0f; rx.m[1][1] = cosf(x); rx.m[1][2] = -sinf(x); rx.m[1][3] = 0.0f; rx.m[2][0] = 0.0f; rx.m[2][1] = sinf(x); rx.m[2][2] = cosf(x) ; rx.m[2][3] = 0.0f; rx.m[3][0] = 0.0f; rx.m[3][1] = 0.0f ; rx.m[3][2] = 0.0f ; rx.m[3][3] = 1.0f; ry.m[0][0] = cosf(y); ry.m[0][1] = 0.0f; ry.m[0][2] = -sinf(y); ry.m[0][3] = 0.0f; ry.m[1][0] = 0.0f ; ry.m[1][1] = 1.0f; ry.m[1][2] = 0.0f ; ry.m[1][3] = 0.0f; ry.m[2][0] = sinf(y); ry.m[2][1] = 0.0f; ry.m[2][2] = cosf(y) ; ry.m[2][3] = 0.0f; ry.m[3][0] = 0.0f ; ry.m[3][1] = 0.0f; ry.m[3][2] = 0.0f ; ry.m[3][3] = 1.0f; rz.m[0][0] = cosf(z); rz.m[0][1] = -sinf(z); rz.m[0][2] = 0.0f; rz.m[0][3] = 0.0f; rz.m[1][0] = sinf(z); rz.m[1][1] = cosf(z) ; rz.m[1][2] = 0.0f; rz.m[1][3] = 0.0f; rz.m[2][0] = 0.0f ; rz.m[2][1] = 0.0f ; rz.m[2][2] = 1.0f; rz.m[2][3] = 0.0f; rz.m[3][0] = 0.0f ; rz.m[3][1] = 0.0f ; rz.m[3][2] = 0.0f; rz.m[3][3] = 1.0f; *this = rz * ry * rx; }
void Matrix4f::setRotateTransform(const Vector3f& rotate) { Matrix4f xy, xz, yz; const float x = ToRadian(rotate.x); const float y = ToRadian(rotate.y); const float z = ToRadian(rotate.z); xy.setMatrix(cosf(z), -sinf(z), 0.0f, 0.0f, sinf(z), cosf(z), 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f); xz.setMatrix(cosf(y), 0.0f, -sinf(y), 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, sinf(y), 0.0f, cosf(y), 0.0f, 0.0f, 0.0f, 0.0f, 1.0f); yz.setMatrix(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, cosf(x), sinf(x), 0.0f, 0.0f, -sinf(x), cosf(x), 0.0f, 0.0f, 0.0f, 0.0f, 1.0f); *this = xy * xz * yz; }
void ColoredCubeApp::initApp() { D3DApp::initApp(); line1.init(md3dDevice, 10.0f, GREEN); line2.init(md3dDevice, 10.0f, BLUE); line3.init(md3dDevice, 10.0f, RED); xLine.init(&line1, Vector3(0,0,0), 5); xLine.setPosition(Vector3(0,0,0)); yLine.init(&line2, Vector3(0,0,0), 5); yLine.setPosition(Vector3(0,0,0)); yLine.setRotationZ(ToRadian(90)); zLine.init(&line3, Vector3(0,0,0), 5); zLine.setPosition(Vector3(0,0,0)); zLine.setRotationY(ToRadian(90)); quad1.init(md3dDevice, 50, CYAN); quad1.setPosition(Vector3(0,-5,0)); quad2.init(md3dDevice, 50, RED); quad2.setPosition(Vector3(0,5,0)); quad3.init(md3dDevice, 50, BEACH_SAND); quad3.setPosition(Vector3(5,5,0)); quad3.setRotXAngle(ToRadian(90)); quad3.setRotYAngle(ToRadian(90)); quad4.init(md3dDevice, 50, BEACH_SAND); quad4.setPosition(Vector3(-5,5,0)); quad4.setRotXAngle(ToRadian(90)); quad4.setRotYAngle(ToRadian(90)); quad4.init(md3dDevice, 50, BEACH_SAND); quad4.setPosition(Vector3(-5,5,0)); quad4.setRotXAngle(ToRadian(90)); quad5.init(md3dDevice, 50, DARKBROWN); quad5.setPosition(Vector3(0,0,-7)); quad5.setRotXAngle(ToRadian(90)); for(int i = 0; i < WALL_SIZE; ++i) { wall[i].init(md3dDevice, 1, GREEN); wall[i].setPosition(Vector3((rand() % MAX_RADIUS) - (MAX_RADIUS / 2), (rand() % MAX_RADIUS) - (MAX_RADIUS / 2), -6)); wall[i].setRotXAngle(ToRadian(90)); wall[i].setRotZAngle(ToRadian(45)); } buildFX(); buildVertexLayouts(); }
void Vector3f::rotate(float angle, Vector3f axis) { const float sinHalfAngle = sinf(ToRadian(angle/2)); const float cosHalfAngle = cosf(ToRadian(angle/2)); Quaternion rot(axis.getX() * sinHalfAngle, axis.getY() * sinHalfAngle, axis.getZ() * sinHalfAngle, cosHalfAngle); Quaternion conj = rot.Conjugate(); Quaternion W = rot * (*this) * conj; x = W.getX(); y = W.getY(); z = W.getZ(); }
void Axes::init() { line1.init(shader->md3dDevice, 10.0f, GREEN); line2.init(shader->md3dDevice, 10.0f, BLUE); line3.init(shader->md3dDevice, 10.0f, RED); xLine.init(&line1, Vector3(0,0,0), 5); xLine.setPosition(Vector3(0,0,0)); yLine.init(&line2, Vector3(0,0,0), 5); yLine.setPosition(Vector3(0,0,0)); yLine.setRotationZ(ToRadian(90)); zLine.init(&line3, Vector3(0,0,0), 5); zLine.setPosition(Vector3(0,0,0)); zLine.setRotationY(ToRadian(90)); }
bool CTransform::SetRotation(int rotation) { if (m_rotation != rotation) { m_rotation = rotation; GraphTypes::REAL radians=ToRadian(m_rotation); GraphTypes::REAL cosinus=cos(radians); GraphTypes::REAL sinus=sin(radians); mR11=cosinus; mR12=-sinus; mR21=sinus; mR22=cosinus; miR11=cosinus; miR12=sinus; miR21=-sinus; miR22=cosinus; MakeComposite(); return true; } return false; }
Matrix3 Matrix3::MakeRotate(float angle, const Vector3 &a) { Matrix3 m; angle = ToRadian(angle); float ct = Cos(angle); float st = Sin(angle); float x2 = a.x * a.x; float y2 = a.y * a.y; float z2 = a.z * a.z; float sx = st * a.x; float sy = st * a.y; float sz = st * a.z; float xy = a.x * a.y; float xz = a.x * a.z; float yz = a.y * a.z; float omct = 1.0f - ct; m(0,0) = x2 + (1 - x2) * ct; m(1,0) = xy * omct + sz; m(2,0) = xz * omct - sy; m(0,1) = xy * omct - sz; m(1,1) = y2 + (1 - y2) * ct; m(2,1) = yz * omct + sx; m(0,2) = xz * omct + sy; m(1,2) = yz * omct - sx; m(2,2) = z2 + (1 - z2) * ct; return m; }
Matrix44f Pipeline::GetProjLookAt() { const float ar = m_OpenGL->GetScreenWidth() / m_OpenGL->GetScreenHeight(); const float zNear = fNear; const float zFar = fFar; const float zRange = zNear - zFar; const float tanHalfFOV = tanf(ToRadian(fFOV / 2.0)); Matrix44f m; m[0] = 1.0f / (tanHalfFOV * ar); m[1] = 0.0f; m[2] = 0.0f; m[3] = 0.0f; m[4] = 0.0f; m[5] = 1.0f / tanHalfFOV; m[6] = 0.0f; m[7] = 0.0f; m[8] = 0.0f; m[9] = 0.0f; m[10] = (-zNear - zFar) / zRange; m[11] = 2.0f * zFar * zNear / zRange; m[12] = 0.0f; m[13] = 0.0f; m[14] = 1.0f; m[15] = 0.0f; return m; }
void Matrix4f::InitPersProjTransform( float FOV, float Width, float Height, float zNear, float zFar ) { const float ar = Width / Height; const float zRange = zNear - zFar; const float tanHalfFOV = tanf( ToRadian( FOV / 2.0f ) ); m[0][0] = 1.0f / ( tanHalfFOV * ar ); m[0][1] = 0.0f; m[0][2] = 0.0f; m[0][3] = 0.0; m[1][0] = 0.0f; m[1][1] = 1.0f / tanHalfFOV; m[1][2] = 0.0f; m[1][3] = 0.0; m[2][0] = 0.0f; m[2][1] = 0.0f; m[2][2] = ( -zNear - zFar ) / zRange; m[2][3] = 2.0f * zFar*zNear / zRange; m[3][0] = 0.0f; m[3][1] = 0.0f; m[3][2] = 1.0f; m[3][3] = 0.0; }
point3D rotateZ(point3D p1, float angle) { float radangle = ToRadian(angle); point3D newPoint(cos(radangle)*p1.x + -sin(radangle)*p1.y + 0*p1.z, sin(radangle)*p1.x + cos(radangle)*p1.y + 0*p1.z , 0*p1.x + 0*p1.y + 1*p1.z); return newPoint; }
void Mesh::surfRev(int numSlices, Vector3 linePoints[], int numPoints) { float angleBetween = ToRadian(360 / numSlices); Matrix rotate; Identity(&rotate); RotateY(&rotate,angleBetween); Vector3* originalPoints = linePoints; //arrays of the points for the caps if they are necessary //in the event the shap has a hole in one of the ends Vector3* topCap = new Vector3[numSlices]; Vector3* bottomCap = new Vector3[numSlices]; for(int i = 0; i < numSlices; i++) { topCap[i] = originalPoints[numPoints-1]; bottomCap[i] = originalPoints[0]; Vector3* newPoints = new Vector3[numPoints]; for(int k = 0; k < numPoints; k++) { Vector4 point = Vector4(originalPoints[k].x,originalPoints[k].y,originalPoints[k].z,1); D3DXVec3Transform(&point,&originalPoints[k],&rotate); newPoints[k] = Vector3(point.x,point.y,point.z); } for(int j = 0; j < numPoints; j++) { Vector3* side = new Vector3[4]; side[0] = originalPoints[j]; if((j+1)<numPoints) { side[1] = originalPoints[j+1]; side[2] = newPoints[j+1]; } else { side[1] = originalPoints[0]; side[2] = newPoints[0]; } side[3] = newPoints[j]; makePolygon(side,4,j); //set the texture coordinates vertices[vertices.size()-4].texC = Vector2((i*numSlices*(1/360)),0); vertices[vertices.size()-3].texC = Vector2((i*numSlices*2*(1/360)),0); vertices[vertices.size()-2].texC = Vector2((i*numSlices*2*(1/260)),1); vertices[vertices.size()-1].texC = Vector2((i*numSlices*(1/360)),1); } originalPoints = newPoints; //delete[] newPoints; } //add end cap if necessary if(linePoints[numPoints-1].x!=0) { makePolygon(topCap,numSlices,0); } if(linePoints[0].x!=0) { makePolygon(bottomCap,numSlices,0); } delete[] topCap; }
double AJ2Radian(const cValueAvionJaune & aVal) { return ToRadian ( aVal.value(), AJStr2UAng(aVal.unit()) ); }
void vec3D::Rotate(float Angle, const vec3D& Axe) { const float SinHalfAngle = sinf(ToRadian(Angle/2)); const float CosHalfAngle = cosf(ToRadian(Angle/2)); const float Rx = Axe.x * SinHalfAngle; const float Ry = Axe.y * SinHalfAngle; const float Rz = Axe.z * SinHalfAngle; const float Rw = CosHalfAngle; Quaternion RotationQ(Rx, Ry, Rz, Rw); Quaternion ConjugateQ = RotationQ.Conjugate(); Quaternion W = RotationQ * (*this) * ConjugateQ; x = W.x; y = W.y; z = W.z; }
Vector3 Vector3::rotate(float angle,const Vector3& axis) const{ const float sinHalfAngle = sinf(ToRadian(angle/2)); const float cosHalfAngle = cosf(ToRadian(angle/2)); const float Rx = axis.x * sinHalfAngle; const float Ry = axis.y * sinHalfAngle; const float Rz = axis.z * sinHalfAngle; const float Rw = cosHalfAngle; Quaternion rotationQ(Rx,Ry,Rz,Rw); Quaternion conjugateQ = rotationQ.conjugate(); Quaternion w = rotationQ *(*this) * conjugateQ; Vector3 ret(w.x,w.y,w.z); return ret; }
//----------------------------------------------------------------------- Quaternion::Quaternion(const Vector3f &axis, float angle) { float fHalfAngle ( 0.5*ToRadian(angle)); float fSin = sinf(fHalfAngle); w = cosf(fHalfAngle); x = fSin*axis.x; y = fSin*axis.y; z = fSin*axis.z; }
void Vector3f::Rotate(float angle, const Vector3f& axis) { const float sinHalfAngle = sin(ToRadian(angle / 2)); const float cosHalfAngle = cos(ToRadian(angle / 2)); const float Rx = axis.x * sinHalfAngle; const float Ry = axis.y * sinHalfAngle; const float Rz = axis.z * sinHalfAngle; const float Rw = cosHalfAngle; Quaternion rotationQ(Rx, Ry, Rz, Rw); Quaternion conjugateQ = rotationQ.Conjugate(); Quaternion W = rotationQ * (*this) * conjugateQ; x = W.x; y = W.y; z = W.z; }
void Vector3f::rotate(float Angle, const Vector3f& Axis) { const float SinHalfAngle = sinf(ToRadian(Angle/2)); const float CosHalfAngle = cosf(ToRadian(Angle/2)); const float Rx = Axis.x * SinHalfAngle; const float Ry = Axis.y * SinHalfAngle; const float Rz = Axis.z * SinHalfAngle; const float Rw = CosHalfAngle; Quaternion RotationQ(Rx, Ry, Rz, Rw); Quaternion ConjugateQ = RotationQ.conjugate(); //ConjugateQ.Normalize(); Quaternion W = RotationQ * (*this) * ConjugateQ; x = W.x; y = W.y; z = W.z; }
void OpenGLRenderSingleTextureEntities(const Entity* entities, int count, int model_location, int texture_id, int texture_location) { glBindTexture(GL_TEXTURE_2D, texture_id); glUniform1i(texture_location, 0);//GL_TEXTURE0 for (int i = 0; i < count; ++i) { Mat4 model; const Entity& e = entities[i]; float c = cos(ToRadian(e.rotation.x)); float s = sin(ToRadian(e.rotation.x)); Mat4 rotation_x = { 1.f, 0.f, 0.f, 0.f, 0.f, c, -s, 0.f, 0.f, s, c, 0.f, 0.f, 0.f, 0.f, 1.f }; c = cos(ToRadian(e.rotation.y)); s = sin(ToRadian(e.rotation.y)); Mat4 rotation_y = { c, 0.f, -s, 0.f, 0.f, 1.f, 0.f, 0.f, s, 0.f, c, 0.f, 0.f, 0.f, 0.f, 1.f }; #if 0//TODO: Not used yet. Use this when has to be used. c = cos(ToRadian(e.rotation.y)); s = sin(ToRadian(e.rotation.y)); float c = cos(ToRadian(e.rotation.y)); float s = sin(ToRadian(e.rotation.y)); Mat4 rotation_z = { c, s, 0.f, 0.f, -s, c, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 0.f, 1.f }; #endif model = Translation(e.position.x, e.position.y, e.position.z) * rotation_x * rotation_y * Scale(e.scale.x, e.scale.y, e.scale.z); glUniformMatrix4fv(model_location, 1, GL_TRUE, model.m); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); } }
void Matrix4f::InitPersProjTransform(const PersProjInfo& p) { const float ar = p.Width / p.Height; const float zRange = p.zNear - p.zFar; const float tanHalfFOV = tanf(ToRadian(p.FOV / 2.0f)); m[0][0] = 1.0f/(tanHalfFOV * ar); m[0][1] = 0.0f; m[0][2] = 0.0f; m[0][3] = 0.0; m[1][0] = 0.0f; m[1][1] = 1.0f/tanHalfFOV; m[1][2] = 0.0f; m[1][3] = 0.0; m[2][0] = 0.0f; m[2][1] = 0.0f; m[2][2] = (-p.zNear - p.zFar)/zRange ; m[2][3] = 2.0f*p.zFar*p.zNear/zRange; m[3][0] = 0.0f; m[3][1] = 0.0f; m[3][2] = 1.0f; m[3][3] = 0.0; }
void Pipeline::InitPersProjTrans(Matrix4f& mat) { const float ar = m_persProj.Width / m_persProj.Height; const float zNear = -m_persProj.zNear; const float zFar = -m_persProj.zFar; const float zRange = zNear - zFar; const float tanHalfFov = tanf(ToRadian(m_persProj.Fov / 2)); mat.mat[0][0] = 1.0f / (ar * tanHalfFov);mat.mat[0][1] = 0.0f;mat.mat[0][2] = 0.0f;mat.mat[0][3] = 0.0f; mat.mat[1][0] = 0.0f;mat.mat[1][1] = 1.0f / (tanHalfFov);mat.mat[1][2] = 0.0f;mat.mat[1][3] = 0.0f; mat.mat[2][0] = 0.0f;mat.mat[2][1] = 0.0f;mat.mat[2][2] = (-zNear-zFar)/zRange;mat.mat[2][3] = (2.0f * zNear * zFar)/zRange; mat.mat[3][0] = 0.0f;mat.mat[3][1] = 0.0f;mat.mat[3][2] = -1.0f;mat.mat[3][3] = 0.0f; }
void Node::getPerspctiveProjection(Matrix4f& dst) { const float ar = ((float)_projection.Width / _projection.Height); const float rangeZ = (_projection.zNear - _projection.zFar); const float focalLength = (1.0f / (tanf(ToRadian(_projection.FOV / 2.0f)))); dst.m[0][0] = (focalLength) / ar; dst.m[1][1] = focalLength; dst.m[2][2] = (-_projection.zNear - _projection.zFar) / rangeZ; dst.m[2][3] = (2.0f * _projection.zNear * _projection.zFar) / rangeZ; dst.m[3][2] = 1.0f; dst.m[3][3] = 0.0f; }
void Pipeline::initRotateTans(Mat4f& world){ Mat4f rx, ry, rz; const float x = ToRadian(m_rotation.x); const float y = ToRadian(m_rotation.y); const float z = ToRadian(m_rotation.z); rx.mat[0][0] = 1.0f; rx.mat[0][1] = 0.0f; rx.mat[0][2] = 0.0f; rx.mat[0][3] = 0.0f; rx.mat[1][0] = 0.0f; rx.mat[1][1] = cosf(x); rx.mat[1][2] = -sinf(x); rx.mat[1][3] = 0.0f; rx.mat[2][0] = 0.0f; rx.mat[2][1] = sinf(x); rx.mat[2][2] = cosf(x); rx.mat[2][3] = 0.0f; rx.mat[3][0] = 0.0f; rx.mat[3][1] = 0.0f; rx.mat[3][2] = 0.0f; rx.mat[3][3] = 1.0f; ry.mat[0][0] = cosf(y); ry.mat[0][1] = 0.0f; ry.mat[0][2] = -sinf(y); ry.mat[0][3] = 0.0f; ry.mat[1][0] = 0.0f; ry.mat[1][1] = 1.0f; ry.mat[1][2] = 0.0f; ry.mat[1][3] = 0.0f; ry.mat[2][0] = sinf(y); ry.mat[2][1] = 0.0f; ry.mat[2][2] = cosf(y); ry.mat[2][3] = 0.0f; ry.mat[3][0] = 0.0f; ry.mat[3][1] = 0.0f; ry.mat[3][2] = 0.0f; ry.mat[3][3] = 1.0f; rz.mat[0][0] = cosf(z); rz.mat[0][1] = -sinf(z); rz.mat[0][2] = 0.0f; rz.mat[0][3] = 0.0f; rz.mat[1][0] = sinf(z); rz.mat[1][1] = cosf(z); rz.mat[1][2] = 0.0f; rz.mat[1][3] = 0.0f; rz.mat[2][0] = 0.0f; rz.mat[2][1] = 0.0f; rz.mat[2][2] = 1.0f; rz.mat[2][3] = 0.0f; rz.mat[3][0] = 0.0f; rz.mat[3][1] = 0.0f; rz.mat[3][2] = 0.0f; rz.mat[3][3] = 1.0f; world = rz * ry * rx; }
void Rotate(float angle, Vector3f axis) { const float RadAngle = (float) ToRadian( angle / 2 ); const float sinAngle = sinf( RadAngle ); const float cosAngle = cosf( RadAngle ); axis *= sinAngle; // quaternion describing an 'angle' rotation around the axis Quaternion Rot( axis, cosAngle ); Quaternion Res = Rot * (*this); Res *= Rot.conjugate(); *this = Res.axis; }
void Lighting::SetSpotLights(unsigned int NumLights, const SpotLight* pLights) { glUniform1i(m_numSpotLightsLocation, NumLights); for (unsigned int i = 0; i < NumLights; i++) { glUniform3f(m_spotLightsLocation[i].Color, pLights[i].Color.x, pLights[i].Color.y, pLights[i].Color.z); glUniform1f(m_spotLightsLocation[i].AmbientIntensity, pLights[i].AmbientIntensity); glUniform1f(m_spotLightsLocation[i].DiffuseIntensity, pLights[i].DiffuseIntensity); glUniform3f(m_spotLightsLocation[i].Position, pLights[i].Position.x, pLights[i].Position.y, pLights[i].Position.z); Vector3 Direction = pLights[i].Direction; Direction.Normalize(); glUniform3f(m_spotLightsLocation[i].Direction, Direction.x, Direction.y, Direction.z); glUniform1f(m_spotLightsLocation[i].Cutoff, cosf(ToRadian(pLights[i].Cutoff))); glUniform1f(m_spotLightsLocation[i].Atten.Constant, pLights[i].Attenuation.Constant); glUniform1f(m_spotLightsLocation[i].Atten.Linear, pLights[i].Attenuation.Linear); glUniform1f(m_spotLightsLocation[i].Atten.Exp, pLights[i].Attenuation.Exp); } }