Вверх ↑
Ответов: 1205
Lono
Рекрут
#1: 2012-10-14 05:25:05 ЛС | профиль | цитата
Собственно, вот, решил поиграться с lwjgl.
Если кого заинтересует, напишу комментарии и обьяснения.

OpenGL - Shoot1 - Screenshot - 101412 - 07:39:43.png

Исходник:

WindowTry.java

package com.semplar.opengllearn;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;

// import of Utils class
// import of Vector3d class
// sorry, cann't uncover commercial classes

public class WindowTry {
	
	private float go = 0.0f;
	private float strafe = 0.0f;
	private float fly = 0.0f;
	
	private float moveSpeed = 1.0f;
	
	private float jaw = 0.0f;
	private float pitch = 0.0f;
	
	private float posx = 0.0f;
	private float posy = 0.0f;
	private float posz = 0.0f;
	
	private int cycleNanos = 0;
	
	private Vector3d moveVector;
	private Vector3d goVector;
	private Vector3d strafeVector;
	private Vector3d upVector;

	public WindowTry() {
		
	}
	
	public boolean hasSpeed() {
		return cycleNanos › 0;
	}
	
	public float secondsElapsed() {
		return cycleNanos / 1000000000.0f;
	}
	
	public void start() {
		try {
			glInit();
			Keyboard.create();
			Mouse.create();
			Mouse.setGrabbed(true);
			
			long prevNanos = System.nanoTime();
			
			while(!Display.isCloseRequested()) {
				
				updateVectors();
				
				glPaint();
				
				GL11.glFlush();
				Display.update();
				
				long currentNanos = System.nanoTime();
				cycleNanos = (int)(currentNanos - prevNanos);
				prevNanos = currentNanos;
				
				processMovement();
				processKeyboard();
				processMouse();
			}
			
			Display.destroy();
			Keyboard.destroy();
			Mouse.destroy();
			
		} catch (LWJGLException e) {
			e.printStackTrace();
		}
	}
	
	private void glInit() throws LWJGLException {
		
		int width = 640;
		int height = 480;
		
		Display.setDisplayMode(new DisplayMode(width, height));
		Display.create();
		GL11.glViewport(0, 0, 640, 480);
		GL11.glLoadIdentity();
		GLU.gluPerspective(45, (float)width / (float)height, 0.1f, 20);
	}
	
//	private static String matrixToString(float[] backing, int index, int max, int fract) {
//		String oneFormat = "%" + max + "." + fract + "f";
//		String lineFormat = oneFormat + ", " + oneFormat + ", " + oneFormat + ", " + oneFormat;
//		String matrixFormat = "{\n\t" + lineFormat + "\n\t" + lineFormat + "\n\t" + lineFormat + "\n\t" + lineFormat + "\n}";
//		return String.format(matrixFormat,
//				backing[index++], backing[index++], backing[index++], backing[index++],
//				backing[index++], backing[index++], backing[index++], backing[index++],
//				backing[index++], backing[index++], backing[index++], backing[index++],
//				backing[index++], backing[index++], backing[index++], backing[index++]);
//	}
//	
	private void glPaint() {
		
		GL11.glMatrixMode(GL11.GL_PROJECTION_MATRIX);
		GL11.glPushMatrix();
		
		GLU.gluLookAt(posx, posy, posz,
				posx + (float)goVector.getXLength(), posy + (float)goVector.getYLength(), posz + (float)goVector.getZLength(),
				(float)upVector.getXLength(), (float)upVector.getYLength(), (float)upVector.getZLength());
		GL11.glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
		GL11.glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
		
		GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
		
		float d = 2.0f;
		
		GL11.glColor3f(1.0f, 1.0f, 1.0f);
		GL11.glBegin(GL11.GL_LINES); {
			
			GL11.glVertex4f(1.0f, 1.0f, 1.0f, d);
			GL11.glVertex4f(1.0f, 1.0f, -1.0f, d);
			GL11.glVertex4f(-1.0f, 1.0f, 1.0f, d);
			GL11.glVertex4f(-1.0f, 1.0f, -1.0f, d);
			GL11.glVertex4f(-1.0f, -1.0f, 1.0f, d);
			GL11.glVertex4f(-1.0f, -1.0f, -1.0f, d);
			GL11.glVertex4f(1.0f, -1.0f, 1.0f, d);
			GL11.glVertex4f(1.0f, -1.0f, -1.0f, d);
			
			GL11.glVertex4f(1.0f, 1.0f, 1.0f, d);
			GL11.glVertex4f(1.0f, -1.0f, 1.0f, d);
			GL11.glVertex4f(-1.0f, 1.0f, 1.0f, d);
			GL11.glVertex4f(-1.0f, -1.0f, 1.0f, d);
			GL11.glVertex4f(-1.0f, 1.0f, -1.0f, d);
			GL11.glVertex4f(-1.0f, -1.0f, -1.0f, d);
			GL11.glVertex4f(1.0f, 1.0f, -1.0f, d);
			GL11.glVertex4f(1.0f, -1.0f, -1.0f, d);
			
			GL11.glVertex4f(1.0f, 1.0f, 1.0f, d);
			GL11.glVertex4f(-1.0f, 1.0f, 1.0f, d);
			GL11.glVertex4f(1.0f, -1.0f, 1.0f, d);
			GL11.glVertex4f(-1.0f, -1.0f, 1.0f, d);
			GL11.glVertex4f(1.0f, -1.0f, -1.0f, d);
			GL11.glVertex4f(-1.0f, -1.0f, -1.0f, d);
			GL11.glVertex4f(1.0f, 1.0f, -1.0f, d);
			GL11.glVertex4f(-1.0f, 1.0f, -1.0f, d);
			
		} GL11.glEnd();
		
		GL11.glMatrixMode(GL11.GL_PROJECTION_MATRIX);
		GL11.glPopMatrix();
		
	}
	
	private void processKeyboard() {
		
		while(Keyboard.next()) {
			
			boolean down = Keyboard.getEventKeyState();
			int key = Keyboard.getEventKey();
			
			float value = down ? 1.0f : 0.0f;
			
			switch(key) {
			
			case Keyboard.KEY_W:
				go = value;
				break;
			case Keyboard.KEY_S:
				go = -value;
				break;
			case Keyboard.KEY_A:
				strafe = value;
				break;
			case Keyboard.KEY_D:
				strafe = -value;
				break;
			case Keyboard.KEY_SPACE:
				fly = value;
				break;
			case Keyboard.KEY_LSHIFT:
				fly = -value;
				break;
			}
		}
	}
	
	private void processMouse() {
		
		while(Mouse.next()) {
			
			int dx = Mouse.getEventDX();
			int dy = Mouse.getEventDY();
			
			jaw -= dx * (1.5d * Math.PI / 2.0d / 360.0d);
			pitch += dy * (1.5d * Math.PI / 2.0d / 360.0d);
		}
	}
	
	private void updateVectors() {
		
		// jaw's zero directed to the positive X
		Vector3d defVector = Utils.rotate(Utils.Z_AXIS, Utils.X_AXIS, jaw);
		strafeVector = Utils.perpendicular(Utils.Z_AXIS, defVector);
		goVector = Utils.rotate(strafeVector, defVector, -pitch);
		upVector = Utils.perpendicular(goVector, strafeVector);
		
		float sec = secondsElapsed();
		
		moveVector = goVector.product(go)
				.concat(strafeVector.product(strafe))
				.concat(Utils.Z_AXIS.product(fly));
		
		if(!moveVector.isZero())
			moveVector = moveVector.normalize().product(moveSpeed * sec);
	}
	
	private void processMovement() {
		
		posx += moveVector.getXLength();
		posy += moveVector.getYLength();
		posz += moveVector.getZLength();
//		
//		System.out.format("posx: %5.2f, posy: %5.2f, posz: %5.2f, ", posx, posy, posz);
//		System.out.format("jaw: %5.2f, pitch: %5.2f, speed: %5.2f\n", jaw, pitch, moveVector.length());
	}
	
	public static void main(String[] args) {
		if(args.length != 0)
			throw new IllegalArgumentException("args is not supported");
		new WindowTry().start();
	}
}

Можно летать вверх/вниз (пробел/shift), влево/вправо (A/D), вперед/назад (W/S), смотреть в разные стороны, двигая мышкой.
карма: 14
semplar2007@gmail.com
0