Here the full source code of the JOGL renderer, “be induldgent” I made it in about 4hours (including opengl learning) because I never use opengl before.
There is no other file in the API using JOGL, further API version will allow custom renderer implementation to allow LWGL suppport or advanced opengl features, so this part and and all related will not be ofuscated anymore.
EDIT: Planned optimisation are displaylist compilation and recompilation for texture and mesh when updated
package dzzd;
import java.awt.*;
import java.awt.event.*;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import java.nio.*;
public final class Render3DJOGL extends Render3D implements GLEventListener
{
private GLCanvas canvas;
private GL gl;
private GLU glu;
private IScene3D scene;
private Point3D rotation;
private float light0Pos[];
private float light0Val[];
private float matAmbient[];
private float matDiffuse[];
private float matSpecular[];
public Canvas getCanvas()
{
return this.canvas;
}
public Render3DJOGL()
{
super();
GLCapabilities glCaps = new GLCapabilities();
glCaps.setRedBits(8);
glCaps.setBlueBits(8);
glCaps.setGreenBits(8);
glCaps.setAlphaBits(8);
this.canvas = new GLCanvas(glCaps);
this.glu=new GLU();
this.canvas.addGLEventListener(this);
this.directInput=new DirectInput(this.canvas);
rotation=new Point3D();
light0Pos=new float[4];
light0Val=new float[4];
matAmbient=new float[4];
matDiffuse=new float[4];
matSpecular=new float[4];
}
public void setSize(int viewPixelWidth,int viewPixelHeight,int maxAntialias)
{
super.setSize(viewPixelWidth,viewPixelHeight,maxAntialias);
this.canvas.setSize(viewPixelWidth,viewPixelHeight);
}
public void renderScene3D(IScene3D scene)
{
this.scene=scene;
this.canvas.display();
}
/**
* Called once for each scene render.
*
* new renderer should overload this method to perform initialisation needed for render the given scene3D.
*
* @param scene scene3D to prepare for a new render
*/
protected void startFrame(IScene3D scene)
{
/**
* Perform OGL renderer initialisation
*/
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); //Clear color buffer & z buffer
gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); //Sets perpective mode
gl.glEnable(GL.GL_DEPTH_TEST ); //Enable zBuffer
gl.glEnable(GL.GL_CULL_FACE ); //Enable faces culling
gl.glEnable(GL.GL_COLOR_MATERIAL );
gl.glCullFace(GL.GL_FRONT); //Sets type of culled faces
gl.glColorMaterial ( GL.GL_FRONT_AND_BACK, GL.GL_DIFFUSE ) ;
gl.glColorMaterial ( GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT ) ;
gl.glColorMaterial ( GL.GL_FRONT_AND_BACK, GL.GL_SPECULAR ) ;
//gl.glEnable(GL.GL_NORMALIZE);
//gl.glEnable(GL.GL_LIGHT0);
//gl.glLightf(GL.GL_LIGHT0, GL.GL_AMBIENT,1);
//gl.glShadeModel(GL.GL_FLAT );
/**
* Perform camera initialisation
*/
gl.glMatrixMode(GL.GL_PROJECTION); //Sets current matrix (Projection => Camera)
gl.glLoadIdentity(); //Reset matrix
double ratio=this.viewPixelHeight;
ratio/=this.viewPixelWidth;
gl.glFrustum(-1,1,-1*ratio,1*ratio,this.focus,(float)Math.min(Float.MAX_VALUE,this.zMax));
/**
* Sets current matrix (Modelview => Ready to draw scene)
*/
gl.glMatrixMode(GL.GL_MODELVIEW);
}
protected void renderFrame(IScene3D scene)
{
super.renderFrame(scene);
float rot=this.numImage;
}
protected void endFrame(IScene3D scene)
{
}
protected void setFaces3DToZBuffer(Mesh3D ob,Face3D faceList[],int faceNum)
{
ob.getAxis3D().getRotationXZY(rotation);
float rx=(float)(rotation.getX()*180.0/Math.PI);
float ry=(float)(rotation.getY()*180.0/Math.PI);
float rz=(float)(rotation.getZ()*180.0/Math.PI);
IPoint3D pos=ob.getAxis3D().getOrigin();
float px=(float)pos.getX();
float py=(float)pos.getY();
float pz=(float)pos.getZ();
IPoint3D pivot=ob.getPivot();
float pivotx=(float)pivot.getX();
float pivoty=(float)pivot.getY();
float pivotz=(float)pivot.getZ();
gl.glLoadIdentity();
gl.glScalef(1,1,-1);
gl.glTranslatef(0,0,0);
gl.glTranslatef(px,py,pz);
gl.glRotatef(-ry,0,1,0);
gl.glRotatef(-rz,0,0,1);
gl.glRotatef(-rx,1,0,0);
if(scene.getSkyBoxMesh3DId()!=ob.getId())
{
gl.glEnable(GL.GL_LIGHT1);
light0Val[0]=1.0f;
light0Val[1]=1.0f;
light0Val[2]=1.0f;
light0Val[3]=1.0f;
gl.glLightfv(GL.GL_LIGHT1, GL.GL_DIFFUSE,light0Val,0);
light0Pos[0]=(float)-mesh3DLocalLight3DBuffer[0].azx;
light0Pos[1]=(float)-mesh3DLocalLight3DBuffer[0].azy;
light0Pos[2]=(float)-mesh3DLocalLight3DBuffer[0].azz;
light0Pos[3]=0.0f;
gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION,light0Pos,0);
gl.glLightModeli(GL.GL_LIGHT_MODEL_LOCAL_VIEWER, GL.GL_TRUE);
gl.glEnable(GL.GL_LIGHTING );
}
else
{
gl.glDisable(GL.GL_LIGHTING );
}
for(int nf=0;nf<faceList.length;nf++)
{
Face3D f=faceList[nf];
IMaterial fm=f.getMaterial();
if(fm!=null)
{
//float mcolor[] = { 1.0f, 0.0f, 0.0f, 1.0f };
//glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mcolor);
matAmbient[0]=((float)(fm.getAmbientColor()>>16&0xFF))*1.0f/255.0f;
matAmbient[1]=((float)(fm.getAmbientColor()>>8&0xFF))*1.0f/255.0f;
matAmbient[2]=((float)(fm.getAmbientColor()&0xFF))*1.0f/255.0f;
matAmbient[3]=1;
gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT, matAmbient,0);
matDiffuse[0]=((float)(fm.getDiffuseColor()>>16&0xFF))*1.0f/255.0f;
matDiffuse[1]=((float)(fm.getDiffuseColor()>>8&0xFF))*1.0f/255.0f;
matDiffuse[2]=((float)(fm.getDiffuseColor()&0xFF))*1.0f/255.0f;
matDiffuse[3]=1;
gl.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, matDiffuse,0);
matSpecular[0]=((float)(fm.getSpecularColor()>>16&0xFF))*1.0f/255.0f;
matSpecular[1]=((float)(fm.getSpecularColor()>>8&0xFF))*1.0f/255.0f;
matSpecular[2]=((float)(fm.getSpecularColor()&0xFF))*1.0f/255.0f;
matSpecular[3]=((float)(fm.getSpecularLevel()>>16&0xFF))*1.0f/255.0f;
gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, matSpecular,0);
gl.glMateriali(GL.GL_FRONT, GL.GL_SHININESS, fm.getSpecularPower()>>2);
}
IPoint3D v0=f.getVertex3D0();
IPoint3D v1=f.getVertex3D1();
IPoint3D v2=f.getVertex3D2();
if(f.material!=null && f.material.diffuseTexture !=null && f.material.diffuseTexture.loaded==1 && f.material.diffuseTexture.error==0)
{
gl.glEnable(GL.GL_TEXTURE_2D); //Enable texture
this.setTexture2D(f.material.diffuseTexture);
gl.glBegin(GL.GL_TRIANGLES);
gl.glNormal3f(f.p0nx,f.p0ny,f.p0nz);
gl.glTexCoord2f(f.u0,-f.v0);
gl.glVertex3f((float)v0.getX(),(float)v0.getY(),(float)v0.getZ());
gl.glNormal3f(f.p1nx,f.p1ny,f.p1nz);
gl.glTexCoord2f(f.u1,-f.v1);
gl.glVertex3f((float)v1.getX(),(float)v1.getY(),(float)v1.getZ());
gl.glNormal3f(f.p2nx,f.p2ny,f.p2nz);
gl.glTexCoord2f(f.u2,-f.v2);
gl.glVertex3f((float)v2.getX(),(float)v2.getY(),(float)v2.getZ());
gl.glEnd();
}
else
{
gl.glDisable(GL.GL_TEXTURE_2D); //Enable texture
gl.glBegin(GL.GL_TRIANGLES);
gl.glNormal3f(f.p0nx,f.p0ny,f.p0nz);
gl.glVertex3f((float)v0.getX(),(float)v0.getY(),(float)v0.getZ());
gl.glNormal3f(f.p1nx,f.p1ny,f.p1nz);
gl.glVertex3f((float)v1.getX(),(float)v1.getY(),(float)v1.getZ());
gl.glNormal3f(f.p2nx,f.p2ny,f.p2nz);
gl.glVertex3f((float)v2.getX(),(float)v2.getY(),(float)v2.getZ());
gl.glEnd();
}
}
}
public void init(GLAutoDrawable drawable)
{
}
public void display(GLAutoDrawable drawable)
{
if(this.scene==null)
return;
this.gl=drawable.getGL();
super.renderScene3D(scene);
}
public void reshape(GLAutoDrawable drawable, int i, int i1, int i2, int i3)
{
}
public void displayChanged(GLAutoDrawable drawable, boolean b, boolean b1)
{
}
private void setTexture2D(ITexture texture)
{
Texture t=(Texture)texture;
if(t.pixels==null)
return;
int id=t.idGL;
//gl.glEnd();
if(gl.glIsTexture(id))
{// System.out.println("tid"+id);
gl.glBindTexture(GL.GL_TEXTURE_2D ,id);
//gl.glBegin(GL.GL_TRIANGLES);
return;
}
//System.out.println("tid="+id);
IntBuffer n=IntBuffer.allocate(1);
gl.glGenTextures(1,n);
n.rewind();
id=n.get();
System.out.println("tid"+id);
t.idGL=id;
gl.glBindTexture(GL.GL_TEXTURE_2D ,id);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NICEST);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NICEST);//GL_NEAREST);
//gl.glTexImage2D(GL.GL_TEXTURE_2D,0,4,t.largeurImage,t.hauteurImage,0,GL.GL_BGRA,GL.GL_UNSIGNED_BYTE,IntBuffer.wrap(t.pixels));
glu.gluBuild2DMipmaps(GL.GL_TEXTURE_2D,3,t.largeurImage,t.hauteurImage,GL.GL_BGRA,GL.GL_UNSIGNED_BYTE,IntBuffer.wrap(t.pixels));
System.out.println("larg="+t.largeurImage);
System.out.println("haut="+t.hauteurImage);
//gl.glBegin(GL.GL_TRIANGLES);
/*gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
*/
}
private void deleteTextureById(ITexture texture)
{
int n[]=new int[1];
n[0]=texture.getId()+1;
gl.glDeleteTextures(1,IntBuffer.wrap(n));
}
public String getImplementationName()
{
return "JOGL";
}
}
Bruno