作者:i_dovelemon
日期:2016/06/10
來源:CSDN
主題:OpenGL,glUniformMatrix4fv
引言
問題描述
void Matrix::MakeScaleMatrix(float sx, float sy, float sz) {
float m[4][4] = {
sx, 0.0f, 0.0f, 0.0f,
0.0f, sy, 0.0f, 0.0f,
0.0f, 0.0f, sz, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
memcpy(&m_Matrix, m, sizeof(m));
}
void Matrix::MakeRotateZMatrix(float rotate_degree) {
float ran = rotate_degree / 180.0f * 3.1415927f;
float m[4][4] = {
cos(ran), -sin(ran), 0.0f, 0.0f,
sin(ran), cos(ran), 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f,
};
memcpy(&m_Matrix, m, sizeof(m));
}
void Matrix::MakeTranslateMatrix(float tx, float ty, float tz) {
float m[4][4] = {
1.0f, 0.0f, 0.0f, tx,
0.0f, 1.0f, 0.0f, ty,
0.0f, 0.0f, 1.0f, tz,
0.0f, 0.0f, 0.0f, 1.0f,
};
memcpy(&m_Matrix, m, sizeof(m));
}
int32_t ShaderProgram::UpdateUniformMatrix(const char* name, float* v) {
int32_t result = 0;
if(name != NULL && v != NULL) {
// Check if this is the first time to update uniform
int32_t location = 0;
for(int32_t i = 0; i < m_UniformMap.size(); i++) {
if(!strcmp(m_UniformMap[i].m_Name, name)) {
location = m_UniformMap[i].m_Location;
break;
}
}
if(location == 0) {
// Yes, get the location of the uniform variable
location = glGetUniformLocation(m_Program, name);
if(location == -1) { // Doesn't get the uniform's location
result = -1;
location = 0;
} else { // Do get the uniform's location and save it
UniformMap uniform_map(location, name);
m_UniformMap.push_back(uniform_map);
}
}
// Update the uniform matrix
if(location != 0) {
<strong>glUniformMatrix4fv(location, 1, false, v); </strong>
}
} else {
GLB_SAFE_ASSERT(false);
}
return result;
}
問題解決
void Matrix::MakeTranslateMatrix(float tx, float ty, float tz) {
float m[4][4] = {
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
tx, ty, tz, 1.0f,
};
memcpy(&m_Matrix, m, sizeof(m));
}
然後在進行嘗試,意外的成功了!WTF,什麼情況啊這是?不是說要column-major的嗎?爲什麼會是row-major的成功了?