package cg; //----------------------------------------------------------------------------- import java.awt.Font; //import java.awt.geom.Rectangle2D; //----------------------------------------------------------------------------- import com.jogamp.opengl.util.awt.TextRenderer; //----------------------------------------------------------------------------- import javax.swing.JFrame; import com.jogamp.opengl.GL2; import com.jogamp.opengl.GLAutoDrawable; import com.jogamp.opengl.GLCapabilities; import com.jogamp.opengl.GLEventListener; import com.jogamp.opengl.GLProfile; import com.jogamp.opengl.awt.GLCanvas; import com.jogamp.opengl.glu.GLU; import com.jogamp.opengl.util.FPSAnimator; //----------------------------------------------------------------------------- public class Main implements GLEventListener { private GLU glu = new GLU(); private float rtri = 0.0f; private TextRenderer renderer; //----------------------------------------------------------------------------- public static void main(String[] args) { final GLProfile profile = GLProfile.get(GLProfile.GL2); GLCapabilities capabilities = new GLCapabilities(profile); final GLCanvas glcanvas = new GLCanvas(capabilities); final int width = 800; final int height = 600; Main m = new Main(); glcanvas.addGLEventListener(m); final JFrame frame = new JFrame ("2D Texte auf Objektflächen - ergänzt den Code und beschriftet die Flächen"); frame.add(glcanvas); frame.setSize(width, height); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final FPSAnimator animator = new FPSAnimator( glcanvas, 60, true ); animator.start(); } //----------------------------------------------------------------------------- @Override public void init(GLAutoDrawable drawable) { GL2 gl = drawable.getGL().getGL2(); gl.glClearColor(0.000f, 0.749f, 1.000f, 1.0f); gl.glEnable( GL2.GL_DEPTH_TEST ); renderer = new TextRenderer(new Font("SansSerif", Font.BOLD, 20)); } //----------------------------------------------------------------------------- @Override public void display(GLAutoDrawable drawable) { GL2 gl = drawable.getGL().getGL2(); gl.glClear( GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT ); gl.glMatrixMode(GL2.GL_MODELVIEW); gl.glLoadIdentity(); glu.gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0); gl.glRotatef(rtri, 1, 1, 1); gl.glScalef(2, 2, 2); gl.glPushMatrix(); gl.glRotatef(-90, 1, 0, 0); // obere Fläche drawFace(gl, 1.0f, "obere" /*, ...*/); gl.glPopMatrix(); // vordere Fläche drawFace(gl, 1.0f, "vordere" /*, ...*/); gl.glPushMatrix(); gl.glRotatef(90, 0, 1, 0); // rechte Fläche drawFace(gl, 1.0f, "rechte" /*, ...*/); gl.glRotatef(90, 0, 1, 0); // hintere Fläche drawFace(gl, 1.0f, "hintere" /*, ...*/); gl.glRotatef(90, 0, 1, 0); // linke Fläche drawFace(gl, 1.0f, "linke" /*, ...*/); gl.glPopMatrix(); gl.glPushMatrix(); gl.glRotatef(90, 1, 0, 0); // untere Fläche drawFace(gl, 1.0f, "untere" /*, ...*/); gl.glPopMatrix(); rtri += 0.4f; } //----------------------------------------------------------------------------- @Override public void dispose(GLAutoDrawable drawable) {} //----------------------------------------------------------------------------- @Override public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { final GL2 gl = drawable.getGL().getGL2(); final float h = ( float ) width / ( float ) height; gl.glViewport( 0, 0, width, height ); gl.glMatrixMode( GL2.GL_PROJECTION ); gl.glLoadIdentity(); glu.gluPerspective( 45.0f, h, 1.0, 20.0 ); gl.glMatrixMode( GL2.GL_MODELVIEW ); gl.glLoadIdentity(); } //----------------------------------------------------------------------------- private void drawFace(GL2 gl, float faceSize, String text /*,...*/) { float halfFaceSize = faceSize / 2; gl.glBegin(GL2.GL_QUADS); gl.glVertex3f(-halfFaceSize, -halfFaceSize, halfFaceSize); gl.glVertex3f( halfFaceSize, -halfFaceSize, halfFaceSize); gl.glVertex3f( halfFaceSize, halfFaceSize, halfFaceSize); gl.glVertex3f(-halfFaceSize, halfFaceSize, halfFaceSize); gl.glEnd(); renderer.begin3DRendering(); //renderer.draw3D(...); //.. renderer.end3DRendering(); } }