2004-01-20 15:24:36 +01:00
|
|
|
package org.lwjgl.opengl.glu;
|
|
|
|
|
|
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
|
import java.nio.ByteOrder;
|
|
|
|
|
import java.nio.FloatBuffer;
|
|
|
|
|
import java.nio.IntBuffer;
|
|
|
|
|
|
2004-01-21 11:06:47 +01:00
|
|
|
import org.lwjgl.opengl.GL11;
|
|
|
|
|
import org.lwjgl.opengl.GL12;
|
2004-01-20 15:24:36 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Util.java
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* Created 7-jan-2004
|
|
|
|
|
* @author Erik Duijs
|
|
|
|
|
*/
|
|
|
|
|
public class Util {
|
|
|
|
|
|
|
|
|
|
/** temp IntBuffer of one for getting an int from some GL functions */
|
2004-02-15 20:41:51 +01:00
|
|
|
private static IntBuffer scratch = createIntBuffer(256);
|
2004-01-20 15:24:36 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return ceiling of integer division
|
|
|
|
|
* @param a
|
|
|
|
|
* @param b
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
protected static int ceil(int a, int b) {
|
|
|
|
|
return (a % b == 0 ? a / b : a / b + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Normalize vector
|
|
|
|
|
* @param v
|
|
|
|
|
* @return float[]
|
|
|
|
|
*/
|
|
|
|
|
protected static float[] normalize(float[] v) {
|
|
|
|
|
float r;
|
|
|
|
|
|
|
|
|
|
r = (float) Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
|
|
|
|
|
if (r == 0.0)
|
|
|
|
|
return v;
|
|
|
|
|
|
|
|
|
|
v[0] /= r;
|
|
|
|
|
v[1] /= r;
|
|
|
|
|
v[2] /= r;
|
|
|
|
|
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calculate cross-product
|
|
|
|
|
* @param v1
|
|
|
|
|
* @param v2
|
|
|
|
|
* @param result
|
|
|
|
|
*/
|
|
|
|
|
protected static void cross(float[] v1, float[] v2, float[] result) {
|
|
|
|
|
result[0] = v1[1] * v2[2] - v1[2] * v2[1];
|
|
|
|
|
result[1] = v1[2] * v2[0] - v1[0] * v2[2];
|
|
|
|
|
result[2] = v1[0] * v2[1] - v1[1] * v2[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method compPerPix.
|
|
|
|
|
* @param format
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
protected static int compPerPix(int format) {
|
|
|
|
|
/* Determine number of components per pixel */
|
|
|
|
|
switch (format) {
|
2004-01-21 11:06:47 +01:00
|
|
|
case GL11.GL_COLOR_INDEX :
|
|
|
|
|
case GL11.GL_STENCIL_INDEX :
|
|
|
|
|
case GL11.GL_DEPTH_COMPONENT :
|
|
|
|
|
case GL11.GL_RED :
|
|
|
|
|
case GL11.GL_GREEN :
|
|
|
|
|
case GL11.GL_BLUE :
|
|
|
|
|
case GL11.GL_ALPHA :
|
|
|
|
|
case GL11.GL_LUMINANCE :
|
2004-01-20 15:24:36 +01:00
|
|
|
return 1;
|
2004-01-21 11:06:47 +01:00
|
|
|
case GL11.GL_LUMINANCE_ALPHA :
|
2004-01-20 15:24:36 +01:00
|
|
|
return 2;
|
2004-01-21 11:06:47 +01:00
|
|
|
case GL11.GL_RGB :
|
|
|
|
|
case GL12.GL_BGR :
|
2004-01-20 15:24:36 +01:00
|
|
|
return 3;
|
2004-01-21 11:06:47 +01:00
|
|
|
case GL11.GL_RGBA :
|
|
|
|
|
case GL12.GL_BGRA :
|
2004-01-20 15:24:36 +01:00
|
|
|
return 4;
|
|
|
|
|
default :
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method nearestPower.
|
|
|
|
|
*
|
|
|
|
|
* Compute the nearest power of 2 number. This algorithm is a little
|
|
|
|
|
* strange, but it works quite well.
|
|
|
|
|
*
|
|
|
|
|
* @param width
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
protected static int nearestPower(int value) {
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
i = 1;
|
|
|
|
|
|
|
|
|
|
/* Error! */
|
|
|
|
|
if (value == 0)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
|
if (value == 1) {
|
|
|
|
|
return i;
|
|
|
|
|
} else if (value == 3) {
|
|
|
|
|
return i * 4;
|
|
|
|
|
}
|
|
|
|
|
value = value >> 1;
|
|
|
|
|
i *= 2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method bytesPerPixel.
|
|
|
|
|
* @param format
|
|
|
|
|
* @param type
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
protected static int bytesPerPixel(int format, int type) {
|
|
|
|
|
int n, m;
|
|
|
|
|
|
|
|
|
|
switch (format) {
|
2004-01-21 11:06:47 +01:00
|
|
|
case GL11.GL_COLOR_INDEX :
|
|
|
|
|
case GL11.GL_STENCIL_INDEX :
|
|
|
|
|
case GL11.GL_DEPTH_COMPONENT :
|
|
|
|
|
case GL11.GL_RED :
|
|
|
|
|
case GL11.GL_GREEN :
|
|
|
|
|
case GL11.GL_BLUE :
|
|
|
|
|
case GL11.GL_ALPHA :
|
|
|
|
|
case GL11.GL_LUMINANCE :
|
2004-01-20 15:24:36 +01:00
|
|
|
n = 1;
|
|
|
|
|
break;
|
2004-01-21 11:06:47 +01:00
|
|
|
case GL11.GL_LUMINANCE_ALPHA :
|
2004-01-20 15:24:36 +01:00
|
|
|
n = 2;
|
|
|
|
|
break;
|
2004-01-21 11:06:47 +01:00
|
|
|
case GL11.GL_RGB :
|
|
|
|
|
case GL12.GL_BGR :
|
2004-01-20 15:24:36 +01:00
|
|
|
n = 3;
|
|
|
|
|
break;
|
2004-01-21 11:06:47 +01:00
|
|
|
case GL11.GL_RGBA :
|
|
|
|
|
case GL12.GL_BGRA :
|
2004-01-20 15:24:36 +01:00
|
|
|
n = 4;
|
|
|
|
|
break;
|
|
|
|
|
default :
|
|
|
|
|
n = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (type) {
|
2004-01-21 11:06:47 +01:00
|
|
|
case GL11.GL_UNSIGNED_BYTE :
|
2004-01-20 15:24:36 +01:00
|
|
|
m = 1;
|
|
|
|
|
break;
|
2004-01-21 11:06:47 +01:00
|
|
|
case GL11.GL_BYTE :
|
2004-01-20 15:24:36 +01:00
|
|
|
m = 1;
|
|
|
|
|
break;
|
2004-01-21 11:06:47 +01:00
|
|
|
case GL11.GL_BITMAP :
|
2004-01-20 15:24:36 +01:00
|
|
|
m = 1;
|
|
|
|
|
break;
|
2004-01-21 11:06:47 +01:00
|
|
|
case GL11.GL_UNSIGNED_SHORT :
|
2004-01-20 15:24:36 +01:00
|
|
|
m = 2;
|
|
|
|
|
break;
|
2004-01-21 11:06:47 +01:00
|
|
|
case GL11.GL_SHORT :
|
2004-01-20 15:24:36 +01:00
|
|
|
m = 2;
|
|
|
|
|
break;
|
2004-01-21 11:06:47 +01:00
|
|
|
case GL11.GL_UNSIGNED_INT :
|
2004-01-20 15:24:36 +01:00
|
|
|
m = 4;
|
|
|
|
|
break;
|
2004-01-21 11:06:47 +01:00
|
|
|
case GL11.GL_INT :
|
2004-01-20 15:24:36 +01:00
|
|
|
m = 4;
|
|
|
|
|
break;
|
2004-01-21 11:06:47 +01:00
|
|
|
case GL11.GL_FLOAT :
|
2004-01-20 15:24:36 +01:00
|
|
|
m = 4;
|
|
|
|
|
break;
|
|
|
|
|
default :
|
|
|
|
|
m = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return n * m;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a FloatBuffer of specified size.
|
|
|
|
|
* @param size
|
|
|
|
|
* @return FloatBuffer
|
|
|
|
|
*/
|
|
|
|
|
protected static FloatBuffer createFloatBuffer(int size) {
|
|
|
|
|
ByteBuffer temp = ByteBuffer.allocateDirect(4 * size);
|
|
|
|
|
temp.order(ByteOrder.nativeOrder());
|
|
|
|
|
|
|
|
|
|
return temp.asFloatBuffer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create IntBuffer of specified size.
|
|
|
|
|
* @param size
|
|
|
|
|
* @return IntBuffer
|
|
|
|
|
*/
|
|
|
|
|
protected static IntBuffer createIntBuffer(int size) {
|
|
|
|
|
ByteBuffer temp = ByteBuffer.allocateDirect(4 * size);
|
|
|
|
|
temp.order(ByteOrder.nativeOrder());
|
|
|
|
|
|
|
|
|
|
return temp.asIntBuffer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convenience method for returning an int,
|
|
|
|
|
* rather than getting it out of a buffer yourself.
|
|
|
|
|
*
|
|
|
|
|
* @param what
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
protected static int glGetIntegerv(int what) {
|
|
|
|
|
scratch.rewind();
|
2004-01-21 11:06:47 +01:00
|
|
|
GL11.glGetInteger(what, scratch);
|
2004-01-20 15:24:36 +01:00
|
|
|
return scratch.get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|