Author Topic: Shading on a Cube with Software Renderer  (Read 3706 times)

Offline samreid

  • byte
  • *
  • Posts: 3
    • View Profile
Shading on a Cube with Software Renderer
« on: October 13, 2009, 08:18:14 am »
Hi, it's my first 60 minutes with jPCT and I'm trying to use the software renderer to create a cube where each face has a different shade, like this (without the text)


But when I use the HelloWorldSoftware, with this new initialization:
Code: [Select]
                world = new World();
world.addLight( new SimpleVector( 30,30,30),Color.yellow );

TextureManager.getInstance().addTexture("box", new Texture(64,64, Color.red));

box = Primitives.getBox(13f, 2f);
box.setTexture("box");
box.setEnvmapped(Object3D.ENVMAP_ENABLED);
box.build();
world.addObject(box);

The object doesn't seem to have well defined edges, and comes out looking like this:


I also tried putting the light at (0,0,30), but that looked qualitatively similar.  What's the easiest/best way to do this?

Also, is there a way to highlight the edges of a cube; for example, to have them appear with a dark outline?

Thanks,
Sam Reid

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Shading on a Cube with Software Renderer
« Reply #1 on: October 13, 2009, 11:04:01 am »
Actually, the results you are getting is what is wanted in most cases. Your example image shows some flat shading instead. You can simulate this in jPCT by calling setShadingMode(Object3D.SHADING_FAKED_FLAT); It's not real flat shading, but it should look similar. About the outlines: There are basically two options...either bake the outlines into the textures or use a separate object that defines the outlines.
         

Offline samreid

  • byte
  • *
  • Posts: 3
    • View Profile
Re: Shading on a Cube with Software Renderer
« Reply #2 on: October 13, 2009, 05:32:49 pm »
Thanks for your quick response.  I tried using SHADING_FAKED_FLAT, and obtained the desired results.  It took a couple of tries to configure the light source so that it looked reasonable.  Here's a screenshot:



And the modified HelloWorldSoftware example code that was used to produce it:

Code: [Select]
package edu.colorado.phet.densityjava.view.jpct;

import java.awt.*;

import com.threed.jpct.*;
import javax.swing.*;

public class HelloJPCT {

private World world;
private FrameBuffer buffer;
private Object3D box;
private JFrame frame;

    public static void main(String[] args) throws Exception {
new HelloJPCT().loop();
}

public HelloJPCT() throws Exception {

frame=new JFrame("Hello world");
frame.setSize(800, 600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

world = new World();
world.addLight( new SimpleVector( -20,-5,-25),Color.gray );

TextureManager.getInstance().addTexture("box", new Texture(64,64, Color.red));

box = Primitives.getBox(1f, 2f);
box.setTexture("box");
box.setEnvmapped(Object3D.ENVMAP_ENABLED);
        box.setShadingMode( Object3D.SHADING_FAKED_FLAT );
box.build();
world.addObject(box);

world.getCamera().setPosition(5, -5, -2);
world.getCamera().lookAt(box.getTransformedCenter());
}

private void loop() throws Exception {
buffer = new FrameBuffer(800, 600, FrameBuffer.SAMPLINGMODE_NORMAL);

while (frame.isShowing()) {
box.rotateY(0.01f);
buffer.clear(java.awt.Color.BLUE);
world.renderScene(buffer);
world.draw(buffer);
buffer.update();
buffer.display(frame.getGraphics());
Thread.sleep(10);
}
buffer.disableRenderer(IRenderer.RENDERER_OPENGL);
buffer.dispose();
frame.dispose();
System.exit(0);
}
}

Thanks again,
Sam Reid