void GraphicsCore::UpdateObjectValues(Object3D object) { vec3 up(0.0f, 1.0f, 0.0f); mat4 Projection = glm::perspective(fov, float(windowWidth) / (float)windowHeight, 0.1f, 200.f); mat4 Model = glm::translate(object.GetWorldPos()); mat4 viewMatrix = mCam.GetRotationMatrix() * glm::lookAt(mEye, mTarget, up); mat4 ModelView = viewMatrix * Model; mat4 MVP = Projection * ModelView; mat3 normalMatrix = glm::transpose(glm::inverse(mat3(ModelView))); uint shaderProgHandle = object.GetShaderID(); uint location = glGetUniformLocation(shaderProgHandle, "NormalMatrix"); //gets the UniformLocation from shader.vertex if( location >= 0 ){ glUniformMatrix3fv(location, 1, GL_FALSE, &normalMatrix[0][0]); } location = glGetUniformLocation(shaderProgHandle, "ProjectionMatrix"); //gets the UniformLocation from shader.vertex if( location >= 0 ){ glUniformMatrix4fv(location, 1, GL_FALSE, &Projection[0][0]); } location = glGetUniformLocation(shaderProgHandle, "ViewMatrix"); //gets the UniformLocation from shader.vertex if( location >= 0 ){ glUniformMatrix4fv(location, 1, GL_FALSE, &viewMatrix[0][0]); } location = glGetUniformLocation(shaderProgHandle, "ModelViewMatrix"); //gets the UniformLocation from shader.vertex if( location >= 0 ){ glUniformMatrix4fv(location, 1, GL_FALSE, &ModelView[0][0]); } location = glGetUniformLocation(shaderProgHandle, "MVP"); //gets the UniformLocation from shader.vertex if( location >= 0 ){ glUniformMatrix4fv(location, 1, GL_FALSE, &MVP[0][0]); } }
void Renderer::drawModel(Model m) { glEnable(GL_BLEND); glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); this->modelShader.use(); this->modelShader.setUniform("outAlpha",0.7f); mat4 mvp=mat4(0.0f); glActiveTexture(GL_TEXTURE0); //set upp uniforms for rendering call mvp=this->projMatrix*this->viewMatrix*m.getModelMatrix(); this->modelShader.setUniform("modelMatrix",m.getModelMatrix()); this->modelShader.setUniform("MVP",mvp); mat3 normalMatrix=transpose(inverse(mat3(this->viewMatrix*m.getModelMatrix()))); this->modelShader.setUniform("normalMatrix",normalMatrix); //get a pointer to a vector with meshes info for each mesh for(unsigned int j=0;j<m.getMeshInfo()->size();j++) { glBindTexture(GL_TEXTURE_2D, m.getMeshInfo()->at(j).getTexh()); glBindVertexArray(m.getMeshInfo()->at(j).getVaoh()); glDrawArrays(GL_TRIANGLES,0,m.getMeshInfo()->at(j).getNrOfVerts()); } glDisable(GL_BLEND); }
mat3_t mat3_inverse(mat3_t m) { float m00 = m.col[0].x; float m10 = m.col[0].y; float m20 = m.col[0].z; float m01 = m.col[1].x; float m11 = m.col[1].y; float m21 = m.col[1].z; float m02 = m.col[2].x; float m12 = m.col[2].y; float m22 = m.col[2].z; float inv_det = 1.0f / (m00 * m11 * m22 + m01 * m12 * m20 + m02 * m10 * m21 - m00 * m12 * m21 - m01 * m10 * m22 - m02 * m11 * m20); float r00 = (m11 * m22 - m12 * m21) * inv_det; float r01 = (m02 * m21 - m01 * m22) * inv_det; float r02 = (m01 * m12 - m02 * m11) * inv_det; float r10 = (m12 * m20 - m10 * m22) * inv_det; float r11 = (m00 * m22 - m02 * m20) * inv_det; float r12 = (m02 * m10 - m00 * m12) * inv_det; float r20 = (m10 * m21 - m11 * m20) * inv_det; float r21 = (m01 * m20 - m00 * m21) * inv_det; float r22 = (m00 * m11 - m01 * m10) * inv_det; return mat3(r00, r10, r20, r01, r11, r21, r02, r12, r22); }
vec3 intersectTriangle(vec3 a, vec3 b, vec3 c, vec3 ray) { mat3 A, Ai; vec3 solution; vec3 betaCoeff = sub(a, b); vec3 gammaCoeff = sub(a, c); A.rows[0] = vec3(ray.x, betaCoeff.x, gammaCoeff.x); A.rows[1] = vec3(ray.y, betaCoeff.y, gammaCoeff.y); A.rows[2] = vec3(ray.z, betaCoeff.z, gammaCoeff.z); float detA = determinant(A); for(int i = 0; i < 3; i++) { Ai = mat3(A); Ai.elements[i] = a.x; Ai.elements[i + 3] = a.y; Ai.elements[i + 6] = a.z; solution.xyz[i] = determinant(Ai) / detA; } float beta = solution.y; float gamma = solution.z; float alfa = beta + gamma; if(alfa >= 0 && alfa <= 1 && beta >= 0 && beta <= 1 && gamma >= 0 && gamma <= 1) return scale(ray, solution.x); else return vec3(); }
Light::Data SpotLight::getData( double time, const mat4 &transform ) const { // Populate the LightData structure. Light::Data params = Light::getData( time, transform ); params.position = vec3( transform * vec4( mPosition, 1 ) ); params.direction = glm::normalize( mat3( transform ) * mDirection ); params.range = mRange; params.attenuation = getAttenuation(); params.angle = getConeParams(); if( mFlags & ( Data::ShadowEnabled | Data::ModulationEnabled ) ) { mat4 invTransform = glm::inverse( transform ); if( mFlags & Data::ShadowEnabled ) { params.shadowMatrix = getShadowMatrix() * invTransform; params.shadowIndex = mShadowIndex; } if( mFlags & Data::ModulationEnabled ) { params.modulationMatrix = getModulationMatrix( time ) * invTransform; params.modulationIndex = mModulationIndex; } } return params; }
int test_cvtColor_RGB2RGB() { cv::Mat mat = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1); if (!mat.data) { std::cout << "read image fail" << std::endl; return -1; } int width = mat.cols; int height = mat.rows; // uchar fbc::Mat3BGR mat1(height, width, mat.data); fbc::Mat3BGR mat2(mat1); fbc::Mat_<uchar, 4> mat3(height, width); fbc::cvtColor(mat2, mat3, fbc::CV_BGR2BGRA); cv::Mat mat1_(height, width, CV_8UC3, mat.data); cv::Mat mat2_; mat1_.copyTo(mat2_); cv::Mat mat3_(height, width, CV_8UC4); cv::cvtColor(mat2_, mat3_, CV_BGR2BGRA); assert(mat3.step == mat3_.step); for (int y = 0; y < mat3.rows; y++) { const fbc::uchar* p = mat3.ptr(y); const uchar* p_ = mat3_.ptr(y); for (int x = 0; x < mat3.step; x++) { assert(p[x] == p_[x]); } } // float cv::Mat matf; mat.convertTo(matf, CV_32FC3); fbc::Mat_<float, 3> mat4(height, width, matf.data); fbc::Mat_<float, 3> mat5(mat4); fbc::Mat_<float, 4> mat6(height, width); fbc::cvtColor(mat5, mat6, fbc::CV_BGR2BGRA); cv::Mat mat4_(height, width, CV_32FC3, matf.data); cv::Mat mat5_; mat4_.copyTo(mat5_); cv::Mat mat6_(height, width, CV_32FC4); cv::cvtColor(mat5_, mat6_, CV_BGR2BGRA); assert(mat6.step == mat6_.step); for (int y = 0; y < mat6.rows; y++) { const fbc::uchar* p = mat6.ptr(y); const uchar* p_ = mat6_.ptr(y); for (int x = 0; x < mat6.step; x++) { assert(p[x] == p_[x]); } } return 0; }
mat3 scaling2D(const vec2 &scaleVector) { return mat3( vec3(scaleVector[VX], 0.0, 0.0), vec3(0.0, scaleVector[VY], 0.0), vec3(0.0, 0.0, 1.0)); }
void TrackballHandler::Trackball::mouseMove ( int i, int j ) { vec3 cur = _ij2xyz(i,j); vec3 axis = cross(last, cur); R0 = mat3(rotate(mat4(),float(acos(dot(last,cur))),axis)); R0R = R0*R; }
void emitAndMult( vec3 v, vec3 n, vec2 t ) { gs_out.normal = mat3(mat_model) * n; gs_out.texCoords = t; gl_Position = mat_projection * mat_view * mat_model * vec4( v, 1 ); EmitVertex(); }
wrl::atom::atom(app::node node, std::string _fill_name, std::string _line_name) : edit_geom(0), body_id(0), fill(0), line(0), fill_name(_fill_name), line_name(_line_name), line_scale(1, 1, 1) { // Load the named file and line units. if (fill_name.empty() && node) fill_name = node.find("file").get_s(); if (line_name.empty() && !fill_name.empty()) line_name = line_from_fill(fill_name); if (!fill_name.empty()) fill = new ogl::unit(fill_name); if (!line_name.empty()) line = new ogl::unit(line_name); // Initialize the transform and body mappings. if (node) { app::node n; quat q; vec3 p; if ((n = node.find("rot_x"))) q[0] = n.get_f(); if ((n = node.find("rot_y"))) q[1] = n.get_f(); if ((n = node.find("rot_z"))) q[2] = n.get_f(); if ((n = node.find("rot_w"))) q[3] = n.get_f(); if ((n = node.find("pos_x"))) p[0] = n.get_f(); if ((n = node.find("pos_y"))) p[1] = n.get_f(); if ((n = node.find("pos_z"))) p[2] = n.get_f(); if ((n = node.find("body"))) body_id = n.get_i(); // Compute and apply the transform. current_M = mat4(mat3(q)); current_M[0][3] = p[0]; current_M[1][3] = p[1]; current_M[2][3] = p[2]; default_M = current_M; } if (fill_name.empty() && line_name.empty() && node == 0) throw std::runtime_error("Empty atom constructor"); }
// Given an actor and desired velocity, calculate a corresponding torque vec3 Behavior::CalculateTorque(Actor &actor, const vec3& dvel) { // 1. Get current rotation matrix mat3 currentRotation(actor.globalOrientation); // 2. Construct desired rotation matrix // (This corresponds to facing in the direction of our desired velocity) // Note: Z points forwards; Y Points UP; and X points left vec3 y(0,1,0), z(dvel); z.Normalize(); vec3 x = y.Cross(z); mat3 desiredRotatioon = mat3(x, y, z).Transpose(); // 3. Compute the change in rotation matrix that will // rotate the actor towards our desired rotation mat3 deltaMatrixRot = desiredRotatioon * currentRotation.Transpose(); // 4. Construct quaternion to get axis and angle from dr Quaternion deltaQuadRot = deltaMatrixRot.ToQuaternion(); vec3 axis; //Dont need rad angle? float radAngle; deltaQuadRot.ToAxisAngle(axis, radAngle); // find torque vec3 w = actor.angularVelocity; mat3 I = actor.globalInertialTensor; return w.Cross(I * w) + (I * ( g_fOriKp * (radAngle * axis) - g_fOriKv * w)); }
mat3 translation2D(const vec2 &v) { return mat3( vec3(1.0, 0.0, v[VX]), vec3(0.0, 1.0, v[VY]), vec3(0.0, 0.0, 1.0)); }
mat3 identity2D() { return mat3( vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0)); }
mat3 mat3::transpose() const { return mat3( vec3(v[0][0], v[1][0], v[2][0]), vec3(v[0][1], v[1][1], v[2][1]), vec3(v[0][2], v[1][2], v[2][2])); }
mat3 mat3::TransposeMatrix() { return mat3( vec3(column0.x, column1.x, column2.x), vec3(column0.y, column1.y, column2.y), vec3(column0.z, column1.z, column2.z)); }
void CQuad::Update(float dt, const LightSource &lights) { #ifdef LIGHTING_WITHCPU if( m_bViewUpdated || m_bTRSUpdated ) { // Model View 的相關矩陣內容有更動 m_mxMVFinal = m_mxView * m_mxTRS; m_mxMV3X3Final = mat3( m_mxMVFinal._m[0].x, m_mxMVFinal._m[1].x, m_mxMVFinal._m[2].x, m_mxMVFinal._m[0].y, m_mxMVFinal._m[1].y, m_mxMVFinal._m[2].y, m_mxMVFinal._m[0].z, m_mxMVFinal._m[1].z, m_mxMVFinal._m[2].z); #ifdef GENERAL_CASE m_mxITMV = InverseTransposeMatrix(m_mxMVFinal); #endif m_bViewUpdated = m_bTRSUpdated = false; } if (m_iMode == FLAT_SHADING) RenderWithFlatShading(lights); else RenderWithGouraudShading(lights); #else // Lighting With GPU if (m_bViewUpdated || m_bTRSUpdated) { m_mxMVFinal = m_mxView * m_mxTRS; m_bViewUpdated = m_bTRSUpdated = false; } m_vLightInView = m_mxView * lights.position; // 將 Light 轉換到鏡頭座標再傳入 // 算出 AmbientProduct DiffuseProduct 與 SpecularProduct 的內容 m_AmbientProduct = m_Material.ka * m_Material.ambient * lights.ambient; m_AmbientProduct.w = m_Material.ambient.w; m_DiffuseProduct = m_Material.kd * m_Material.diffuse * lights.diffuse; m_DiffuseProduct.w = m_Material.diffuse.w; m_SpecularProduct = m_Material.ks * m_Material.specular * lights.specular; m_SpecularProduct.w = m_Material.specular.w; #endif }
mat3 operator * (mat3& a, mat3& b) { #define ROWCOL(i, j) \ a.v[i].n[0]*b.v[0][j] + a.v[i].n[1]*b.v[1][j] + a.v[i].n[2]*b.v[2][j] return mat3(vec3(ROWCOL(0,0), ROWCOL(0,1), ROWCOL(0,2)), vec3(ROWCOL(1,0), ROWCOL(1,1), ROWCOL(1,2)), vec3(ROWCOL(2,0), ROWCOL(2,1), ROWCOL(2,2))); #undef ROWCOL }
mat3 ComponentPhysicsGeom::getOrientation() const { const dReal *r = dGeomGetRotation(geom); return mat3(vec3((float)r[0], (float)r[1], (float)r[2]), vec3((float)r[4], (float)r[5], (float)r[6]), vec3((float)r[8], (float)r[9], (float)r[10])); }
Light::Data DirectionalLight::getData( double time, const mat4 &transform ) const { // Populate the LightData structure. Light::Data params = Light::getData( time, transform ); params.direction = glm::normalize( mat3( transform ) * mDirection ); return params; }
// Helper rotation function. Please implement this. mat3 Transform::rotate(const double degrees, const vec3& axis) { mat3 ret; // YOUR CODE FOR HW2 HERE // Please implement this. Likely the same as in HW 1. vec3 axis_norm = glm::normalize(axis); mat3 axisSkew = mat3( 0, axis_norm.z, -axis_norm.y, // first column -axis_norm.z, 0, axis_norm.x, // second column axis_norm.y, -axis_norm.x, 0 // third column ); ret = (glm::cos(glm::radians(degrees)) * mat3(1.0f)) + ((1-glm::cos(glm::radians(degrees))) * glm::core::function::matrix::outerProduct(axis_norm, axis_norm)) + (glm::sin(glm::radians(degrees)) * axisSkew); return ret; }
/* Given a angle in degrees for rotation returns a rotation matrix. */ mat3 mat3::rotation2D(float angle) { /* Convert to radians and create the rotation matrix */ return mat3(vec3((float)cos((PI*angle) / 180), (float)(-1 * sin((PI*angle) / 180)), 0), vec3((float)sin((PI*angle) / 180), (float)cos((PI*angle) / 180), 0), vec3(0, 0, 1)); }
// Helper rotation function. Please implement this. mat3 Transform::rotate(const float degrees, const vec3& axis) { mat3 ret; // YOUR CODE FOR HW2 HERE // Please implement this. Likely the same as in HW 1. float radius = pi * degrees / 180; mat3 Identity3x3 = mat3(1, 0, 0, 0, 1, 0, 0, 0, 1); mat3 part2 = mat3(axis.x*axis.x, axis.x*axis.y, axis.x*axis.z, axis.x*axis.y, axis.y*axis.y, axis.y*axis.z, axis.x*axis.z, axis.y*axis.z, axis.z*axis.z); mat3 part3 = mat3(0, axis.z, -axis.y, -axis.z, 0, axis.x, axis.y, -axis.x, 0); mat3 part1 = Identity3x3 * cos(radius); ret = part1 + (1 - cos(radius)) * part2 + sin(radius) * part3; return ret; }
void Paint::SetMatrices() { mat4 mv = _view * _model; _shader.SetUniform("ModelViewMatrix", mv); _shader.SetUniform("NormalMatrix", mat3(vec3(mv[0]), vec3(mv[1]), vec3(mv[2]))); _shader.SetUniform("MVP", _projection * mv); }
void SceneRenderToTex::setMatrices() { mat4 mv = view * model; prog.setUniform("ModelViewMatrix", mv); prog.setUniform("NormalMatrix", mat3( vec3(mv[0]), vec3(mv[1]), vec3(mv[2]) )); prog.setUniform("MVP", projection * mv); }
mat3 operator-(const mat3& a) { return mat3( -a(0,0), -a(0,1), -a(0,2), -a(1,0), -a(1,1), -a(1,2), -a(2,0), -a(2,1), -a(2,2) ); }
void CameraMove(Camera &camera, float x, float y, float z) { // сначала нам надо перевести вектор направления в локальные координаты камеры // для этого нам надо инвертировать матрицу вращения камеры и умножить ее на вектор // однако для матрицы вращения транспонирование дает тот же результат что инвертирование vec3 move = transpose(mat3(GLRotation(camera.rotation))) * vec3(x, y, z); camera.position += move; }
mat3 rotation2D(vec2& Center, const double angleDeg) { double angleRad = angleDeg * M_PI / 180.0, c = cos(angleRad), s = sin(angleRad); return mat3(vec3(c, -s, Center[VX] * (1.0-c) + Center[VY] * s), vec3(s, c, Center[VY] * (1.0-c) - Center[VX] * s), vec3(0.0, 0.0, 1.0)); }
mat3 matrixCross(vec3 v) { return mat3 ( vec3(0, -v[2], v[1]), vec3(v[2], 0, -v[0]), vec3(-v[1], v[0], 0) ); }
void Transform::up(float degrees, vec3& eye, vec3& up) { float magnitude = sqrt(pow(eye.x,2) + pow(eye.y,2) + pow(eye.z,2)); vec3 axis = glm::cross(eye, up); axis = glm::normalize(axis); mat3 R = rotate(degrees, axis); eye = eye * R; eye = glm::normalize(eye) * mat3(magnitude); up = up * R; up = glm::normalize(up); }
mat3 Transform::rotate(const float degrees, const vec3& axis) { mat3 R; float degrees_rad = glm::radians(degrees); float x = axis[0]; float y = axis[1]; float z = axis[2]; mat3 I = cos(degrees_rad) * mat3(1.0); mat3 parallel = (1 - cos(degrees_rad)) * mat3(x*x, x*y, x*z, x*y, y*y, y*z, x*z, y*z, z*z); mat3 perpendicular = sin(degrees_rad) * mat3(0, -z, y, z, 0, -x, -y, x, 0); R = I + parallel + perpendicular; return R ; }