3D TPP LWJGL Game – Textures #3

javaw 2013-01-06 13-57-40-75

LWJGL(Lightweight java game library) game project in third person perspective (TPP) #3

Main noticeable thing- Textures! Yea, one for walls and for floor. They were added using Slick-Util Library (helper libraries for LWJGL). Also I had to change this moving cube(from last post)  into something that looks more like character, very primitive (one quad and a few lines). Also to make it look more cool, there is fog enabled to hide objects that are too far. It starts to look somehow 😀

Here You can see how it looks:

For two days I was studying OpenGL 3.2 a bit, because I was about to use it in that project, but finally I decided to stay with legacy OpenGL 1.1 .Why? OpenGL 3.2+ have everything diferent, its kinda hard to understand, but main point is that I would have to change everything, so its better to use it in another project. In this project I use display lists to render, and for that not-advanced project its fair enough. I want to focus more about making it with all elements that game need: physics, collision detection, graphics etc and at end -> gameplay.

Btw I should post some usefull sites:

LWJGL wiki – there are a lot usefull tutorials : http://www.lwjgl.org/wiki/index.php?title=Main_Page

OpenGL “Red” Book – very nice to learn OpenGL 1.1 : http://www.glprogramming.com/red/index.html

For people that are intersted with code I will show it below. Remeber, to run this u need to have LWJGL library added to your project.

Class Main:

package main;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.util.vector.Vector3f;
import org.newdawn.slick.Color;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.gluPerspective;
import static org.lwjgl.util.glu.GLU.gluLookAt;

public class Main {

private boolean running = false;
private Walls box; // simple wall for tests
private Walls box3; // small cube in middle’
private Character character;

private float fov = 60; // field of view
private Vector3f pos = new Vector3f(0, 0, 0); // position vector
private float rotY = 0; // rotation along Y axis
private float rotY2 = 0; // rotation along Y axis
private float rotX = 0; // rotation along X axis
private float view = 0; // for character view transformations (along z axis)
private float posYheight = 0;

//fog parameters
private float fogNear = 10f;
private float fogFar = 30f;
private Color fogColor = new Color(0f, 0f, 0f, 1f);

public Main() {
// set Display

try {
Display.setDisplayMode(new DisplayMode(800, 600));
Display.setTitle(“twolwjgl”);
Display.create();

} catch (LWJGLException e) {
System.err.println(“LWJGLException! Can’t create Display!”);
e.printStackTrace();
System.exit(0);
}
}

public void init() {
// initialize stuff
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, 800 / 600, 0.3f, 50.0f);
glViewport(0, 0, 800, 600);
gluLookAt(0.0f, 2.0f, 10.0f, 0.0f, 0, 0, 0.0f, 1.0f, 0.0f); // shows character from behind
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glEnable(GL_FOG);

//buffer for colours
FloatBuffer fogColours = BufferUtils.createFloatBuffer(4);
fogColours.put(new float[] { fogColor.r, fogColor.g, fogColor.b,
fogColor.a });
glClearColor(fogColor.r, fogColor.g, fogColor.b, fogColor.a);
fogColours.flip();

glFog(GL_FOG_COLOR, fogColours);
glFogi(GL_FOG_MODE, GL_LINEAR);
glHint(GL_FOG_HINT, GL_NICEST);
glFogf(GL_FOG_START, fogNear);
glFogf(GL_FOG_END, fogFar);
glFogf(GL_FOG_DENSITY, 0.0005f);

glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

running = true;

float l = 20.0f; // need for Box

// Box vertices
Vector3f a = new Vector3f(-l, 32, -l);
Vector3f b = new Vector3f(l, 32, -l);
Vector3f c = new Vector3f(l, -3, -l);
Vector3f d = new Vector3f(-l, -3, -l);

Vector3f e = new Vector3f(-l, 32, l);
Vector3f f = new Vector3f(l, 32, l);
Vector3f g = new Vector3f(l, -3, l);
Vector3f h = new Vector3f(-l, -3, l);

box = new Walls(a, b, c, d, e, f, g, h); // creating Box (walls)

float l2 = 0.8f;
// Box3 vertices

// small cube in middle
box3 = new Walls(new Vector3f(-l2, l2, -4), new Vector3f(l2, l2, -4),
new Vector3f(l2, -l2, -4), new Vector3f(-l2, -l2, -4),
new Vector3f(-l2, l2, -2), new Vector3f(l2, l2, -2),
new Vector3f(l2, -l2, -2), new Vector3f(-l2, -l2, -2));

character = new Character(new Vector3f(-0.4f, 0.3f, 0), new Vector3f(
0.4f, 0.3f, 0), new Vector3f(0.4f, -0.6f, 0), new Vector3f(
0.0f, -0.6f, 0), new Vector3f(-0.4f, -0.6f, 0), new Vector3f(
0.0f, -1.7f, 0), new Vector3f(0.8f, -1.7f, 0), new Vector3f(
-0.8f, -1.7f, 0), new Vector3f(-0.3f, -3.0f, 0), new Vector3f(
0.3f, -3.0f, 0), new Vector3f(0.0f, -1.0f, 0));
}

public void start() {
// start method

while (running) {
// game loop

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clearing
// screen buffer

glEnable(GL_DEPTH_TEST);

glColor3f(1, 0.8f, 0.8f);

box.build(); // make walls

box3.build(); // make cube

glColor3f(1, 1.0f, 1.0f);
glBegin(GL_LINES); // x axis
glVertex3f(-700.0f, 0.0f, 0.0f);
glVertex3f(700.0f, 0, 0);
glEnd();

glColor3f(1, 0.1f, 1.0f);
glBegin(GL_LINES);// y axis
glVertex3f(0.0f, -500.0f, 0.0f);
glVertex3f(0.0f, 500.0f, 0);
glEnd();

glColor3f(1, 1.0f, 0.1f);
glBegin(GL_LINES);// z axis
glVertex3f(0.0f, 0.0f, -400.0f);
glVertex3f(0.0f, 0.0f, 400.0f);
glEnd();

glBegin(GL_QUADS); // world floor
glColor3f(0.4f, 1.1f, 0.1f);
glVertex3d(-400, -3.1f, 400);
glVertex3d(-400, -3.1f, -400);
glVertex3d(400, -3.1f, -400);
glVertex3d(400, -3.1f, 400);
glEnd();

glLoadIdentity();

glTranslatef(0, 0, view);
glRotatef(rotY2, 0, 1.0f, 0);
glRotatef(rotX, 1.0f, 0.0f, 0);
glLineWidth(2.0f);
character.build();
glTranslatef(0, posYheight, 0);
glLineWidth(1.0f);

glRotatef(0, 0, 0, 1.0f);
glRotatef(rotY, 0, 1.0f, 0);
glRotatef(rotX, 1.0f, 0, 0);
glTranslatef(pos.x, 0, pos.z);

// closing on “X”
if (Display.isCloseRequested()) {
stop();
}

readinput(); // read keys
Display.update();
Display.sync(60);
}
box.destroy();
box3.destroy();
character.destroy();
Display.destroy();

}

public void stop() {
// stop method
running = false;
}

public void readinput() {
// reading input(keyboard, mouse) method

// rotation parameter max 360 degree
if (rotY == 360 | rotY == -360)
rotY = 0;

if (rotY2 == 360 | rotY2 == -360)
rotY2 = 0;

if (rotX == 360 | rotX == -360)
rotX = 0;

if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
// moving code for key A
float x = (float) (0.1 * Math.cos(Math.toRadians(180 – rotY)));
float z = (float) (0.1 * Math.sin(Math.toRadians(180 – rotY)));
pos.x -= x;
pos.z += z;

}

if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
// moving code for key S

float z = (float) (0.1 * Math.sin(Math.toRadians(90 – rotY)));
float x = (float) (0.1 * Math.cos(Math.toRadians(90 – rotY)));

pos.z -= z;
pos.x += x;

}

if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
// moving code for key W

float z = (float) (0.1 * Math.sin(Math.toRadians(90 – rotY)));
float x = (float) (0.1 * Math.cos(Math.toRadians(90 – rotY)));

pos.x -= x;
pos.z += z;

}

if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
// moving code for key D

float x = (float) (0.1 * Math.cos(Math.toRadians(180 – rotY)));
float z = (float) (0.1 * Math.sin(Math.toRadians(180 – rotY)));

pos.x += x;
pos.z -= z;

}

if (Keyboard.isKeyDown(Keyboard.KEY_E)) {
// clockwise rotation for key E
rotY += 3.0f;

}

if (Keyboard.isKeyDown(Keyboard.KEY_Q)) {
// counter clockwise rotation for key Q

rotY -= 3.0f;

}

if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
// clockwise rotation for key RIGHT (showing character’s side)
rotY2 += 3.0f;

}

if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
// counter clockwise rotation for key LEFT (showing character’s
// side)

rotY2 -= 3.0f;

}

if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
// counter clockwise rotation for key UP
rotX += 3.0f;

}

if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
// counter clockwise rotation for key DOWN (showing character’s
// side)

rotX -= 3.0f;

}

if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
// floating up
posYheight -= (0.1 * Math.sin(Math.toRadians(90 – rotX)));

}

// change view close or third person
if (Keyboard.next()) {
if (Keyboard.isKeyDown(Keyboard.KEY_V)) {
// gluLookAt(0.0f, 1.0f, 0.0f, 0.0f, 0.2f, 0, 0.0f, 1.0f, 0.0f);
if (view == 8.0f)
view = 0;
else
view = 8.0f;
}
}

// System.out.println(“angle ” + rotY + ” z ” + pos.z + ” x ” + pos.x);

}

public static void main(String[] args) {
Main m = new Main();
m.init();
m.start();

}

}

class Walls:

package main;

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

import java.io.IOException;

import org.lwjgl.util.vector.Vector3f;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

public class Walls {
// Class for creating simple Box(Cube)

Vector3f a, b, c, d, e, f, g, h;
int displaylist;

Texture texture;
Texture texture2;

public Walls(Vector3f a, Vector3f b, Vector3f c, Vector3f d, Vector3f e,
Vector3f f, Vector3f g, Vector3f h) {
// constructor
this.a = a;
this.b = b;
this.c = c;
this.d = d;

this.e = e;
this.f = f;
this.g = g;
this.h = h;

loadTexture();
}

public void build() {

createlist();
glCallList(displaylist); // display list
}

public void destroy() {
glDeleteLists(displaylist, 1); // removing display list
}

public void createlist() {
// creating display list
displaylist = glGenLists(1);
glNewList(displaylist, GL_COMPILE);
texture.bind(); //bind wall texture
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3d(a.x, a.y, a.z);
glTexCoord2f(10, 0);
glVertex3d(b.x, b.y, b.z);
glTexCoord2f(10, 10);
glVertex3d(c.x, c.y, c.z);
glTexCoord2f(0, 10);
glVertex3d(d.x, d.y, d.z);
glEnd();

glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3d(b.x, b.y, b.z);
glTexCoord2f(10, 0);
glVertex3d(f.x, f.y, f.z);
glTexCoord2f(10, 10);
glVertex3d(g.x, g.y, g.z);
glTexCoord2f(0, 10);
glVertex3d(c.x, c.y, c.z);
glEnd();

glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3d(a.x, a.y, a.z);
glTexCoord2f(10, 0);
glVertex3d(e.x, e.y, e.z);
glTexCoord2f(10, 10);
glVertex3d(h.x, h.y, h.z);
glTexCoord2f(0, 10);
glVertex3d(d.x, d.y, d.z);
glEnd();

glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3d(b.x, b.y, b.z);
glTexCoord2f(10, 0);
glVertex3d(a.x, a.y, a.z);
glTexCoord2f(10, 10);
glVertex3d(e.x, e.y, e.z);
glTexCoord2f(0, 10);
glVertex3d(f.x, f.y, f.z);

glEnd();
texture2.bind(); //bind floor texture
glBegin(GL_QUADS);

glTexCoord2f(0, 0);
glVertex3d(c.x, c.y, c.z);
glTexCoord2f(20, 0);
glVertex3d(g.x, g.y, g.z);
glTexCoord2f(20, 20);
glVertex3d(h.x, h.y, h.z);
glTexCoord2f(0, 20);
glVertex3d(d.x, d.y, d.z);
glEnd();
texture.bind();
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3d(e.x, e.y, e.z);
glTexCoord2f(10, 0);
glVertex3d(f.x, f.y, f.z);
glTexCoord2f(10, 10);
glVertex3d(g.x, g.y, g.z);
glTexCoord2f(0, 10);
glVertex3d(h.x, h.y, h.z);
glEnd();

glEndList();
}

public void move(Vector3f a, Vector3f b, Vector3f c, Vector3f d,
Vector3f e, Vector3f f, Vector3f g, Vector3f h) {

// method for changing Objects vertices coordinate (x,y,z)

this.a.x += a.x;
this.a.y += a.y;
this.a.z += a.z;

this.b.x += b.x;
this.b.y += b.y;
this.b.z += b.z;

this.c.x += c.x;
this.c.y += c.y;
this.c.z += c.z;

this.d.x += d.x;
this.d.y += d.y;
this.d.z += d.z;

this.e.x += e.x;
this.e.y += e.y;
this.e.z += e.z;

this.f.x += f.x;
this.f.y += f.y;
this.f.z += f.z;

this.g.x += g.x;
this.g.y += g.y;
this.g.z += g.z;

this.h.x += h.x;
this.h.y += h.y;
this.h.z += h.z;
}

public void loadTexture() {
//loader for textures (using slick2d helper)

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

try {
// load texture from PNG file
texture = TextureLoader.getTexture(“PNG”,
ResourceLoader.getResourceAsStream(“res/wall.png”));

texture2 = TextureLoader.getTexture(“PNG”,
ResourceLoader.getResourceAsStream(“res/floor2.png”));
} catch (IOException e) {
e.printStackTrace();
}

}
}

class Character

package main;

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

import org.lwjgl.util.vector.Vector3f;

public class Character {
//Class for creating a Character

Vector3f a,b,c,d, e, f, g, h, i, j, k ;
int displaylistch;

public Character(Vector3f a, Vector3f b, Vector3f c, Vector3f d, Vector3f e, Vector3f f, Vector3f g, Vector3f h, Vector3f i, Vector3f j, Vector3f k) {
//constructor
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
this.f = f;
this.g = g;
this.h = h;
this.i = i;
this.j = j;
this.k = k;

}

public void build() {
createlist();
glCallList(displaylistch); //display list
}

public void destroy() {
glDeleteLists(displaylistch, 1); //removing display list
}

public void createlist() {
//creating display list
displaylistch = glGenLists(1);
glNewList(displaylistch, GL_COMPILE);

glBegin(GL_QUADS); //head
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3d(a.x, a.y, a.z);
glVertex3d(b.x, b.y, b.z);
glColor3f(1.0f, 0.6f, 0.3f);
glVertex3d(c.x, c.y, c.z);
glVertex3d(e.x, e.y, e.z);
glEnd();
glColor3f(1.0f, 0.6f, 0.3f);
glBegin(GL_LINES);
glVertex3d(d.x, d.y, d.z);
glVertex3d(k.x, k.y, k.z);
glEnd();
glColor3f(0.0f, 0.0f, 0.0f);
glBegin(GL_LINES);
glVertex3d(k.x, k.y, k.z);
glVertex3d(f.x, f.y, f.z);
glEnd();
glColor3f(1.0f, 0.6f, 0.3f);
glBegin(GL_LINES);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3d(k.x, k.y, k.z);
glColor3f(1.0f, 0.6f, 0.3f);
glVertex3d(g.x, g.y, g.z);
glEnd();
glColor3f(1.0f, 0.6f, 0.3f);
glBegin(GL_LINES);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3d(k.x, k.y, k.z);
glColor3f(1.0f, 0.6f, 0.3f);
glVertex3d(h.x, h.y, h.z);
glEnd();
glColor3f(1.0f, 0.6f, 0.3f);
glBegin(GL_LINES);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3d(f.x, f.y, f.z);
glColor3f(1.0f, 0.6f, 0.3f);
glVertex3d(i.x, i.y, i.z);
glEnd();

glBegin(GL_LINES);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3d(f.x, f.y, f.z);
glColor3f(1.0f, 0.6f, 0.3f);
glVertex3d(j.x, j.y, j.z);
glEnd();

glEndList();
}

public void add(Vector3f a, Vector3f b, Vector3f c, Vector3f d) {

//method for changing Objects vertices coordinate (x,y,z)

this.a.x += a.x;
this.a.y = a.y;
this.a.z += a.z;

this.b.x += b.x;
this.b.y = b.y;
this.b.z += b.z;

this.c.x += c.x;
this.c.y = c.y;
this.c.z += c.z;

this.d.x += d.x;
this.d.y = d.y;
this.d.z += d.z;

}

public void move(Vector3f a, Vector3f b, Vector3f c, Vector3f d) {

//method for changing Objects vertices coordinate (x,y,z)

this.a.x = a.x;
this.a.y = a.y;
this.a.z = a.z;

this.b.x = b.x;
this.b.y = b.y;
this.b.z = b.z;

this.c.x = c.x;
this.c.y = c.y;
this.c.z = c.z;

this.d.x = d.x;
this.d.y = d.y;
this.d.z = d.z;

}
}

Advertisements
About

my interests: Game development (atm programming in java) 3D Graphics (Favourite program: Blender)

Tagged with: , , , , , , , , , ,
Posted in Java Game Programming

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

morrky

morrky

my interests: Game development (atm programming in java) 3D Graphics (Favourite program: Blender)

Personal Links

Verified Services

View Full Profile →

Ad time
prywatne pożyczki pod zastaw nieruchomości bez BIK BIG i KRD
%d bloggers like this: