optimizations
This commit is contained in:
parent
7da8d7d01d
commit
10b03d418f
|
@ -1,108 +1,112 @@
|
||||||
package org.lwjgl.opengl.glu;
|
package org.lwjgl.opengl.glu;
|
||||||
|
|
||||||
|
import org.lwjgl.BufferUtils;
|
||||||
|
import org.lwjgl.opengl.GL11;
|
||||||
|
|
||||||
import java.nio.FloatBuffer;
|
import java.nio.FloatBuffer;
|
||||||
import java.nio.IntBuffer;
|
import java.nio.IntBuffer;
|
||||||
|
|
||||||
import org.lwjgl.opengl.GL11;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Project.java
|
* Project.java
|
||||||
*
|
* <p/>
|
||||||
*
|
* <p/>
|
||||||
* Created 11-jan-2004
|
* Created 11-jan-2004
|
||||||
|
*
|
||||||
* @author Erik Duijs
|
* @author Erik Duijs
|
||||||
*/
|
*/
|
||||||
public class Project extends Util implements GLUConstants {
|
public class Project extends Util implements GLUConstants {
|
||||||
|
|
||||||
private static FloatBuffer matrix = createFloatBuffer(16);
|
private static final float[] IDENTITY_MATRIX = new float[] {
|
||||||
private static FloatBuffer finalMatrix = createFloatBuffer(16);
|
1.0f, 0.0f, 0.0f, 0.0f,
|
||||||
|
0.0f, 1.0f, 0.0f, 0.0f,
|
||||||
|
0.0f, 0.0f, 1.0f, 0.0f,
|
||||||
|
0.0f, 0.0f, 0.0f, 1.0f
|
||||||
|
};
|
||||||
|
|
||||||
|
private static final FloatBuffer matrix = BufferUtils.createFloatBuffer(16);
|
||||||
|
private static final FloatBuffer finalMatrix = BufferUtils.createFloatBuffer(16);
|
||||||
|
|
||||||
|
private static final float[][] tempMatrix = new float[4][4];
|
||||||
|
private static final float[] in = new float[4];
|
||||||
|
private static final float[] out = new float[4];
|
||||||
|
|
||||||
|
private static final float[] forward = new float[3];
|
||||||
|
private static final float[] side = new float[3];
|
||||||
|
private static final float[] up = new float[3];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make matrix an identity matrix
|
* Make matrix an identity matrix
|
||||||
*/
|
*/
|
||||||
private static void __gluMakeIdentityf(FloatBuffer m) {
|
private static void __gluMakeIdentityf(FloatBuffer m) {
|
||||||
m.put(0 + 4 * 0, 1);
|
int oldPos = m.position();
|
||||||
m.put(0 + 4 * 1, 0);
|
m.put(IDENTITY_MATRIX);
|
||||||
m.put(0 + 4 * 2, 0);
|
m.position(oldPos);
|
||||||
m.put(0 + 4 * 3, 0);
|
|
||||||
m.put(1 + 4 * 0, 0);
|
|
||||||
m.put(1 + 4 * 1, 1);
|
|
||||||
m.put(1 + 4 * 2, 0);
|
|
||||||
m.put(1 + 4 * 3, 0);
|
|
||||||
m.put(2 + 4 * 0, 0);
|
|
||||||
m.put(2 + 4 * 1, 0);
|
|
||||||
m.put(2 + 4 * 2, 1);
|
|
||||||
m.put(2 + 4 * 3, 0);
|
|
||||||
m.put(3 + 4 * 0, 0);
|
|
||||||
m.put(3 + 4 * 1, 0);
|
|
||||||
m.put(3 + 4 * 2, 0);
|
|
||||||
m.put(3 + 4 * 3, 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method __gluMultMatrixVecf
|
* Method __gluMultMatrixVecf
|
||||||
|
*
|
||||||
* @param finalMatrix
|
* @param finalMatrix
|
||||||
* @param in
|
* @param in
|
||||||
* @param out
|
* @param out
|
||||||
*/
|
*/
|
||||||
private static void __gluMultMatrixVecf(FloatBuffer finalMatrix, float[] in, float[] out) {
|
private static void __gluMultMatrixVecf(FloatBuffer finalMatrix, float[] in, float[] out) {
|
||||||
|
for ( int i = 0; i < 4; i++ ) {
|
||||||
for (int i=0; i<4; i++) {
|
out[i] = in[0] * finalMatrix.get((0 * 4) + i)
|
||||||
out[i] =
|
+ in[1] * finalMatrix.get((1 * 4) + i)
|
||||||
in[0] * finalMatrix.get(0*4+i) +
|
+ in[2] * finalMatrix.get((2 * 4) + i)
|
||||||
in[1] * finalMatrix.get(1*4+i) +
|
+ in[3] * finalMatrix.get((3 * 4) + i);
|
||||||
in[2] * finalMatrix.get(2*4+i) +
|
|
||||||
in[3] * finalMatrix.get(3*4+i);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param src
|
* @param src
|
||||||
* @param inverse
|
* @param inverse
|
||||||
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private static boolean __gluInvertMatrixf(FloatBuffer src, FloatBuffer inverse) {
|
private static boolean __gluInvertMatrixf(FloatBuffer src, FloatBuffer inverse) {
|
||||||
int i, j, k, swap;
|
int i, j, k, swap;
|
||||||
float t;
|
float t;
|
||||||
float[][] temp = new float[4][4];
|
float[][] temp = Project.tempMatrix;
|
||||||
|
|
||||||
for (i=0; i<4; i++) {
|
for ( i = 0; i < 4; i++ ) {
|
||||||
for (j=0; j<4; j++) {
|
for ( j = 0; j < 4; j++ ) {
|
||||||
temp[i][j] = src.get(i*4+j);
|
temp[i][j] = src.get((i << 2) + j);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
__gluMakeIdentityf(inverse);
|
__gluMakeIdentityf(inverse);
|
||||||
|
|
||||||
for (i = 0; i < 4; i++) {
|
for ( i = 0; i < 4; i++ ) {
|
||||||
/*
|
/*
|
||||||
** Look for largest element in column
|
** Look for largest element in column
|
||||||
*/
|
*/
|
||||||
swap = i;
|
swap = i;
|
||||||
for (j = i + 1; j < 4; j++) {
|
for ( j = i + 1; j < 4; j++ ) {
|
||||||
/*if (fabs(temp[j][i]) > fabs(temp[i][i])) {
|
/*if (fabs(temp[j][i]) > fabs(temp[i][i])) {
|
||||||
swap = j;
|
swap = j;
|
||||||
}*/
|
}*/
|
||||||
if (Math.abs(temp[j][i]) > Math.abs(temp[i][i])) {
|
if ( Math.abs(temp[j][i]) > Math.abs(temp[i][i]) ) {
|
||||||
swap = j;
|
swap = j;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (swap != i) {
|
if ( swap != i ) {
|
||||||
/*
|
/*
|
||||||
** Swap rows.
|
** Swap rows.
|
||||||
*/
|
*/
|
||||||
for (k = 0; k < 4; k++) {
|
for ( k = 0; k < 4; k++ ) {
|
||||||
t = temp[i][k];
|
t = temp[i][k];
|
||||||
temp[i][k] = temp[swap][k];
|
temp[i][k] = temp[swap][k];
|
||||||
temp[swap][k] = t;
|
temp[swap][k] = t;
|
||||||
|
|
||||||
t = inverse.get(i*4+k);
|
t = inverse.get((i << 2) + k);
|
||||||
inverse.put(i*4+k, inverse.get(swap*4+k));
|
inverse.put((i << 2) + k, inverse.get((swap << 2) + k));
|
||||||
inverse.put(swap*4+k,t);
|
inverse.put((swap << 2) + k, t);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (temp[i][i] == 0) {
|
if ( temp[i][i] == 0 ) {
|
||||||
/*
|
/*
|
||||||
** No non-zero pivot. The matrix is singular, which shouldn't
|
** No non-zero pivot. The matrix is singular, which shouldn't
|
||||||
** happen. This means the user gave us a bad matrix.
|
** happen. This means the user gave us a bad matrix.
|
||||||
|
@ -111,16 +115,16 @@ public class Project extends Util implements GLUConstants {
|
||||||
}
|
}
|
||||||
|
|
||||||
t = temp[i][i];
|
t = temp[i][i];
|
||||||
for (k = 0; k < 4; k++) {
|
for ( k = 0; k < 4; k++ ) {
|
||||||
temp[i][k] /= t;
|
temp[i][k] /= t;
|
||||||
inverse.put(i*4+k, inverse.get(i*4+k) / t);
|
inverse.put((i << 2) + k, inverse.get((i << 2) + k) / t);
|
||||||
}
|
}
|
||||||
for (j = 0; j < 4; j++) {
|
for ( j = 0; j < 4; j++ ) {
|
||||||
if (j != i) {
|
if ( j != i ) {
|
||||||
t = temp[j][i];
|
t = temp[j][i];
|
||||||
for (k = 0; k < 4; k++) {
|
for ( k = 0; k < 4; k++ ) {
|
||||||
temp[j][k] -= temp[i][k]*t;
|
temp[j][k] -= temp[i][k] * t;
|
||||||
inverse.put(j*4+k, inverse.get(j*4+k) - inverse.get(i*4+k)*t);
|
inverse.put((j << 2) + k, inverse.get((j << 2) + k) - inverse.get((i << 2) + k) * t);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -134,20 +138,20 @@ public class Project extends Util implements GLUConstants {
|
||||||
* @param r
|
* @param r
|
||||||
*/
|
*/
|
||||||
private static void __gluMultMatricesf(FloatBuffer a, FloatBuffer b, FloatBuffer r) {
|
private static void __gluMultMatricesf(FloatBuffer a, FloatBuffer b, FloatBuffer r) {
|
||||||
|
for ( int i = 0; i < 4; i++ ) {
|
||||||
for (int i = 0; i < 4; i++) {
|
for ( int j = 0; j < 4; j++ ) {
|
||||||
for (int j = 0; j < 4; j++) {
|
r.put((i << 2) + j,
|
||||||
r.put(i*4+j,
|
a.get((i << 2) + 0) * b.get((0 * 4) + j) +
|
||||||
a.get(i*4+0)*b.get(0*4+j) +
|
a.get((i << 2) + 1) * b.get((1 * 4) + j) +
|
||||||
a.get(i*4+1)*b.get(1*4+j) +
|
a.get((i << 2) + 2) * b.get((2 * 4) + j) +
|
||||||
a.get(i*4+2)*b.get(2*4+j) +
|
a.get((i << 2) + 3) * b.get((3 * 4) + j));
|
||||||
a.get(i*4+3)*b.get(3*4+j));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method gluPerspective.
|
* Method gluPerspective.
|
||||||
|
*
|
||||||
* @param fovy
|
* @param fovy
|
||||||
* @param aspect
|
* @param aspect
|
||||||
* @param zNear
|
* @param zNear
|
||||||
|
@ -158,17 +162,19 @@ public class Project extends Util implements GLUConstants {
|
||||||
float radians = fovy / 2 * PI / 180;
|
float radians = fovy / 2 * PI / 180;
|
||||||
|
|
||||||
deltaZ = zFar - zNear;
|
deltaZ = zFar - zNear;
|
||||||
sine = (float) Math.sin(radians);
|
sine = (float)Math.sin(radians);
|
||||||
if ((deltaZ == 0) || (sine == 0) || (aspect == 0)) {
|
|
||||||
|
if ( (deltaZ == 0) || (sine == 0) || (aspect == 0) ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
cotangent = (float) Math.cos(radians) / sine;
|
|
||||||
|
cotangent = (float)Math.cos(radians) / sine;
|
||||||
|
|
||||||
__gluMakeIdentityf(matrix);
|
__gluMakeIdentityf(matrix);
|
||||||
|
|
||||||
matrix.put(0 * 4 + 0, cotangent / aspect);
|
matrix.put(0 * 4 + 0, cotangent / aspect);
|
||||||
matrix.put(1 * 4 + 1, cotangent);
|
matrix.put(1 * 4 + 1, cotangent);
|
||||||
matrix.put(2 * 4 + 2, - (zFar + zNear) / deltaZ);
|
matrix.put(2 * 4 + 2, -(zFar + zNear) / deltaZ);
|
||||||
matrix.put(2 * 4 + 3, -1);
|
matrix.put(2 * 4 + 3, -1);
|
||||||
matrix.put(3 * 4 + 2, -2 * zNear * zFar / deltaZ);
|
matrix.put(3 * 4 + 2, -2 * zNear * zFar / deltaZ);
|
||||||
matrix.put(3 * 4 + 3, 0);
|
matrix.put(3 * 4 + 3, 0);
|
||||||
|
@ -178,6 +184,7 @@ public class Project extends Util implements GLUConstants {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method gluLookAt
|
* Method gluLookAt
|
||||||
|
*
|
||||||
* @param eyex
|
* @param eyex
|
||||||
* @param eyey
|
* @param eyey
|
||||||
* @param eyez
|
* @param eyez
|
||||||
|
@ -188,20 +195,12 @@ public class Project extends Util implements GLUConstants {
|
||||||
* @param upy
|
* @param upy
|
||||||
* @param upz
|
* @param upz
|
||||||
*/
|
*/
|
||||||
public static void gluLookAt(
|
public static void gluLookAt(float eyex, float eyey, float eyez,
|
||||||
float eyex,
|
float centerx, float centery, float centerz,
|
||||||
float eyey,
|
float upx, float upy, float upz) {
|
||||||
float eyez,
|
float[] forward = Project.forward;
|
||||||
float centerx,
|
float[] side = Project.side;
|
||||||
float centery,
|
float[] up = Project.up;
|
||||||
float centerz,
|
|
||||||
float upx,
|
|
||||||
float upy,
|
|
||||||
float upz) {
|
|
||||||
|
|
||||||
float[] forward = new float[3];
|
|
||||||
float[] side = new float[3];
|
|
||||||
float[] up = new float[3];
|
|
||||||
|
|
||||||
forward[0] = centerx - eyex;
|
forward[0] = centerx - eyex;
|
||||||
forward[1] = centery - eyey;
|
forward[1] = centery - eyey;
|
||||||
|
@ -239,6 +238,7 @@ public class Project extends Util implements GLUConstants {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method gluProject
|
* Method gluProject
|
||||||
|
*
|
||||||
* @param objx
|
* @param objx
|
||||||
* @param objy
|
* @param objy
|
||||||
* @param objz
|
* @param objz
|
||||||
|
@ -246,40 +246,40 @@ public class Project extends Util implements GLUConstants {
|
||||||
* @param projMatrix
|
* @param projMatrix
|
||||||
* @param viewport
|
* @param viewport
|
||||||
* @param win_pos
|
* @param win_pos
|
||||||
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static boolean gluProject(float objx, float objy, float objz,
|
public static boolean gluProject(float objx, float objy, float objz,
|
||||||
FloatBuffer modelMatrix,
|
FloatBuffer modelMatrix,
|
||||||
FloatBuffer projMatrix,
|
FloatBuffer projMatrix,
|
||||||
IntBuffer viewport,
|
IntBuffer viewport,
|
||||||
FloatBuffer win_pos)
|
FloatBuffer win_pos) {
|
||||||
{
|
float[] in = Project.in;
|
||||||
float[] in = new float[4];
|
float[] out = Project.out;
|
||||||
float[] out = new float[4];
|
|
||||||
|
in[0] = objx;
|
||||||
|
in[1] = objy;
|
||||||
|
in[2] = objz;
|
||||||
|
in[3] = 1.0f;
|
||||||
|
|
||||||
in[0]=objx;
|
|
||||||
in[1]=objy;
|
|
||||||
in[2]=objz;
|
|
||||||
in[3]=1.0f;
|
|
||||||
__gluMultMatrixVecf(modelMatrix, in, out);
|
__gluMultMatrixVecf(modelMatrix, in, out);
|
||||||
__gluMultMatrixVecf(projMatrix, out, in);
|
__gluMultMatrixVecf(projMatrix, out, in);
|
||||||
if (in[3] == 0.0) return false;
|
|
||||||
in[0] /= in[3];
|
|
||||||
in[1] /= in[3];
|
|
||||||
in[2] /= in[3];
|
|
||||||
// Map x, y and z to range 0-1
|
|
||||||
in[0] = in[0] * 0.5f + 0.5f;
|
|
||||||
in[1] = in[1] * 0.5f + 0.5f;
|
|
||||||
in[2] = in[2] * 0.5f + 0.5f;
|
|
||||||
|
|
||||||
// Map x,y to viewport
|
if ( in[3] == 0.0 )
|
||||||
in[0] = in[0] * viewport.get(2) + viewport.get(0);
|
return false;
|
||||||
in[1] = in[1] * viewport.get(3) + viewport.get(1);
|
|
||||||
|
in[3] = (1.0f / in[3]) * 0.5f;
|
||||||
|
|
||||||
|
// Map x, y and z to range 0-1
|
||||||
|
in[0] = in[0] * in[3] + 0.5f;
|
||||||
|
in[1] = in[1] * in[3] + 0.5f;
|
||||||
|
in[2] = in[2] * in[3] + 0.5f;
|
||||||
|
|
||||||
int pos = win_pos.position();
|
int pos = win_pos.position();
|
||||||
|
|
||||||
win_pos.put(pos++, in[0]);
|
// Map x,y to viewport
|
||||||
win_pos.put(pos++, in[1]);
|
win_pos.put(pos++, in[0] * viewport.get(2) + viewport.get(0));
|
||||||
|
win_pos.put(pos++, in[1] * viewport.get(3) + viewport.get(1));
|
||||||
win_pos.put(pos, in[2]);
|
win_pos.put(pos, in[2]);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -287,6 +287,7 @@ public class Project extends Util implements GLUConstants {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method gluUnproject
|
* Method gluUnproject
|
||||||
|
*
|
||||||
* @param winx
|
* @param winx
|
||||||
* @param winy
|
* @param winy
|
||||||
* @param winz
|
* @param winz
|
||||||
|
@ -294,24 +295,26 @@ public class Project extends Util implements GLUConstants {
|
||||||
* @param projMatrix
|
* @param projMatrix
|
||||||
* @param viewport
|
* @param viewport
|
||||||
* @param obj_pos
|
* @param obj_pos
|
||||||
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static boolean gluUnProject(float winx, float winy, float winz,
|
public static boolean gluUnProject(float winx, float winy, float winz,
|
||||||
FloatBuffer modelMatrix,
|
FloatBuffer modelMatrix,
|
||||||
FloatBuffer projMatrix,
|
FloatBuffer projMatrix,
|
||||||
IntBuffer viewport,
|
IntBuffer viewport,
|
||||||
FloatBuffer obj_pos)
|
FloatBuffer obj_pos) {
|
||||||
{
|
float[] in = Project.in;
|
||||||
float[] in = new float[4];
|
float[] out = Project.out;
|
||||||
float[] out = new float[4];
|
|
||||||
|
|
||||||
__gluMultMatricesf(modelMatrix, projMatrix, finalMatrix);
|
__gluMultMatricesf(modelMatrix, projMatrix, finalMatrix);
|
||||||
if (!__gluInvertMatrixf(finalMatrix, finalMatrix)) return false;
|
|
||||||
|
|
||||||
in[0]=winx;
|
if ( !__gluInvertMatrixf(finalMatrix, finalMatrix) )
|
||||||
in[1]=winy;
|
return false;
|
||||||
in[2]=winz;
|
|
||||||
in[3]=1.0f;
|
in[0] = winx;
|
||||||
|
in[1] = winy;
|
||||||
|
in[2] = winz;
|
||||||
|
in[3] = 1.0f;
|
||||||
|
|
||||||
// Map x and y from window coordinates
|
// Map x and y from window coordinates
|
||||||
in[0] = (in[0] - viewport.get(0)) / viewport.get(2);
|
in[0] = (in[0] - viewport.get(0)) / viewport.get(2);
|
||||||
|
@ -323,43 +326,39 @@ public class Project extends Util implements GLUConstants {
|
||||||
in[2] = in[2] * 2 - 1;
|
in[2] = in[2] * 2 - 1;
|
||||||
|
|
||||||
__gluMultMatrixVecf(finalMatrix, in, out);
|
__gluMultMatrixVecf(finalMatrix, in, out);
|
||||||
if (out[3] == 0.0) return false;
|
|
||||||
out[0] /= out[3];
|
if ( out[3] == 0.0 )
|
||||||
out[1] /= out[3];
|
return false;
|
||||||
out[2] /= out[3];
|
|
||||||
|
out[3] = 1.0f / out[3];
|
||||||
|
|
||||||
int pos = obj_pos.position();
|
int pos = obj_pos.position();
|
||||||
|
|
||||||
obj_pos.put(pos++, out[0]);
|
obj_pos.put(pos++, out[0] * out[3]);
|
||||||
obj_pos.put(pos++, out[1]);
|
obj_pos.put(pos++, out[1] * out[3]);
|
||||||
obj_pos.put(pos, out[2]);
|
obj_pos.put(pos, out[2] * out[3]);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method gluPickMatrix
|
* Method gluPickMatrix
|
||||||
|
*
|
||||||
* @param x
|
* @param x
|
||||||
* @param y
|
* @param y
|
||||||
* @param deltax
|
* @param deltaX
|
||||||
* @param deltay
|
* @param deltaY
|
||||||
* @param viewport
|
* @param viewport
|
||||||
*/
|
*/
|
||||||
public static void gluPickMatrix(
|
public static void gluPickMatrix(float x, float y, float deltaX, float deltaY, IntBuffer viewport) {
|
||||||
float x,
|
if ( deltaX <= 0 || deltaY <= 0 ) {
|
||||||
float y,
|
|
||||||
float deltax,
|
|
||||||
float deltay,
|
|
||||||
IntBuffer viewport) {
|
|
||||||
if (deltax <= 0 || deltay <= 0) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Translate and scale the picked region to the entire window */
|
/* Translate and scale the picked region to the entire window */
|
||||||
GL11.glTranslatef(
|
GL11.glTranslatef((viewport.get(2) - 2 * (x - viewport.get(0))) / deltaX,
|
||||||
(viewport.get(2) - 2 * (x - viewport.get(0))) / deltax,
|
(viewport.get(3) - 2 * (y - viewport.get(1))) / deltaY,
|
||||||
(viewport.get(3) - 2 * (y - viewport.get(1))) / deltay,
|
0);
|
||||||
0);
|
GL11.glScalef(viewport.get(2) / deltaX, viewport.get(3) / deltaY, 1.0f);
|
||||||
GL11.glScalef(viewport.get(2) / deltax, viewport.get(3) / deltay, 1.0f);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,29 +1,32 @@
|
||||||
package org.lwjgl.opengl.glu;
|
package org.lwjgl.opengl.glu;
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import org.lwjgl.BufferUtils;
|
||||||
import java.nio.ByteOrder;
|
|
||||||
import java.nio.FloatBuffer;
|
|
||||||
import java.nio.IntBuffer;
|
|
||||||
|
|
||||||
import org.lwjgl.opengl.GL11;
|
import org.lwjgl.opengl.GL11;
|
||||||
import org.lwjgl.opengl.GL12;
|
import org.lwjgl.opengl.GL12;
|
||||||
|
|
||||||
|
import java.nio.IntBuffer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Util.java
|
* Util.java
|
||||||
*
|
* <p/>
|
||||||
*
|
* <p/>
|
||||||
* Created 7-jan-2004
|
* Created 7-jan-2004
|
||||||
|
*
|
||||||
* @author Erik Duijs
|
* @author Erik Duijs
|
||||||
*/
|
*/
|
||||||
public class Util {
|
public class Util {
|
||||||
|
|
||||||
/** temp IntBuffer of one for getting an int from some GL functions */
|
/**
|
||||||
private static IntBuffer scratch = createIntBuffer(256);
|
* temp IntBuffer of one for getting an int from some GL functions
|
||||||
|
*/
|
||||||
|
private static IntBuffer scratch = BufferUtils.createIntBuffer(16);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return ceiling of integer division
|
* Return ceiling of integer division
|
||||||
|
*
|
||||||
* @param a
|
* @param a
|
||||||
* @param b
|
* @param b
|
||||||
|
*
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
protected static int ceil(int a, int b) {
|
protected static int ceil(int a, int b) {
|
||||||
|
@ -32,25 +35,30 @@ public class Util {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Normalize vector
|
* Normalize vector
|
||||||
|
*
|
||||||
* @param v
|
* @param v
|
||||||
|
*
|
||||||
* @return float[]
|
* @return float[]
|
||||||
*/
|
*/
|
||||||
protected static float[] normalize(float[] v) {
|
protected static float[] normalize(float[] v) {
|
||||||
float r;
|
float r;
|
||||||
|
|
||||||
r = (float) Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
|
r = (float)Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
|
||||||
if (r == 0.0)
|
if ( r == 0.0 )
|
||||||
return v;
|
return v;
|
||||||
|
|
||||||
v[0] /= r;
|
r = 1.0f / r;
|
||||||
v[1] /= r;
|
|
||||||
v[2] /= r;
|
v[0] *= r;
|
||||||
|
v[1] *= r;
|
||||||
|
v[2] *= r;
|
||||||
|
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculate cross-product
|
* Calculate cross-product
|
||||||
|
*
|
||||||
* @param v1
|
* @param v1
|
||||||
* @param v2
|
* @param v2
|
||||||
* @param result
|
* @param result
|
||||||
|
@ -63,28 +71,30 @@ public class Util {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method compPerPix.
|
* Method compPerPix.
|
||||||
|
*
|
||||||
* @param format
|
* @param format
|
||||||
|
*
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
protected static int compPerPix(int format) {
|
protected static int compPerPix(int format) {
|
||||||
/* Determine number of components per pixel */
|
/* Determine number of components per pixel */
|
||||||
switch (format) {
|
switch ( format ) {
|
||||||
case GL11.GL_COLOR_INDEX :
|
case GL11.GL_COLOR_INDEX:
|
||||||
case GL11.GL_STENCIL_INDEX :
|
case GL11.GL_STENCIL_INDEX:
|
||||||
case GL11.GL_DEPTH_COMPONENT :
|
case GL11.GL_DEPTH_COMPONENT:
|
||||||
case GL11.GL_RED :
|
case GL11.GL_RED:
|
||||||
case GL11.GL_GREEN :
|
case GL11.GL_GREEN:
|
||||||
case GL11.GL_BLUE :
|
case GL11.GL_BLUE:
|
||||||
case GL11.GL_ALPHA :
|
case GL11.GL_ALPHA:
|
||||||
case GL11.GL_LUMINANCE :
|
case GL11.GL_LUMINANCE:
|
||||||
return 1;
|
return 1;
|
||||||
case GL11.GL_LUMINANCE_ALPHA :
|
case GL11.GL_LUMINANCE_ALPHA:
|
||||||
return 2;
|
return 2;
|
||||||
case GL11.GL_RGB :
|
case GL11.GL_RGB:
|
||||||
case GL12.GL_BGR :
|
case GL12.GL_BGR:
|
||||||
return 3;
|
return 3;
|
||||||
case GL11.GL_RGBA :
|
case GL11.GL_RGBA:
|
||||||
case GL12.GL_BGRA :
|
case GL12.GL_BGRA:
|
||||||
return 4;
|
return 4;
|
||||||
default :
|
default :
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -93,11 +103,11 @@ public class Util {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method nearestPower.
|
* Method nearestPower.
|
||||||
*
|
* <p/>
|
||||||
* Compute the nearest power of 2 number. This algorithm is a little
|
* Compute the nearest power of 2 number. This algorithm is a little strange, but it works quite well.
|
||||||
* strange, but it works quite well.
|
|
||||||
*
|
*
|
||||||
* @param value
|
* @param value
|
||||||
|
*
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
protected static int nearestPower(int value) {
|
protected static int nearestPower(int value) {
|
||||||
|
@ -106,14 +116,14 @@ public class Util {
|
||||||
i = 1;
|
i = 1;
|
||||||
|
|
||||||
/* Error! */
|
/* Error! */
|
||||||
if (value == 0)
|
if ( value == 0 )
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
for (;;) {
|
for ( ; ; ) {
|
||||||
if (value == 1) {
|
if ( value == 1 ) {
|
||||||
return i;
|
return i;
|
||||||
} else if (value == 3) {
|
} else if ( value == 3 ) {
|
||||||
return i * 4;
|
return i << 2;
|
||||||
}
|
}
|
||||||
value >>= 1;
|
value >>= 1;
|
||||||
i <<= 1;
|
i <<= 1;
|
||||||
|
@ -122,62 +132,64 @@ public class Util {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method bytesPerPixel.
|
* Method bytesPerPixel.
|
||||||
|
*
|
||||||
* @param format
|
* @param format
|
||||||
* @param type
|
* @param type
|
||||||
|
*
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
protected static int bytesPerPixel(int format, int type) {
|
protected static int bytesPerPixel(int format, int type) {
|
||||||
int n, m;
|
int n, m;
|
||||||
|
|
||||||
switch (format) {
|
switch ( format ) {
|
||||||
case GL11.GL_COLOR_INDEX :
|
case GL11.GL_COLOR_INDEX:
|
||||||
case GL11.GL_STENCIL_INDEX :
|
case GL11.GL_STENCIL_INDEX:
|
||||||
case GL11.GL_DEPTH_COMPONENT :
|
case GL11.GL_DEPTH_COMPONENT:
|
||||||
case GL11.GL_RED :
|
case GL11.GL_RED:
|
||||||
case GL11.GL_GREEN :
|
case GL11.GL_GREEN:
|
||||||
case GL11.GL_BLUE :
|
case GL11.GL_BLUE:
|
||||||
case GL11.GL_ALPHA :
|
case GL11.GL_ALPHA:
|
||||||
case GL11.GL_LUMINANCE :
|
case GL11.GL_LUMINANCE:
|
||||||
n = 1;
|
n = 1;
|
||||||
break;
|
break;
|
||||||
case GL11.GL_LUMINANCE_ALPHA :
|
case GL11.GL_LUMINANCE_ALPHA:
|
||||||
n = 2;
|
n = 2;
|
||||||
break;
|
break;
|
||||||
case GL11.GL_RGB :
|
case GL11.GL_RGB:
|
||||||
case GL12.GL_BGR :
|
case GL12.GL_BGR:
|
||||||
n = 3;
|
n = 3;
|
||||||
break;
|
break;
|
||||||
case GL11.GL_RGBA :
|
case GL11.GL_RGBA:
|
||||||
case GL12.GL_BGRA :
|
case GL12.GL_BGRA:
|
||||||
n = 4;
|
n = 4;
|
||||||
break;
|
break;
|
||||||
default :
|
default :
|
||||||
n = 0;
|
n = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (type) {
|
switch ( type ) {
|
||||||
case GL11.GL_UNSIGNED_BYTE :
|
case GL11.GL_UNSIGNED_BYTE:
|
||||||
m = 1;
|
m = 1;
|
||||||
break;
|
break;
|
||||||
case GL11.GL_BYTE :
|
case GL11.GL_BYTE:
|
||||||
m = 1;
|
m = 1;
|
||||||
break;
|
break;
|
||||||
case GL11.GL_BITMAP :
|
case GL11.GL_BITMAP:
|
||||||
m = 1;
|
m = 1;
|
||||||
break;
|
break;
|
||||||
case GL11.GL_UNSIGNED_SHORT :
|
case GL11.GL_UNSIGNED_SHORT:
|
||||||
m = 2;
|
m = 2;
|
||||||
break;
|
break;
|
||||||
case GL11.GL_SHORT :
|
case GL11.GL_SHORT:
|
||||||
m = 2;
|
m = 2;
|
||||||
break;
|
break;
|
||||||
case GL11.GL_UNSIGNED_INT :
|
case GL11.GL_UNSIGNED_INT:
|
||||||
m = 4;
|
m = 4;
|
||||||
break;
|
break;
|
||||||
case GL11.GL_INT :
|
case GL11.GL_INT:
|
||||||
m = 4;
|
m = 4;
|
||||||
break;
|
break;
|
||||||
case GL11.GL_FLOAT :
|
case GL11.GL_FLOAT:
|
||||||
m = 4;
|
m = 4;
|
||||||
break;
|
break;
|
||||||
default :
|
default :
|
||||||
|
@ -188,34 +200,10 @@ public class Util {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a FloatBuffer of specified size.
|
* Convenience method for returning an int, rather than getting it out of a buffer yourself.
|
||||||
* @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
|
* @param what
|
||||||
|
*
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
protected static int glGetIntegerv(int what) {
|
protected static int glGetIntegerv(int what) {
|
||||||
|
|
Loading…
Reference in New Issue