Author Topic: JPCT Hello World  (Read 5253 times)

Offline Redstop

  • byte
  • *
  • Posts: 2
    • View Profile
JPCT Hello World
« on: June 11, 2006, 01:12:16 pm »
JPCT looks like just what I need, but I'm having trouble putting together a minimal sample to explore. The two sample programs go far beyond what I need and I'd rather start from a "Hello World" sample and build up. Can anyone point me to a simple example that shows just a primitive (eg. a box) using the software renderer?

Here's my code, which doesn't work:

Code: [Select]

// this is done once, just in case shapes must have a texture
// must they?
TextureManager tm = TextureManager.getInstance();
tm.addTexture("default", tm.getDummyTexture()); // should be white

// here's the paint:
public void paint(Graphics g)
{
  Dimension sz = getSize();
  World w = new World();

  // just copied from the sample
  w.getLights().setOverbrightLighting(
      Lights.OVERBRIGHT_LIGHTING_DISABLED);
  w.getLights().setRGBScale(Lights.RGB_SCALE_2X);
  w.setAmbientLight(25, 30, 30);

  // these just copied from the car example
  // no idea if ambient light is sufficient
  w.addLight(new SimpleVector(0, -150, 0), 25, 22, 19);
  w.addLight(new SimpleVector(-1000, -150, 1000), 22, 5, 4);
  w.addLight(new SimpleVector(1000, -150, -1000), 4, 2, 22);

  Object3D box = Primitives.getBox(100, 100);
  box.setOrigin(new SimpleVector(200,200,200));
  box.setVisibility(true); // is this the default?
  box.setTexture("default"); // will it take dummy texture otherwise?
  w.addObject(box);
  w.getCamera().lookAt(box.getOrigin());

  FrameBuffer buffer=new FrameBuffer(sz.width, sz.height,  
      FrameBuffer.SAMPLINGMODE_NORMAL);
  buffer.enableRenderer(IRenderer.RENDERER_SOFTWARE);
   
  buffer.setBoundingBoxMode(FrameBuffer.BOUNDINGBOX_NOT_USED);
  // I found staring at an empty black screen
  // after each attempt at getting this to work too depressing
  buffer.clear(Color.yellow);
  w.renderScene(buffer);
  buffer.update();
  buffer.display(g);
}


All I get is a blank (yellow) screen.
What I want to see is a white box on a yellow background.

Any help appreciated. Thanks, Redstop.

Offline Melssj5

  • double
  • *****
  • Posts: 715
    • View Profile
JPCT Hello World
« Reply #1 on: June 11, 2006, 09:09:28 pm »
MMMM, what kind of class is it. (Applet, JApplet, Frame, JFrame, Canvas).

1.- As you should know the paint method is usable on awt classes. So, you will need a Frame to begin.

2.- The paint method contain the information that will be shown on screen using the Graphics object, so you must put only the render code there, but you are making much more stuff that make fail your program.

buffer.clear(Color.yellow);
buffer.update();
w.renderScene(buffer);
w.draw (buffer);
buffer.display(g);


3.- The paint method contains information for presenting just one image, so you will need to call the repaint method to view a continue render.

4.- You must not forget to add an ambient light or you will se all black.

5.- The world and the rest of stuff (World, FrameBuffer, Cameras, etc) have to be initialized on the constructor of the Frame, not in the paint method.

6.- You must build the objects before watching them. You already added the Object3D to the World but you missed this "w.buildAllObjects ();" if not nothing will appear. Thats why you just see a yellow screen.

Try watching at the examples I made on the download section, the maze one is messy but is done with SW rendering at a Applet.

7.- Dont forget to call to repaint () for each iteration of the render. you can do a loop with a while or even have a thread for that.
Nada por ahora

Offline Redstop

  • byte
  • *
  • Posts: 2
    • View Profile
JPCT Hello World
« Reply #2 on: June 11, 2006, 10:21:15 pm »
Thanks for your reply.

For testing I'm just using a JFrame and putting everything in paint for simplicity (except for the TextureManager because I guess there's no point adding the same texture repeatedly).

I don't need any animation at this point, just a single rendered image which later will be generated on a server with no video card (hence the need for a software renderer).

I've added the w.buildAllObjects(); as you suggest, but I'm still not getting anything.

I'll take a look at your maze example and see if I can extract the essentials.

Regards, Redstop.

Offline Melssj5

  • double
  • *****
  • Posts: 715
    • View Profile
JPCT Hello World
« Reply #3 on: June 11, 2006, 11:16:57 pm »
I guess that your camera is placed on the (0, 0, 0) position so it means that is on the center of the cube (inside). So you must move your camera.

Camera cam=w.getCamera ();
cam.setPosition (0, 0, -180);
cam.lookAt (new SimepleVector (0, 0, 0));
Nada por ahora

Offline raft

  • quad
  • ******
  • Posts: 1966
    • View Profile
    • http://www.aptalkarga.com
JPCT Hello World
« Reply #4 on: June 12, 2006, 10:28:19 pm »
you need to call world.draw(buffer) after world.renderScene(buffer);

Code: [Select]
r a f t

Offline Melssj5

  • double
  • *****
  • Posts: 715
    • View Profile
JPCT Hello World
« Reply #5 on: June 13, 2006, 07:34:44 am »
You dont need a video card to do the 3d stuff neither in OGL nor in SW rendering. The FrameBuffer have to be initializaed with the video options, but if you server wont do any render on a screen, just pass the objects to make each client render them, then you are free from video settings.

Dont forget to review the advices I made and what raft said. I guess you didnt noticed the draw method were missing, I put it on my posts but I dont know if you noticed it.

When the camera is inside a object3d, then all the object will be invisible. you may not being able to look your cube because you are inside of it. (Move the camera)
Nada por ahora

Offline athanazio

  • byte
  • *
  • Posts: 49
    • View Profile
    • http://www.athanazio.pro.br
JPCT Hello World
« Reply #6 on: November 20, 2006, 04:39:52 am »
I'm also trying to put together my Hello World, I only get a black screen with some lights (I think...) on. here is the code:

Code: [Select]

import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;

import com.threed.jpct.Camera;
import com.threed.jpct.Config;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.IRenderer;
import com.threed.jpct.Lights;
import com.threed.jpct.Object3D;
import com.threed.jpct.Primitives;
import com.threed.jpct.SimpleVector;
import com.threed.jpct.TextureManager;
import com.threed.jpct.World;
import com.threed.jpct.util.KeyMapper;
import com.threed.jpct.util.KeyState;

public class HelloWorld extends JFrame {

private FrameBuffer buffer = null;

private World world = null;

private TextureManager textureManager = null;

private Camera camera = null;

private int width = 640;

private int height = 480;

private Graphics g = null;

/**
* main method
*/
private static final long serialVersionUID = 1L;

public static void main(String[] args) {
HelloWorld h = new HelloWorld();
}

private boolean exit = false;

private int titleBarHeight;

private int leftBorderWidth;

private KeyMapper keyMapper;

private void gameLoop() {
World.setDefaultThread(Thread.currentThread());

buffer = new FrameBuffer(width, height, FrameBuffer.SAMPLINGMODE_NORMAL);
buffer.enableRenderer(IRenderer.RENDERER_SOFTWARE);

while (!exit) {
buffer.clear();
world.renderScene(buffer);
world.draw(buffer);
buffer.update();
poll();
buffer.display(g, leftBorderWidth, titleBarHeight);
Thread.yield();

try {
Thread.sleep(200);
} catch (InterruptedException e) {
}
}

System.exit(0);
}

/**
*/
public HelloWorld() {
setFrameStuff();
add3DStuff();
gameLoop();
}

private void setFrameStuff() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("jPCT Hello World (" + Config.getVersion() + ")");
pack();
Insets insets = getInsets();
titleBarHeight = insets.top;
leftBorderWidth = insets.left;
setSize(width + leftBorderWidth + insets.right, height + titleBarHeight
+ insets.bottom);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
g = getGraphics();
keyMapper = new KeyMapper(this);
}

private void add3DStuff() {
world = new World();
textureManager = TextureManager.getInstance();
Object3D box = Primitives.getBox(50, 50);
box.setOrigin(new SimpleVector(0, 0, 0));
world.addObject(box);
world.buildAllObjects();
/**
* Place the camera at the starting position.
*/
setCamera(box);
setLight();
}

private void setCamera(Object3D box) {
camera = world.getCamera();
camera.setPosition(0, 0, -180);
camera.lookAt(box.getTransformedCenter());
}

private void setLight() {
Config.fadeoutLight = false;
world.getLights().setOverbrightLighting(
Lights.OVERBRIGHT_LIGHTING_DISABLED);
world.getLights().setRGBScale(Lights.RGB_SCALE_2X);
world.setAmbientLight(25, 30, 30);

/**
* Place the lightsources...
*/
world.addLight(new SimpleVector(0, -150, 0), 25, 22, 19);
world.addLight(new SimpleVector(-100, -150, 100), 22, 5, 4);
world.addLight(new SimpleVector(100, -150, -100), 4, 2, 22);

}

/**
* Use the KeyMapper to poll the keyboard
*/
private void poll() {
KeyState state = null;
do {
state = keyMapper.poll();
if (state != KeyState.NONE) {
keyAffected(state);
}
} while (state != KeyState.NONE);
}

private void keyAffected(KeyState state) {
int code = state.getKeyCode();
boolean event = state.getState();

switch (code) {
case (KeyEvent.VK_ESCAPE): {
exit = event;
break;
}
}
}
}


Online EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11749
    • View Profile
    • http://www.jpct.net
JPCT Hello World
« Reply #7 on: November 20, 2006, 01:20:44 pm »
Primitives.getBox(<float>,<float>)'s method signature is different from what you want to do. The first parameter is the width, the second is the height relative to the width, i.e. 50,50 will make the cube 50 units wide and 2500 units high. Try Primitives.getBox(50, 1); instead and you are fine.

Offline athanazio

  • byte
  • *
  • Posts: 49
    • View Profile
    • http://www.athanazio.pro.br
JPCT Hello World
« Reply #8 on: November 20, 2006, 01:44:41 pm »
Thanks !!! now it works :)
http://www.athanazio.pro.br/?p=929