如何固定随模型旋转的照明?

时间:2019-03-24 15:43:59

标签: opengl lwjgl

我的目标是创建带有照明的小场景。但是,灯光似乎与我的渲染对象(测试立方体)一起旋转。

我认为,在大多数阅读的回答中,我应该在投影和模型矩阵模式下使用GL11.glLight。如果我错了,请纠正我。但是,结果是照明有效,但是强度随立方体旋转而变化。

这是我为Stack准备的示例:

package main;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;

public class Stack {

    static int rot;
    static int rot2;

    public static void main(String[] args) {
        Stack r = new Stack();
        r.start();
    }

    public void start() {
        try {
            createWindow();
            initGL();
            run();
        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    private void run() {
        while (!Display.isCloseRequested()) {
            try {
                render();
                Display.update();
                Display.sync(60);
            } catch (Exception e) {

            }
        }
        Display.destroy();
    }

    private void render() {
        rot++;
        rot2++;
        rot2++;
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        GL11.glLoadIdentity();
        GL11.glLight(GL11.GL_LIGHT0,GL11.GL_POSITION,floatBuffer(5,0,10,0));

        GL11.glTranslatef(0f, 0.0f, -7f);
        GL11.glRotatef(rot, 0.0f, 1.0f, 0.0f);
        GL11.glRotatef(rot2, 1.0f, 0.0f, 0.0f);
        renderCube();
        GL11.glRotatef(-rot2, 1.0f, 0.0f, 0.0f);
        GL11.glRotatef(-rot, 0.0f, 1.0f, 0.0f);
        GL11.glTranslatef(0f, 0.0f, 7f);
    }

    public FloatBuffer floatBuffer(float a, float b, float c, float d) {
        float[] data = new float[] { a, b, c, d };
        FloatBuffer fb = BufferUtils.createFloatBuffer(data.length);
        fb.put(data);
        fb.flip();
        return fb;
    }

    private void renderCube() {

        GL11.glBegin(GL11.GL_QUADS);
        GL11.glColor3f(1.0f, 0.7f, 0.7f);
        GL11.glVertex3f(1.0f, 1.0f, -1.0f);
        GL11.glVertex3f(-1.0f, 1.0f, -1.0f);
        GL11.glVertex3f(-1.0f, 1.0f, 1.0f);
        GL11.glVertex3f(1.0f, 1.0f, 1.0f);
        GL11.glColor3f(1.0f, 0.5f, 0.0f);
        GL11.glVertex3f(1.0f, -1.0f, 1.0f);
        GL11.glVertex3f(-1.0f, -1.0f, 1.0f);
        GL11.glVertex3f(-1.0f, -1.0f, -1.0f);
        GL11.glVertex3f(1.0f, -1.0f, -1.0f);
        GL11.glColor3f(1.0f, 0.0f, 0.0f);
        GL11.glVertex3f(1.0f, 1.0f, 1.0f);
        GL11.glVertex3f(-1.0f, 1.0f, 1.0f);
        GL11.glVertex3f(-1.0f, -1.0f, 1.0f);
        GL11.glVertex3f(1.0f, -1.0f, 1.0f);
        GL11.glColor3f(1.0f, 1.0f, 0.0f);
        GL11.glVertex3f(1.0f, -1.0f, -1.0f);
        GL11.glVertex3f(-1.0f, -1.0f, -1.0f);
        GL11.glVertex3f(-1.0f, 1.0f, -1.0f);
        GL11.glVertex3f(1.0f, 1.0f, -1.0f);
        GL11.glColor3f(0.0f, 0.0f, 1.0f);
        GL11.glVertex3f(-1.0f, 1.0f, 1.0f);
        GL11.glVertex3f(-1.0f, 1.0f, -1.0f);
        GL11.glVertex3f(-1.0f, -1.0f, -1.0f);
        GL11.glVertex3f(-1.0f, -1.0f, 1.0f);
        GL11.glColor3f(1.0f, 0.0f, 1.0f);
        GL11.glVertex3f(1.0f, 1.0f, -1.0f);
        GL11.glVertex3f(1.0f, 1.0f, 1.0f);
        GL11.glVertex3f(1.0f, -1.0f, 1.0f);
        GL11.glVertex3f(1.0f, -1.0f, -1.0f);
        GL11.glEnd();
    }

    DisplayMode displayMode;

    private void createWindow() throws Exception {
        Display.setFullscreen(false);
        DisplayMode d[] = Display.getAvailableDisplayModes();
        for (int i = 0; i < d.length; i++) {
            if (d[i].getWidth() == 640 && d[i].getHeight() == 480 && d[i].getBitsPerPixel() == 32) {
                displayMode = d[i];
                break;
            }
        }
        Display.setDisplayMode(displayMode);
        Display.setTitle("Stack Example");
        Display.create();
    }

    private void initGL() {
        GL11.glEnable(GL11.GL_TEXTURE_2D);
        GL11.glShadeModel(GL11.GL_SMOOTH);
        GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        GL11.glClearDepth(1.0);
        GL11.glEnable(GL11.GL_DEPTH_TEST);
        GL11.glDepthFunc(GL11.GL_LEQUAL);

        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();

        GLU.gluPerspective(45.0f, (float) displayMode.getWidth() / (float) displayMode.getHeight(), 0.1f, 100.0f);

        GL11.glEnable(GL11.GL_LIGHTING); // enable lighting
        GL11.glEnable(GL11.GL_LIGHT0); // enable light 0
        GL11.glLightModeli(GL11.GL_LIGHT_MODEL_TWO_SIDE, GL11.GL_TRUE);
        GL11.glEnable(GL11.GL_COLOR_MATERIAL);
        GL11.glColorMaterial(GL11.GL_FRONT_AND_BACK, GL11.GL_AMBIENT_AND_DIFFUSE);

        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
    }
}

enter image description here enter image description here

如果在转换之前使用GL11.glLight,我希望照明是静态的。我很高兴为您提供解释,因为我查找的大多数教程都使用着色器来达到照明效果。

1 个答案:

答案 0 :(得分:3)

根据要评估灯光模型的每个点的局部表面法线矢量来计算照明。

您没有为立方体提供任何法线向量,因此GL将为每个顶点使用非常相同的法线向量。结果,所有立方体面都照亮,就像它们朝向相同的方向。