All right now that I have some more free time I am attaching the source code. For the applet, I just am using the basic code from the wiki and adding the same classes into the loop.
Main class:
package tffsbase;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class TFFSbase {
public static void main(String[] args) {
tffsbase.frame frame = new frame();
try {
frame.start();
} catch (IOException ex) {
Logger.getLogger(TFFSbase.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
frame:
public class frame {
renderer renderer = new renderer();
FPCameraController camera = new FPCameraController(0, 0, 0);
float dx = 0.0f;
float dy = 0.0f;
float dt = 0.0f; //length of frame
float lastTime = 0.0f; // when the last frame was
float time = 0.0f;
float mouseSensitivity = 0.15f;
float movementSpeed = 10.0f;
int quadangle = 10;
int width = 800;
int height = 600;
int gamemode = 0;
public void start() throws IOException {
try {
Display.setDisplayMode(new DisplayMode(800, 600));
Display.setTitle("TFFS");
Display.create();
} catch (LWJGLException e) {
System.exit(0);
}
Mouse.setGrabbed(true);
GL11.glViewport(0, 0, 800, 600); // Reset The Current Viewport
GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
GL11.glLoadIdentity(); // Reset The Projection Matrix
GLU.gluPerspective(45.0f, ((float) width / (float) height), 0.1f, 100.0f); // Calculate The Aspect Ratio Of The Window
GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix
GL11.glLoadIdentity(); // Reset The Modelview Matrix
GL11.glShadeModel(GL11.GL_SMOOTH); // Enables Smooth Shading
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
GL11.glClearDepth(1.0f); // Depth Buffer Setup
GL11.glEnable(GL11.GL_DEPTH_TEST); // Enables Depth Testing
GL11.glDepthFunc(GL11.GL_LEQUAL); // The Type Of Depth Test To Do
GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST); // Really Nice Perspective Calculations
while (!Display.isCloseRequested()) {
time = Sys.getTime();
dt = (time - lastTime) / 1000.0f;
lastTime = time;
dx = Mouse.getDX();
dy = Mouse.getDY();
camera.yaw(dx * mouseSensitivity);
camera.pitch(-dy * mouseSensitivity);
if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
camera.walkForward(movementSpeed * dt);
}
if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
camera.walkBackwards(movementSpeed * dt);
}
if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
camera.strafeLeft(movementSpeed * dt);
}
if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
camera.strafeRight(movementSpeed * dt);
}
GL11.glLoadIdentity();
camera.lookThrough();
//id, x, y, z
renderer.draw(1, 0, 1, 0);
renderer.draw(2, 0, 2, 0);
renderer.draw(3, 0, 3, 0);
renderer.draw(4, 0, 4, 0);
Display.update();
Display.sync(100);
}
Display.destroy();
}
}
I left out the imports
FPCameraController:
public class FPCameraController {
//3d vector to store the camera's position in
private Vector3f position = null;
//the rotation around the Y axis of the camera
private float yaw = 0.0f;
//the rotation around the X axis of the camera
private float pitch = 0.0f;
public FPCameraController(float x, float y, float z){
//instantiate position Vector3f to the x y z params.
position = new Vector3f(x, y, z);
}
public void yaw(float amount){
//increment the yaw by the amount param
yaw += amount;
}
//increment the camera's current yaw rotation
public void pitch(float amount){
//increment the pitch by the amount param
pitch += amount;
}
public void walkForward(float distance){
position.x -= distance * (float)Math.sin(Math.toRadians(yaw));
position.z += distance * (float)Math.cos(Math.toRadians(yaw));
position.y += distance * (float)Math.sin(Math.toRadians(pitch));
}
//moves the camera backward relative to its current rotation (yaw)
public void walkBackwards(float distance){
position.x += distance * (float)Math.sin(Math.toRadians(yaw));
position.z -= distance * (float)Math.cos(Math.toRadians(yaw));
position.y -= distance * (float)Math.sin(Math.toRadians(pitch));
}
//strafes the camera left relitive to its current rotation (yaw)
public void strafeLeft(float distance){
position.x -= distance * (float)Math.sin(Math.toRadians(yaw-90));
position.z += distance * (float)Math.cos(Math.toRadians(yaw-90));
}
//strafes the camera right relitive to its current rotation (yaw)
public void strafeRight(float distance){
position.x -= distance * (float)Math.sin(Math.toRadians(yaw+90));
position.z += distance * (float)Math.cos(Math.toRadians(yaw+90));
}
public void lookThrough(){
//roatate the pitch around the X axis
GL11.glRotatef(pitch, 1.0f, 0.0f, 0.0f);
//roatate the yaw around the Y axis
GL11.glRotatef(yaw, 0.0f, 1.0f, 0.0f);
//translate to the position vector's location
GL11.glTranslatef(position.x, position.y, position.z);
}
}
Renderer:
class renderer {
block block = new block(1);
void renderer() {
}
void draw(int id, int x, int y, int z) throws IOException {
Texture texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("tffsbase/"+block.gettexture(id)));
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glEnable(GL11.GL_TEXTURE_2D);
texture.bind();
GL11.glBegin(GL11.GL_QUADS); // Start Drawing The Cube
GL11.glTexCoord2f(0.0f, 0.0f); // Set The Color To Green
GL11.glVertex3f(x+1.0f, y+1.0f, z-1.0f); // Top Right Of The Quad (Top)
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(x-1.0f, y+1.0f, z-1.0f); // Top Left Of The Quad (Top)
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f(x-1.0f, y+1.0f, z+1.0f); // Bottom Left Of The Quad (Top)
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f(x+1.0f, y+1.0f, z+1.0f); // Bottom Right Of The Quad (Top)
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f(x+1.0f, y-1.0f, z+1.0f); // Top Right Of The Quad (Bottom)
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(x-1.0f, y-1.0f, z+1.0f); // Top Left Of The Quad (Bottom)
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f(x-1.0f, y-1.0f, z-1.0f); // Bottom Left Of The Quad (Bottom)
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f(x+1.0f, y-1.0f, z-1.0f); // Bottom Right Of The Quad (Bottom)
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f(x+1.0f, y+1.0f, z+1.0f); // Top Right Of The Quad (Front)
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(x-1.0f, y+1.0f, z+1.0f); // Top Left Of The Quad (Front)
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f(x-1.0f, y-1.0f, z+1.0f); // Bottom Left Of The Quad (Front)
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f(x+1.0f, y-1.0f, z+1.0f); // Bottom Right Of The Quad (Front)
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f(x+1.0f, y-1.0f, z-1.0f); // Bottom Left Of The Quad (Back)
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(x-1.0f, y-1.0f, z-1.0f); // Bottom Right Of The Quad (Back)
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f(x-1.0f, y+1.0f, z-1.0f); // Top Right Of The Quad (Back)
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f(x+1.0f, y+1.0f, z-1.0f); // Top Left Of The Quad (Back)
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f(x-1.0f, y+1.0f, z+1.0f); // Top Right Of The Quad (Left)
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(x-1.0f, y+1.0f, z-1.0f); // Top Left Of The Quad (Left)
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f(x-1.0f, y-1.0f, z-1.0f); // Bottom Left Of The Quad (Left)
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f(x-1.0f, y-1.0f, z+1.0f); // Bottom Right Of The Quad (Left)
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f(x+1.0f, y+1.0f, z-1.0f); // Top Right Of The Quad (Right)
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(x+1.0f, y+1.0f, z+1.0f); // Top Left Of The Quad (Right)
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f(x+1.0f, y-1.0f, z+1.0f); // Bottom Left Of The Quad (Right)
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f(x+1.0f, y-1.0f, z-1.0f); // Bottom Right Of The Quad (Right)
GL11.glEnd();
}
}
Block:
public class block {
int id;
block(int idcode) {
id = idcode;
}
String gettexture(int side){
/*returns the texture for each side,
* where 1 = top, 2 = bottom, 3 = +x,
* 4 = -x, 5 = +y, 6 = -y. */
if(id==1)
return("dirt.png");
if(id==2)
return("stone.png");
if(id==3)
return("cobblestone.png");
else{
return("error.png");
}
}
double getphysics(int type){
double phys = 0.0;
//returns the phyical properties of a block where type 1 = solidity, 2 = slide
//dirt
if(id==1) {
if(type==1)
phys = 100;
if(type==2)
phys = .15;
}
//stone
if(id==2) {
if(type==1)
phys = 100;
if(type==2)
phys = .05;
}
//cobbestone
if(id==3) {
if(type==1)
phys = 100;
if(type==2)
phys = .02;
}
return(phys);
}
public void setid(int idq){
id = idq;
}
}
The basic loop is to start at frame, go to FPCameraController, move the camera, go to renderer, load data from block, and loop again. Please help me!
Thanks,
dangerdoc