Ursprungsmitteilung
Thema 3D-Grafik mit OpenGL 
Autor Aegidius Plüss 
Eingangsdatum 2017-10-25 09:44:22.0 
Mitteilung Neuerdings wurde auch Java binding for OpenGL (JOGL) in TigerJython eingebaut. Bei minimalen Kenntnissen von Java ist es möglich, die Programme relativ einfach von Java nach Python zu portieren. Eine Dokumentation für die Python-Version ist in Arbeit. Am besten geht man der Java Dokumentation von JOGL bzw. einem JOGL Tutorial aus.

Hier ein Beispiel aus dem JOGL Tutorial (JOGL - 3D Cube)

Java-Version:

// Cube.java

import java.awt.DisplayMode;
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 javax.swing.JFrame;

import com.jogamp.opengl.util.FPSAnimator;

public class Cube implements GLEventListener
{

public static DisplayMode dm, dm_old;
private final GLU glu = new GLU();
private float rquad = 0.0f;

@Override
public void display(GLAutoDrawable drawable)
{
final GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(0f, 0f, -5.0f);

// Rotate The Cube On X, Y & Z
gl.glRotatef(rquad, 1.0f, 1.0f, 1.0f);

//giving different colors to different sides
gl.glBegin(GL2.GL_QUADS); // Start Drawing The Cube
gl.glColor3f(1f, 0f, 0f); //red color
gl.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Top)
gl.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Top)
gl.glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
gl.glVertex3f(1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)

gl.glColor3f(0f, 1f, 0f); //green color
gl.glVertex3f(1.0f, -1.0f, 1.0f); // Top Right Of The Quad
gl.glVertex3f(-1.0f, -1.0f, 1.0f); // Top Left Of The Quad
gl.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad
gl.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad

gl.glColor3f(0f, 0f, 1f); //blue color
gl.glVertex3f(1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
gl.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
gl.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Quad
gl.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Right Of The Quad

gl.glColor3f(1f, 1f, 0f); //yellow (red + green)
gl.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad
gl.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad
gl.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Back)
gl.glVertex3f(1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Back)

gl.glColor3f(1f, 0f, 1f); //purple (red + green)
gl.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
gl.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Left)
gl.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad
gl.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Quad

gl.glColor3f(0f, 1f, 1f); //sky blue (blue +green)
gl.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Right)
gl.glVertex3f(1.0f, 1.0f, 1.0f); // Top Left Of The Quad
gl.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Left Of The Quad
gl.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad
gl.glEnd(); // Done Drawing The Quad
gl.glFlush();

rquad -= 0.15f;
}

@Override
public void dispose(GLAutoDrawable drawable)
{
}

@Override
public void init(GLAutoDrawable drawable)
{
final GL2 gl = drawable.getGL().getGL2();
gl.glShadeModel(GL2.GL_SMOOTH);
gl.glClearColor(0f, 0f, 0f, 0f);
gl.glClearDepth(1.0f);
gl.glEnable(GL2.GL_DEPTH_TEST);
gl.glDepthFunc(GL2.GL_LEQUAL);
gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
}

@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
{
final GL2 gl = drawable.getGL().getGL2();
if (height == 0)
height = 1;

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();
}

public static void main(String[] args)
{
final GLProfile profile = GLProfile.get(GLProfile.GL2);
GLCapabilities capabilities = new GLCapabilities(profile);

final GLCanvas glcanvas = new GLCanvas(capabilities);
Cube cube = new Cube();

glcanvas.addGLEventListener(cube);
glcanvas.setSize(400, 400);

final JFrame frame = new JFrame(" Multicolored cube");
frame.getContentPane().add(glcanvas);
frame.setSize(frame.getContentPane().getPreferredSize());
frame.setVisible(true);
final FPSAnimator animator = new FPSAnimator(glcanvas, 300, true);

animator.start();
}
}


Im Folgenden eine möglichst direkte Portierung auf TigerJython:


# RotatingCube.py

from com.jogamp.opengl import *
from com.jogamp.opengl.glu import GLU
from com.jogamp.opengl.awt import GLCanvas
from com.jogamp.opengl.util import FPSAnimator
from javax.swing import JFrame

class Cube(GLEventListener):
glu = GLU()
rquad = 0

def display(self, drawable):
gl = drawable.getGL().getGL2()
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT)
gl.glLoadIdentity()
gl.glTranslatef(0, 0, -5)

# Rotate The Cube On X, Y & Z
gl.glRotatef(Cube.rquad, 1, 1, 1)

# giving different colors to different sides
gl.glBegin(GL2.GL_QUADS) # Start Drawing The Cube
gl.glColor3f(1, 0, 0) # red color
gl.glVertex3f(1, 1, -1) # Top Right Of The Quad (Top)
gl.glVertex3f(-1, 1, -1) # Top Left Of The Quad (Top)
gl.glVertex3f(-1, 1, 1) # Bottom Left Of The Quad (Top)
gl.glVertex3f(1, 1, 1) # Bottom Right Of The Quad (Top)

gl.glColor3f(0, 1, 0) # green color
gl.glVertex3f(1, -1, 1) # Top Right Of The Quad
gl.glVertex3f(-1, -1, 1) # Top Left Of The Quad
gl.glVertex3f(-1, -1, -1) # Bottom Left Of The Quad
gl.glVertex3f(1, -1, -1) # Bottom Right Of The Quad

gl.glColor3f(0, 0, 1) # blue color
gl.glVertex3f(1, 1, 1) # Top Right Of The Quad (Front)
gl.glVertex3f(-1, 1, 1) # Top Left Of The Quad (Front)
gl.glVertex3f(-1, -1, 1) # Bottom Left Of The Quad
gl.glVertex3f(1, -1, 1) # Bottom Right Of The Quad

gl.glColor3f(1, 1, 0) # yellow (red + green)
gl.glVertex3f(1, -1, -1) # Bottom Left Of The Quad
gl.glVertex3f(-1, -1, -1) # Bottom Right Of The Quad
gl.glVertex3f(-1, 1, -1) # Top Right Of The Quad (Back)
gl.glVertex3f(1, 1, -1) # Top Left Of The Quad (Back)

gl.glColor3f(1, 0, 1) #purple (red + green)
gl.glVertex3f(-1, 1, 1) # Top Right Of The Quad (Left)
gl.glVertex3f(-1, 1, -1) # Top Left Of The Quad (Left)
gl.glVertex3f(-1, -1, -1) # Bottom Left Of The Quad
gl.glVertex3f(-1, -1, 1) # Bottom Right Of The Quad

gl.glColor3f(0, 1, 1) #sky blue (blue +green)
gl.glVertex3f(1, 1, -1) # Top Right Of The Quad (Right)
gl.glVertex3f(1, 1, 1) # Top Left Of The Quad
gl.glVertex3f(1, -1, 1) # Bottom Left Of The Quad
gl.glVertex3f(1, -1, -1) # Bottom Right Of The Quad
gl.glEnd() # Done Drawing The Quad
gl.glFlush()

Cube.rquad -= 0.15

def init(self, drawable):
gl = drawable.getGL().getGL2()
gl.glShadeModel(GL2.GL_SMOOTH)
gl.glClearColor(0, 0, 0, 0)
gl.glClearDepth(1)
gl.glEnable(GL2.GL_DEPTH_TEST)
gl.glDepthFunc(GL2.GL_LEQUAL)
gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST)

def reshape(self, drawable, x, y, width, height):
gl = drawable.getGL().getGL2()
if height == 0:
height = 1
h = float(width) / float(height)
gl.glViewport(0, 0, width, height)
gl.glMatrixMode(GL2.GL_PROJECTION)
gl.glLoadIdentity()

Cube.glu.gluPerspective(45, h, 1, 20)
gl.glMatrixMode(GL2.GL_MODELVIEW)
gl.glLoadIdentity()
# ------------ end of class Cube ----------------------

profile = GLProfile.get(GLProfile.GL2)
capabilities = GLCapabilities(profile)

glcanvas = GLCanvas(capabilities)
glcanvas = GLCanvas()
cube = Cube()

glcanvas.addGLEventListener(cube)
glcanvas.setSize(400, 400)

frame = JFrame("Multicolored Cube")
frame.getContentPane().add(glcanvas)
frame.setSize(frame.getContentPane().getPreferredSize())
frame.setVisible(True)

animator = FPSAnimator(glcanvas, 300, True)
animator.start()


Bemerkungen:
Die Java Klasse Cube wird direkt in Python abgebildet. Ein Java implements wird in in Jython als Klassenableitung aufgefasst, wobei nicht alle Methoden definiert werden müssen. Der Code aus public static void main() wird am Ende des Python-Programms übernommen.

Es ist eine interessante Aufgabe, einen Python-Wrapper zu JOGL zu schreiben, der sich an der C-Version von OpenGL orientiert, also keine Klassenkonstrukte verwendet.


 
 
      
Antworten
Kein Eintrag