https://www.jpct.net/wiki/index.php?title=Software_mode_demo&feed=atom&action=history
Software mode demo - Revision history
2024-03-28T13:29:12Z
Revision history for this page on the wiki
MediaWiki 1.23.15
https://www.jpct.net/wiki/index.php?title=Software_mode_demo&diff=728&oldid=prev
Corey: /* Source code */
2017-09-21T05:01:58Z
<p><span dir="auto"><span class="autocomment">Source code</span></span></p>
<table class='diff diff-contentalign-left'>
<col class='diff-marker' />
<col class='diff-content' />
<col class='diff-marker' />
<col class='diff-content' />
<tr style='vertical-align: top;'>
<td colspan='2' style="background-color: white; color:black; text-align: center;">← Older revision</td>
<td colspan='2' style="background-color: white; color:black; text-align: center;">Revision as of 05:01, 21 September 2017</td>
</tr><tr><td colspan="2" class="diff-lineno">Line 19:</td>
<td colspan="2" class="diff-lineno">Line 19:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"><div>import com.threed.jpct.*;</div></td><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"><div>import com.threed.jpct.*;</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">/**</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"> * A bare bones jPCT software mode application.</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"> * @author 'words' - jPCT.net</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"> */</ins></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"><div>@SuppressWarnings("serial")</div></td><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"><div>@SuppressWarnings("serial")</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"><div>public class Engine extends JFrame implements KeyEventDispatcher, MouseListener, MouseMotionListener {</div></td><td class='diff-marker'> </td><td style="background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;"><div>public class Engine extends JFrame implements KeyEventDispatcher, MouseListener, MouseMotionListener {</div></td></tr>
</table>
Corey
https://www.jpct.net/wiki/index.php?title=Software_mode_demo&diff=727&oldid=prev
Corey: Created page with "=== Software mode demo === ==== Details ==== This is a bare minimum software renderer example... This is nothing fancy, but may be helpful for first time jPCT users or anybod..."
2017-09-18T05:04:42Z
<p>Created page with "=== Software mode demo === ==== Details ==== This is a bare minimum software renderer example... This is nothing fancy, but may be helpful for first time jPCT users or anybod..."</p>
<p><b>New page</b></p><div>=== Software mode demo ===<br />
<br />
==== Details ====<br />
This is a bare minimum software renderer example... This is nothing fancy, but may be helpful for first time jPCT users or anybody interested in using the jPCT software renderer mode to design their 3D application.<br />
<br />
==== Screen shot ====<br />
[[File:Software_Example320x320.png]]<br />
<br />
==== Source code ====<br />
<pre><br />
<br />
import java.awt.*;<br />
import java.awt.event.*;<br />
import java.util.Random;<br />
import java.util.concurrent.TimeUnit;<br />
<br />
import javax.swing.*;<br />
<br />
import com.threed.jpct.*;<br />
<br />
@SuppressWarnings("serial")<br />
public class Engine extends JFrame implements KeyEventDispatcher, MouseListener, MouseMotionListener {<br />
<br />
// "Engine" variables...<br />
private final Graphics graphics;<br />
private FrameBuffer buffer;<br />
private final World world;<br />
private boolean keepAlive = false;<br />
private int mouseX = -1;<br />
private int mouseY = -1;<br />
private int frameCount = 0;<br />
<br />
// Program content variables...<br />
private Object3D cube;<br />
private Random random = new Random();<br />
<br />
static {<br />
Logger.setLogLevel(Logger.ERROR);<br />
<br />
/*<br />
* Configure the 3D engine (jPCT) settings.<br />
* http://www.jpct.net/doc/com/threed/jpct/Config.html<br />
*/<br />
<br />
// When using the software renderer on a multi core setup, it's a good idea to use all cores (or at least some of them) to speed up rendering.<br />
// To do so, set Config.useMultipleThreads to true before instantiating the frame buffer.<br />
// By default, jPCT will use four cores, which matches a quad core cpu. If the setup that runs the application uses less cores, it's advised to adjust this value before instantiating the frame buffer.<br />
// http://www.jpct.net/wiki/index.php?title=Multithreading#The_threading_framework<br />
Config.useMultipleThreads = Runtime.getRuntime().availableProcessors() > 1;<br />
<br />
// Dynamic load balancing usually is the better choice.<br />
// It's not enable by default simply because it can cause small artifacts in combination with the texel filtering that jPCT uses (some pixels may flicker from time to time).<br />
Config.loadBalancingStrategy = 1;<br />
<br />
// Usually, one have to call build() on all objects that have to be rendered.<br />
Config.autoBuild = true;<br />
<br />
// The maximum size of the VisList.<br />
Config.maxPolysVisible = 32000; // 32k<br />
<br />
/*<br />
* Submit a garbage collection to the virtual machine.<br />
*/<br />
Runtime.getRuntime().runFinalization();<br />
Runtime.getRuntime().gc();<br />
}<br />
<br />
public static void main(String[] args) {<br />
new Engine();<br />
}<br />
<br />
public Engine() {<br />
this.setTitle("jPCT " + Config.getVersion());<br />
this.setPreferredSize(new Dimension(320, 320));<br />
this.setBackground(Color.BLACK);<br />
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);<br />
this.pack();<br />
this.setSize(320, 320);<br />
this.setResizable(true);<br />
this.setLocationRelativeTo(null);<br />
this.setVisible(true);<br />
this.graphics = getGraphics();<br />
<br />
/*<br />
* Bind the hardware input listeners.<br />
*/<br />
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this);<br />
this.addWindowListener(new WindowExitListener(this));<br />
this.addMouseListener(this);<br />
this.addMouseMotionListener(this);<br />
<br />
/*<br />
* Initialize a "re-sizable" frame buffer.<br />
*/<br />
this.buffer = new FrameBuffer(320, 320, FrameBuffer.SAMPLINGMODE_NORMAL);<br />
buffer.disableRenderer(IRenderer.RENDERER_OPENGL);<br />
buffer.enableRenderer(IRenderer.RENDERER_SOFTWARE);<br />
buffer.optimizeBufferAccess();<br />
this.addComponentListener(new ComponentListener() {<br />
@Override<br />
public void componentResized(ComponentEvent e) {<br />
int w = getWidth();<br />
int h = getHeight();<br />
buffer = new FrameBuffer(w, h, FrameBuffer.SAMPLINGMODE_NORMAL);<br />
buffer.refresh();<br />
}<br />
@Override<br />
public void componentHidden(ComponentEvent e) { }<br />
@Override<br />
public void componentMoved(ComponentEvent e) { }<br />
@Override<br />
public void componentShown(ComponentEvent e) { }<br />
});<br />
<br />
// Initialize worlds<br />
this.world = new World();<br />
world.setAmbientLight(85, 85, 85); // Overall world brightness<br />
<br />
// Create the cube object<br />
this.cube = Primitives.getCube(5);<br />
world.addObject(cube);<br />
<br />
// Move camera<br />
Camera cam = world.getCamera();<br />
cam.moveCamera(Camera.CAMERA_MOVEOUT, 30);<br />
cam.moveCamera(Camera.CAMERA_MOVEUP, 15);<br />
cam.lookAt(cube.getTransformedCenter());<br />
cam.setFOV(1.5f);<br />
<br />
/*<br />
* Run the program.<br />
*/<br />
keepAlive = true;<br />
execute();<br />
}<br />
<br />
@Override<br />
public void mousePressed(MouseEvent e) {<br />
int button = e.getButton();<br />
if (button == 1) {<br />
System.out.println("Mouse -> L click @ " + mouseX + "," + mouseY);<br />
}<br />
if (button == 3) {<br />
System.out.println("Mouse -> R click @ " + mouseX + "," + mouseY);<br />
}<br />
}<br />
<br />
@Override<br />
public void mouseReleased(MouseEvent e) {<br />
int button = e.getButton();<br />
if (button == 1) {<br />
System.out.println("Mouse -> left button released");<br />
}<br />
if (button == 3) {<br />
System.out.println("Mouse -> right button released");<br />
}<br />
}<br />
<br />
@Override<br />
public void mouseMoved(MouseEvent e) {<br />
this.mouseX = e.getX();<br />
this.mouseY = e.getY();<br />
}<br />
<br />
@Override<br />
public void mouseClicked(MouseEvent e) { }<br />
<br />
@Override<br />
public void mouseEntered(MouseEvent e) { }<br />
<br />
@Override<br />
public void mouseExited(MouseEvent e) { }<br />
<br />
@Override<br />
public void mouseDragged(MouseEvent e) { }<br />
<br />
@Override<br />
public boolean dispatchKeyEvent(KeyEvent e) {<br />
if (e.getID() == KeyEvent.KEY_RELEASED && e.getKeyCode() == KeyEvent.VK_ESCAPE) {<br />
keepAlive = false;<br />
}<br />
return true;<br />
}<br />
<br />
private void execute() {<br />
World.setDefaultThread(Thread.currentThread());<br />
<br />
long lastSecond = 0L;<br />
while (keepAlive) {<br />
long currentTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime());<br />
<br />
// Reset the frame counter.<br />
if (lastSecond < currentTime - 1000) {<br />
lastSecond = currentTime;<br />
setTitle("FPS: " + frameCount);<br />
frameCount = 0;<br />
<br />
// Re-color the cube<br />
cube.setAdditionalColor(random.nextInt(255), random.nextInt(255), random.nextInt(255));<br />
}<br />
<br />
// Rotate the cube<br />
cube.rotateZ(0.01f * random.nextFloat());<br />
<br />
// Clear the previous frame.<br />
buffer.clear();<br />
<br />
/*<br />
* Draw the new frame.<br />
*/<br />
frameCount++;<br />
world.renderScene(buffer);<br />
world.draw(buffer);<br />
buffer.update();<br />
buffer.display(graphics);<br />
}<br />
<br />
/*<br />
* Terminate the program.<br />
*/<br />
System.exit(0);<br />
}<br />
<br />
/**<br />
* Listen for the window exit event and use exit prompt.<br />
*/<br />
public class WindowExitListener extends WindowAdapter {<br />
<br />
private final JFrame frame;<br />
<br />
public WindowExitListener(final JFrame frame) {<br />
this.frame = frame;<br />
}<br />
<br />
@Override<br />
public void windowClosing(final WindowEvent e) {<br />
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);<br />
String ObjButtons[] = { "Yes", "No" };<br />
int result = JOptionPane.showOptionDialog(null, "Exit jPCT + " + Config.getVersion() + " ?", "Confirm Exit", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, ObjButtons, ObjButtons[1]);<br />
if (result == JOptionPane.YES_OPTION) {<br />
System.exit(0);<br />
}<br />
}<br />
}<br />
<br />
}<br />
</pre><br />
<br />
[[Category:jPCT]]</div>
Corey