Updated OpenGL examples

This commit is contained in:
codeanticode
2012-02-12 16:19:55 +00:00
parent 8b72e8e52a
commit be51cbc787
9 changed files with 12 additions and 618 deletions
@@ -24,9 +24,7 @@ void draw(){
fill(182, 62, 29);
noStroke();
// Add basic light setup
ambientLight(100, 100, 100);
directionalLight(255, 255, 255, 0, 1, 0);
lights();
translate(width/2, height*1.2, -380);
// Tip tower to see inside
rotateX(radians(-45));
@@ -14,6 +14,7 @@ color boxFill;
void setup() {
size(640, 360, P3D);
noStroke();
hint(DISABLE_DEPTH_TEST);
}
void draw() {
@@ -1,147 +0,0 @@
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.DataBufferInt;
import java.io.IOException;
import java.io.InputStream;
BufferedImage loadBitmap(String file) throws IOException {
BufferedImage image;
InputStream input = null;
try {
input = createInput(file);
int bitmapFileHeaderLength = 14;
int bitmapInfoHeaderLength = 40;
byte bitmapFileHeader[] = new byte[bitmapFileHeaderLength];
byte bitmapInfoHeader[] = new byte[bitmapInfoHeaderLength];
input.read(bitmapFileHeader, 0, bitmapFileHeaderLength);
input.read(bitmapInfoHeader, 0, bitmapInfoHeaderLength);
int nSize = bytesToInt(bitmapFileHeader, 2);
int nWidth = bytesToInt(bitmapInfoHeader, 4);
int nHeight = bytesToInt(bitmapInfoHeader, 8);
int nBiSize = bytesToInt(bitmapInfoHeader, 0);
int nPlanes = bytesToShort(bitmapInfoHeader, 12);
int nBitCount = bytesToShort(bitmapInfoHeader, 14);
int nSizeImage = bytesToInt(bitmapInfoHeader, 20);
int nCompression = bytesToInt(bitmapInfoHeader, 16);
int nColoursUsed = bytesToInt(bitmapInfoHeader, 32);
int nXPixelsMeter = bytesToInt(bitmapInfoHeader, 24);
int nYPixelsMeter = bytesToInt(bitmapInfoHeader, 28);
int nImportantColours = bytesToInt(bitmapInfoHeader, 36);
if (nBitCount == 24) {
image = read24BitBitmap(nSizeImage, nHeight, nWidth, input);
}
else if (nBitCount == 8) {
image = read8BitBitmap(nColoursUsed, nBitCount, nSizeImage, nWidth, nHeight, input);
}
else {
System.out.println("Not a 24-bit or 8-bit Windows Bitmap, aborting...");
image = null;
}
}
finally {
try {
if (input != null)
input.close();
}
catch (IOException e) {
}
}
return image;
}
BufferedImage read8BitBitmap(int nColoursUsed, int nBitCount, int nSizeImage, int nWidth, int nHeight, InputStream input) throws IOException {
int nNumColors = (nColoursUsed > 0) ? nColoursUsed : (1 & 0xff) << nBitCount;
if (nSizeImage == 0) {
nSizeImage = ((((nWidth * nBitCount) + 31) & ~31) >> 3);
nSizeImage *= nHeight;
}
int npalette[] = new int[nNumColors];
byte bpalette[] = new byte[nNumColors * 4];
readBuffer(input, bpalette);
int nindex8 = 0;
for (int n = 0; n < nNumColors; n++) {
npalette[n] = (255 & 0xff) << 24 |
(bpalette[nindex8 + 2] & 0xff) << 16 |
(bpalette[nindex8 + 1] & 0xff) << 8 |
(bpalette[nindex8 + 0] & 0xff);
nindex8 += 4;
}
int npad8 = (nSizeImage / nHeight) - nWidth;
BufferedImage bufferedImage = new BufferedImage(nWidth, nHeight, BufferedImage.TYPE_INT_ARGB);
DataBufferInt dataBufferByte = ((DataBufferInt) bufferedImage.getRaster().getDataBuffer());
int[][] bankData = dataBufferByte.getBankData();
byte bdata[] = new byte[(nWidth + npad8) * nHeight];
readBuffer(input, bdata);
nindex8 = 0;
for (int j8 = nHeight - 1; j8 >= 0; j8--) {
for (int i8 = 0; i8 < nWidth; i8++) {
bankData[0][j8 * nWidth + i8] = npalette[((int) bdata[nindex8] & 0xff)];
nindex8++;
}
nindex8 += npad8;
}
return bufferedImage;
}
BufferedImage read24BitBitmap(int nSizeImage, int nHeight, int nWidth, InputStream input) throws IOException {
int npad = (nSizeImage / nHeight) - nWidth * 3;
if (npad == 4 || npad < 0)
npad = 0;
int nindex = 0;
BufferedImage bufferedImage = new BufferedImage(nWidth, nHeight, BufferedImage.TYPE_4BYTE_ABGR);
DataBufferByte dataBufferByte = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer());
byte[][] bankData = dataBufferByte.getBankData();
byte brgb[] = new byte[(nWidth + npad) * 3 * nHeight];
readBuffer(input, brgb);
for (int j = nHeight - 1; j >= 0; j--) {
for (int i = 0; i < nWidth; i++) {
int base = (j * nWidth + i) * 4;
bankData[0][base] = (byte) 255;
bankData[0][base + 1] = brgb[nindex];
bankData[0][base + 2] = brgb[nindex + 1];
bankData[0][base + 3] = brgb[nindex + 2];
nindex += 3;
}
nindex += npad;
}
return bufferedImage;
}
int bytesToInt(byte[] bytes, int index) {
return (bytes[index + 3] & 0xff) << 24 |
(bytes[index + 2] & 0xff) << 16 |
(bytes[index + 1] & 0xff) << 8 |
bytes[index + 0] & 0xff;
}
short bytesToShort(byte[] bytes, int index) {
return (short) (((bytes[index + 1] & 0xff) << 8) |
(bytes[index + 0] & 0xff));
}
void readBuffer(InputStream in, byte[] buffer) throws IOException {
int bytesRead = 0;
int bytesToRead = buffer.length;
while (bytesToRead > 0) {
int read = in.read(buffer, bytesRead, bytesToRead);
bytesRead += read;
bytesToRead -= read;
}
}
@@ -1,235 +0,0 @@
// Nehe by Andres Colubri
// Example of direct OpenGL use inside Processing with the
// OPENGL2 renderer.
// Ported from NeHe tutorial 8:
// http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=08
import javax.media.opengl.*;
import java.nio.*;
import javax.media.opengl.glu.gl2.GLUgl2;
boolean lighting = true;
boolean blending = true;
boolean depthTest = true;
boolean depthMask = false;
boolean texturing = true;
int selBlend = 1;
int selFilter = 0;
float transparency = 0.5f;
// The color depth of the sketch can be set with this
// method. The 6 numbers separated by colons correspond
// to the red, green, blue, alpha, depth and stencil bits.
// If this method is not defined, then Processing will let
// OpenGL to automatically choose the color depth.
String sketchColordepth() {
return "8:8:8:8:16:0";
}
// Whether the sketch surface supports translucenty or not.
boolean sketchTranslucency() {
return true;
}
FloatBuffer[] cubeVertexBfr;
FloatBuffer[] cubeNormalBfr;
FloatBuffer[] cubeTextureBfr;
FloatBuffer lightAmbBfr;
FloatBuffer lightDifBfr;
FloatBuffer lightPosBfr;
private IntBuffer texturesBuffer;
private float xRot;
private float yRot;
float xSpeed = 0.2f;
float ySpeed = 0.2f;
GLUgl2 glu;
void setup() {
size(400, 400, P3D);
glu = new GLUgl2();
int SIZEOF_FLOAT = Float.SIZE / 8;
cubeVertexBfr = new FloatBuffer[6];
cubeNormalBfr = new FloatBuffer[6];
cubeTextureBfr = new FloatBuffer[6];
for (int i = 0; i < 6; i++) {
ByteBuffer vbb = ByteBuffer.allocateDirect(4 * 3 * SIZEOF_FLOAT);
vbb.order(ByteOrder.nativeOrder());
cubeVertexBfr[i] = vbb.asFloatBuffer();
cubeVertexBfr[i].put(cubeVertexCoords[i]);
cubeVertexBfr[i].flip();
ByteBuffer nbb = ByteBuffer.allocateDirect(4 * 3 * SIZEOF_FLOAT);
nbb.order(ByteOrder.nativeOrder());
cubeNormalBfr[i] = nbb.asFloatBuffer();
cubeNormalBfr[i].put(cubeNormalCoords[i]);
cubeNormalBfr[i].flip();
ByteBuffer tbb = ByteBuffer.allocateDirect(4 * 2 * SIZEOF_FLOAT);
tbb.order(ByteOrder.nativeOrder());
cubeTextureBfr[i] = tbb.asFloatBuffer();
cubeTextureBfr[i].put(cubeTextureCoords[i]);
cubeTextureBfr[i].flip();
}
lightAmbBfr = FloatBuffer.wrap(lightAmb);
lightDifBfr = FloatBuffer.wrap(lightDif);
lightPosBfr = FloatBuffer.wrap(lightPos);
PGraphicsOpenGL pgl = (PGraphicsOpenGL)g;
GL gl = pgl.beginGL();
Texture teximage = null;
try {
teximage = readTexture("glass.bmp");
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
texturesBuffer = IntBuffer.allocate(3);
gl.glGenTextures(3, texturesBuffer);
gl.glEnable(GL.GL_TEXTURE_2D);
// setup texture 0 with nearest filtering
gl.glBindTexture(GL.GL_TEXTURE_2D, texturesBuffer.get(0));
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE);
makeRGBTexture(gl, glu, teximage, GL.GL_TEXTURE_2D, false);
// setup texture 1 with linear filtering for both minification and magnification,
// this is usually called bilinear sampling
gl.glBindTexture(GL.GL_TEXTURE_2D, texturesBuffer.get(1));
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE);
makeRGBTexture(gl, glu, teximage, GL.GL_TEXTURE_2D, false);
// setup texture 2 with linear filtering for magnification and linear-linear mipmapping
// (trilinear sampling)
gl.glBindTexture(GL.GL_TEXTURE_2D, texturesBuffer.get(2));
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR_MIPMAP_NEAREST);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE);
makeRGBTexture(gl, glu, teximage, GL.GL_TEXTURE_2D, true);
gl.glDisable(GL.GL_TEXTURE_2D);
pgl.endGL();
}
public void draw() {
background(0);
PGraphicsOpenGL pgl = (PGraphicsOpenGL)g;
GL gl = pgl.beginGL();
GL2 gl2 = gl.getGL2();
gl2.glShadeModel(GL2.GL_SMOOTH);
gl.glClearColor(0, 0, 0, 0);
if (depthTest) {
gl.glClearDepthf(1.0f);
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glDepthFunc(GL.GL_LEQUAL);
}
else {
gl.glDisable(GL.GL_DEPTH_TEST);
}
gl.glDepthMask(depthMask);
gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
// lighting
gl.glEnable(GL2.GL_LIGHT0);
gl2.glLightfv(GL2.GL_LIGHT0, GL2.GL_AMBIENT, lightAmbBfr);
gl2.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, lightDifBfr);
gl2.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, lightPosBfr);
// blending
gl.glEnable(GL2.GL_COLOR_MATERIAL);
gl2.glColor4f(1.0f, 1.0f, 1.0f, transparency);
if (selBlend == 0) {
gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE);
}
else if (selBlend == 1) {
gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
}
gl.glViewport(0, 0, width, height);
// setup projection matrix
gl2.glMatrixMode(GL2.GL_PROJECTION);
gl2.glLoadIdentity();
glu.gluPerspective(45.0f, (float)width / (float)height, 1.0f, 100.0f);
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl2.glMatrixMode(GL2.GL_MODELVIEW);
gl2.glLoadIdentity();
// update lighting
if (lighting) {
gl.glEnable(GL2.GL_LIGHTING);
}
else {
gl.glDisable(GL2.GL_LIGHTING);
}
// update blending
if (blending) {
gl.glEnable(GL.GL_BLEND);
gl.glDisable(GL.GL_CULL_FACE);
}
else {
gl.glDisable(GL.GL_BLEND);
gl.glEnable(GL.GL_CULL_FACE);
}
gl2.glTranslatef(0, 0, -6);
gl2.glRotatef(xRot, 1, 0, 0);
gl2.glRotatef(yRot, 0, 1, 0);
if (texturing) {
gl.glEnable(GL.GL_TEXTURE_2D);
gl.glBindTexture(GL.GL_TEXTURE_2D, texturesBuffer.get(selFilter));
}
gl2.glEnableClientState(GL2.GL_VERTEX_ARRAY);
gl2.glEnableClientState(GL2.GL_NORMAL_ARRAY);
if (texturing) gl2.glEnableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
for (int i = 0; i < 6; i++) // draw each face
{
gl2.glVertexPointer(3, GL.GL_FLOAT, 0, cubeVertexBfr[i]);
if (texturing) gl2.glTexCoordPointer(2, GL.GL_FLOAT, 0, cubeTextureBfr[i]);
gl2.glNormalPointer(GL.GL_FLOAT, 0, cubeNormalBfr[i]);
gl2.glDrawArrays(GL.GL_TRIANGLE_FAN, 0, 4);
}
gl2.glDisableClientState(GL2.GL_VERTEX_ARRAY);
gl2.glDisableClientState(GL2.GL_NORMAL_ARRAY);
if (texturing) {
gl2.glDisableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
gl.glDisable(GL.GL_TEXTURE_2D);
}
// update rotations
xRot += xSpeed;
yRot += ySpeed;
pgl.endGL();
}
void makeRGBTexture(GL gl, GLUgl2 glu, Texture img, int target, boolean mipmapped) {
if (mipmapped) {
glu.gluBuild2DMipmaps(target, GL.GL_RGB8, img.getWidth(), img.getHeight(), GL.GL_RGB, GL.GL_UNSIGNED_BYTE, img.getPixels());
} else {
gl.glTexImage2D(target, 0, GL.GL_RGB, img.getWidth(), img.getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, img.getPixels());
}
}
@@ -1,82 +0,0 @@
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
import java.io.IOException;
import java.nio.ByteBuffer;
Texture readTexture(String filename) throws IOException {
return readTexture(filename, false);
}
Texture readTexture(String filename, boolean storeAlphaChannel) throws IOException {
BufferedImage bufferedImage;
if (filename.endsWith(".bmp")) {
bufferedImage = loadBitmap(filename);
}
else {
bufferedImage = readImage(filename);
}
return readPixels(bufferedImage, storeAlphaChannel);
}
BufferedImage readImage(String resourceName) throws IOException {
return ImageIO.read(createInput(resourceName));
}
Texture readPixels(BufferedImage img, boolean storeAlphaChannel) {
int[] packedPixels = new int[img.getWidth() * img.getHeight()];
PixelGrabber pixelgrabber = new PixelGrabber(img, 0, 0, img.getWidth(), img.getHeight(), packedPixels, 0, img.getWidth());
try {
pixelgrabber.grabPixels();
}
catch (InterruptedException e) {
throw new RuntimeException();
}
int bytesPerPixel = storeAlphaChannel ? 4 : 3;
//ByteBuffer unpackedPixels = BufferUtil.newByteBuffer(packedPixels.length * bytesPerPixel);
ByteBuffer unpackedPixels = ByteBuffer.allocate(packedPixels.length * bytesPerPixel);
for (int row = img.getHeight() - 1; row >= 0; row--) {
for (int col = 0; col < img.getWidth(); col++) {
int packedPixel = packedPixels[row * img.getWidth() + col];
unpackedPixels.put((byte) ((packedPixel >> 16) & 0xFF));
unpackedPixels.put((byte) ((packedPixel >> 8) & 0xFF));
unpackedPixels.put((byte) ((packedPixel >> 0) & 0xFF));
if (storeAlphaChannel) {
unpackedPixels.put((byte) ((packedPixel >> 24) & 0xFF));
}
}
}
unpackedPixels.flip();
return new Texture(unpackedPixels, img.getWidth(), img.getHeight());
}
class Texture {
ByteBuffer pixels;
int width;
int height;
public Texture(ByteBuffer pixels, int width, int height) {
this.height = height;
this.pixels = pixels;
this.width = width;
}
public int getHeight() {
return height;
}
public ByteBuffer getPixels() {
return pixels;
}
public int getWidth() {
return width;
}
}
@@ -1,147 +0,0 @@
float[][] cubeVertexCoords = new float[][] {
new float[] { // top
1, 1,-1,
-1, 1,-1,
-1, 1, 1,
1, 1, 1
}
,
new float[] { // bottom
1,-1, 1,
-1,-1, 1,
-1,-1,-1,
1,-1,-1
}
,
new float[] { // front
1, 1, 1,
-1, 1, 1,
-1,-1, 1,
1,-1, 1
}
,
new float[] { // back
1,-1,-1,
-1,-1,-1,
-1, 1,-1,
1, 1,-1
}
,
new float[] { // left
-1, 1, 1,
-1, 1,-1,
-1,-1,-1,
-1,-1, 1
}
,
new float[] { // right
1, 1,-1,
1, 1, 1,
1,-1, 1,
1,-1,-1
}
,
};
float[][] cubeNormalCoords = new float[][] {
new float[] { // top
0, 1, 0,
0, 1, 0,
0, 1, 0,
0, 1, 0
}
,
new float[] { // bottom
0,-1, 0,
0,-1, 0,
0,-1, 0,
0,-1, 0
}
,
new float[] { // front
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1
}
,
new float[] { // back
0, 0,-1,
0, 0,-1,
0, 0,-1,
0, 0,-1
}
,
new float[] { // left
-1, 0, 0,
-1, 0, 0,
-1, 0, 0,
-1, 0, 0
}
,
new float[] { // right
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0
}
,
};
float[][] cubeTextureCoords = new float[][] {
new float[] { // top
1, 0,
1, 1,
0, 1,
0, 0
}
,
new float[] { // bottom
0, 0,
1, 0,
1, 1,
0, 1
}
,
new float[] { // front
1, 1,
0, 1,
0, 0,
1, 0
}
,
new float[] { // back
0, 1,
0, 0,
1, 0,
1, 1
}
,
new float[] { // left
1, 1,
0, 1,
0, 0,
1, 0
}
,
new float[] { // right
0, 1,
0, 0,
1, 0,
1, 1
}
,
};
float lightAmb[]= {
0.5f, 0.5f, 0.5f, 1.0f
};
float lightDif[]= {
1.0f, 1.0f, 1.0f, 1.0f
};
float lightPos[]= {
0.0f, 0.0f, 2.0f, 1.0f
};
Binary file not shown.

Before

Width:  |  Height:  |  Size: 48 KiB

@@ -25,8 +25,9 @@ void draw() {
background(255);
hint(DISABLE_DEPTH_TEST);
// Center and spin grid
pushMatrix();
translate(width/2, height/2, -depth);
rotateY(frameCount * 0.01);
rotateX(frameCount * 0.01);
@@ -46,6 +47,7 @@ void draw() {
}
}
}
popMatrix();
fcount += 1;
int m = millis();
@@ -55,5 +57,6 @@ void draw() {
lastm = m;
println("fps: " + frate);
}
}
fill(0);
text("fps: " + frate, 10, 20);
}
@@ -90,11 +90,13 @@ void draw() {
hint(DISABLE_DEPTH_TEST);
// Center and spin grid
pushMatrix();
translate(width/2, height/2, -depth);
rotateY(frameCount * 0.01);
rotateX(frameCount * 0.01);
shape(grid);
popMatrix();
fcount += 1;
int m = millis();
@@ -104,5 +106,6 @@ void draw() {
lastm = m;
println("fps: " + frate);
}
fill(0);
text("fps: " + frate, 10, 20);
}