Jirr/Irrlicht Tutorials (looking to give, not take)

Discussion about Irrlicht's Java wrapper
Post Reply
jestermax
Posts: 17
Joined: Mon Feb 26, 2007 4:36 pm

Jirr/Irrlicht Tutorials (looking to give, not take)

Post by jestermax »

Hey, i guess you could say that i'm fairly new to Irrlicht and Jirr but i think that i could possibly help with some tutorials/guides. The wiki looks kind of sad right now but i'd like to help change that. Right now i use Irrlicht via Jirr so maybe i should start with some tutorials for that. Not that the provided ones are lacking or anything, but maybe some specific case scenarios could be nice.

Any thoughts on this? Anyone actually want me to help?
Frodenius
Posts: 64
Joined: Sun Aug 29, 2004 11:24 am
Location: Germany/Frankfurt
Contact:

Post by Frodenius »

well im currently up on learning java for fun but i am already familiar with c++ an several other programming languages so this wont be so hard i think. i want to use irrlicht via jirr so i would read your tutorials and say how to make them better ;) as you see i can only offer criticism... maybe also some ideas :D

[EDIT] well i am with irrlicht since 0.6 :D
worst programming style ever seen...
jestermax
Posts: 17
Joined: Mon Feb 26, 2007 4:36 pm

Post by jestermax »

sure, i'm always looking to improve so any criticism would be appreciated.

It might take me a little while to get enough time to A) create some projects or case examples and B) write up some user friendly tutorials and C) figure out how to post them but i'll keep at it.

A couple ideas i came up with include setting up a basic Jirr project from scratch and a few example demos like a simple adventure-style game system (3d platformer).

If i make case examples, then anyone using standard Irrlicht would be able to translate them pretty quickly to the C++ version. Anyways, like i said i'm still getting settled in with Irrlicht and figuring out a few things (for example: trying to define custom collision reaction events).

I'll keep you posted
jestermax
Posts: 17
Joined: Mon Feb 26, 2007 4:36 pm

Simple Portals using Jirr

Post by jestermax »

Hey, i built a demo that uses a form of simple portals. It's kinda neat to take a quick look at i think but don't use it as an example for brilliant software design, hehe.

Code: Select all

/**
 * @(#)PortalTest.java
 *
 * PortalTest application
 *
 * @author 
 * @version 1.00 2007/3/8
 */
 
 
import net.sf.jirr.*;

import java.io.IOException;
 
 
 

public class PortalTest {
    
   static
   {
      System.loadLibrary("irrlicht_wrap");
   }

   static IrrlichtDevice device = null;

/*
	The next few lines start up the engine. Just like in most other tutorials
	before. But in addition, we ask the user if he wants this example to use
	high level shaders if he selected a driver which is capable of doing so.
*/

	public static void main(String[] args)
	{
		// The portal's destination
   		vector3df PortalTargetPosition = new vector3df(123, 600, -578);
   		// The portal's view direction
   		vector3df PortalTargetView = new vector3df(350, 530, -115);
   	
   	
   	
      // Set driver to use OpenGL
      E_DRIVER_TYPE driverType = E_DRIVER_TYPE.EDT_OPENGL;

		// create device and exit if creation failed
		device = Jirr.createDeviceJava(
				driverType, 
				640, 
				480, 
				32, 
				false, 
				false, 
				false, 
				null
			);
		
		if (device == null)
		{
			System.exit(1); // could not create selected driver.
		}

		IVideoDriver driver = device.getVideoDriver();
		ISceneManager smgr = device.getSceneManager();
		IGUIEnvironment env = device.getGUIEnvironment();


		//===== MAP =====//
		ISceneNode mapNode = null;	// Level node
		// Load the map
		device.getFileSystem().addZipFileArchive("../media/map-20kdm2.pk3", true, true);
		IAnimatedMesh mesh = smgr.getMesh("20kdm2.bsp");
		
		// Add map as an octtree scene node
		if (mesh != null)
			mapNode = smgr.addOctTreeSceneNode(mesh, null, -1, 128);
		
		// Move the map to the proper location
		if (mapNode != null)
			mapNode.setPosition(new vector3df(-1300,-144,-1249));
		
		// Enable fog for this map
		mapNode.setMaterialFlag(E_MATERIAL_FLAG.EMF_FOG_ENABLE, true);
		//===== /MAP =====//


		
   		//===== Fog =====//
		driver.setFog(  new SColor(0, 138, 125, 81), // Colour
						true, 	// Linear fog
						250, 	// start
						1000, 	// end
						0, 		// density
						true, 	// Pixel fog
						false);	// Range fog
		//===== /Fog =====//
		
   		
		// Add fps camera
		ICameraSceneNode fpsCamera = smgr.addCameraSceneNodeFPS();
      
      
      
      //===== Triangle-map selector =====//
		ITriangleSelector selector = null;	// Selector
		if (mapNode != null)
		{
			// Create a triangle selector for the map
			selector = smgr.createOctTreeTriangleSelector(mesh.getMesh(0), mapNode, 128);
			
			// Assign the selector to the map's scene node
			mapNode.setTriangleSelector(selector);
			selector.drop();
		}
		//===== /Triangle-map selector =====//
		
		
      //===== Camera Collisions =====//		
		ISceneNodeAnimator anim = smgr.createCollisionResponseAnimator(
					selector, 					// Triangle selector
					fpsCamera, 					// Scene node for collisions
					new vector3df(30, 50, 30), 	// Ellipsoid Radius
					new vector3df(0, -3, 0),	// Gravity per second
					new vector3df(0, 50, 0)		// Ellipsoid translation; default is center of node
		);
		// Add auto collision response to the camera
		fpsCamera.addAnimator(anim);
		anim.drop();
		//===== /Camera Collisions =====//
		
		
		

		//===== PORTAL =====//
		// Create portal
		ISceneNode portal = smgr.addSphereSceneNode(60);
		// Sphere map the portal's texture
		portal.setMaterialType( E_MATERIAL_TYPE.EMT_SPHERE_MAP );
		
		// Disable lighting from the portal
		portal.setMaterialFlag(E_MATERIAL_FLAG.EMF_LIGHTING, false);
		//===== /PORTAL =====//
		
		
		//===== GUI =====//
		// set window caption
		device.setWindowCaption("Irrlicht Engine @java - Render to Texture and Specular Highlights example");
		// disable mouse cursor
		device.getCursorControl().setVisible(false);
		//===== /GUI =====//

		//===== SET UP PORTAL =====//
		// create render target
		ITexture rt = null;
		ICameraSceneNode fixedCam = null;
		
		// Check if driver supports rendering to a texture
		if (driver.queryFeature(E_VIDEO_DRIVER_FEATURE.EVDF_RENDER_TO_TARGET))
		{
			// Create a new render target (as a texture)
		 	rt = driver.createRenderTargetTexture(new dimension2di(256, 256));
			portal.setMaterialTexture(0, rt); // set material of portal to render target
		
		 	// add fixed camera for the portal view
		 	fixedCam = smgr.addCameraSceneNode(
	 						null, 							// Parent scene node
	 						new vector3df(280, 140, -400),	// Destination
	 						new vector3df(240, 140, -600)	// Direction
	         			);
		}
		// Error case
		else
		{
			// create problem text
			IGUISkin skin = env.getSkin();
			IGUIFont font = env.getFont("../media/fonthaettenschweiler.bmp");
			if (font != null)
			{
            	skin.setFont(font);
         	}

         	IGUIStaticText text = env.addStaticText(
                 "Your hardware or this renderer is not able to use the "
                         + "render to texture feature. RTT Disabled.",
                 new recti(150, 20, 470, 60));

         	text.setOverrideColor(new SColor(100, 255, 255, 255));
      	}
		//===== /SET UP PORTAL =====//
		
		// RENDERING
      	int lastFPS = -1;

      	while (device.run())
      	{
         	if (device.isWindowActive())
			{
            	driver.beginScene(true, true, new SColor(0,255,255,255));

            	if (rt != null)
            	{
					// Change the render target to the texture
					driver.setRenderTarget(rt, true, true, new SColor(0, 0, 0, 0));
					
					// Don't let the portal show up in itself
					portal.setVisible(false);
					
					// Set the portal cam as the
					smgr.setActiveCamera(fixedCam);
				
					// draw whole scene into render buffer
					smgr.drawAll();
					
					// Set back old render target
					driver.setRenderTarget(null);
				
					// make the cube visible and set the user controlled camera as active one
					portal.setVisible(true);
					smgr.setActiveCamera(fpsCamera);
            	}

	            // draw scene normally
	            smgr.drawAll();
	            env.drawAll();
	       
	            
	            // LOS intersections
				// Line for calculating collisions on
				line3df line1 = new line3df();
				// Assign the camera position as the start of the line
				line1.setStart(fpsCamera.getPosition());
				//
				line1.setEnd(
					
					add(
						line1.getStart(), 
						mult(
							sub(
								fpsCamera.getTarget(), 
								line1.getStart()
							).normalize(), // Subtract and then normalize
							1000.0f
						)// multiply
					)// add
				);
				
				
	
	            driver.endScene();
	
	
				// PORTAL VIEW
				fixedCam.setRotation(fpsCamera.getRotation());
				// /PORTAL VIEW
				
				// CALCULATE PORTAL 
				// get a line from the camera to the portal
				line3df line = new line3df();;
				line.setStart( fpsCamera.getPosition() );
				line.setEnd( portal.getPosition() );
	
				// Player is in the portal
				if( line.getLength() < 60 ){
					fpsCamera.setPosition( fixedCam.getPosition());
				}
				// /CALCULATE PORTAL


	
	            // not in original code but still interesting data
	            int fps = driver.getFPS();

	            if (lastFPS != fps)
	            {
	               String str = "Portal Demo [";
	               str += driver.getName();
	               str += "] FPS:";
	               str += fps;
					str += " " + fpsCamera.getPosition().getX() + ", ";
					str += " " + fpsCamera.getPosition().getY() + ", ";
					str += " " + fpsCamera.getPosition().getZ() + " ";
	               device.setWindowCaption(str);
	               
	               lastFPS = fps;
	            }
			}// if window is active
		}// while

		//===== CLEANUP =====//
		if (rt != null)
		{
         	rt.drop(); // drop render target because we created if with a create() method
      	}

      	device.drop(); // drop device
      	System.exit(0);
	}// main
   
   
   
   
   
	// helper methods
	private static vector3df add(vector3df a, vector3df b)
	{
		vector3df myVector3df = new vector3df(a.getX() + b.getX(),
		                                      a.getY() + b.getY(),
		                                      a.getZ() + b.getZ());
		return myVector3df;
	}

	private static vector3df sub(vector3df a, vector3df b)
	{
		vector3df myVector3df = new vector3df(a.getX() - b.getX(),
		                                      a.getY() - b.getY(),
		                                      a.getZ() - b.getZ());
		return myVector3df;
	}

	private static vector3df mult(vector3df a, float f)
	{
		vector3df myVector3df = new vector3df(a.getX() * f,
		                                      a.getY() * f,
		                                      a.getZ() * f);
		return myVector3df;
	}
	
	
	
}// class PortalTest

Frodenius
Posts: 64
Joined: Sun Aug 29, 2004 11:24 am
Location: Germany/Frankfurt
Contact:

Post by Frodenius »

hey,
sry for the late answer but im currently a bit stressed with exams..
this portal is a funny thing.. :D
it would look better to place the sphere in the middle and not stuck in the wall..

Code: Select all

portal.setPosition(new vector3df(100,5,100));
after creation.
there is one thing thats irritating, why do you need the line1 ?? for what collisions?

and last (not least) its a bit akward to have such a long title... you cant see it in fullscreen and it would be better to have a small textbox in some corner of the screen.

but all in all a nice way to implement a portal :D (i would've done it using a billboard with a beatiful burning border...)
worst programming style ever seen...
jestermax
Posts: 17
Joined: Mon Feb 26, 2007 4:36 pm

Post by jestermax »

whoops, you are indeed right, i had a bit of leftover code from a few tests i was playing with. sorry about that :(

As for the billboards, yeah, i could've easily done that and maybe i will next time i'm playing with that. :) i just thought it was fairly easy to use a sphere so i just used it for the sake of using it.
jestermax
Posts: 17
Joined: Mon Feb 26, 2007 4:36 pm

Post by jestermax »

btw, i forgot to thank you for your input :).

Also theory-wise, how would YOU create portals with the burning border? (not asking for code, just how you'd go abouts doing that)
Frodenius
Posts: 64
Joined: Sun Aug 29, 2004 11:24 am
Location: Germany/Frankfurt
Contact:

Post by Frodenius »

btw, i forgot to thank you for your input
np its a pleasure.. :)

well i wouldve made a "mask" with a fire ring (the rest is transparent)
create two billboards, the mask is in the front but the ring has to be thick enough to cover the parts of the rendertarget which are not in the circle...
hmm sth like that...
or.. a mask with the filled circle in the middle (maybe black) and the rest maybe white an then a mask-operation (only the pixels from rendertarget which are black in the mask) like bitwise changing of numbers, after that adding the ring in another one.. huh that would be a bit more action but doesnt need the ring to be thick...

hugh if one thinks it over its trickier than thought..
worst programming style ever seen...
jestermax
Posts: 17
Joined: Mon Feb 26, 2007 4:36 pm

Post by jestermax »

Actually, to be honest, i'm not quite sure how to make a circular billboard. However, to be fair i haven't really done an EXTENSIVE search on it. I've read the API a few times over and don't remember seeing any thing that could be helpful though.
Something i want to do sometime is make some facade classes to "add on" to the functionality of irrlicht, for example maybe using collisions properly without having to add a physics library or the pre-made FPS camera.
The physics one is high on my priority since ODE for Java is pretty much the only solution for physics with Jirr but i haven't found ANYONE that knows anyhting about it. but that could be my fault, i don't even know how to use the normal versions (non-java) so maybe if i figured that out i'd be able to use it with java :)
Frodenius
Posts: 64
Joined: Sun Aug 29, 2004 11:24 am
Location: Germany/Frankfurt
Contact:

Post by Frodenius »

well a round billboard is not possible because a billboard is just a rectangle plane with a texture on it. one would have to make a round plane for that... hmm the polycount would rise depending on the smooothness of the whateverform.. probably pretty unefficient..
worst programming style ever seen...
franklyn
Posts: 36
Joined: Thu Mar 15, 2007 7:03 pm

Post by franklyn »

cant you just apply a round alpha map ?
Frodenius
Posts: 64
Joined: Sun Aug 29, 2004 11:24 am
Location: Germany/Frankfurt
Contact:

Post by Frodenius »

but then its only visual so you cannot e.g. shoot or walk through the transparent area unless your collisionmanager can handle those sort of things..
worst programming style ever seen...
cyber_knight
Posts: 1
Joined: Thu Apr 19, 2007 2:18 pm

Need a Class

Post by cyber_knight »

I don't seem to have Jirr.createDeviceJava. Can you send me it?
jestermax
Posts: 17
Joined: Mon Feb 26, 2007 4:36 pm

Post by jestermax »

Ah, the latest versions of Jirr don't have the function name anymore. Also that may not be the only function call that has changed so i can't offer any quick fixes just yet. I'm in a few projects right now so i don't have too much time to play around with it just yet but when i do get some time i'll have a look at the new API and rewrite that code :) unless you beat me to it first of course
jirr
Posts: 36
Joined: Sat Feb 19, 2005 8:05 am

Re: Need a Class

Post by jirr »

cyber_knight wrote:I don't seem to have Jirr.createDeviceJava. Can you send me it?
Simply use Jirr.createDevice - it has nearly the same functions - except that you have to use new dimension2di(width, height) instead of the two ints ...
Post Reply