PDA

View Full Version : Opengl Java implementation.



smultring
11-13-2009, 01:30 PM
This is the opengl sourcecode example i want to implement:

/////////////////////////////////////////////////////////////////////////
//
// sphere.h - Renderfunctions
//
// This file was created by the OpenGL-Export-Script for Blender.
//
// Export-Time: 12.11.2009, 06:06:52
//
// Script written by Michael Gantenbrinker.
//
/////////////////////////////////////////////////////////////////////////

#ifndef __SPHERE_H__
#define __SPHERE_H__


/////////////////////////////////////////////////////////////////////////
// DEFINES
#define NUM_MESHES 1
#define NUM_LIGHTS 0


/////////////////////////////////////////////////////////////////////////
// CONSTANTS
// Vertex-Array for each Mesh
const float fVertices_1[994][3] = { };

// Normal-Array for each

Mesh
const float fNormals_1[994][3] = { };

// Triangle-Indices for each Mesh
const unsigned long ulTriangleIndices_1[64][3] = {
};


// Quad-Indices for each Mesh
const unsigned long ulQuadIndices_1[960][4] = {
};

// Array with Vertex-Array-Pointers
const float* pVertexPointers[] = {&fVertices_1[0][0]};
// Array with Normal-Array-Pointers
const float* pNormalPointers[] = {&fNormals_1[0][0]};
// Array with TriangleIndex-Array-Pointers
const unsigned long* pTriangleIndexPointers[] = {&ulTriangleIndices_1[0][0]};
// Array with QuadIndex-Array-Pointers
const unsigned long* pQuadIndexPointers[] = {&ulQuadIndices_1[0][0]};

// Array with Vertex-Array-Lengths
const unsigned long ulVertexLengths[] = {994};
// Array with TriangleIndex-Array-Lengths
const unsigned long ulTriangleIndexLengths[] = {64};
// Array with QuadIndex-Array-Lengths
const unsigned long ulQuadIndexLengths[] = {960};


void Init()
{
}

void Render(int iWidth = 1024, int iHeight = 768)
{
// Setup Projektion-Matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double dAspectRatio = (double)iWidth / (double)iHeight;
gluPerspective(45.0f, dAspectRatio, 1.0f, 100.0f);
glViewport(0, 0, iWidth, iHeight);
gluLookAt(5.0f, 5.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);

// Setup Lights
float lPos[] = {5.0f, 5.0f, 5.0f, 1.0f};
float lDir[] = {-0.707f, -0.707f, -0.707f};
glLightfv(GL_LIGHT0, GL_AMBIENT, StdColor_White);
glLightfv(GL_LIGHT0, GL_DIFFUSE, StdColor_BrightGrey);
glLightfv(GL_LIGHT0, GL_SPECULAR, StdColor_White);
glLightfv(GL_LIGHT0, GL_POSITION, lPos);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, lDir);
glLightf (GL_LIGHT0, GL_SPOT_CUTOFF, 180.f);
glLightf (GL_LIGHT0, GL_SPOT_EXPONENT, 1.0f);
glLightf (GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.0f);
glLightf (GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.6f);
glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.06f);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
// Setup Scene-Content
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
float* fVertices = 0;
float* fNormals = 0;
unsigned long* ulTriangleIndices = 0;
unsigned long* ulQuadIndices = 0;
unsigned long* ulPtr = 0;

for(int i = 0; i < NUM_MESHES; i++)
{
fVertices = (float*)pVertexPointers[i];
fNormals = (float*)pNormalPointers[i];
ulTriangleIndices = (unsigned long*)pTriangleIndexPointers[i];
ulQuadIndices = (unsigned long*)pQuadIndexPointers[i];

if(ulTriangleIndices)
{
glBegin(GL_TRIANGLES);
unsigned long t = 0;
while(t < ulTriangleIndexLengths[i]*3)
{
ulPtr = &amp;ulTriangleIndices[t];
glNormal3fv(&amp;fNormals[ulPtr[0]*3]);
glVertex3fv(&amp;fVertices[ulPtr[0]*3]);

glNormal3fv(&amp;fNormals[ulPtr[1]*3]);
glVertex3fv(&amp;fVertices[ulPtr[1]*3]);

glNormal3fv(&amp;fNormals[ulPtr[2]*3]);
glVertex3fv(&amp;fVertices[ulPtr[2]*3]);

t += 3;
}
glEnd();
}

if(ulQuadIndices)
{
glBegin(GL_QUADS);
unsigned long q = 0;
while(q < ulQuadIndexLengths[i]*4)
{
ulPtr = &amp;ulQuadIndices[q];
glNormal3fv(&amp;fNormals[ulPtr[0]*3]);
glVertex3fv(&amp;fVertices[ulPtr[0]*3]);

glNormal3fv(&amp;fNormals[ulPtr[1]*3]);
glVertex3fv(&amp;fVertices[ulPtr[1]*3]);

glNormal3fv(&amp;fNormals[ulPtr[2]*3]);
glVertex3fv(&amp;fVertices[ulPtr[2]*3]);

glNormal3fv(&amp;fNormals[ulPtr[3]*3]);
glVertex3fv(&amp;fVertices[ulPtr[3]*3]);

q += 4;
}
glEnd();
}
}
}

#endif /* __SPHERE_H__ */

smultring
11-13-2009, 01:35 PM
This is a example of the java code i write.

class CubeCanvas extends GameCanvas implements Runnable {
private static final byte[] s_cubeVertices =
{
-10, 10, 10, 10, -10, 10, 10, 10, 10, -10, -10, 10,

-10, 10, -10, 10, -10, -10, 10, 10, -10, -10, -10, -10,

-10, -10, 10, 10, -10, -10, 10, -10, 10, -10, -10, -10,

-10, 10, 10, 10, 10, -10, 10, 10, 10, -10, 10, -10,

10, -10, 10, 10, 10, -10, 10, 10, 10, 10, -10, -10,

-10, -10, 10, -10, 10, -10, -10, 10, 10, -10, -10, -10
};
private static final byte[] s_cubeColors =
{
(byte)40, (byte)80, (byte)160, (byte)255, (byte)40, (byte)80, (byte)160, (byte)255,
(byte)40, (byte)80, (byte)160, (byte)255, (byte)40, (byte)80, (byte)160, (byte)255,

(byte)40, (byte)80, (byte)160, (byte)255, (byte)40, (byte)80, (byte)160, (byte)255,
(byte)40, (byte)80, (byte)160, (byte)255, (byte)40, (byte)80, (byte)160, (byte)255,

(byte)128, (byte)128, (byte)128, (byte)255, (byte)128, (byte)128, (byte)128, (byte)255,
(byte)128, (byte)128, (byte)128, (byte)255, (byte)128, (byte)128, (byte)128, (byte)255,

(byte)128, (byte)128, (byte)128, (byte)255, (byte)128, (byte)128, (byte)128, (byte)255,
(byte)128, (byte)128, (byte)128, (byte)255, (byte)128, (byte)128, (byte)128, (byte)255,

(byte)255, (byte)110, (byte)10, (byte)255, (byte)255, (byte)110, (byte)10, (byte)255,
(byte)255, (byte)110, (byte)10, (byte)255, (byte)255, (byte)110, (byte)10, (byte)255,

(byte)255, (byte)70, (byte)60, (byte)255, (byte)255, (byte)70, (byte)60, (byte)255,
(byte)255, (byte)70, (byte)60, (byte)255, (byte)255, (byte)70, (byte)60, (byte)255
};
private static final byte[] s_cubeIndices =
{
0, 3, 1, 2, 0, 1, /* front */
6, 5, 4, 5, 7, 4, /* back */
8, 11, 9, 10, 8, 9, /* top */
15, 12, 13, 12, 14, 13, /* bottom */
16, 19, 17, 18, 16, 17, /* right */
23, 20, 21, 20, 22, 21 /* left */
};
private static final byte[] s_cubeNormals =
{
0, 0, 127, 0, 0, 127, 0, 0, 127, 0, 0, 127,

0, 0, -128, 0, 0, -128, 0, 0, -128, 0, 0, -128,

0, -128, 0, 0, -128, 0, 0, -128, 0, 0, -128, 0,

0, 127, 0, 0, 127, 0, 0, 127, 0, 0, 127, 0,

127, 0, 0, 127, 0, 0, 127, 0, 0, 127, 0, 0,

-128, 0, 0, -128, 0, 0, -128, 0, 0, -128, 0, 0
};
boolean initialized = false;
int frame = 0;
float time = 0.0f;
Graphics g;
int width;
int height;
Cube cube;
EGL10 egl;
GL10 gl;
EGLConfig eglConfig;
EGLDisplay eglDisplay;
EGLSurface eglWindowSurface;
EGLContext eglContext;
ByteBuffer cubeVertices;
ByteBuffer cubeColors;
ByteBuffer cubeNormals;
ByteBuffer cubeIndices;

public CubeCanvas(Cube cube) {
super(true);
this.cube = cube;
this.g = this.getGraphics();

this.width = getWidth();
this.height = getHeight();
}

private int getProperty(String propName, int def) {
String s = cube.getAppProperty(propName);
int val = (s == null) ? def : Integer.parseInt(s);

return val;
}

public void init() {
this.egl = (EGL10)EGLContext.getEGL();

this.eglDisplay = egl.eglGetDisplay(egl.EGL_DEFAULT_DISPLAY);

int[] major_minor = new int[2];
egl.eglInitialize(eglDisplay, major_minor);

int[] num_config = new int[1];

egl.eglGetConfigs(eglDisplay, null, 0, num_config);
System.out.println ("num_config[0] = " + num_config[0]);

int redSize = getProperty("jsr239.redSize", 8);
int greenSize = getProperty("jsr239.greenSize", 8);
int blueSize = getProperty("jsr239.blueSize", 8);
int alphaSize = getProperty("jsr239.alphaSize", 0);
int depthSize = getProperty("jsr239.depthSize", 32);
int stencilSize = getProperty("jsr239.stencilSize", EGL10.EGL_DONT_CARE);

int[] s_configAttribs =
{
EGL10.EGL_RED_SIZE, redSize, EGL10.EGL_GREEN_SIZE, greenSize, EGL10.EGL_BLUE_SIZE,
blueSize, EGL10.EGL_ALPHA_SIZE, alphaSize, EGL10.EGL_DEPTH_SIZE, depthSize,
EGL10.EGL_STENCIL_SIZE, stencilSize, EGL10.EGL_NONE
};

EGLConfig[] eglConfigs = new EGLConfig[num_config[0]];
egl.eglChooseConfig(eglDisplay, s_configAttribs, eglConfigs, eglConfigs.length, num_config);
System.out.println ("num_config[0] = " + num_config[0]);


this.eglConfig = eglConfigs[0];

this.eglContext = egl.eglCreateContext(eglDisplay, eglConfig, EGL10.EGL_NO_CONTEXT, null);

this.gl = (GL10)eglContext.getGL();

this.eglWindowSurface = egl.eglCreateWindowSurface(eglDisplay, eglConfig, g, null);

// Initialize data Buffers
this.cubeVertices = ByteBuffer.allocateDirect(s_cubeVertices.length);
cubeVertices.put(s_cubeVertices);
cubeVertices.rewind();

this.cubeColors = ByteBuffer.allocateDirect(s_cubeColors.length);
cubeColors.put(s_cubeColors);
cubeColors.rewind();


this.cubeNormals = ByteBuffer.allocateDirect(s_cubeNormals.length);
cubeNormals.put(s_cubeNormals);
cubeNormals.rewind();

this.cubeIndices = ByteBuffer.allocateDirect(s_cubeIndices.length);
cubeIndices.put(s_cubeIndices);
cubeIndices.rewind();

this.initialized = true;
}

private void perspective(float fovy, float aspect, float zNear, float zFar) {
float xmin;
float xmax;
float ymin;
float ymax;

ymax = zNear * (float)Math.tan((fovy * Math.PI) / 360.0);
ymin = -ymax;
xmin = ymin * aspect;
xmax = ymax * aspect;

gl.glFrustumf(xmin, xmax, ymin, ymax, zNear, zFar);
}

private void updateState(int width, int height) {
float[] light_position = { -50.f, 50.f, 50.f, 0.f };
float[] light_ambient = { 0.125f, 0.125f, 0.125f, 1.f };
float[] light_diffuse = { 1.0f, 1.0f, 1.0f, 1.f };
float[] material_spec = { 1.0f, 1.0f, 1.0f, 0.f };
float[] zero_vec4 = { 0.0f, 0.0f, 0.0f, 0.f };

float aspect = (height != 0) ? ((float)width / (float)height) : 1.0f;

gl.glViewport(0, 0, width, height);
gl.glScissor(0, 0, width, height);

gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();

gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, light_position, 0);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, light_ambient, 0);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, light_diffuse, 0);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPECULAR, zero_vec4, 0);
gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR, material_spec, 0);

gl.glEnable(GL10.GL_NORMALIZE);
gl.glEnable(GL10.GL_LIGHTING);
gl.glEnable(GL10.GL_LIGHT0);
gl.glEnable(GL10.GL_COLOR_MATERIAL);
gl.glEnable(GL10.GL_CULL_FACE);

gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);

gl.glShadeModel(GL10.GL_SMOOTH);
gl.glDisable(GL10.GL_DITHER);

// Clear background to blue
gl.glClearColor(5.0f, 5.0f, 2.0f, 1.0f);

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();

perspective(90.f, aspect, 0.1f, 100.f);

gl.glFinish();


}

private void drawScene() {
// Make the context current on this thread
egl.eglMakeCurrent(eglDisplay, eglWindowSurface, eglWindowSurface, eglContext);

// Perform setup and clear background using GL
egl.eglWaitNative(EGL10.EGL_CORE_NATIVE_ENGINE, g);

updateState(width, height);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glFinish();

// Wait for GL to complete
egl.eglWaitGL();


// Draw the scene using GL
egl.eglWaitNative(EGL10.EGL_CORE_NATIVE_ENGINE, g);

gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();

gl.glTranslatef(0.f, 3.f, -90.f);
gl.glRotatef(50.77f, 1.0f, 2.0f, 0.0f);


gl.glVertexPointer(3, GL10.GL_BYTE, 0, cubeVertices);
gl.glColorPointer(4, GL10.GL_UNSIGNED_BYTE, 0, cubeColors);
gl.glNormalPointer(GL10.GL_BYTE, 0, cubeNormals);

gl.glDrawElements(GL10.GL_TRIANGLES, 6 * 6, GL10.GL_UNSIGNED_BYTE, cubeIndices);

gl.glFinish();


time += 0.1f;


egl.eglWaitGL();

// Release the context
egl.eglMakeCurrent(eglDisplay, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE,
EGL10.EGL_NO_CONTEXT);

// Draw a red square using MIDP
g.setColor(255, 0, 0);
g.fillRect(25, 25, 25, 25);
// Draw a green square using MIDP
g.setColor(0, 255, 0);
g.fillRect(10, 200, 25, 25);

}

public void shutdown() {
egl.eglMakeCurrent(eglDisplay, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE,
EGL10.EGL_NO_CONTEXT);
egl.eglDestroyContext(eglDisplay, eglContext);
egl.eglDestroySurface(eglDisplay, eglWindowSurface);
egl.eglTerminate(eglDisplay);
}

public void run() {
if (!initialized) {
init();
}

try {
while (!cube.isFinished()) {
if (!cube.paused) {
Thread.sleep(2);
drawScene();
flushGraphics();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}

shutdown();
}
}


public class Cube extends MIDlet implements CommandListener {
private final Command exitCommand = new Command("Exit", Command.EXIT, 1);
Display display;
CubeCanvas canvas;
boolean started = false;
boolean paused = false;
boolean finished = false;
Thread drawThread;

public Cube() {
this.display = Display.getDisplay(this);
this.canvas = new CubeCanvas(this);
this.canvas.setCommandListener(this);
this.canvas.addCommand(exitCommand);
}

public void startApp() {
if (!started) {
started = true;
display.setCurrent(canvas);
drawThread = new Thread(canvas);
drawThread.start();
}

paused = finished = false;
}

public void pauseApp() {
paused = true;
}

public void destroyApp(boolean unconditional) {
// Wait for draw thread to die
setFinished();

try {
drawThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

public void paint(Graphics g) {
}

public void commandAction(Command cmd, Displayable disp) {
if (cmd == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
}

public synchronized boolean isFinished() {
return finished;
}

public synchronized void setFinished() {
finished = true;
}
}

smultring
11-15-2009, 02:07 PM
Any ideas?
java + opengl anyone?

ZbuffeR
11-16-2009, 02:18 AM
What is your question ?

smultring
11-18-2009, 11:31 AM
A example of that code in a java contex, much appricated.

smultring
11-21-2009, 03:56 PM
If you look at the ogl code ive removed the big byte arrays of its contents.

My question is i want to start using that code in my java code.
Maby you know about any books i may want to read on the subject.

mfort
11-23-2009, 01:28 PM
The Java code you presented does not use OpenGL.
It uses OpenGL ES. See JSR 239 Java OpenGL ES binding here:
http://java.sun.com/javame/reference/apis/jsr239/

If you want to use Java and OpenGL for desktop,
search for JOGL