void Shader::UpdateUniforms(RenderingEngine& _renderingEngine) { for (unsigned int i = 0; i < shaderData->uniformNames.size(); i++) { string uniformName = shaderData->uniformNames[i]; string uniformType = shaderData->uniformTypes[i]; if (uniformName.substr(0, 2) == "R_") { string unprefixedName = uniformName.substr(2, uniformName.length()); if (unprefixedName == "lightMatrix") { SetMatrix4(uniformName, 1, _renderingEngine.GetLightMatrix()); } else if (uniformType == "sampler2D") { int samplerSlot = _renderingEngine.GetSamplerSlot(unprefixedName); _renderingEngine.GetTexture(unprefixedName)->Bind(samplerSlot); SetInt(uniformName, samplerSlot); } else if (uniformType == "vec3") { SetVector3(uniformName, *_renderingEngine.GetVector3(unprefixedName)); } else if (uniformType == "float") { SetFloat(uniformName, *_renderingEngine.GetFloat(unprefixedName)); } else if (uniformType == "int") { if (uniformName == "R_DIR_LIGHT_COUNT") { SetInt(uniformName, _renderingEngine.GetActiveDirLights().size()); } if (uniformName == "R_POINT_LIGHT_COUNT") { SetInt(uniformName, _renderingEngine.GetActivePointLights().size()); } } else if (uniformType == "DirLight") { SetDirectionalLights(uniformName, _renderingEngine.GetActiveDirLights()); } else if (uniformType == "PointLight") { SetPointLights(uniformName, _renderingEngine.GetActivePointLights()); } else { throw "Invalid R_ Uniform:" + uniformName; } } } }
/************************************************************************* function :SetMatrix4 description :×óϽÇΪÆðʼµãµÄ˳ʱÕëÂÝÐýÅÅÁÐ(µÝ¹éÐÍ) **************************************************************************/ void SetMatrix4(DWORD **rotaMatrix, DWORD **oriMatrix, int x, int y, DWORD index, int m, int n, int M, int N) { int i, j; if (n<=0 || m<=0) return; if ((n==1) && (m==1)) { rotaMatrix[x][y] = oriMatrix[index/N][index%N]; return; } else if((m==1)&&(n!=1)) { for (i = x; i < x + n; i++) // Éϲ¿ { rotaMatrix[y][i] = oriMatrix[index/N][index%N]; index++; } return ; } else if((m!=1)&&(n==1)) { for (j = y; j > (M-y-2); j--) // ×ó±ß { rotaMatrix[j][x] = oriMatrix[index/N][index%N]; index++; } return ; } for (j = y; j > (M-y-1); j--) // ×ó±ß { rotaMatrix[j][x] = oriMatrix[index/N][index%N]; index++; } for (i = x; i < x + n-1; i++) // Éϲ¿ { rotaMatrix[y-m+1][i] = oriMatrix[index/N][index%N]; index++; } for (j = y-m+1; j < y; j++) // ÓÒ±ß { rotaMatrix[j][x+n-1] = oriMatrix[index/N][index%N]; index++; } for (i = x+n-1; i > x; i--) // µ×²¿ { rotaMatrix[y][i] = oriMatrix[index/N][index%N]; index++; } // printf("\n"); // disMatrix(matrix, M, N); SetMatrix4(rotaMatrix, oriMatrix, x+1, y-1, index, m-2, n-2, M, N); /* µÝ¹é */ }
void Shader::UpdateTransformUniforms(const Transform& _transform) { mat4 worldMatrix = _transform.worldMatrix; for (unsigned int i = 0; i < shaderData->uniformNames.size(); i++) { string uniformName = shaderData->uniformNames[i]; string uniformType = shaderData->uniformTypes[i]; if (uniformName.substr(0, 2) == "T_") { if (uniformName == "T_model") { SetMatrix4(uniformName, 1, worldMatrix); } else { throw "Invalid Transform Uniform: " + uniformName; } } } }
void Shader::UpdateCameraUniforms(const Camera& _camera) { mat4 viewProj = _camera.projectionMatrix * _camera.viewMatrix; for (unsigned int i = 0; i < shaderData->uniformNames.size(); i++) { string uniformName = shaderData->uniformNames[i]; string uniformType = shaderData->uniformTypes[i]; if (uniformName.substr(0, 2) == "C_") { //uniformName = uniformName.substr(2, uniformName.length()); if (uniformName == "C_eyePos") { vec3 cameraPos = _camera.transform->position; if (_camera.transform->parent) { cameraPos += _camera.transform->parent->position; } SetVector3(uniformName, cameraPos); } else if (uniformName == "C_viewProj") { SetMatrix4(uniformName, 1, viewProj); } else { throw "Invalid Camera Uniform: " + uniformName; } } } }
/************************************************************************* function :RotaMatrix param :matrix, M, N, opr return :none description :½«matrix¾ØÕóµÄÄÚÈÝ°´Ö¸¶¨µÄ·½Ê½ÂÝÐýÅÅÁÐ **************************************************************************/ void RotaMatrix(DWORD **matrix, int M, int N, int opr) { DWORD **rotaMatrix; rotaMatrix = (DWORD **)malloc(M * sizeof(DWORD *)); //Ϊ¾ØÕó·ÖÅä¿Õ¼ä for (int i = 0; i<M; i++) rotaMatrix[i] = (DWORD *)malloc(N * sizeof(DWORD)); switch (opr) { case 1: SetMatrix1(rotaMatrix, matrix, 0, 0, 0, M, N, M, N); break; case 2: SetMatrix2(rotaMatrix, matrix, N-1, 0, 0, M, N, M, N); break; case 3: SetMatrix3(rotaMatrix, matrix, N-1, M-1, 0, M, N, M, N); break; case 4: SetMatrix4(rotaMatrix, matrix, 0, M-1, 0, M, N, M, N); break; default: break; } //equ equMatrix(matrix, rotaMatrix, M, N); //release buffer for (int i = 0; i<M; i++) free(rotaMatrix[i]); free(rotaMatrix); }
void ShaderConstant::SetMatrix( const Matrix4& matrix ) { MEDUSA_ASSERT(mDataType==GraphicsUniformDataType::FloatMat4,"ErrorDataType"); SetMatrix4(1,matrix.Items()); }
void ShaderUniform::SetMatrix(const Matrix4& matrix) { MEDUSA_ASSERT(mDataType == GraphicsUniformDataType::FloatMat4, "ErrorDataType"); SetMatrix4(MemoryFloatData::FromStatic(matrix.Items(), 16 * sizeof(float))); }