Thanks a lot. It is unnecessarily complicated, though, isn't it?
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
Show posts Menu
import java.awt.*;
import java.awt.event.*;
import com.threed.jpct.*;
public class Transparency extends Frame implements WindowListener {
private World theWorld;
private FrameBuffer buffer;
private Camera cam;
private Object3D obj;
private boolean keepGoing, turnRight, turnLeft;
private com.threed.jpct.util.KeyMapper keyMapper;
private Texture boxMap;
private int transparency = 0;
private FadeoutEffect effect;
public Transparency() {
this.setTitle("BR's");
theWorld = new World();
buffer = new FrameBuffer(1440, 900, FrameBuffer.SAMPLINGMODE_NORMAL);
obj = com.threed.jpct.util.ExtendedPrimitives.createBox(new SimpleVector(2f, 2f, 2f));//Primitives.getCube(1f);
TextureManager.getInstance().addTexture("boxMap", (boxMap=new Texture("BoxMap.png", true)));
boxMap.setEffect((effect=new FadeoutEffect()));
obj.setTexture("boxMap");
obj.build();
obj.setTransparencyMode(Object3D.TRANSPARENCY_MODE_DEFAULT);
obj.setTransparency(transparency);
Object3D backgroundPlane = Primitives.getPlane(1, 4f);
backgroundPlane.build();
backgroundPlane.translate(0f, -2f, 0f);
theWorld.addObject(backgroundPlane);
cam = theWorld.getCamera();
cam.moveCamera(Camera.CAMERA_MOVEOUT, 8f);
theWorld.addObject(obj);
obj.setLighting( Object3D.LIGHTING_NO_LIGHTS );
keyMapper = new com.threed.jpct.util.KeyMapper(this);
this.addWindowListener(this);
this.setSize(buffer.getWidth(), buffer.getHeight());
this.setVisible(true);
loop();
}
public void loop() {
keepGoing = true;
while (keepGoing) {
buffer.clear();
com.threed.jpct.util.KeyState keyState = keyMapper.poll();
if (keyState.getState() == com.threed.jpct.util.KeyState.PRESSED)
keyPressed(keyState.getKeyCode());
else if (keyState.getState() == com.threed.jpct.util.KeyState.RELEASED)
keyReleased(keyState.getKeyCode());
if (turnRight)
obj.rotateY(-.01f);
if (turnLeft)
obj.rotateY(.01f);
theWorld.renderScene(buffer);
theWorld.draw(buffer);
//theWorld.drawWireframe(buffer, Color.yellow);
buffer.display(this.getGraphics());
Thread.yield();
}
buffer.dispose();
this.dispose();
System.exit(0);
}
private void keyPressed(int keyCode) {
if (keyCode == KeyEvent.VK_RIGHT)
turnRight = true;
if (keyCode == KeyEvent.VK_LEFT)
turnLeft = true;
}
private void keyReleased(int keyCode) {
if (keyCode == KeyEvent.VK_RIGHT)
turnRight = false;
if (keyCode == KeyEvent.VK_LEFT)
turnLeft = false;
if (keyCode == KeyEvent.VK_T) {
float fade = 1.00f;
if (transparency < 255)
fade = (float)((transparency+=8)/255f);
effect.transparency = 255-transparency;
obj.setTransparency(255-transparency);
//boxMap.setAlpha(255-transparency);
TextureManager.getInstance().replaceTexture("boxMap", boxMap);
//obj.setAdditionalColor(intensity(boxColor, fade));
//boxMap.applyEffect();
Logger.log("Transparency: "+transparency, Logger.MESSAGE);
}
}
public void windowClosing(WindowEvent e) {
keepGoing = false;
}
public void windowClosed(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public static void main(String[] args) {
new Transparency();
}
}
class FadeoutEffect implements ITextureEffect {
public int transparency;
private Texture tex;
public FadeoutEffect() {
}
public void init(Texture texture) {
this.tex = texture;
}
public void apply(int[] dest, int[] src) {
/*for (int y =0; y < 512;y++) {
for (int x = 0; x < 512; x++) {
dest[y*x] = transparency;
}
}*/
tex.setAlpha(transparency);
}
public boolean containsAlpha() {
return false;
}
}
class Keyframe {
private double time;
private Matrix transform;
public Keyframe(double time, Matrix transform) {
this.time = time;
// Normalize the rotation component of the transform
Quaternion rotation = new Quaternion(transform); // Extracts the rotational component
rotation.normalize();
Matrix rotationMatrix = rotation.toMatrix();
// Adjust the transform to the jPCT coordinate system
this.transform = adjustToJpctCoordinateSystem(rotationMatrix);
// Extract and reset translation
SimpleVector translation = extractTranslation(transform);
resetTranslation(transform);
// Extract, check, and apply uniform scale
SimpleVector scale = extractScale(transform);
if (!isUniformScale(scale)) {
scale = new SimpleVector(1f, 1f, 1f); // (1, 1, 1) Use uniform scale if it's not uniform
}
Utilities.setScale(this.transform, scale);
// Combine rotation and scale, reapply translation
this.transform = combineTransforms(translation, this.transform, scale);
}
// Method to adjust a transformation matrix to jPCT's coordinate system
private Matrix adjustToJpctCoordinateSystem(Matrix transform) {
Matrix adjustedMatrix = new Matrix(transform);
// Flip the Y-axis
adjustedMatrix.set(1, 0, -adjustedMatrix.get(1, 0));
adjustedMatrix.set(1, 1, -adjustedMatrix.get(1, 1));
adjustedMatrix.set(1, 2, -adjustedMatrix.get(1, 2));
// If there's translation on Y-axis, flip it as well
adjustedMatrix.set(1, 3, -adjustedMatrix.get(1, 3));
// Flip the Z-axis
adjustedMatrix.set(2, 0, -adjustedMatrix.get(2, 0));
adjustedMatrix.set(2, 1, -adjustedMatrix.get(2, 1));
adjustedMatrix.set(2, 2, -adjustedMatrix.get(2, 2));
// If there's translation on Z-axis, flip it as well
adjustedMatrix.set(2, 3, -adjustedMatrix.get(2, 3));
return adjustedMatrix;
}
public double getTime() {
return time;
}
// Extract the translation component from a transformation matrix
private SimpleVector extractTranslation(Matrix transform) {
return new SimpleVector(transform.get(3, 0), transform.get(3, 1), transform.get(3, 2));
}
public Matrix getTransform() {
return transform;
}
// Reset the translation component of a transformation matrix to zero
private void resetTranslation(Matrix transform) {
transform.set(3, 0, 0);
transform.set(3, 1, 0);
transform.set(3, 2, 0);
}
// Helper method to extract scale from a transformation matrix
private SimpleVector extractScale(Matrix transform) {
// Assuming the scale is represented by the length of the axis vectors in the matrix
SimpleVector scaleX = new SimpleVector(transform.get(0, 0), transform.get(1, 0), transform.get(2, 0));
SimpleVector scaleY = new SimpleVector(transform.get(0, 1), transform.get(1, 1), transform.get(2, 1));
SimpleVector scaleZ = new SimpleVector(transform.get(0, 2), transform.get(1, 2), transform.get(2, 2));
return new SimpleVector(scaleX.length(), scaleY.length(), scaleZ.length());
}
// Combine the translation, rotation, and scale into a single transformation matrix
private Matrix combineTransforms(SimpleVector translation, Matrix rotation, SimpleVector scale) {
Matrix scaleMatrix = new Matrix();
scaleMatrix.setIdentity();
Utilities.setScale(scaleMatrix, scale);
Matrix combined = new Matrix();
combined.setIdentity();
combined.matMul(rotation);
combined.matMul(scaleMatrix);
combined.translate(translation);
return combined;
}
// Helper method to check if the scale is uniform
private boolean isUniformScale(SimpleVector scale) {
// Check if all components are the same within a small epsilon to account for floating-point precision
final float epsilon = 0.0001f;
return Math.abs(scale.x - scale.y) < epsilon && Math.abs(scale.y - scale.z) < epsilon;
}
// Method to apply scale to a transformation matrix
private void applyScaleToTransform(Matrix transform, SimpleVector scale) {
// Reset the scale component of the matrix to 1
for (int i = 0; i < 3; i++) {
float length = new SimpleVector(transform.get(0, i), transform.get(1, i), transform.get(2, i)).length();
if (length != 0) { // Avoid division by zero
transform.set(0, i, transform.get(0, i) / length);
transform.set(1, i, transform.get(1, i) / length);
transform.set(2, i, transform.get(2, i) / length);
}
}
// Create a scale matrix
Matrix scaleMatrix = new Matrix();
scaleMatrix.setIdentity(); // Start with an identity matrix
scaleMatrix.set(0, 0, scale.x);
scaleMatrix.set(1, 1, scale.y);
scaleMatrix.set(2, 2, scale.z);
// Apply scale by multiplying the existing transform with the scale matrix
transform.matMul(scaleMatrix);
}
}
Page created in 0.045 seconds with 12 queries.