我需要在OpenGL项目中轻松更改多边形比例,但是由于某种原因,光强度与多边形的大小成正比。
我试图将光的方向移到不同的地方。更改缩放器上的光强度和div法线向量,但这无济于事。
在我的代码中scaler
表示多边形压缩=>如果scaler = 2
,poly
变成0.5大小
顶点坐标:
verticesMatrix[i][k] = new Vector3f((float) (i - cellCount / 2) / scaler,
(float) 0, (float) (k - cellCount / 2) / scaler);
计算法线:
public void calcNormal(Vector3f s1, Vector3f s2, Vector3f s3) {
Vector3f a = new Vector3f(s2.x - s1.x, s2.y - s1.y, s2.z - s1.z);
Vector3f b = new Vector3f(s3.x - s2.x, s3.y - s2.y, s3.z - s2.z);
Vector3f normal = new Vector3f(a.y * b.z - a.z * b.y, a.z * b.x - a.x
* b.z, a.x * b.y - a.y * b.x);
glNormal3f(normal.x, normal.y, normal.z);
}
灯光设置:
private static float[] lightPosition = {-2.19f, 100.36f, 11.45f, 1f};
private static void setUpLighting() {
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightModel(GL_LIGHT_MODEL_AMBIENT,
BufferTools.asFlippedFloatBuffer(new float[]{0.0f, 0.0f, 0.0f, 1f}));
glLight(GL_LIGHT0, GL_POSITION,
BufferTools.asFlippedFloatBuffer(lightPosition));
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_DIFFUSE);
}
我需要使用不同的定标器使用相同的光源。我不知道它如何影响光,因为我检查了所有代码,并且不对光使用定标器。结果图片:https://yadi.sk/i/kxQLvRSTP7LYVA
答案 0 :(得分:0)
我需要像这样规范正常:
public void calcNormal(Vector3f s1, Vector3f s2, Vector3f s3) {
Vector3f a = new Vector3f(s2.x - s1.x, s2.y - s1.y, s2.z - s1.z);
Vector3f b = new Vector3f(s3.x - s2.x, s3.y - s2.y, s3.z - s2.z);
Vector3f normal = new Vector3f(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
float length = (float)Math.pow(normal.x * normal.x + normal.y * normal.y + normal.z * normal.z, 0.5f);
glNormal3f(normal.x / length, normal.y / length, normal.z / length);
}