Hello,
My application works inside netbeans, but I cant get it to work outside the IDE. I can't get even get past the applet errors like
setVisible(true);
The IDE won't let it compile. Can you tell me what to do for Netbeans?
Also, rendering only two cubes my app can crash my computer and run as slow as minecraft. It is all on one thread.
Thanks,
dangerdoc
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
The biggest problem I see is that you are calling this:
Texture texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("tffsbase/"+block.gettexture(id)));
each time you call draw (i.e. four times per frame). This reloads the texture from disk and creates a new OpenGL texture for it. What you want to do is make that call somewhere in your initialization code and just call texture.bind() in your draw() method.
EDIT: It's also strange to call
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
in your draw method since that clears the screen. I would think you would want to do that outside your draw() method (like right before you call glLoadIdentity() in your main loop).
Thank you, Fool Running!
I can see how those two things can make it lag, and I will correct them soon. Thanks for braving through all the code, and telling me what I did wrong.
One final thing that hasn't been answered yet... How do you make the applet work? I used the code from the tutorial on the site, but it has errors like this in Netbeans:
setVisible(true);
It says "cannot find method setVisible()"
With solutions like "create method (blah blah bah) in your project, or create a field" I can understand if this is part of applet development, but is there a work-around for this in Netbeans?
Thanks,
dangerdoc
How are you defining your applet? That's a call that should definitely work, so unless something fundamental is wrong, I'm not sure what could cause that. Please post the code for your applet class.
package tffsapplet;
import java.awt.BorderLayout;
import org.lwjgl.opengl.Display;
public class TFFSApplet extends Applet {
Canvas display_parent;
/** Thread which runs the main game loop */
Thread gameThread;
/** is the game loop running */
boolean running = false;
public void startLWJGL() {
gameThread = new Thread() {
public void run() {
running = true;
try {
Display.setParent(display_parent);
Display.create();
initGL();
} catch (LWJGLException e) {
e.printStackTrace();
return;
}
gameLoop();
}
};
gameThread.start();
}
/**
* Tell game loop to stop running, after which the LWJGL Display will
* be destoryed. The main thread will wait for the Display.destroy().
*/
private void stopLWJGL() {
running = false;
try {
gameThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void start() {
}
public void stop() {
}
/**
* Applet Destroy method will remove the canvas,
* before canvas is destroyed it will notify stopLWJGL()
* to stop the main game loop and to destroy the Display
*/
public void destroy() {
remove(display_parent);
super.destroy();
}
public void init() {
setLayout(new BorderLayout());
try {
display_parent = new Canvas() {
public final void addNotify() {
super.addNotify();
startLWJGL();
}
public final void removeNotify() {
stopLWJGL();
super.removeNotify();
}
};
display_parent.setSize(getWidth(),getHeight());
add(display_parent);
display_parent.setFocusable(true);
display_parent.requestFocus();
display_parent.setIgnoreRepaint(true);
setVisible(true);
} catch (Exception e) {
System.err.println(e);
throw new RuntimeException("Unable to create display");
}
}
protected void initGL() {
}
public void gameLoop() {
while(running) {
Display.sync(60);
Display.update();
}
Display.destroy();
}
}
What do you mean by "define my applet"?
[edit] Also, would it be possible to integrate the natives into the jar?
Thanks,
dangerdoc
OK, I fixed the errors in the compiling and the performance, and made a .jar of the applet. I followed the applet deployment tuts on the wiki and the screen has no applet. I can't even view the gears applet. Any ideas? (also I tested my applet and the gears applet on another computer and the same results, neither worked)
Are you getting an error when trying to run the applet? Please give more info...
QuoteAlso, would it be possible to integrate the natives into the jar?
I do not know of a way to place the natives inside a jar and load them, but as you already have the lwjgl supporting jars that you have to have outside the main jar it really does not matter.
It just won't load at all when I go to the applet page.