3D TPP LWJGL Game – Character model from Blender #6

javaw 2013-01-18 21-57-37-78 lwjglrender

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

Hi, really I didn’t liked previous character, it was build from lines and quad as head hehe, so I made fast model in blender. Also texture was painted in it. Then I had to implement loader for obj files. And here it is. One thing I need to say, that if You use blender texture(.obj file)  in opengl, make sure to put (x, -y) coordinate. What’s more, I made tower – walls are very high, and I rendered 525 cubes. And as You can see, walls and cubes have two kinds of textures. Hm, ah yes, he can jump, not perfect, but he can 😛 and jump again when he’s in air. For better look I added light and fixed aspect.

Here You can see how it looks:

Link for textures and model: 

http://www.mediafire.com/?6d2z5ujs33ou3ys

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.Sys;
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[] walls; // array for walls
private Floor floor;
private Character character;

private final float FOV = 60; // field of view
private final int WIDTH = 800;
private final int HEIGHT = 600;
private final float ASPECT = (float) WIDTH / HEIGHT;

private Vector3f pos = new Vector3f(0, 0, 0); // position vector
private Vector3f posfix = new Vector3f(0, 0, 0);
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.0f; // Y position of character
private float z; // variable to move in z axis
private float x; // variable to move in x axis

// variables for jumping
private int height = 0;
private int maxHeight = 30;
private int countjump = 0;
private boolean dontjump = false;
private int jumptimes = 2;

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

private String walltex = “res/wall.png”;
private String walltex2 = “res/wall2.png”;
private LevelGenerator level; // object level will create random cubes in
private int displaylist;
// scene
private int countL = 525; // number of cubes (and apparently also height)
boolean collide; // if collide then stop gravity

private long lastFrame; // time at last frame

private int fps; // fps

private long lastFPS; // last fps

// lightning properties and objects
private float mat_ambient[] = { 1.0f, 1.0f, 1.0f, 1.0f };
private float light_position[] = { 0.0f, -3.0f, 0.0f, 0.0f };
private float lmodel_ambient[] = { 0.7f, 0.7f, 0.7f, 0.7f };
private float mat_amb_diff[] = { 0.1f, 0.5f, 0.8f, 1.0f };

// buffers for lightning
private FloatBuffer buf;
private FloatBuffer buf3;
private FloatBuffer buf4;
private FloatBuffer buf5;

public Main() {
// set Display

try {
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
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, ASPECT, 0.3f, 50.0f);
glViewport(0, 0, 800, 600);

// view behind character
gluLookAt(0.0f, 2.0f, 10.0f, 0.0f, 0, 0, 0.0f, 1.0f, 0.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, 800, 600);
glEnable(GL_FOG);

// buffer for colors
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();

// other fog parameters
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);

// buffers for lightning
buf = BufferUtils.createFloatBuffer(mat_ambient.length);
buf.put(mat_ambient, 0, mat_ambient.length);
buf.flip();

buf3 = BufferUtils.createFloatBuffer(light_position.length);
buf3.put(light_position, 0, light_position.length);
buf3.flip();

buf4 = BufferUtils.createFloatBuffer(lmodel_ambient.length);
buf4.put(lmodel_ambient, 0, lmodel_ambient.length);
buf4.flip();

buf5 = BufferUtils.createFloatBuffer(mat_amb_diff.length);
buf5.put(mat_amb_diff, 0, mat_amb_diff.length);
buf5.flip();

// parameters for lightning
glShadeModel(GL_SMOOTH);
glMaterial(GL_FRONT, GL_AMBIENT, buf);
glLight(GL_LIGHT0, GL_POSITION, buf3);
glLightModel(GL_LIGHT_MODEL_AMBIENT, buf4);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

running = true;

float l = 20.0f; // helper parametr for Walls

// Box vertices
Vector3f c = new Vector3f(l, -3, -l);
Vector3f d = new Vector3f(-l, -3, -l);
Vector3f g = new Vector3f(l, -3, l);
Vector3f h = new Vector3f(-l, -3, l);

// create floor
floor = new Floor(c, d, g, h);
int k = 0;

// create walls
walls = new Walls[15];
for (int i = 0; i < walls.length; i++) {

Vector3f a2 = new Vector3f(-l, 32 + k, -l);
Vector3f b2 = new Vector3f(l, 32 + k, -l);
Vector3f c2 = new Vector3f(l, -3 + k, -l);
Vector3f d2 = new Vector3f(-l, -3 + k, -l);

Vector3f e2 = new Vector3f(-l, 32 + k, l);
Vector3f f2 = new Vector3f(l, 32 + k, l);
Vector3f g2 = new Vector3f(l, -3 + k, l);
Vector3f h2 = new Vector3f(-l, -3 + k, l);

k += 35;
if (i % 2 == 0) {
walls[i] = new Walls(a2, b2, c2, d2, e2, f2, g2, h2, walltex);
} else {
walls[i] = new Walls(a2, b2, c2, d2, e2, f2, g2, h2, walltex2);

}

}

// create character
character = new Character();
character.createCharacter();

// generate level
level = new LevelGenerator(countL); // create and generate level

getDelta();
lastFPS = getTime();

}

public int getDelta() {
// method to get delta value
long time = getTime();
int delta = (int) (time – lastFrame);
lastFrame = time;
return delta;
}

public long getTime() {
// actual time
return (Sys.getTime() * 1000) / Sys.getTimerResolution();
}

public void updateFPS() {
// fps counter
if (getTime() – lastFPS > 1000) {
Display.setTitle(“FPS: ” + fps);
fps = 0;
lastFPS += 1000;
}
fps++;

}

public void start() {
// start method

while (running) {
// game loop
glEnable(GL_SCISSOR_TEST);

int delta = getDelta();
readinput(delta); // read keys
checkCollisions(); // check for collisions
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear screen
// // screen
// buffer
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);

glColor3f(1, 0.8f, 0.8f);

// render floor
floor.build();

// render walls
displaylist = glGenLists(1);
glNewList(displaylist, GL_COMPILE);
for (int i = 0; i < walls.length; i++) {
walls[i].build();
}
glEndList();
glCallList(displaylist);

glDisable(GL_CULL_FACE);

// render level
level.generate();

glLoadIdentity();

// character translation and rotation
glTranslatef(0, 0, view);
glRotatef(rotY2, 0, 1.0f, 0);
glRotatef(rotX, 1.0f, 0.0f, 0);

// render character
glLight(GL_LIGHT0, GL_POSITION, buf3);
character.build();

posfix.x = -pos.x; // fixed value of pos (x axis)
posfix.z = -pos.z; // fixed value of pos (z axis)

// translate for y character axis
glTranslatef(0, -posYheight, 0);
glLineWidth(1.0f);

// world translation and rotation
glRotatef(0, 0, 0, 1.0f);
glRotatef(rotY, 0, 1.0f, 0);
glTranslatef(pos.x, 0, pos.z);

posfix.y = posYheight;

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

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

}
floor.destroy();
character.destroy();
level.destroy();
glDeleteLists(displaylist, 1);
Display.destroy();

}

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

public void readinput(int delta) {
// 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_LEFT)
&& !Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
// rotate back(Y axis) when view keys not pressed
rotY2 = 0;
}

if (!Keyboard.isKeyDown(Keyboard.KEY_UP)
&& !Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
// rotate back(X axis) when view keys not pressed
rotX = 0;
}

if (Keyboard.isKeyDown(Keyboard.KEY_A)
&& !Keyboard.isKeyDown(Keyboard.KEY_S)
&& !Keyboard.isKeyDown(Keyboard.KEY_D)
&& Keyboard.isKeyDown(Keyboard.KEY_W)
&& !Keyboard.isKeyDown(Keyboard.KEY_E)
&& !Keyboard.isKeyDown(Keyboard.KEY_Q)) {
// moving code for key A and W
x = (float) (0.1 * Math.sin(Math.toRadians(45 – rotY)) * delta * 0.1f);
z = (float) (0.1 * Math.cos(Math.toRadians(45 – rotY)) * delta * 0.1f);

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

}

if (!Keyboard.isKeyDown(Keyboard.KEY_A)
&& !Keyboard.isKeyDown(Keyboard.KEY_S)
&& Keyboard.isKeyDown(Keyboard.KEY_D)
&& Keyboard.isKeyDown(Keyboard.KEY_W)
&& !Keyboard.isKeyDown(Keyboard.KEY_E)
&& !Keyboard.isKeyDown(Keyboard.KEY_Q)) {
// moving code for key D and W
x = (float) -(0.1 * Math.cos(Math.toRadians(45 – rotY)) * delta * 0.1f);
z = (float) (0.1 * Math.sin(Math.toRadians(45 – rotY)) * delta * 0.1f);

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

}

if (Keyboard.isKeyDown(Keyboard.KEY_A)
&& !Keyboard.isKeyDown(Keyboard.KEY_S)
&& !Keyboard.isKeyDown(Keyboard.KEY_D)
&& !Keyboard.isKeyDown(Keyboard.KEY_W)
&& !Keyboard.isKeyDown(Keyboard.KEY_E)
&& !Keyboard.isKeyDown(Keyboard.KEY_Q)) {
// moving code for key A
x = (float) -(0.1 * Math.cos(Math.toRadians(180 – rotY)) * delta * 0.1f);
z = (float) (0.1 * Math.sin(Math.toRadians(180 – rotY)) * delta * 0.1f);

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

}

if (Keyboard.isKeyDown(Keyboard.KEY_S)
&& !Keyboard.isKeyDown(Keyboard.KEY_A)
&& !Keyboard.isKeyDown(Keyboard.KEY_D)
&& !Keyboard.isKeyDown(Keyboard.KEY_W)
&& !Keyboard.isKeyDown(Keyboard.KEY_E)
&& !Keyboard.isKeyDown(Keyboard.KEY_Q)) {
// moving code for key S

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

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

}

if (Keyboard.isKeyDown(Keyboard.KEY_W)
&& !Keyboard.isKeyDown(Keyboard.KEY_A)
&& !Keyboard.isKeyDown(Keyboard.KEY_S)
&& !Keyboard.isKeyDown(Keyboard.KEY_D)
&& !Keyboard.isKeyDown(Keyboard.KEY_E)
&& !Keyboard.isKeyDown(Keyboard.KEY_Q)) {
// moving code for key W

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

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

}

if (Keyboard.isKeyDown(Keyboard.KEY_D)
&& !Keyboard.isKeyDown(Keyboard.KEY_A)
&& !Keyboard.isKeyDown(Keyboard.KEY_S)
&& !Keyboard.isKeyDown(Keyboard.KEY_W)
&& !Keyboard.isKeyDown(Keyboard.KEY_E)
&& !Keyboard.isKeyDown(Keyboard.KEY_Q)) {
// moving code for key D

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

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.next()) {

if (Keyboard.isKeyDown(Keyboard.KEY_V)) {
// change player’s view
if (view == 8.0f)
view = 0;
else
view = 8.0f;
}

if (Keyboard.isKeyDown(Keyboard.KEY_SPACE) && !dontjump) {
// jump counter
if (countjump >= jumptimes) {
dontjump = true;
height = 0;
} else {

if (countjump < jumptimes) {
countjump++;
height = 0;

}
}

}

// max height for jump
if (height >= maxHeight && !Keyboard.isKeyDown(Keyboard.KEY_SPACE)
&& !dontjump) {
height = 0;

}

}

if (Keyboard.isKeyDown(Keyboard.KEY_SPACE) && !dontjump) {
// jump

if (height < maxHeight) {
if (!dontjump)
posYheight += 0.7f;
}
height++;

}

if (Keyboard.isKeyDown(Keyboard.KEY_Z)) {
// floating up
posYheight += (0.9);

}

updateFPS();
}

public void checkCollisions() {
// check for collisions (wall and box)

// Wall checkers
if ((int) pos.x == 19)
pos.x = 19;

if ((int) pos.x == -19)
pos.x = -19;

if ((int) pos.z == 19)
pos.z = 19;

if ((int) pos.z == -19)
pos.z = -19;

// collision checks for level cubes, also collision response
for (int i = 0; i < level.boxNumber; i++) {
if (posYheight >= level.box[i].getC().y + 1
&& posYheight <= level.box[i].getA().y + 3) {
if (posfix.x >= level.box[i].getA().x
& posfix.x <= level.box[i].getB().x) {

if (posfix.z > level.box[i].getA().z
& posfix.z < level.box[i].getE().z) {

if ((int) posYheight >= level.box[i].getC().y + 1) {
// posYheight -= 0.5f ;
collide = true;

} else
collide = false;

if ((int) posYheight <= level.box[i].getA().y + 4) {
posYheight += 0.25f;
collide = true;

dontjump = false;
countjump = 0;
height = 0;
} else
collide = false;

if (posYheight < level.box[i].getC().y + 0.9f) {
collide = true;
posYheight -= 0.25f;
}
collide = false;

if ((int) posfix.x >= level.box[i].getA().x
&& posYheight <= level.box[i].getA().y + 2
&& posYheight >= level.box[i].getC().y) {
pos.x -= x;

} else {

if ((int) posfix.x <= level.box[i].getB().x
&& posYheight <= level.box[i].getA().y + 2
&& posYheight >= level.box[i].getC().y) {
pos.x -= x;
}
}

if ((int) posfix.z >= level.box[i].getA().z
&& posYheight <= level.box[i].getA().y + 2
&& posYheight >= level.box[i].getC().y) {
pos.z -= z;
} else {

if ((int) posfix.z <= level.box[i].getE().z
&& posYheight <= level.box[i].getA().y + 2
&& posYheight >= level.box[i].getC().y) {
pos.z -= z;
}
}

}
}
} else
collide = false;
}

// don’t fall when touching floor, and clear height counter
if (posYheight <= 0.2f) {
dontjump = false;
countjump = 0;
height = 0;
posYheight = 0.2f;
if (posYheight <= 0.0f && Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
posYheight = 0.7f;
posYheight -= 0.5f;

}

}
// if player not collide posYheight will drop = gravity
if (!collide) {
posYheight -= 0.25f;
}

}

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;
String tex;

public Walls(Vector3f a, Vector3f b, Vector3f c, Vector3f d, Vector3f e,
Vector3f f, Vector3f g, Vector3f h, String tex) {
// 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.tex = tex;

loadTexture();
}

public void build() {

texture.bind(); // bind wall texture
glColor3f(1.0f, 1.0f, 1.0f);
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(d.x, d.y, d.z);
glTexCoord2f(0, 10);
glVertex3d(c.x, c.y, c.z);
glEnd();

glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3d(f.x, f.y, f.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(g.x, g.y, g.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(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();

}

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(tex));

} catch (IOException e) {
e.printStackTrace();
}

}
}

class Character

package main;

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

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.lwjgl.opengl.Display;
import org.lwjgl.util.vector.Vector2f;
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 Character {
// Class for creating a Character

Texture texture;
String tex = “res/tex3.png”;
int displaylistch;
int displaylistchar;
LoaderObj obj;
static String loc = “res/lwjglcharactertest3.obj”;

public Character() {
// constructor
loadTexture();
}

public void build() {
texture.bind();
glCallList(displaylistchar); // display list
}

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

public void createCharacter() {

displaylistchar = glGenLists(1);
glNewList(displaylistchar, GL_COMPILE);

Model m = null;
try {
m = LoaderObj.load(new File(loc));
} catch (FileNotFoundException e) {
e.printStackTrace();
Display.destroy();
System.exit(1);
} catch (IOException e) {
e.printStackTrace();
Display.destroy();
System.exit(1);
}

glBegin(GL_TRIANGLES);

for (Face face : m.faces) {
Vector2f t1 = m.textures.get((int) face.textures.x – 1);
// System.out.println((int)face.textures.x – 1);
glTexCoord2f(t1.x, 1 – t1.y);
Vector3f n1 = m.normals.get((int) face.normal.x – 1);
glNormal3f(n1.x, n1.y, n1.z);
Vector3f v1 = m.vertices.get((int) face.vertex.x – 1);
glVertex3f(v1.x, v1.y, v1.z);
Vector2f t2 = m.textures.get((int) face.textures.y – 1);
glTexCoord2f(t2.x, 1 – t2.y);
Vector3f n2 = m.normals.get((int) face.normal.y – 1);
glNormal3f(n2.x, n2.y, n2.z);
Vector3f v2 = m.vertices.get((int) face.vertex.y – 1);
glVertex3f(v2.x, v2.y, v2.z);
Vector2f t3 = m.textures.get((int) face.textures.z – 1);
glTexCoord2f(t3.x, 1 – t3.y);
Vector3f n3 = m.normals.get((int) face.normal.z – 1);
glNormal3f(n3.x, n3.y, n3.z);
Vector3f v3 = m.vertices.get((int) face.vertex.z – 1);
glVertex3f(v3.x, v3.y, v3.z);

}
glEnd();

glEndList();

}

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

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

} catch (IOException e) {
e.printStackTrace();
}

}

}

class Box

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 Box {
// Class for creating simple Box(Cube)

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

Texture texture;
String textureloc;

public Box(Vector3f a, Vector3f b, Vector3f c, Vector3f d, Vector3f e,
Vector3f f, Vector3f g, Vector3f h, String textureloc) {
// 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.textureloc = textureloc;

loadTexture();
}

public void build() {

texture.bind(); // bind wall texture
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3d(b.x, b.y, b.z);
glTexCoord2f(1, 0);
glVertex3d(a.x, a.y, a.z);
glTexCoord2f(1, 1);
glVertex3d(d.x, d.y, d.z);
glTexCoord2f(0, 1);
glVertex3d(c.x, c.y, c.z);
glEnd();

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

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

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

glEnd();
glBegin(GL_QUADS);

glTexCoord2f(0, 0);
glVertex3d(c.x, c.y, c.z);
glTexCoord2f(1, 0);
glVertex3d(d.x, d.y, d.z);
glTexCoord2f(1, 1);
glVertex3d(h.x, h.y, h.z);
glTexCoord2f(0, 1);
glVertex3d(g.x, g.y, g.z);
glEnd();

glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3d(e.x, e.y, e.z);
glTexCoord2f(1, 0);
glVertex3d(f.x, f.y, f.z);
glTexCoord2f(1, 1);
glVertex3d(g.x, g.y, g.z);
glTexCoord2f(0, 1);
glVertex3d(h.x, h.y, h.z);
glEnd();
}

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

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

} catch (IOException e) {
e.printStackTrace();
}

}

public Vector3f getA() {
return a;
}

public Vector3f getB() {
return b;
}

public Vector3f getC() {
return c;
}

public Vector3f getD() {
return d;
}

public Vector3f getE() {
return e;
}

public Vector3f getF() {
return f;
}

public Vector3f getG() {
return g;
}

public Vector3f getH() {
return h;
}
}

class Floor

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 Floor {
// Class for creating simple Box(Cube)

Vector3f c, d, g, h;
int displaylistf;

Texture texture2;

public Floor(Vector3f c, Vector3f d, Vector3f g, Vector3f h) {
// constructor

this.c = c;
this.d = d;

this.g = g;
this.h = h;

loadTexture();
}

public void build() {

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

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

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

texture2.bind(); // bind floor texture
glBegin(GL_QUADS);
glColor3f(1, 1.0f, 1.0f);
glTexCoord2f(0, 0);
glVertex3d(c.x, c.y, c.z);
glTexCoord2f(20, 0);
glVertex3d(d.x, d.y, d.z);
glTexCoord2f(20, 20);
glVertex3d(h.x, h.y, h.z);
glTexCoord2f(0, 20);
glVertex3d(g.x, g.y, g.z);
glEnd();

glEndList();
}

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

try {
// load texture from PNG file

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

}
}

class LevelGenerator

package main;

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

import java.util.Random;

import org.lwjgl.util.vector.Vector3f;

public class LevelGenerator {
// class that generate Box objects

public int boxNumber;
public Box[] box;
String textureloc = “res/plaid.png”;
String textureloc2 = “res/lvl3.png”;
Random random = new Random();
Random random2 = new Random();
int displaylist;

public LevelGenerator(int boxNumber) {
// initiate Box
this.boxNumber = boxNumber;
box = new Box[boxNumber];

for (int i = 0; i < boxNumber; i++) {
// loop to create random coordinates
int x = random.nextInt(19); // random value for Vector
int z = random.nextInt(19);
int k = random2.nextInt(2); // values to also set random – values
int l = random2.nextInt(2);
if (k == 0)
x = -x;

if (l == 0)
z = -z;

// System.out.println(x);
if (i % 2 == 0) {
box[i] = new Box(new Vector3f(x – 2, i, z – 2), new Vector3f(x,
i, z – 2), new Vector3f(x, i – 2, z – 2), new Vector3f(
x – 2, i – 2, z – 2), new Vector3f(x – 2, i, z),
new Vector3f(x, i, z), new Vector3f(x, i – 2, z),
new Vector3f(x – 2, i – 2, z), textureloc);
} else {
box[i] = new Box(new Vector3f(x – 2, i, z – 2), new Vector3f(x,
i, z – 2), new Vector3f(x, i – 2, z – 2), new Vector3f(
x – 2, i – 2, z – 2), new Vector3f(x – 2, i, z),
new Vector3f(x, i, z), new Vector3f(x, i – 2, z),
new Vector3f(x – 2, i – 2, z), textureloc2);
}

}
}

public void generate() {
// build cubes

displaylist = glGenLists(0);
glNewList(displaylist, GL_COMPILE);
for (int i = 0; i < boxNumber; i++) {
box[i].build();
}
glEndList();
glCallList(displaylist);
}

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

}

class Face

package main;

import org.lwjgl.util.vector.Vector3f;

public class Face {
public Vector3f vertex = new Vector3f();
public Vector3f normal = new Vector3f();
public Vector3f textures = new Vector3f();

public Face(Vector3f vertex, Vector3f textures, Vector3f normal) {
this.vertex = vertex;
this.normal = normal;
this.textures = textures;
}
}

class Model

package main;

import java.util.ArrayList;
import java.util.List;

import org.lwjgl.util.vector.Vector2f;
import org.lwjgl.util.vector.Vector3f;

public class Model {

public List<Vector3f> vertices = new ArrayList<Vector3f>();
public List<Vector3f> normals = new ArrayList<Vector3f>();
public List<Vector2f> textures = new ArrayList<Vector2f>();
public List<Face> faces = new ArrayList<Face>();

public Model() {

}

}

class LoaderObj

package main;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.lwjgl.util.vector.Vector2f;
import org.lwjgl.util.vector.Vector3f;

public class LoaderObj {

public static Model load(File file) throws FileNotFoundException,
IOException {
Model m = new Model();

BufferedReader reader = new BufferedReader(new FileReader(file));
// read coords from obj file
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith(“v “)) {
float x = Float.valueOf(line.split(” “)[1]);
float y = Float.valueOf(line.split(” “)[2]);
float z = Float.valueOf(line.split(” “)[3]);
m.vertices.add(new Vector3f(x, y, z));
} else if (line.startsWith(“vn “)) {
float x = Float.valueOf(line.split(” “)[1]);
float y = Float.valueOf(line.split(” “)[2]);
float z = Float.valueOf(line.split(” “)[3]);
m.normals.add(new Vector3f(x, y, z));
} else if (line.startsWith(“vt “)) {
float x = Float.valueOf(line.split(” “)[1]);
float y = Float.valueOf(line.split(” “)[2]);
m.textures.add(new Vector2f(x, y));
} else if (line.startsWith(“f “)) {
Vector3f vertexIndices = new Vector3f(Float.valueOf(line
.split(” “)[1].split(“/”)[0]), Float.valueOf(line
.split(” “)[2].split(“/”)[0]), Float.valueOf(line
.split(” “)[3].split(“/”)[0]));
Vector3f textureIndices = new Vector3f(Float.valueOf(line
.split(” “)[1].split(“/”)[1]), Float.valueOf(line
.split(” “)[2].split(“/”)[1]), Float.valueOf(line
.split(” “)[3].split(“/”)[1]));
Vector3f normalIndices = new Vector3f(Float.valueOf(line
.split(” “)[1].split(“/”)[2]), Float.valueOf(line
.split(” “)[2].split(“/”)[2]), Float.valueOf(line
.split(” “)[3].split(“/”)[2]));
m.faces.add(new Face(vertexIndices, textureIndices,
normalIndices));
}
}
reader.close();
return m;
}

}

Advertisements
About

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

Tagged with: , , , , , , , , , , , ,
Posted in Java Game Programming
11 comments on “3D TPP LWJGL Game – Character model from Blender #6
  1. William says:

    And we also need the download to the models / textures if you could please.

  2. atique says:

    did you export model from Blender to Netbeans?

  3. atique says:

    do you know how to attach a camera to the moving object(like car) and i’ll get an effect of “moving surroundings?? any idea? in lwjgl

  4. atique says:

    Can you please tell me how to attach this file…lwjglcharactertest3.obj…i mean how to make it work….it’s not a class file right? should i take the help of the blender to export it to eclipse? how?

  5. atique says:

    when i go to compile, it shows the following error. probably the problem is with the lwjglcharactertest3.obj

    Fri Jun 28 18:21:21 ALMT 2013 INFO:Use Java PNG Loader = true
    Exception in thread “main” java.lang.NumberFormatException: For input string: “v”
    at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
    at java.lang.Float.valueOf(Unknown Source)
    at main.LoaderObj.load(LoaderObj.java:23)
    at main.Character.createCharacter(Character.java:47)
    at main.Main.init(Main.java:194)
    at main.Main.main(Main.java:630)

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: