using Slick-util + texture Image Loading

Started by nilotic, June 02, 2012, 06:25:52

Previous topic - Next topic

nilotic

Hello.
First, My English is not good, So I ask for your understanding regarding this matter.


I try to load image for texture. But, I can't find problem.

Error Message is
-----------------------------------------------------------------------------------------------------------------------
java.lang.reflect.InvocationTargetException
   at java.awt.EventQueue.invokeAndWait(EventQueue.java:1242)
   at com.bulletphysics.demos.applet.JBulletApplet.init(JBulletApplet.java:62)
   at sun.applet.AppletPanel.run(AppletPanel.java:434)
   at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.RuntimeException: Resource not found: res/block1.png
   at org.newdawn.slick.util.ResourceLoader.getResourceAsStream(ResourceLoader.java:69)
   at com.bulletphysics.demos.applet.SoftwareGL.init(SoftwareGL.java:72)
   at com.bulletphysics.demos.applet.DemoPanel.<init>(DemoPanel.java:70)
   at com.bulletphysics.demos.applet.JBulletApplet$1.run(JBulletApplet.java:116)
   at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:241)
   at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
   at java.awt.EventQueue.access$000(EventQueue.java:101)
   at java.awt.EventQueue$3.run(EventQueue.java:666)
   at java.awt.EventQueue$3.run(EventQueue.java:664)
   at java.security.AccessController.doPrivileged(Native Method)
   at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
   at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
   at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
   at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
   at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

--------------------------------------------------------------------------------------------------------------------------------------


I checked path (res-folder  in Project folder) and image size ( 64, 128..ect.. pow(n,2))

Can i get some tips?

CodeBunny


nilotic

You can find code   in init() function

And I try to draw block or box. So, I modify drawCube() function


/*
 * Java port of Bullet (c) 2008 Martin Dvorak <jezek2@advel.cz>
 *
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the authors be held liable for any damages arising from
 * the use of this software.
 * 
 * Permission is granted to anyone to use this software for any purpose, 
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 * 
 * 1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software. If you use this software
 *    in a product, an acknowledgment in the product documentation would be
 *    appreciated but is not required.
 * 2. Altered source versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 */

package com.bulletphysics.demos.applet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import javax.vecmath.Matrix4f;

import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

import com.bulletphysics.demos.opengl.IGL;
/**
 *
 * @author jezek2
 */
public class SoftwareGL implements IGL {
	
	
	
	private Graphics3D gl;
	private int matrixMode = GL_MODELVIEW;
	private Matrix4f tmpMat = new Matrix4f();
	private Graphics g;
	Texture texture;
	
	public SoftwareGL() {
		gl = new Graphics3D();
	}
	
	public void init(BufferedImage img) throws IOException{
		gl.init(((DataBufferInt)img.getRaster().getDataBuffer()).getData(), img.getWidth(), img.getHeight());
		g = img.getGraphics();
		g.setFont(new Font("Dialog", Font.PLAIN, 10));
		g.setColor(Color.BLACK);

		Graphics2D g2 = (Graphics2D)g;
		g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
		
		
		//texture load
		try{		
			texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/block1.png"));
		}catch(IOException e){
			e.printStackTrace();
		}
	}

	public void glViewport(int x, int y, int width, int height) {
	}

	public void glLight(int light, int pname, float[] params) {
		Light l = gl.getLight(light - GL_LIGHT0);
		switch (pname) {
			case GL_AMBIENT: l.ambient.set(params); break;
			case GL_DIFFUSE: l.diffuse.set(params); break;
			case GL_SPECULAR: l.specular.set(params); break;
			case GL_POSITION:
				l.position.set(params);
				if (l.position.w == 0f) {
					float invlen = 1f / (float)Math.sqrt(l.position.x*l.position.x + l.position.y*l.position.y + l.position.z*l.position.z);
					l.position.x *= invlen;
					l.position.y *= invlen;
					l.position.z *= invlen;
				}
				break;
		}
	}

	public void glEnable(int cap) {
		if (cap >= GL_LIGHT0 && cap <= GL_LIGHT1) {
			gl.getLight(cap - GL_LIGHT0).enabled = true;
			return;
		}
		
		switch (cap) {
			case GL_LIGHTING: gl.setLightingEnabled(true); break;
		}
	}

	public void glDisable(int cap) {
		if (cap >= GL_LIGHT0 && cap <= GL_LIGHT1) {
			gl.getLight(cap - GL_LIGHT0).enabled = false;
			return;
		}
		
		switch (cap) {
			case GL_LIGHTING: gl.setLightingEnabled(false); break;
		}
	}

	public void glShadeModel(int mode) {
	}

	public void glDepthFunc(int func) {
	}

	public void glClearColor(float red, float green, float blue, float alpha) {
		gl.setClearColor(red, red, blue);
	}

	public void glMatrixMode(int mode) {
		matrixMode = mode;
	}
	
	private void setMatrix(Matrix4f mat) {
		switch (matrixMode) {
			case GL_MODELVIEW: gl.setViewMatrix(mat); break;
			case GL_PROJECTION: gl.setProjMatrix(mat); break;
			default: throw new IllegalStateException();
		}
	}
	
	private void mulMatrix(Matrix4f mat) {
		switch (matrixMode) {
			case GL_MODELVIEW: gl.mulViewMatrix(mat); break;
			case GL_PROJECTION: gl.mulProjMatrix(mat); break;
			default: throw new IllegalStateException();
		}
	}

	public void glLoadIdentity() {
		tmpMat.setIdentity();
		setMatrix(tmpMat);
	}

	public void glFrustum(double left, double right, double bottom, double top, double zNear, double zFar) {
		Utils.setFrustum(tmpMat, (float)left, (float)right, (float)bottom, (float)top, (float)zNear, (float)zFar);
		mulMatrix(tmpMat);
	}

	public void gluLookAt(float eyex, float eyey, float eyez, float centerx, float centery, float centerz, float upx, float upy, float upz) {
		Utils.setLookAt(tmpMat, eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz);
		mulMatrix(tmpMat);
	}

	public void glPushMatrix() {
		if (matrixMode == GL_MODELVIEW) {
			gl.pushViewMatrix();
		}
	}

	public void glPopMatrix() {
		if (matrixMode == GL_MODELVIEW) {
			gl.popViewMatrix();
		}
	}

	public void gluOrtho2D(float left, float right, float bottom, float top) {
	}

	public void glScalef(float x, float y, float z) {
		tmpMat.setIdentity();
		tmpMat.m00 = x;
		tmpMat.m11 = y;
		tmpMat.m22 = z;
		mulMatrix(tmpMat);
	}

	public void glTranslatef(float x, float y, float z) {
		tmpMat.setIdentity();
		tmpMat.m03 = x;
		tmpMat.m13 = y;
		tmpMat.m23 = z;
		mulMatrix(tmpMat);
	}

	public void glColor3f(float red, float green, float blue) {
		gl.setColor(red, green, blue);
	}

	public void glClear(int glMask) {
		int mask = 0;
		if ((glMask & GL_COLOR_BUFFER_BIT) != 0) mask |= Graphics3D.COLOR_BUFFER;
		if ((glMask & GL_DEPTH_BUFFER_BIT) != 0) mask |= Graphics3D.DEPTH_BUFFER;
		gl.clear(mask);
	}

	public void glBegin(int mode) {
		switch (mode) {
			case GL_LINES: gl.begin(Graphics3D.LINES); break;
			case GL_TRIANGLES: gl.begin(Graphics3D.TRIANGLES); break;
			case GL_QUADS: gl.begin(Graphics3D.QUADS); break;
			default: throw new IllegalArgumentException();
		}
	}

	public void glEnd() {
		gl.end();
	}

	public void glVertex3f(float x, float y, float z) {
		gl.addVertex(x, y, z);
	}

	public void glLineWidth(float width) {
	}

	public void glPointSize(float size) {
	}

	public void glNormal3f(float nx, float ny, float nz) {
		gl.setNormal(nx, ny, nz);
	}

	public void glMultMatrix(float[] m) {
		tmpMat.m00 = m[0];
		tmpMat.m10 = m[1];
		tmpMat.m20 = m[2];
		tmpMat.m30 = m[3];
		tmpMat.m01 = m[4];
		tmpMat.m11 = m[5];
		tmpMat.m21 = m[6];
		tmpMat.m31 = m[7];
		tmpMat.m02 = m[8];
		tmpMat.m12 = m[9];
		tmpMat.m22 = m[10];
		tmpMat.m32 = m[11];
		tmpMat.m03 = m[12];
		tmpMat.m13 = m[13];
		tmpMat.m23 = m[14];
		tmpMat.m33 = m[15];
		mulMatrix(tmpMat);
	}

	
	public void drawCube(float extent) {
		
		//TextureLoader   textureLoader= new TextureLoader();
		//Sprite boxTexture= new Sprite(textureLoader, "box1.jpg");
		/*Texture texture = null;
		int width;
		int height;*/
		
		/*try {
			texture = textureLoader.getTexture("res/box1.jpg");
			width = texture.getImageWidth();
			height = texture.getImageHeight();
		} catch (IOException ioe) {
			// TODO Auto-generated catch block
			ioe.printStackTrace();
			System.exit(-1);
		}*/

		//texture.bind();
		
		
			
		
		
		
		
		extent = extent * 0.5f;
		
	    glBegin(GL_QUADS);
        glNormal3f( 1f, 0f, 0f); 
       //glTexCoord2f(0, 0);
        glVertex3f(+extent,-extent,+extent); 
      //  glTexCoord2f(0, texture.getHeight());
        glVertex3f(+extent,-extent,-extent); 
       // glTexCoord2f(texture.getWidth(), texture.getHeight());
        glVertex3f(+extent,+extent,-extent);
      //  glTexCoord2f(texture.getWidth(), 0);
        glVertex3f(+extent,+extent,+extent);
        
        
        glNormal3f( 0f, 1f, 0f);
        glVertex3f(+extent,+extent,+extent); 
        glVertex3f(+extent,+extent,-extent); 
        glVertex3f(-extent,+extent,-extent); 
        glVertex3f(-extent,+extent,+extent);
        
        
        glNormal3f( 0f, 0f, 1f); 
        glVertex3f(+extent,+extent,+extent); 
        glVertex3f(-extent,+extent,+extent); 
        glVertex3f(-extent,-extent,+extent); 
        glVertex3f(+extent,-extent,+extent);
        
        
        glNormal3f(-1f, 0f, 0f); 
        glVertex3f(-extent,-extent,+extent); 
        glVertex3f(-extent,+extent,+extent); 
        glVertex3f(-extent,+extent,-extent); 
        glVertex3f(-extent,-extent,-extent);
        
        
        glNormal3f( 0f,-1f, 0f); 
        glVertex3f(-extent,-extent,+extent); 
        glVertex3f(-extent,-extent,-extent); 
        glVertex3f(+extent,-extent,-extent); 
        glVertex3f(+extent,-extent,+extent);
        
        
        glNormal3f( 0f, 0f,-1f); 
        glVertex3f(-extent,-extent,-extent); 
        glVertex3f(-extent,+extent,-extent); 
        glVertex3f(+extent,+extent,-extent); 
        glVertex3f(+extent,-extent,-extent);
		glEnd();
	}

	private Sphere sphere = new Sphere();
	private Cylinder cylinder = new Cylinder();
	private Disk disk = new Disk();
	
	public void drawSphere(float radius, int slices, int stacks) {
		sphere.draw(gl, radius, 6, 6);
	}

	public void drawCylinder(float radius, float halfHeight, int upAxis) {
		glPushMatrix();
		
		Matrix4f mat = new Matrix4f();
		
		switch (upAxis) {
			case 0:
				//glRotatef(-90f, 0.0f, 1.0f, 0.0f);
				mat.rotY((float)Math.PI * -0.5f);
				mulMatrix(mat);
				glTranslatef(0.0f, 0.0f, -halfHeight);
				break;
			case 1:
				//glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
				mat.rotX((float)Math.PI * -0.5f);
				mulMatrix(mat);
				glTranslatef(0.0f, 0.0f, -halfHeight);
				break;
			case 2:
				glTranslatef(0.0f, 0.0f, -halfHeight);
				break;
			default: {
				assert (false);
			}
		}

		// The gluCylinder subroutine draws a cylinder that is oriented along the z axis. 
		// The base of the cylinder is placed at z = 0; the top of the cylinder is placed at z=height. 
		// Like a sphere, the cylinder is subdivided around the z axis into slices and along the z axis into stacks.

		disk.setDrawStyle(Quadric.GLU_FILL);
		disk.setNormals(Quadric.GLU_SMOOTH);
		disk.draw(gl, 0, radius, 8, 1);
		
		cylinder.setDrawStyle(Quadric.GLU_FILL);
		cylinder.setNormals(Quadric.GLU_SMOOTH);
		cylinder.draw(gl, radius, radius, 2f * halfHeight, 8, 1);
		
		glTranslatef(0f, 0f, 2f * halfHeight);
		mat.rotY(-(float)Math.PI);
		mulMatrix(mat);
		disk.draw(gl, 0, radius, 8, 1);

		glPopMatrix();
	}

	public void drawString(CharSequence s, int x, int y, float red, float green, float blue) {
		g.setColor(new Color(red, green, blue));
		g.drawString(s.toString(), x, y);
	}

	public void drawShpere(float scale) {
		// TODO Auto-generated method stub
		
	}

	public void glAreTexturesResident(IntBuffer textures, ByteBuffer residences) {
		// TODO Auto-generated method stub
		
	}

	public void glBindTexture(int target, int texture) {
		// TODO Auto-generated method stub
		
	}

	public void glBitmap(int width, int height, float xorig, float yorig,
			float xmove, float ymove, ByteBuffer bitmap) {
		// TODO Auto-generated method stub
		
	}

	public void glCopyTexImage1D(int target, int level, int internalFormat,
			int x, int y, int width, int border) {
		// TODO Auto-generated method stub
		
	}

	public void glCopyTexImage2D(int target, int level, int internalFormat,
			int x, int y, int width, int height, int border) {
		// TODO Auto-generated method stub
		
	}

	public void glCopyTexSubImage1D(int target, int level, int xoffset, int x,
			int y, int width) {
		// TODO Auto-generated method stub
		
	}

	public void glCopyTexSubImage2D(int target, int level, int xoffset,
			int yoffset, int x, int y, int width, int height) {
		// TODO Auto-generated method stub
		
	}

	public void glDeleteTextures(IntBuffer textures) {
		// TODO Auto-generated method stub
		
	}

	public void glTexCoord1d(double s) {
		// TODO Auto-generated method stub
		
	}

	public void glTexCoord1f(float s) {
		// TODO Auto-generated method stub
		
	}

	public void glTexCoord2d(double s, double t) {
		// TODO Auto-generated method stub
		
	}

	public void glTexCoord2f(float s, float t) {
		// TODO Auto-generated method stub
		
	}

	public void glTexCoord3d(double s, double t, double r) {
		// TODO Auto-generated method stub
		
	}

	public void glTexCoord3f(float s, float t, float r) {
		// TODO Auto-generated method stub
		
	}

	public void glTexCoord4d(double s, double t, double r, double q) {
		// TODO Auto-generated method stub
		
	}

	public void glTexCoord4f(float s, float t, float r, float q) {
		// TODO Auto-generated method stub
		
	}

	public void glGenTextures(IntBuffer textures) {
		// TODO Auto-generated method stub
		
	}

	public void glTexEnv(int target, int pname, FloatBuffer params) {
		// TODO Auto-generated method stub
		
	}

	public void glTexEnv(int target, int pname, IntBuffer params) {
		// TODO Auto-generated method stub
		
	}

	public void glTexEnvf(int target, int pname, float param) {
		// TODO Auto-generated method stub
		
	}

	public void glTexEnvi(int target, int pname, int param) {
		// TODO Auto-generated method stub
		
	}

	public void glTexParameter(int target, int pname, FloatBuffer param) {
		// TODO Auto-generated method stub
		
	}

	public void glTexParameter(int target, int pname, IntBuffer param) {
		// TODO Auto-generated method stub
		
	}

	public void glTexParameterf(int target, int pname, float param) {
		// TODO Auto-generated method stub
		
	}

	public void glTexParameteri(int target, int pname, int param) {
		// TODO Auto-generated method stub
		
	}

	public void glBlendFunc(int sfactor, int dfactor) {
		// TODO Auto-generated method stub
		
	}

}

matheus23

You said, that "res/" is in the Project folder. But the stack trace shows a method named "getResourceAsSteam".
In that case (I'm not pretty sure... I did not work much with Slick...), you should place "res/" somewhere as a package under the "src".

An alternative would be to use BufferedImageUtil form slick, and previously loading that BufferedImage with java's ImageIO. Thats the way I used to do it in the beginning. It's acctually slow, but that shouldn't be a big bottleneck, since you only have to load it one time.
With that way, you have total control about from where that Image is loaded. (You can either use the Class's getResourceAsStream or use a File.)
My github account and currently active project: https://github.com/matheus23/UniverseEngine

nilotic

Thanks for reply.

I tried  2 solution(you suggested). But, Occur same problem.

Maybe , I have to implement myself. :(

mattdesl

Hello, as the error states the problem is that the resource isn't being found. Make sure your "res" folder is under your source folder, as matheus23 explained, and the problem should be resolved.

Here is what my typical project setup looks like:


Notice where "res" is placed. :)

dangerdoc

I was having errors like this. If you put the texure in the same .jar, you can link to it like this:

Texture texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("a package/yourTexure.png);


I hope this helps! It worked for me!
“We build but to tear down. Most of our work and resource is squandered. Our onward march is marked by devastation. Everywhere there is an appalling loss of time, effort and life. A cheerless view, but true.” - Nikola Tesla