Author Topic: Disable shading?  (Read 3411 times)

Offline jasef

  • byte
  • *
  • Posts: 5
    • View Profile
Disable shading?
« on: December 07, 2012, 05:40:34 pm »
I'm using the software renderer only.

Is it possible to completely turn off shading? For example, here's a shot from my (modified) "Hello World" example:



(created image using buffer.getOutputBuffer() and ImageIO.write())

I want the only colors in the image to be blue and green, without any blending or anti-aliasing.

Is this possible with jPCT?

Thanks!

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Disable shading?
« Reply #1 on: December 07, 2012, 09:42:26 pm »
If you assign no additional light sources and no or a unified texture, there should be no shading.

Offline jasef

  • byte
  • *
  • Posts: 5
    • View Profile
Re: Disable shading?
« Reply #2 on: December 07, 2012, 10:37:12 pm »
What I really want is flat shading, like GL_FLAT back in the day. I tried making my own IRenderHook and did this:

Code: [Select]
public void beforeRendering(int polyID) {
            GL11.glShadeModel(GL11.GL_FLAT);
        }

but no luck.

Are there any examples of using flat shading using the GLSLShader or IRenderHook classes?

Thanks.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Disable shading?
« Reply #3 on: December 07, 2012, 10:44:30 pm »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Disable shading?
« Reply #4 on: December 07, 2012, 10:53:50 pm »
BTW: The documentation for that method isn't quite complete. When using compiled objects (Object3D.compile()), you'll get real flat shading, not just faked one.

Offline jasef

  • byte
  • *
  • Posts: 5
    • View Profile
Re: Disable shading?
« Reply #5 on: December 07, 2012, 11:00:01 pm »
Tried it, doesn't work. I'm trying a GLSLShader next.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Disable shading?
« Reply #6 on: December 07, 2012, 11:03:52 pm »
"Doesn't work" is a bit vague. There's actually not much that can't work if you enable flat shading that way. It might not give you what you expect though. Judging from your screen shot, i actually don't see any gouraud shading but some weird artifacts at the edges. Can you post the modified code?

Offline jasef

  • byte
  • *
  • Posts: 5
    • View Profile
Re: Disable shading?
« Reply #7 on: December 10, 2012, 04:43:54 pm »
Here's my code (background is now black):

Code: [Select]
import com.threed.jpct.*;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

public class JPCTTest {

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

    public JPCTTest() throws Exception {

        world = new World();
        world.setAmbientLight(255, 255, 255);

        Texture green = new Texture(8, 8, Color.GREEN);
        TextureManager.getInstance().addTexture("green", green);

        box = Primitives.getBox(16f, 2f);

        box.setTexture("green");
        box.setEnvmapped(Object3D.ENVMAP_ENABLED);
        box.setShadingMode(Object3D.SHADING_FAKED_FLAT);

        box.build();
        world.addObject(box);

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

        buffer = new FrameBuffer(100, 100, FrameBuffer.SAMPLINGMODE_NORMAL);

        buffer.enableRenderer(IRenderer.RENDERER_SOFTWARE);
        buffer.disableRenderer(IRenderer.RENDERER_OPENGL);

        buffer.clear(java.awt.Color.BLACK);

        world.renderScene(buffer);
        world.draw(buffer);
        buffer.update();

        BufferedImage image = (BufferedImage)(buffer.getOutputBuffer());

        DataBufferInt buf = (DataBufferInt) image.getRaster().getDataBuffer();
        int[] colors = buf.getData();

        HashMap<String, Boolean> colorsMap = new HashMap<String, Boolean>();

        for(int i = 0; i < colors.length; i++) {
            Color c = new Color(colors[i]);

            String rgb = Integer.toHexString(c.getRGB());
            rgb = rgb.substring(2, rgb.length());

            //System.out.print(rgb+",");

            if(!colorsMap.containsKey(rgb)) {
                colorsMap.put(rgb, true);
            }
        }

        System.out.println("num colors = "+colorsMap.size());

        try {
            File outputfile = new File("saved.png");
            ImageIO.write(image, "png", outputfile);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

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

Here's the image that's saved:



As you can see, I count the number of colors and the result is 26.

If it was true "flat" shading, I'd expect two colors in the whole file: pure green (0, 255, 0) and black. But there is some blending/dithering going on.

Any advice appreciated, thanks!

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Disable shading?
« Reply #8 on: December 10, 2012, 08:54:51 pm »
I gave your test case a try and you are right...there are these color banding artifacts when using the software renderer. They are caused by the interpolation across the scanlines of the polygons. One might think that there's no problem to interpolate between 255 and 255, but that's not quite what happens. To make the shading perspective correct, you actually interpolate between 255/z1 and 255/z2. To get the actual color at a point, this value is multiplied by z which itself is being calculated based on a linear interpolation from 1/z1 to 1/z2. All these interpolations happen in integer arithmetic and they do have some accuracy issues, which leads to colors of a scanline being slightly off at the right edges in some case. You can improve this, by rendering into a larger frame buffer, which increases the accuracy.

If you really need completely uniform results, you have two options:
  • do some post-processing on your own
  • abuse the fact that the software renderer supports overbright lighting, i.e. you can use lighting values for ambient larger than 255. Try 500,500,500 for example, and add box.setLighting(Object3D.LIGHTING_NO_LIGHTS); (and remove box.setEnvmapped(..) and box.setShadingMode(...)) and you'll get the desired result. However, this only works for colors with either 0 or 255 in their rgb-components.

Offline jasef

  • byte
  • *
  • Posts: 5
    • View Profile
Re: Disable shading?
« Reply #9 on: December 10, 2012, 08:57:40 pm »
Gotcha, thanks!