3D Würfel will sich nicht drehen! [LWJGL]

CookieSoft

Mitglied
Hallo liebe User,
befasse mich gerade mit LWJGL und wollte einen drehenden 3D Würfel zeichnen doch alles was ich zu sehen bekomme sind komische Linien die größer und kleiner werden. Wenn ich den Würfel ohne Rotation anzeige dann sehe ich halt einen 2D Würfel :shock:! Bitte um Hilfe der Code:
Java:
package org.cookiesoft.display;

import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.openal.AL;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

import static org.lwjgl.opengl.GL11.*;

public class Game {
	
	public static int fps = 60;
	public static int DisplayWidth = 800;
	public static int DisplayHeight = 500;
	public static String DisplayTitle = "Game";
	private static float CubeRot = 0;
	
	public static void main(String[] args) {
		Game.createDisplay();
		Game.init();
	}
	public static void init(){			
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		glOrtho(0.0, Display.getDisplayMode().getWidth(), 0.0, Display.getDisplayMode().getHeight(), -1.0, 1.0);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
		while(!(Display.isCloseRequested())){
			if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)){
				glViewport(10, 0, Display.getDisplayMode().getWidth(), Display.getDisplayMode().getHeight());
			}
			
			paint();
			Display.update();
			Display.sync(60);
		}
		Display.destroy();
	}
	public static void createDisplay(){
		try {
			Display.setDisplayMode(new DisplayMode(DisplayWidth, DisplayHeight));
			Display.setTitle(DisplayTitle);
			Display.create();
		} catch (LWJGLException e) {
			System.err.println("Display can't create!");
		}
	}
	public static void paint(){
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);	
    	GL11.glLoadIdentity();	
    	
		glTranslatef(0.0f, 0.0f,0.0f);	
		GL11.glRotatef(CubeRot,0.0f,1.0f,0.0f);					// Rotate The cube around the Y axis
		GL11.glRotatef(CubeRot,1.0f,1.0f,1.0f);
    	glBegin(GL_QUADS);
    	
    	    //Front
    	    glColor3f(1.0f,0.5f,0.0f);	
    	    glVertex3i(100, 100, 0);
    	    glVertex3i(200, 100, 0);
    	    glVertex3i(200, 200, 0);
    	    glVertex3i(100, 200, 0);
    	    
    	    //Back
    	    glColor3f(0.0f,0.5f,0.0f);	
    	    glVertex3i(100, 100, 100);
    	    glVertex3i(200, 100, 100);
    	    glVertex3i(200, 200, 100);
    	    glVertex3i(100, 200, 100);
    	    
    	    //Left
    	    glColor3f(0.0f,0.5f,0.0f);
    	    glVertex3i(100, 100, 100);
    	    glVertex3i(100, 100, 200);
    	    glVertex3i(100, 200, 200);
    	    glVertex3i(100, 200, 100);
    	    
    	    //Right
    	    glColor3f(0.0f,0.5f,0.0f);
    	    glVertex3i(200, 100, 100);
    	    glVertex3i(200, 100, 200);
    	    glVertex3i(200, 200, 200);
    	    glVertex3i(200, 200, 100);
    	    
    	    //Down
    	    glColor3f(0.0f,0.5f,0.0f);
    	    glVertex3i(100, 100, 100);
    	    glVertex3i(100, 100, 200);
    	    glVertex3i(200, 100, 200);
    	    glVertex3i(200, 100, 100);
    	    
    	    //Up
    	    glColor3f(0.0f,0.5f,0.0f);
    	    glVertex3i(100, 200, 100);
    	    glVertex3i(100, 200, 200);
    	    glVertex3i(200, 200, 200);
    	    glVertex3i(200, 200, 100);
    	    
    	glEnd();
    	
    	CubeRot += 0.1f;
	}

}
 
Moin,

mit glOrtho definierst Du den Bereich, welcher gezeichnet werden soll. Dabei hast Du einen "z" Bereich von -1 bis +1 definiert. Beim Würfel unten verwendest Du dann aber Zahlen von 0 bis 200, wodurch immer nur eine dünne Scheibe des Würfels gezeichnet wird (das sieht dann aus wie Linien).

Spätestens wenn es Richtung 3D geht ist die Angabe von Koordinaten in Pixeln aber sowieso eine schlechte Idee. Nimm lieber etwas "Normierteres". Z.B. (-1,+1) auf allen Achsen. Außerdem möchtest Du wahrscheinlich (Glaskugel) Deinen Würfel nicht orthogonal, sondern perspektivisch korrekt zeichnen, das wäre dann nicht glOrtho sondern glFrustum oder gluPerspective.

Dein Code sähe dann etwa so aus:

Java:
package org.cookiesoft.display;

import static org.lwjgl.opengl.GL11.GL_CULL_FACE;
import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST;
import static org.lwjgl.opengl.GL11.GL_LEQUAL;
import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glDepthFunc;
import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glFrustum;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glRotatef;
import static org.lwjgl.opengl.GL11.glTranslatef;
import static org.lwjgl.opengl.GL11.glVertex3f;
import static org.lwjgl.opengl.GL11.glViewport;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

public class Game {

    public static int    fps           = 60;
    public static int    displayWidth  = 800;
    public static int    displayHeight = 500;
    public static String displayTitle  = "Game";
    private static float cubeRot       = 0;


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

        Game.createDisplay();
        Game.init();

    }


    public static void init() {

        glEnable(GL_DEPTH_TEST);
        glDepthFunc(GL_LEQUAL);
        glEnable(GL_CULL_FACE);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();

        final double fov = 45.0;
        final double zNear = 0.01;
        final double zFar = 100;
        final double aspect = (double) Display.getDisplayMode().getWidth() / (double) Display.getDisplayMode().getHeight();
        final double yPlane = Math.tan(Math.toRadians(fov / 2.0)) * zNear;
        final double xPlane = yPlane * aspect;

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glFrustum(-xPlane, +xPlane, -yPlane, +yPlane, zNear, zFar);

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

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

            paint();
            Display.update();
            Display.sync(60);

        }

        Display.destroy();

    }


    public static void createDisplay() {

        try {

            Display.setDisplayMode(new DisplayMode(displayWidth, displayHeight));
            Display.setTitle(displayTitle);
            Display.create();

        } catch (final LWJGLException e) {

            System.err.println("Display can't create!");

        }
    }


    public static void paint() {

        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        GL11.glLoadIdentity();

        glTranslatef(0.0f, 0.0f, -5f);
        glRotatef(cubeRot, 0.0f, 1.0f, 0.0f); // Rotate The cube around the Y axis
        glRotatef(cubeRot, 1.0f, 1.0f, 1.0f);

        glBegin(GL_QUADS);

        // Front
        glColor3f(1.0f, 0.5f, 0.0f);
        glVertex3f(-1.0f, -1.0f, +1.0f);
        glVertex3f(+1.0f, -1.0f, +1.0f);
        glVertex3f(+1.0f, +1.0f, +1.0f);
        glVertex3f(-1.0f, +1.0f, +1.0f);

        // Back
        glColor3f(0.0f, 0.5f, 0.0f);
        glVertex3f(-1.0f, -1.0f, -1.0f);
        glVertex3f(-1.0f, +1.0f, -1.0f);
        glVertex3f(+1.0f, +1.0f, -1.0f);
        glVertex3f(+1.0f, -1.0f, -1.0f);

        // Left
        glColor3f(0.0f, 0.5f, 1.0f);
        glVertex3f(-1.0f, -1.0f, -1.0f);
        glVertex3f(-1.0f, -1.0f, +1.0f);
        glVertex3f(-1.0f, +1.0f, +1.0f);
        glVertex3f(-1.0f, +1.0f, -1.0f);

        // Right
        glColor3f(0.5f, 1.0f, 0.0f);
        glVertex3f(+1.0f, -1.0f, -1.0f);
        glVertex3f(+1.0f, +1.0f, -1.0f);
        glVertex3f(+1.0f, +1.0f, +1.0f);
        glVertex3f(+1.0f, -1.0f, +1.0f);

        // Down
        glColor3f(0.5f, 0.0f, 1.0f);
        glVertex3f(-1.0f, -1.0f, +1.0f);
        glVertex3f(-1.0f, -1.0f, -1.0f);
        glVertex3f(+1.0f, -1.0f, -1.0f);
        glVertex3f(+1.0f, -1.0f, +1.0f);
        
        // Up
        glColor3f(1.0f, 0.0f, 0.5f);
        glVertex3f(-1.0f, +1.0f, -1.0f);
        glVertex3f(-1.0f, +1.0f, +1.0f);
        glVertex3f(+1.0f, +1.0f, +1.0f);
        glVertex3f(+1.0f, +1.0f, -1.0f);

        glEnd();

        cubeRot += 0.5f;

    }

}

Viel Grüße,
Fancy
 

Zurück
Oben