Painting a Mouse Cursor-Selected Polygon

Started by AGP, February 07, 2017, 06:52:55 AM

Previous topic - Next topic

AGP

My best attempt. Chunk isn't null but Interact2D.project3D2D's product is.


     IntelligentPlane chunk = level.get(level.selectedX, level.selectedY);
     SimpleVector pV = Interact2D.project3D2D(theCamera, buffer, chunk.getPolygonManager().getTransformedVertex(constructionPolyID, 0));
System.out.println("Is chunk null? "+(chunk==null) +" Is pV? "+(pV==null));

EgonOlsen

I'm confused now, so I took a step back and created a simple prototype of what I think this is supposed to do.

This works for software rendering or uncompiled objects only. If it should be applied to compiled objects, one would have to switch from individual textures to a texture atlas and changing uv-coordinates instead. Anyway, I hope this helps. It draws red squares on a green surface (or yellow ones if you press the button and drag the mouse). It uses it's own method to create the plane but it should work with planes from Primitives as well. But this way, the texturing can be handled more flexible.


import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;

import com.threed.jpct.Camera;
import com.threed.jpct.CollisionEvent;
import com.threed.jpct.CollisionListener;
import com.threed.jpct.Config;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.Interact2D;
import com.threed.jpct.Lights;
import com.threed.jpct.Object3D;
import com.threed.jpct.SimpleVector;
import com.threed.jpct.Texture;
import com.threed.jpct.TextureInfo;
import com.threed.jpct.TextureManager;
import com.threed.jpct.World;

public class DuneReloaded extends JFrame implements MouseMotionListener, CollisionListener {

private static final long serialVersionUID = 1L;
private int x, y;
private Graphics g;
private World world;
private FrameBuffer fb;
private Object3D plane;
private boolean yellow=false;

public static void main(String args[]) {
new DuneReloaded().run();
}

private void run() {
init();

while (true) {
follow();
fb.clear();
world.renderScene(fb);
world.draw(fb);
fb.update();
fb.display(g);
}
}

private void follow() {
SimpleVector ray = Interact2D.reproject2D3DWS(world.getCamera(), fb, x, y);
if (ray != null) {
ray = ray.normalize();
plane.calcMinDistance(world.getCamera().getPosition(), ray, 1000);
}
}

private void init() {
TextureManager.getInstance().addTexture("texture", new Texture(512, 512, Color.GREEN));
TextureManager.getInstance().addTexture("marker", new Texture(32, 32, Color.RED));
TextureManager.getInstance().addTexture("yellow", new Texture(32, 32, Color.YELLOW));

plane = createPlane();
world = new World();
world.addObject(plane);

fb = new FrameBuffer(800, 600, FrameBuffer.SAMPLINGMODE_NORMAL);

Camera cam = world.getCamera();
cam.moveCamera(Camera.CAMERA_MOVEOUT, 100);
cam.moveCamera(Camera.CAMERA_MOVEUP, 160);
cam.lookAt(plane.getTransformedCenter());

world.getLights().setOverbrightLighting(Lights.OVERBRIGHT_LIGHTING_DISABLED);
}

private Object3D createPlane() {
int x, z, id = TextureManager.getInstance().getTextureID("texture");
float xSizeF = 129, zSizeF = 129, scale = 3, deelx = 1;
TextureInfo ti;
int size = (int) ((xSizeF - 1) * (zSizeF - 1) * 2);
Object3D plane = new Object3D(size);
Config.maxPolysVisible = size;
for (z = 0; z < zSizeF - 1; z++) {
for (x = 0; x < xSizeF - 1; x++) {
float xx = x;
ti = new TextureInfo(id, (xx / xSizeF / deelx), (z / zSizeF), ((xx + 1) / xSizeF / deelx), (z / zSizeF), (xx / xSizeF / deelx), ((z + 1) / zSizeF));
plane.addTriangle(new SimpleVector(x * scale, 1, z * scale), new SimpleVector((x + 1) * scale, 1, z * scale), new SimpleVector(x * scale, 1, (z + 1) * scale), ti);
ti = new TextureInfo(id, (xx / xSizeF / deelx), ((z + 1) / zSizeF), ((xx + 1) / xSizeF / deelx), (z / zSizeF), ((xx + 1) / xSizeF / deelx), ((z + 1) / zSizeF));
plane.addTriangle(new SimpleVector(x * scale, 1, (z + 1) * scale), new SimpleVector((x + 1) * scale, 1, z * scale), new SimpleVector((x + 1) * scale, 1, (z + 1)
* scale), ti);
}
}

plane.calcNormals();
plane.build();
plane.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);
plane.addCollisionListener(this);
return plane;
}

public DuneReloaded() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setSize(800, 600);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
this.addMouseMotionListener(this);
g = getGraphics();
}

@Override
public void mouseMoved(MouseEvent m) {
x = m.getX();
y = m.getY();
yellow=false;
}

@Override
public void mouseDragged(MouseEvent m) {
x = m.getX();
y = m.getY();
yellow=true;
}

@Override
public void collision(CollisionEvent colEvent) {
int polyId = (colEvent.getPolygonIDs()[0]) & 0xfffffffe;
int tid=yellow?TextureManager.getInstance().getTextureID("yellow"):TextureManager.getInstance().getTextureID("marker");
plane.getPolygonManager().setPolygonTexture(polyId, tid);
plane.getPolygonManager().setPolygonTexture(polyId + 1, tid);
}

@Override
public boolean requiresPolygonIDs() {
return true;
}

}


AGP

Sorry for taking this long. I was writing a long update when your version came through. So I implemented it your way. It worked, thanks very much.

AGP

The game is coming along nicely, but now I'm just abusing the CPU. From what I gather of what you wrote, I'm not going to have a problem with the mouse (the "if it should be applied to compiled objects, one would have to switch from individual textures..." comment was only about the painting of the polygon) in hardware. Right?

EgonOlsen