Jump to content
  • Advertisement
Sign in to follow this  
Scarrion_Storm

Lesson06(java)

This topic is 2300 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok so I found this... http://www.java-gami...p?topic=25000.0

and am on tutorial 6 in eclipse. I've copied the image files themselves into every single folder in my eclipse workspace (I'm posting here in desperation after trying everything I can think of and beating my head against the wall for really a very long time).

When I run my code it just instantly closes with the following error:

Detected screen size 1920x1200
Exception in thread "Timer-0" java.lang.NullPointerException
at learning.Tutorial_06.loadTexture(Tutorial_06.java:236)
at learning.Tutorial_06.access$1(Tutorial_06.java:230)
at learning.Tutorial_06$2.init(Tutorial_06.java:73)
at jogamp.opengl.GLDrawableHelper.init(GLDrawableHelper.java:132)
at jogamp.opengl.GLDrawableHelper.displayImpl(GLDrawableHelper.java:170)
at jogamp.opengl.GLDrawableHelper.display(GLDrawableHelper.java:159)
at com.jogamp.newt.opengl.GLWindow$DisplayAction.run(GLWindow.java:588)
at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:343)
at com.jogamp.newt.opengl.GLWindow.display(GLWindow.java:534)
at com.jogamp.opengl.util.AWTAnimatorImpl.display(AWTAnimatorImpl.java:74)
at com.jogamp.opengl.util.AnimatorBase.display(AnimatorBase.java:142)
at com.jogamp.opengl.util.FPSAnimator$1.run(FPSAnimator.java:128)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)


Code attached:


package learning;

import com.jogamp.newt.event.KeyAdapter;
import com.jogamp.newt.event.KeyEvent;
import com.jogamp.newt.event.WindowAdapter;
import com.jogamp.newt.event.WindowEvent;

import com.jogamp.newt.opengl.GLWindow;

import com.jogamp.opengl.util.FPSAnimator;

import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.TextureIO;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;

import javax.media.opengl.glu.GLU;

/**
*
* @author Sebastiaan
*/
public class Tutorial_06 {
private GLWindow window;
private boolean fullscreen = false;

private float xrot; // X Rotation ( NEW )
private float yrot; // Y Rotation ( NEW )
private float zrot; // Z Rotation ( NEW )

private Texture cubeTexture; // Stores our BMP as texture (NEW)

public Tutorial_06() {
// Choose the GL profile
GLProfile glp = GLProfile.getMaxFixedFunc();
// Configure the GL capabilities
GLCapabilities caps = new GLCapabilities(glp);

// Create a new GL NEWT window with the GL capabilities
window = GLWindow.create(caps);
window.setSize(400, 300);
window.setVisible(true);
window.setTitle("Tutorial 6: Texture Mapping");

window.addWindowListener(new WindowAdapter() {
@Override
public void windowDestroyNotify(WindowEvent arg0) {
closeTutorial();
};
});

// Add a GL eventlistener to the window
window.addGLEventListener(new GLEventListener() {
/**
* This method is called when we first create our opengl window
*/
@Override
public void init(GLAutoDrawable drawable) {
final GL2 gl = drawable.getGL().getGL2();
gl.glClearDepth(1.0f); // Depth Buffer Setup
gl.glEnable(GL2.GL_DEPTH_TEST); // Enables Depth Testing
gl.glDepthFunc(GL2.GL_LEQUAL); // The Type Of Depth Testing To Do
gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST); // Really Nice Perspective Calculations
gl.glShadeModel(GL2.GL_SMOOTH);

// load the texture for the cube (NEW)
cubeTexture = loadTexture();

// Something went wrong loading texture, return to selecting menu (NEW)
if(cubeTexture == null)
closeTutorial();

// enable the texture we just loaded (NEW)
cubeTexture.enable(gl);
}

/**
* This method is called when we destroy our opengl window
*/
@Override
public void dispose(GLAutoDrawable drawable) {}

/**
* Here happens the actual opengl drawing
*/
@Override
public void display(GLAutoDrawable drawable) {
// get the GL2 context on which we can draw
final GL2 gl = drawable.getGL().getGL2();

// Clear The Screen And The Depth Buffer
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);

// Sets the background color of our window to black (RGB alpha)
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

// reset the scene to its origin point
gl.glLoadIdentity();

// reset the scene to its origin point
gl.glLoadIdentity();

// Move Left 1.5 Units And Into The Screen 6.0
gl.glTranslatef(0.0f,0.0f,-6.0f);

// Rotate The Cube around X, Y and Z Axis (NEW)
gl.glRotatef(xrot,1.0f,0.0f,0.0f); // Rotate On The X Axis
gl.glRotatef(yrot,0.0f,1.0f,0.0f); // Rotate On The Y Axis
gl.glRotatef(zrot,0.0f,0.0f,1.0f); // Rotate On The Z Axis

// draw the cube (NEW)
drawTexturedCube(gl);

// adjust the rotation angle (NEW)
xrot+=0.3f; // X Axis Rotation
yrot+=0.2f; // Y Axis Rotation
zrot+=0.4f; // Z Axis Rotation

gl.glFlush();
}

/**
* This method is called when our window his dimension is changed
*/
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
final GL2 gl = drawable.getGL().getGL2();
final GLU glu = new GLU();

if (height <= 0) // avoid a divide by zero error!
height = 1;

gl.glViewport(0, 0, width, height);
// change matrixmode to projection for perspective
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();

final float h = (float)width / (float)height;
glu.gluPerspective(45.0f, h, 1.0, 20.0);
// restore the matrix mode to modelview
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
}
});

// add a KeyListener so we can respond to user input
window.addKeyListener(new KeyAdapter() {
// We stop the tutorial if we press the escape key
@Override
public void keyPressed(KeyEvent ke){
if(ke.getKeyCode() == KeyEvent.VK_ESCAPE)
closeTutorial();

if(ke.getKeyCode() == KeyEvent.VK_F1){
fullscreen = !fullscreen;
window.setFullscreen(fullscreen);
}
}
}
);

// create a new FPS Animator
FPSAnimator animator = new FPSAnimator(window, 60);
animator.add(window);
animator.start();
}

/**
* Closes the tutorial and retuns to the selection menu
*/
private void closeTutorial(){
if(window != null){
window.setVisible(false);
window.destroy();
}
}

/**
* This method draws a textured cube on the screen (NEW)
* @param gl The gl context on which we draw
*/
private void drawTexturedCube(GL2 gl) {
// Use the texture on our cube
cubeTexture.bind(gl);

gl.glBegin(GL2.GL_QUADS);
// Front Face
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
// Back Face
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
// Top Face
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Texture and Quad
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Texture and Quad
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
// Bottom Face
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f); // Top Right Of The Texture and Quad
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f); // Top Left Of The Texture and Quad
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
// Right face
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
// Left Face
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
gl.glEnd();
}

/**
* Loads the texture for the cube (NEW)
* @return The texture
*/
private Texture loadTexture() {
Texture texture = null;
File imgFile = null;

try {
URL url = this.getClass().getClassLoader().getResource("nehe.bmp");
imgFile = new File(url.getFile());
texture = TextureIO.newTexture(imgFile, true);// bool - mipmap.
}
catch (IOException e) {
System.out.println("fail openning file... " + imgFile.getName());
System.out.println(e);
}

return texture;
}

public static void main(String argv[]){
Tutorial_06 tut = new Tutorial_06();
}
}


I think the problem is with this line:

URL url = this.getClass().getClassLoader().getResource("nehe.bmp");

I've tried ("\nehe.bmp"), I've tried "C:\nehe.bmp", I've tried all sorts of other options, I've even tried using paint to make my own. Nothing has worked. Please save me. Edited by Caste
put code in code-tag

Share this post


Link to post
Share on other sites
Advertisement
He Storm.

I faced with same issue. It was found that mentioned function reads file from source folder where your source.java file is located. I just put image to this folder and used this call: texture = TextureIO.newTexture(getClass().getClassLoader().getResource("nehe.png"), false, textureFileType);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!