Привет всем, помогите пожалуйста в OpenGL создать прозрачный куб, у которого видны только грани (линии), а те грани, которые скрытые (имеющие z-index ниже) являются пунктирными (dotted).
Как создать сам куб я знаю, но не получается прозраным. Без разницы на каком языке программирования будет. Спасибо большое.
import javax.media.opengl.*;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
import javax.media.opengl.awt.GLJPanel;
public class SimpleCube extends GLJPanel implements GLEventListener, KeyListener {
private float rotateX, rotateY, rotateZ;
public static void main(String[] args) {
JFrame window = new JFrame("JOGL Cube Scene");
GLCapabilities caps = new GLCapabilities(null);
SimpleCube panel = new SimpleCube(caps);
window.setContentPane(panel);
window.pack();
window.setLocation(50,50);
window.setResizable(false);
window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
window.setVisible(true);
panel.requestFocusInWindow();
}
public SimpleCube(GLCapabilities capabilities) {
super(capabilities);
setPreferredSize( new Dimension(500,500) );
addGLEventListener(this);
addKeyListener(this);
rotateX = 15;
rotateY = 15;
rotateZ = 0;
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if ( key == KeyEvent.VK_LEFT )
rotateY -= 15;
else if ( key == KeyEvent.VK_RIGHT )
rotateY += 15;
else if ( key == KeyEvent.VK_DOWN)
rotateX += 15;
else if ( key == KeyEvent.VK_UP )
rotateX -= 15;
else if ( key == KeyEvent.VK_PAGE_UP )
rotateZ += 15;
else if ( key == KeyEvent.VK_PAGE_DOWN )
rotateZ -= 15;
else if ( key == KeyEvent.VK_HOME )
rotateX = rotateY = rotateZ = 0;
repaint();
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
private void drawCube(GL2 gl, float size) {
gl.glBegin(GL2.GL_LINES);
// a
gl.glVertex3f(0f, 0f, 0f);
gl.glVertex3f(size, 0f, 0f);
// b
gl.glVertex3f(0f, 0f, 0f);
gl.glVertex3f(0f, size, 0f);
// c
gl.glVertex3f(0f, size, 0f);
gl.glVertex3f(size, size, 0f);
// d
gl.glVertex3f(size, size, 0f);
gl.glVertex3f(size, 0f, 0f);
// e
gl.glVertex3f(0f, 0f, 0f);
gl.glVertex3f(0f, 0f, size);
// g
gl.glVertex3f(0f, size, 0f);
gl.glVertex3f(0f, size, size);
// h
gl.glVertex3f(size, size, 0f);
gl.glVertex3f(size, size, size);
// i
gl.glVertex3f(size, 0f, 0f);
gl.glVertex3f(size, 0f, size);
// j
gl.glVertex3f(0f, 0f, size);
gl.glVertex3f(0f, size, size);
// k
gl.glVertex3f(0f, size, size);
gl.glVertex3f(size, size, size);
gl.glVertex3f(size, size, size);
gl.glVertex3f(size, 0f, size);
gl.glVertex3f(size, 0f, size);
gl.glVertex3f(0f, 0f, size);
gl.glEnd();
}
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClearColor(0,0,0,0);
gl.glClear( GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT );
gl.glMatrixMode(GL2.GL_PROJECTION); // Set up the projection.
gl.glLoadIdentity();
gl.glOrtho(-1,1,-1,1,-2,2);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity(); // Set up modelview transform.
gl.glRotatef(rotateZ,0,0,1);
gl.glRotatef(rotateY,0,1,0);
gl.glRotatef(rotateX,1,0,0);
gl.glColor3f(1, 1, 1);
gl.glEnable(GL2.GL_DEPTH_TEST);
//////////////////
gl.glCullFace(GL2.GL_FRONT);
this.drawCube(gl, 0.7f);
/////////////////
gl.glCullFace(GL2.GL_BACK);
gl.glPushAttrib(GL2.GL_ENABLE_BIT);
gl.glLineStipple(1, (short)0xAAAA);
gl.glEnable(GL2.GL_LINE_STIPPLE);
this.drawCube(gl, 0.7f);
gl.glDisable(GL2.GL_LINE_STIPPLE);
gl.glPopAttrib();
}
public void init(GLAutoDrawable drawable) {
// called when the panel is created
GL2 gl = drawable.getGL().getGL2();
gl.glClearColor(0.8F, 0.8F, 0.8F, 1.0F);
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glEnable(GL2.GL_LIGHTING);
gl.glEnable(GL2.GL_LIGHT0);
gl.glEnable(GL2.GL_COLOR_MATERIAL);
}
public void dispose(GLAutoDrawable drawable) {
// called when the panel is being disposed
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
// called when user resizes the window
}
}