Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.nio.FloatBuffer;
- import java.nio.IntBuffer;
- import java.util.ArrayList;
- import org.lwjgl.BufferUtils;
- import org.lwjgl.LWJGLException;
- import org.lwjgl.Sys;
- 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.opengl.GL15;
- import org.lwjgl.util.glu.GLU;
- import org.newdawn.slick.opengl.Texture;
- import org.newdawn.slick.opengl.TextureLoader;
- public class WorldTest {
- static float[] GRASS = Block.texCoords(1, 0, 0, 1, 0, 0, 4);
- static float[] SAND = Block.texCoords(1, 1, 1, 1, 1, 1, 4);
- static float[] BRICK = Block.texCoords(2, 0, 2, 0, 2, 0, 4);
- static float[] STONE = Block.texCoords(2, 1, 2, 1, 2, 1, 4);
- int width = 800;
- int height = 600;
- long lastFrame;
- int fps;
- long lastFPS;
- private Texture texture;
- private Vector position = new Vector(0, -1.8f, 0);
- private float yaw = 0;
- private float pitch = 0;
- private float[] cubeVerts;
- private int vHandle;
- private int tHandle;
- public void start()
- {
- try {
- Display.setDisplayMode(new DisplayMode(width, height));
- Display.create();
- } catch (LWJGLException e) {
- e.printStackTrace();
- System.exit(0);
- }
- Mouse.setGrabbed(true);
- try {
- texture = TextureLoader.getTexture("PNG", new FileInputStream(new File("texture.png")));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- initGL();
- setupBasicOpenGL();
- getDelta();
- lastFPS = getTime();
- texture.bind();
- IntBuffer ib = BufferUtils.createIntBuffer(2);
- GL15.glGenBuffers(ib);
- vHandle = ib.get(0);
- tHandle = ib.get(1);
- init();
- while (!Display.isCloseRequested()) {
- int delta = getDelta();
- GL11.glLoadIdentity();
- lookThrough();
- update(delta);
- renderGL();
- Display.update();
- Display.sync(60); // cap fps to 60fps
- }
- GL11.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
- GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
- ib.put(0, vHandle);
- ib.put(1, tHandle);
- GL15.glDeleteBuffers(ib);
- Display.destroy();
- }
- private void init()
- {
- ArrayList<Block> blocks = new ArrayList<Block>();
- int n = 10; //1/2 width and height of world
- int s = 1;
- int y = 0;
- for (int x = -n; x < n + 1; x += s)
- {
- for (int z = -n; z < n + 1; z += s)
- {
- blocks.add(new Block(new Vector(x, y - 2, z), GRASS));
- }
- }
- updateVBO(blocks);
- }
- public void updateVBO(ArrayList<Block> blocks)
- {
- ArrayList<Float> cubeVertList = new ArrayList<Float>();
- ArrayList<Float> texCoordList = new ArrayList<Float>();
- for (Block block : blocks)
- {
- for (float f : block.getCubeVerts())
- cubeVertList.add(f);
- for (float f : block.getTexCoords())
- texCoordList.add(f);
- System.out.println(block.getPosition());
- }
- cubeVerts = new float[cubeVertList.size()];
- for (int i = 0; i < cubeVertList.size(); i++)
- cubeVerts[i] = cubeVertList.get(i);
- FloatBuffer vertBuffer = BufferUtils.createFloatBuffer(cubeVerts.length);
- vertBuffer.put(cubeVerts);
- vertBuffer.flip();
- float[] textures = new float[texCoordList.size()];
- for (int i = 0; i < texCoordList.size(); i++)
- textures[i] = texCoordList.get(i);
- FloatBuffer texBuffer = BufferUtils.createFloatBuffer(textures.length);
- texBuffer.put(textures);
- texBuffer.flip();
- GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
- GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
- GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vHandle);
- GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertBuffer, GL15.GL_STATIC_DRAW);
- GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0L);
- GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, tHandle);
- GL15.glBufferData(GL15.GL_ARRAY_BUFFER, texBuffer, GL15.GL_STATIC_DRAW);
- GL11.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0L);
- }
- public void update(int delta)
- {
- float mouseSensitivity = .15f;
- float movementSpeed = 2 * (float)delta / 1000.0f;
- float dx = Mouse.getDX();
- //distance in mouse movement from the last getDY() call.
- float dy = Mouse.getDY();
- yaw += dx * mouseSensitivity;
- pitch -= dy * mouseSensitivity;
- if (Keyboard.isKeyDown(Keyboard.KEY_W))
- {
- position.x -= movementSpeed * (float)Math.sin(Math.toRadians(yaw));
- position.z += movementSpeed * (float)Math.cos(Math.toRadians(yaw));
- }
- //moves the camera backward relitive to its current rotation (yaw)
- if (Keyboard.isKeyDown(Keyboard.KEY_S))
- {
- position.x += movementSpeed * (float)Math.sin(Math.toRadians(yaw));
- position.z -= movementSpeed * (float)Math.cos(Math.toRadians(yaw));
- }
- //strafes the camera left relitive to its current rotation (yaw)
- if (Keyboard.isKeyDown(Keyboard.KEY_A))
- {
- position.x -= movementSpeed * (float)Math.sin(Math.toRadians(yaw-90));
- position.z += movementSpeed * (float)Math.cos(Math.toRadians(yaw-90));
- }
- //strafes the camera right relitive to its current rotation (yaw)
- if (Keyboard.isKeyDown(Keyboard.KEY_D))
- {
- position.x -= movementSpeed * (float)Math.sin(Math.toRadians(yaw+90));
- position.z += movementSpeed * (float)Math.cos(Math.toRadians(yaw+90));
- }
- if (Keyboard.isKeyDown(Keyboard.KEY_SPACE))
- position.y -= movementSpeed * 2;
- if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT))
- position.y += movementSpeed * 2;
- updateFPS(); // update FPS Counter
- }
- public int getDelta()
- {
- long time = getTime();
- int delta = (int) (time - lastFrame);
- lastFrame = time;
- return delta;
- }
- public static long getTime()
- {
- return (Sys.getTime() * 1000) / Sys.getTimerResolution();
- }
- public void updateFPS()
- {
- if (getTime() - lastFPS > 1000)
- {
- Display.setTitle("FPS: " + fps);
- fps = 0;
- lastFPS += 1000;
- }
- fps++;
- }
- public void initGL()
- {
- GL11.glMatrixMode(GL11.GL_PROJECTION);
- GL11.glLoadIdentity();
- GLU.gluPerspective(45.0f,((float)width)/((float)height),0.1f,100.0f);
- GL11.glMatrixMode(GL11.GL_MODELVIEW);
- GL11.glLoadIdentity();
- GL11.glEnable(GL11.GL_TEXTURE_2D);
- GL11.glShadeModel(GL11.GL_SMOOTH);
- GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
- GL11.glClearDepth(1.0f);
- GL11.glEnable(GL11.GL_DEPTH_TEST);
- GL11.glDepthFunc(GL11.GL_LEQUAL);
- GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
- GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP);
- }
- public void lookThrough()
- {
- GL11.glRotatef(pitch, 1.0f, 0.0f, 0.0f);
- GL11.glRotatef(yaw, 0.0f, 1.0f, 0.0f);
- GL11.glTranslatef(position .x, position.y, position.z);
- }
- public void renderGL()
- {
- GL11.glClear(GL11.GL_COLOR_BUFFER_BIT|GL11.GL_DEPTH_BUFFER_BIT);
- GL11.glTranslatef(0.0f,0f,-5.0f);
- GL11.glDrawArrays(GL11.GL_QUADS, 0, cubeVerts.length);
- GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
- }
- public void setupBasicOpenGL()
- {
- GL11.glClearColor(0.5f, 0.69f, 1.0f, 1f);
- GL11.glEnable(GL11.GL_CULL_FACE);
- GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
- GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
- FloatBuffer fogColor = BufferUtils.createFloatBuffer(4);
- fogColor.put(.5f).put(.69f).put(1f).put(1f).flip();
- GL11.glEnable(GL11.GL_DEPTH_TEST);
- GL11.glEnable(GL11.GL_FOG);
- GL11.glFog(GL11.GL_FOG_COLOR, fogColor);
- GL11.glHint(GL11.GL_FOG_HINT, GL11.GL_DONT_CARE);
- GL11.glFogf(GL11.GL_FOG_DENSITY, 0.035f);
- GL11.glFogf(GL11.GL_FOG_START, 20.0f);
- GL11.glFogf(GL11.GL_FOG_END, 60.0f);
- }
- public static void main(String[] argv)
- {
- WorldTest worldTest = new WorldTest();
- worldTest.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement