Sign in to follow this  
InfernoZeus

OpenGL (jogl) drawing objects from arraylist

Recommended Posts

Im coding in JOGL but i think the principles and most method names are still the same. This is a simplified version of another project. I want to be able to retrieve objects from an arraylist and draw them and then for new objects to added to to the arrayList at a later point. Can anybody help me please.

import java.awt.BorderLayout;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.util.ArrayList;


import javax.media.opengl.GL;

import javax.media.opengl.GLAutoDrawable;

import javax.media.opengl.GLCapabilities;

import javax.media.opengl.GLEventListener;

import javax.media.opengl.GLJPanel;

import javax.media.opengl.glu.GLU;

import javax.swing.JFrame;

import com.sun.opengl.util.GLUT;

import externals.glskeleton;


public class TriangleDemo extends glskeleton implements GLEventListener,

KeyListener, MouseListener {

private GL gl;

private GLU glu;

private GLUT glut;


private float deltaX = 0, deltaY = 0;

private float spinX = 0, spinY = 0;


private ArrayList<Triangle> triangles = new ArrayList<Triangle>();


private Triangle triangle1, triangle2;

public TriangleDemo() {

}


public static void main(String[] args) {

GLCapabilities caps = new GLCapabilities();

GLJPanel canvas = new GLJPanel(caps);

TriangleDemo demo = new TriangleDemo();


demo.setCanvas(canvas);

canvas.addGLEventListener(demo);

canvas.addKeyListener(demo);

canvas.addMouseListener(demo);


demo.setDefaultAnimator(50);


JFrame frame = new JFrame("Lego Brick :D");

frame.setSize(1000, 500);

frame.setLocationRelativeTo(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().setLayout(new BorderLayout());


frame.getContentPane().add(canvas, BorderLayout.CENTER);


frame.setVisible(true);

canvas.requestFocusInWindow();


demo.animate();

}




public void init(GLAutoDrawable drawable) {

GL gl = drawable.getGL();

glu = new GLU();

glut = new GLUT();


gl.glClearColor(0f,0f,0f,0f);

gl.glColor3f(0, 0, 1);

gl.glShadeModel(GL.GL_SMOOTH);

gl.glDepthFunc(GL.GL_LESS);

gl.glEnable(GL.GL_DEPTH_TEST);


triangle1 = new Triangle(gl);

triangles.add(triangle1);

}



public synchronized void generateTriangle(){

triangle2 = new Triangle(gl); 

triangles.add(triangle2);

}



public synchronized void display(GLAutoDrawable drawable){

GL gl = drawable.getGL();


gl.glLoadIdentity();


glu.gluLookAt(0,5,-15,

0,0,0,

0,1,0);


gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

gl.glPushMatrix();


gl.glTranslated(0,0,5);

gl.glRotated((double)spinX, 0, 1, 0);

gl.glRotated((double)spinY,1,0,0);


gl.glPushMatrix();


gl.glRotated(-25,1,0,0);

gl.glScaled(0.5, 0.5, 0.5);


for(int i =0;i<triangles.size();i++){

triangles.get(i).draw();

gl.glTranslated(2,0,0);

}





gl.glPopMatrix();


gl.glPopMatrix();


gl.glFlush();


rotate();



}


public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,

boolean deviceChanged) { 

}


private void rotate() {

spinX = (spinX + deltaX) % 360;

spinY = (spinY + deltaY) % 360;

}


public void reshape(GLAutoDrawable drawable, int x, int y, int w,

int h) {

GL gl = drawable.getGL();

if(h==0)h++;


gl.glViewport(0,0,w,h);

gl.glMatrixMode(GL.GL_PROJECTION);

gl.glLoadIdentity();

glu.gluPerspective(40.0, (float) w / (float) h, 0.01, 200.0);

gl.glMatrixMode(GL.GL_MODELVIEW);

gl.glLoadIdentity(); 

}



 

public void keyPressed(KeyEvent key) {

}


public void keyReleased(KeyEvent key) { 

}



public void keyTyped(KeyEvent key) { 

}


public void mouseClicked(MouseEvent mouse) {


}


public void mouseEntered(MouseEvent mouse) { 


}


public void mouseExited(MouseEvent mouse) {

}


public void mousePressed(MouseEvent mouse) {

switch (mouse.getButton()) {

case MouseEvent.BUTTON1:


generateTriangle();

break;

case MouseEvent.BUTTON2:

deltaX = 0;

deltaY = 0;

break;

case MouseEvent.BUTTON3:


if(mouse.isShiftDown()==true)

deltaY = 1;

else deltaY = -1;

break;

}

super.refresh();

}


public void mouseReleased(MouseEvent mouse) { 

}

}

Share this post


Link to post
Share on other sites
I've got no idea what an arraylist is, so I'll assume its some kind of java container (and not really relevant to the OpenGL part of the question).

Is your question how you can store an OpenGL mesh inside a data structure and then draw it? For each object you just need to store arrays of whatever relevant data you have (vertices, colors, texture coordinates, vertex indices), and then draw the object based on that data.

If your question is more specific than that you need to restate it.

Also please post only relevant sections of your code, we don't really need to see your mouse/keyboard handlers for this question (just makes it harder to read).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this