PDA

View Full Version : Element not rendering in JOGL



CSG
03-07-2007, 07:28 AM
Im currently writing a 3D Lego program, which lets the user add Lego elements to the canvas when they click on a button...

ive thrown together a test window to ensure the element's file is read and rendered to the screen, but nothing seems to want to render...

I just wondered if i'd made any glaringly obvious mistakes?

Cheers

Chris

Element .DAT File:

0 Created by: Chris Gedrim
0 Creation Date: 07/03/07
0 Used for testing Gedo
0 ------------------------
0 Main Body
0 ------------------------
4 16 -10 0 -10 -10 0 10 10 0 10 10 0 -10
4 16 -10 0 -10 -10 0 10 -10 24 10 -10 24 -10
4 16 -10 0 -10 10 0 -10 10 24 -10 -10 24 -10
4 16 -10 0 10 10 0 10 10 24 10 -10 24 10
4 16 10 0 -10 10 0 10 10 24 10 10 24 -10
4 16 10 24 -10 10 24 10 -10 24 10 -10 24 -10
0 ------------------------
0 Stud
0 ------------------------
4 16 -6 24 2 -2 24 6 -2 28 6 -6 28 2
4 16 -2 24 6 2 24 6 2 28 6 -2 28 6
4 16 2 24 6 6 24 2 6 28 2 2 28 6
4 16 6 24 2 6 24 -2 6 28 -2 6 28 2
4 16 6 24 -2 2 24 -6 2 28 -6 2 28 -2
4 16 2 24 -6 -2 24 -6 -2 28 -6 2 28 -6
4 16 -2 24 -6 -6 24 -2 -6 28 -2 -2 28 -6
4 16 -6 24 -6 -6 24 -2 -6 24 -2 -2 28 -6
0 ------------------------
0 Stud top
0 ------------------------
4 16 -2 28 2 2 28 2 2 28 -2 -2 28 -2
4 16 -2 28 2 2 28 2 2 28 6 -2 28 6
4 16 2 28 2 2 28 -2 6 28 -2 6 28 2
4 16 2 28 -2 -2 28 -2 -2 28 -6 2 28 -6
4 16 -2 28 -2 -2 28 2 -6 28 2 -6 28 -2
3 16 -2 28 2 -2 28 6 -6 28 2
3 16 2 28 2 2 28 6 6 28 2
3 16 2 28 -2 6 28 -2 2 28 -6
3 16 -2 28 -6 -2 28 -6 -6 28 -2
0 ------------------------
0 End
0 ------------------------
Canvas Window:

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;
import javax.swing.*;
import net.java.games.jogl.*;

public class DrawingFrame extends JFrame implements ActionListener{

DrawingFrameEventListener listener = new DrawingFrameEventListener();
GLCanvas glCanvas;
String[] elementDesign = null;

public static void main(String[] args) {
final DrawingFrame app = new DrawingFrame();

// show what we've done
SwingUtilities.invokeLater (
new Runnable() {
public void run() {
app.setVisible(true);
}
}
);
}

public DrawingFrame() {
//set the JFrame title
super("Gedo - Lego Design");

//kill the process when the JFrame is closed
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Setting up our southern JPanel
JPanel jp = new JPanel();

//adding the JButton
JButton jb = new JButton("Brick 1x1");
jb.addActionListener(this);
jp.add(jb);

getContentPane().add("South", jp);

//only three JOGL lines of code ... and here they are
GLCapabilities glCaps = new GLCapabilities();

glCanvas =
GLDrawableFactory.getFactory().createGLCanvas(glCa ps);

glCanvas.addGLEventListener(listener);

//add the GLCanvas just like we would any Component
getContentPane().add(glCanvas, BorderLayout.CENTER);
setSize(640, 480);

//center the JFrame on the screen
centerWindow(this);
}

public void centerWindow(Component frame) {
Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();

if (frameSize.width > screenSize.width )
frameSize.width = screenSize.width;
if (frameSize.height > screenSize.height)
frameSize.height = screenSize.height;

frame.setLocation (
(screenSize.width - frameSize.width ) >> 1,
(screenSize.height - frameSize.height) >> 1
);
}

/**
* Implementation of our ActionListener. This allows the
* buttons to perform an action. In this case they set
* the "whatToDraw" String and ask for a repaint of the
* GLCanvas.
*/
public void actionPerformed(ActionEvent ae) {
listener.elementDesign = getDesign();
glCanvas.repaint();
}

public String[] getDesign()
{
String line = "", i = "";
File elementFile = new File("PARTS\\001.DAT");
if(elementDesign == null)
{
try
{
BufferedReader tIn = new BufferedReader(new FileReader(elementFile));
try
{
while(( i = tIn.readLine()) != null)
{
line = line + "\n" + i;
}

elementDesign = line.split("\n");

tIn.close();
}
catch(IOException eIOE)
{
System.out.println("Could not read from file : " + elementFile);
}

}
catch(FileNotFoundException eFNF)
{
System.out.println("File : " + elementFile + " could not be found");
}
}
else
{
System.out.println("File in memory");
}
return elementDesign;
}
}
Canvas Listener:

import net.java.games.jogl.*;

public class DrawingFrameEventListener
implements GLEventListener {

public String[] elementDesign = null;
public double[] matrix = {1,0,0,0,1,0,0,0,1};
/**
* Take care of initialization here.
*/
public void init(GLDrawable gld) {
GL gl = gld.getGL();
GLU glu = gld.getGLU();

gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

gl.glViewport(-320, -240, 320, 150);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f, 640/480, -100.0, 100.0);
//glu.gluOrtho2D(-320.0, 320.0, -240.0, 240.0);

//glu.gluLookAt(100.0, 100.0, 100.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

}

/**
* Take care of drawing here.
*/
public void display(GLDrawable drawable) {

float red = 0.5f;
float green = 0.0f;
float blue = 0.5f;

GL gl = drawable.getGL();

gl.glClear(GL.GL_COLOR_BUFFER_BIT);

gl.glColor3f(red, green, blue);

/*gl.glBegin(gl.GL_QUADS);
gl.glVertex3d(10,10,0);
gl.glVertex3d(-10,10,0);
gl.glVertex3d(-10,-10,0);
gl.glVertex3d(10,-10,0);
gl.glEnd();*/

if(elementDesign != null)
{
for(int i=0;i<elementDesign.length;i++) // Loop through the part
{
String[] temp;

double x,y,z;

temp = elementDesign[i].split(" "); // Split the design section into component parts

//System.out.println("elementDesign[i] = " + elementDesign[i]);
if(elementDesign[i].length() > 1)
{
switch(Integer.parseInt(temp[0])) // Switch based on the first line of the element design
{
// Comment or META-TAG
case 0:



break;

// External Part
/*case 1:

temp = elementDesign[i].split(" "); // Split the design section into component parts

LegoElement tEleTemp = new LegoElement(temp[14], "P\\" + temp[14]);

x = Double.parseDouble(temp[2]);
y = Double.parseDouble(temp[3]);
z = Double.parseDouble(temp[4]);

tEleTemp.sematrix(new double[]{Double.parseDouble(temp[5]),Double.parseDouble(temp[6]),Double.parseDouble(temp[7]),Double.parseDouble(temp[8]),Double.parseDouble(temp[9]),Double.parseDouble(temp[10]),Double.parseDouble(temp[11]),Double.parseDouble(temp[12]),Double.parseDouble(temp[13])});
tEleTemp.addElement();
tEleTemp.resematrix();

gl.glTranslated(x,y,z);

gl.glCallList(Integer.parseInt(temp[14].substring(0,temp[14].indexOf("."))));

break;
*/
// Line
case 2:
System.out.println("2");
gl.glBegin(gl.GL_LINES);

x = Double.parseDouble(temp[2]);
y = Double.parseDouble(temp[3]);
z = Double.parseDouble(temp[4]);

x = (x*matrix[0]) + (x*matrix[3]) + (x*matrix[6]);
y = (x*matrix[1]) + (x*matrix[4]) + (x*matrix[7]);
z = (x*matrix[2]) + (x*matrix[5]) + (x*matrix[8]);

System.out.println(x + " " + y + " " + z);
gl.glVertex3d(x,y,z);

x = Double.parseDouble(temp[5]);
y = Double.parseDouble(temp[6]);
z = Double.parseDouble(temp[7]);

x = (x*matrix[0]) + (x*matrix[3]) + (x*matrix[6]);
y = (x*matrix[1]) + (x*matrix[4]) + (x*matrix[7]);
z = (x*matrix[2]) + (x*matrix[5]) + (x*matrix[8]);
System.out.println(x + " " + y + " " + z);
gl.glVertex3d(x,y,z);

gl.glEnd();

break;

// Triangle
case 3:
System.out.println("3");
gl.glBegin(gl.GL_TRIANGLES);

x = Double.parseDouble(temp[2]);
y = Double.parseDouble(temp[3]);
z = Double.parseDouble(temp[4]);

x = (x*matrix[0]) + (x*matrix[3]) + (x*matrix[6]);
y = (x*matrix[1]) + (x*matrix[4]) + (x*matrix[7]);
z = (x*matrix[2]) + (x*matrix[5]) + (x*matrix[8]);
System.out.println(x + " " + y + " " + z);
gl.glVertex3d(x,y,z);

x = Double.parseDouble(temp[5]);
y = Double.parseDouble(temp[6]);
z = Double.parseDouble(temp[7]);

x = (x*matrix[0]) + (x*matrix[3]) + (x*matrix[6]);
y = (x*matrix[1]) + (x*matrix[4]) + (x*matrix[7]);
z = (x*matrix[2]) + (x*matrix[5]) + (x*matrix[8]);
System.out.println(x + " " + y + " " + z);
gl.glVertex3d(x,y,z);

x = Double.parseDouble(temp[8]);
y = Double.parseDouble(temp[9]);
z = Double.parseDouble(temp[10]);

x = (x*matrix[0]) + (x*matrix[3]) + (x*matrix[6]);
y = (x*matrix[1]) + (x*matrix[4]) + (x*matrix[7]);
z = (x*matrix[2]) + (x*matrix[5]) + (x*matrix[8]);
System.out.println(x + " " + y + " " + z);
gl.glVertex3d(x,y,z);

gl.glEnd();

break;

// Quad
case 4:
System.out.println("4");
gl.glBegin(gl.GL_QUADS);

x = Double.parseDouble(temp[2]);
y = Double.parseDouble(temp[3]);
z = Double.parseDouble(temp[4]);

x = (x*matrix[0]) + (x*matrix[3]) + (x*matrix[6]);
y = (x*matrix[1]) + (x*matrix[4]) + (x*matrix[7]);
z = (x*matrix[2]) + (x*matrix[5]) + (x*matrix[8]);
System.out.println(x + " " + y + " " + z);
gl.glVertex3d(x,y,z);

x = Double.parseDouble(temp[5]);
y = Double.parseDouble(temp[6]);
z = Double.parseDouble(temp[7]);

x = (x*matrix[0]) + (x*matrix[3]) + (x*matrix[6]);
y = (x*matrix[1]) + (x*matrix[4]) + (x*matrix[7]);
z = (x*matrix[2]) + (x*matrix[5]) + (x*matrix[8]);
System.out.println(x + " " + y + " " + z);
gl.glVertex3d(x,y,z);

x = Double.parseDouble(temp[8]);
y = Double.parseDouble(temp[9]);
z = Double.parseDouble(temp[10]);

x = (x*matrix[0]) + (x*matrix[3]) + (x*matrix[6]);
y = (x*matrix[1]) + (x*matrix[4]) + (x*matrix[7]);
z = (x*matrix[2]) + (x*matrix[5]) + (x*matrix[8]);
System.out.println(x + " " + y + " " + z);
gl.glVertex3d(x,y,z);

x = Double.parseDouble(temp[11]);
y = Double.parseDouble(temp[12]);
z = Double.parseDouble(temp[13]);

x = (x*matrix[0]) + (x*matrix[3]) + (x*matrix[6]);
y = (x*matrix[1]) + (x*matrix[4]) + (x*matrix[7]);
z = (x*matrix[2]) + (x*matrix[5]) + (x*matrix[8]);
System.out.println(x + " " + y + " " + z);
gl.glVertex3d(x,y,z);

gl.glEnd();

break;

// Option Line
case 5:

break;

default:
break;
}
}
}
}
}

public void reshape(
GLDrawable drawable,
int x,
int y,
int width,
int height
) {}

public void displayChanged(
GLDrawable drawable,
boolean modeChanged,
boolean deviceChanged
) {}
}