mirror of
https://github.com/shadowfacts/lwjgl2-arm64.git
synced 2026-03-10 07:23:49 +01:00
227 lines
4 KiB
Java
227 lines
4 KiB
Java
|
|
package org.lwjgl.opengl.glu;
|
||
|
|
|
||
|
|
import java.nio.ByteBuffer;
|
||
|
|
import java.nio.ByteOrder;
|
||
|
|
import java.nio.FloatBuffer;
|
||
|
|
import java.nio.IntBuffer;
|
||
|
|
|
||
|
|
import org.lwjgl.opengl.GL;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Util.java
|
||
|
|
*
|
||
|
|
*
|
||
|
|
* Created 7-jan-2004
|
||
|
|
* @author Erik Duijs
|
||
|
|
*/
|
||
|
|
public class Util {
|
||
|
|
|
||
|
|
/** temp IntBuffer of one for getting an int from some GL functions */
|
||
|
|
private static IntBuffer scratch = createIntBuffer(1);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 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) {
|
||
|
|
case GL.GL_COLOR_INDEX :
|
||
|
|
case GL.GL_STENCIL_INDEX :
|
||
|
|
case GL.GL_DEPTH_COMPONENT :
|
||
|
|
case GL.GL_RED :
|
||
|
|
case GL.GL_GREEN :
|
||
|
|
case GL.GL_BLUE :
|
||
|
|
case GL.GL_ALPHA :
|
||
|
|
case GL.GL_LUMINANCE :
|
||
|
|
return 1;
|
||
|
|
case GL.GL_LUMINANCE_ALPHA :
|
||
|
|
return 2;
|
||
|
|
case GL.GL_RGB :
|
||
|
|
case GL.GL_BGR :
|
||
|
|
return 3;
|
||
|
|
case GL.GL_RGBA :
|
||
|
|
case GL.GL_BGRA :
|
||
|
|
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) {
|
||
|
|
case GL.GL_COLOR_INDEX :
|
||
|
|
case GL.GL_STENCIL_INDEX :
|
||
|
|
case GL.GL_DEPTH_COMPONENT :
|
||
|
|
case GL.GL_RED :
|
||
|
|
case GL.GL_GREEN :
|
||
|
|
case GL.GL_BLUE :
|
||
|
|
case GL.GL_ALPHA :
|
||
|
|
case GL.GL_LUMINANCE :
|
||
|
|
n = 1;
|
||
|
|
break;
|
||
|
|
case GL.GL_LUMINANCE_ALPHA :
|
||
|
|
n = 2;
|
||
|
|
break;
|
||
|
|
case GL.GL_RGB :
|
||
|
|
case GL.GL_BGR :
|
||
|
|
n = 3;
|
||
|
|
break;
|
||
|
|
case GL.GL_RGBA :
|
||
|
|
case GL.GL_BGRA :
|
||
|
|
n = 4;
|
||
|
|
break;
|
||
|
|
default :
|
||
|
|
n = 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
switch (type) {
|
||
|
|
case GL.GL_UNSIGNED_BYTE :
|
||
|
|
m = 1;
|
||
|
|
break;
|
||
|
|
case GL.GL_BYTE :
|
||
|
|
m = 1;
|
||
|
|
break;
|
||
|
|
case GL.GL_BITMAP :
|
||
|
|
m = 1;
|
||
|
|
break;
|
||
|
|
case GL.GL_UNSIGNED_SHORT :
|
||
|
|
m = 2;
|
||
|
|
break;
|
||
|
|
case GL.GL_SHORT :
|
||
|
|
m = 2;
|
||
|
|
break;
|
||
|
|
case GL.GL_UNSIGNED_INT :
|
||
|
|
m = 4;
|
||
|
|
break;
|
||
|
|
case GL.GL_INT :
|
||
|
|
m = 4;
|
||
|
|
break;
|
||
|
|
case GL.GL_FLOAT :
|
||
|
|
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();
|
||
|
|
GL.glGetInteger(what, scratch);
|
||
|
|
return scratch.get();
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|