int x[2], y[2], z[2]; m_pNormals = new float [3 * m_nSumPointOfDem]; float normal[3], rate; for (int i = 0; i < m_nDemY - 1; i++) { for (int j = 0; j < m_nDemX - 1; j++) { x[0] = m_pDemX[(i + 1) * m_nDemX + j] - m_pDemX[i * m_nDemX + j]; x[1] = m_pDemX[i * m_nDemX + (j + 1)] - m_pDemX[i * m_nDemX + j]; y[0] = m_pDemY[(i + 1) * m_nDemX + j] - m_pDemY[i * m_nDemX + j]; y[1] = m_pDemY[i * m_nDemX + (j + 1)] - m_pDemY[i * m_nDemX + j]; z[0] = m_pDemH[(i + 1) * m_nDemX + j] - m_pDemH[i * m_nDemX + j]; z[1] = m_pDemH[i * m_nDemX + (j + 1)] - m_pDemH[i * m_nDemX + j]; normal[0] = (float)(y[1] * z[0] - z[1] * y[0]); normal[1] = (float)(z[1] * x[0] - x[1] * z[0]); normal[2] = (float)(x[1] * y[0] - y[1] * x[0]); rate = (float)sqrt(normal[0] * normal[0] + normal[1] * normal[1] + normal[2] * normal[2]); normal[0] /= rate; normal[1] /= rate; normal[2] /= rate; m_pNormals[(i * m_nDemX + j) * 3 + 0] = normal[0]; m_pNormals[(i * m_nDemX + j) * 3 + 1] = normal[1]; m_pNormals[(i * m_nDemX + j) * 3 + 2] = normal[2]; } } |
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); // 定义材质的环境反射光 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); // 定义材质的漫反射光 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); // 定义材质镜面反射光 glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); // 定义反射光亮度 |
// 定义材质镜面反射光 mat_shininess[0] = 50.0f; // 定义材质的环境反射光 mat_ambient[0] = 0.3f;mat_ambient[1] = 0.3f;mat_ambient[2] = 0.3f; mat_ambient[3] = 1.0f; // 定义材质的漫反射光 mat_diffuse[0] = 0.9f;mat_diffuse[1] = 0.9f;mat_diffuse[2] = 0.7f;mat_diffuse[3] = 1.0f; // 定义材质镜面反射光 mat_specular[0]=1.0f;mat_specular[1] = 1.0f;mat_specular[2] = 1.0f;mat_specular[3] = 1.0f; 即使初始定义了材质,在之后的执行过程中也随时可以更改材质设定: if (dlg.m_bDiffuseColor){ // 定义材质的漫反射光 RGBToGLfloatv(dlg.m_crDiffuseColor,r,g,b); mat_diffuse[0] = r; mat_diffuse[1] = g; mat_diffuse[2] = b; mat_diffuse[3] = 1.0f; } if (dlg.m_bAmbientColor) { // 定义材质的环境反射光 RGBToGLfloatv(dlg.m_crAmbientColor,r,g,b); mat_ambient[0] = r;mat_ambient[1] = g;mat_ambient[2] = b;mat_ambient[3] = 1.0f; } if (dlg.m_bSpecularColor) { // 定义材质镜面反射光 RGBToGLfloatv(dlg.m_crSpecularColor,r,g,b); mat_specular[0] = r;mat_specular[1] = g;mat_specular[2] = b;mat_specular[3] = 1.0f; } mat_shininess[0] = (GLfloat)dlg.m_nShininess; // 光亮度 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); // 定义材质的环境反射光 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); // 定义材质的漫反射光 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); // 定义材质镜面反射光 glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); // 定义材质镜面反射光 |
BYTE r = (BYTE)(color & 0x000000FF); // 提取RGB各分量取值 BYTE g = (BYTE)((color & 0x0000FF00) >> 8); BYTE b = (BYTE)((color & 0x00FF0000) >> 16); rf = r / 255.0f; gf = g / 255.0f; bf = b / 255.0f; |
glNewList(Terrain, GL_COMPILE); // 开始一个新的显示列表 for (int i = 0; i < m_nDemY - 1; i++) { // 绘制三角网格 for(int j = 0; j < m_nDemX - 1; j++) {// 开始定义三角带 glBegin(GL_TRIANGLE_STRIP); glNormal3d(m_pNormals[(i * m_nDemX + j) * 3 + 0], m_pNormals[(i * m_nDemX + j) * 3 + 1], m_pNormals[(i * m_nDemX + j) * 3 + 2]); // 设置当前法向向量 glVertex3d(m_pDemX[i * m_nDemX + j], m_pDemY[i * m_nDemX + j], m_pDemH[i * m_nDemX + j]); // 定义顶点坐标 glNormal3d(m_pNormals[((i + 1) * m_nDemX + j) * 3 + 0], m_pNormals[((i + 1) * m_nDemX + j) * 3 + 1], m_pNormals[((i + 1) * m_nDemX + j) * 3 + 2]); glVertex3d(m_pDemX[(i + 1) * m_nDemX + j], m_pDemY[(i + 1) * m_nDemX + j], m_pDemH[(i + 1) * m_nDemX + j]); glNormal3d(m_pNormals[(i * m_nDemX + j + 1) * 3 + 0], m_pNormals[(i * m_nDemX + j + 1) * 3 + 1], m_pNormals[(i * m_nDemX + j + 1) * 3 + 2]); glVertex3d(m_pDemX[i * m_nDemX + j + 1], m_pDemY[i * m_nDemX + j + 1], m_pDemH[i * m_nDemX + j + 1]); glNormal3d(m_pNormals[((i + 1) * m_nDemX + j + 1) * 3 + 0], m_pNormals[((i + 1) * m_nDemX + j + 1) * 3 + 1], m_pNormals[((i + 1) * m_nDemX + j + 1) * 3 + 2]); glVertex3d(m_pDemX[(i + 1) * m_nDemX + j + 1], m_pDemY[(i + 1) * m_nDemX + j + 1], m_pDemH[(i + 1) * m_nDemX + j + 1]); glEnd(); // 停止定义三角带 } } glEndList(); // 结束显示列表 |
// 以点建模 glPolygonMode(GL_BACK, GL_POINT); // 以线建模 glPolygonMode(GL_BACK, GL_LINE); // 以面建模 glPolygonMode(GL_BACK, GL_FILL); |