Author Topic: problem with uv coordinates  (Read 2036 times)

Offline tony-kun

  • byte
  • *
  • Posts: 7
    • View Profile
problem with uv coordinates
« on: February 06, 2013, 04:47:55 pm »
Hi, first of all, thanks very much for this software. I played a little with this 3d thing with jpct some time ago, but now I started to do a complete game.

I have encountered several (and very basic) problems. Perhaps I must be doing something very wrong. The first problem, is that normally when I use uv coordinates greater than 10 or so, I get strange graphics. I post an example. This example uses one of the textures in tp.zip downloadable from the page.

I'm using the latest jpct (1.26). The same problem happens with previous version.

Code: [Select]
import com.threed.jpct.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;

public class Prueba extends JFrame implements Runnable
{

private World world;
private FrameBuffer buffer;

private boolean finish = false;

public static void main(String[] args)throws Exception
{
    Prueba p = new Prueba();
    p.setSize(800, 600);

    p.initWorld(800, 600);//new MyPanel());//(JPanel)p.getContentPane().getComponent(0));
    p.setVisible(true);
   
    new Thread(p).start();
}

public Prueba()
{
    getContentPane().add(new MyPanel(), java.awt.BorderLayout.CENTER);

    setUndecorated(true);

    setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    this.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent ev) {
            finish = true;
        }
    });
}

public void initWorld(int sizeX, int sizeY)throws Exception//JPanel panel)throws Exception
{
    world = new World();
    world.setAmbientLight(255, 255, 255);

    File f = new File("expo_stones2.png");
    System.out.println(f.getAbsolutePath());
    BufferedImage img = ImageIO.read(f);
    TextureManager.getInstance().addTexture("stones", new Texture(img));

    Object3D grass = new Object3D(2);

    float factorX = 20;
    float factorY = 20;

    grass.addTriangle(getSV(0, 0, 0), 0, factorY, getSV(50, 0, 0), factorX, factorY, getSV(50, 50, 0), factorX, 0,
            TextureManager.getInstance().getTextureID("stones"));
    grass.addTriangle(getSV(50, 50, 0), factorX, 0, getSV(0, 50, 0), 0, 0, getSV(0, 0, 0), 0, factorY,
            TextureManager.getInstance().getTextureID("stones"));

    grass.build();
    world.addObject(grass);

    world.getCamera().setPosition(getSV(-3, -3, 10));
    world.getCamera().lookAt(getSV(10, 10, 0));

    buffer = new FrameBuffer(sizeX, sizeY, FrameBuffer.SAMPLINGMODE_NORMAL);
    buffer.enableRenderer(IRenderer.RENDERER_SOFTWARE);
}

public void run()
{
    while (!finish)
    {
buffer.clear(new Color(0, 100, 255));
world.renderScene(buffer);
world.draw(buffer);
buffer.update();
        repaint();//buffer.display(getContentPane().getGraphics());
try
        { Thread.sleep(10); }
        catch (Exception e) {}
    }
    setVisible(false);
    dispose();
    System.exit(0);
}

public SimpleVector getSV (float x, float y, float z)
{
    return new SimpleVector(x, -z, y);
}

class MyPanel extends JPanel
{
    public void paintComponent(Graphics g)
    {
        buffer.display(g);
    }
}

}
« Last Edit: February 06, 2013, 09:00:55 pm by EgonOlsen »

Offline tony-kun

  • byte
  • *
  • Posts: 7
    • View Profile
Re: problem with uv coordinates
« Reply #1 on: February 06, 2013, 05:02:50 pm »
Note, I called in the example, buffer.display() inside the paintComponent() method, because the wiki says "It seems that there is no other way than extending a swing component" and also do it that way. Normally I prefer to use panel.getGraphics instead and draw somewhere else. I guess there is actually no difference? I checked one of the demos and it uses getGraphics also.

Note the wiki has a mistake in the picking section, it says:

Object3D picked=world.getObject(res[1]);

but off course it should say Object3D picked=world.getObject(res[0]);

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: problem with uv coordinates
« Reply #2 on: February 06, 2013, 09:14:05 pm »
Note: I've edited your post to wrap the code in code-tags, so that it's nicer to read.

To your problem: Yes, that's a limitation of the software renderer. Large u/v values exceed the range of the fixed point renderer and that results in these visual glitches. There's nothing you can do about it except not to do it. Split your geometry if that is a real issue for you. Another "solution" is to shrink the texture, i.e. if (for example) the last working value for a 512*512 texture is 18, the last working value for a 256*256 will be 36.

You are right about the wiki. I've fixed that.

Offline tony-kun

  • byte
  • *
  • Posts: 7
    • View Profile
Re: problem with uv coordinates
« Reply #3 on: February 06, 2013, 10:21:49 pm »
Thanks for the precise information. Maybe it's somewhere else, I didn't see it.