2d Szene - Bildschirm- zu Weltkoordinaten?


#41

gluUnProject macht im wesentlichen dasselbe. Und in einem KSKB verhält sich das auch so, wie man es erwarten würde:

import java.nio.IntBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.ContextAttribs;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.PixelFormat;
import org.lwjgl.util.glu.GLU;


public final class KskbOpenGLUnProject {

    private static final int WIDTH = 800;
    private static final int HEIGHT = 600;

    private FloatBuffer modelview;
    private FloatBuffer projection;
    private IntBuffer viewport;

    private FloatBuffer unproject;

    private boolean exit;


    public static void main(final String[] args) throws LWJGLException {

        final DisplayMode displayMode = new DisplayMode(WIDTH, HEIGHT);
        final PixelFormat pixelFormat = new PixelFormat();
        final ContextAttribs contextAttribs = new ContextAttribs();

        new KskbOpenGLUnProject().run(displayMode, pixelFormat, contextAttribs);

    }


    void run(final DisplayMode displayMode, final PixelFormat pixelFormat, final ContextAttribs contextAttribs) throws LWJGLException {

        Display.setDisplayMode(displayMode);
        Display.create(pixelFormat, contextAttribs);
        Display.setVSyncEnabled(true);

        glInit();

        while (!Display.isCloseRequested() && !exit) {

            glDisplay();
            Display.update();

        }

        Display.destroy();

    }


    private void glInit() {

        modelview = getModelviewMatrix();
        projection = getProjectionMatrix();
        viewport = getViewport();

        unproject = BufferUtils.createFloatBuffer(3);

    }


    private void glDisplay() {

        if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
            exit = true;

        final int mouseWcX = Mouse.getX();
        final int mouseWcY = Mouse.getY();

        final boolean result = GLU.gluUnProject(mouseWcX, mouseWcY, 0, modelview, projection, viewport, unproject);

        if (!result)
            throw new RuntimeException();

        final float mouseObjectX = unproject.get(0);
        final float mouseObjectY = unproject.get(1);

        System.out.println("Mouse in Window Coordinates: " + mouseWcX + ", " + mouseWcY + "; Mouse in Object Coordinates: " + mouseObjectX + ", " + mouseObjectY);

    }


    private FloatBuffer getModelviewMatrix() {

        final FloatBuffer buffer = BufferUtils.createFloatBuffer(16);

        for (int i = 0; i < 4; i++)
            buffer.put(i * 5, 1.0f);

        buffer.rewind();

        return buffer;

    }


    private FloatBuffer getProjectionMatrix() {

        final float aspect = (float) Display.getWidth() / (float) Display.getHeight();

        System.out.println("glOrtho( -" + aspect + ", " + aspect + ", -1, 1, -1, 1)");

        final FloatBuffer buffer = BufferUtils.createFloatBuffer(16);

        buffer.put(0, 2.0f / (aspect + aspect));
        buffer.put(5, 1.0f);
        buffer.put(10, -1);
        buffer.put(15, 1);

        buffer.rewind();

        return buffer;

    }


    private IntBuffer getViewport() {

        final IntBuffer buffer = BufferUtils.createIntBuffer(4);

        buffer.put(0);
        buffer.put(0);
        buffer.put(Display.getWidth());
        buffer.put(Display.getHeight());

        buffer.rewind();

        return buffer;

    }


}

Viele Grüße
Fancy