package GeomUtils;
import org.lwjgl.opengl.*;
import java.nio.*;
import Math.*;
public class Torus{
private static final int SLICE = 64,
STACK = 64;
private static float OUTER_RADIUS = 1.0f,
INNER_RADIUS = 0.5f;
private static int numberOfIndices = 0,
numberElements;
private static String name = "";
private static IntBuffer indices = null;
private static FloatBuffer vertexData = null,
normalData = null;
static{
Tuple3f torusVertices[] = new Tuple3f[(SLICE + 1)],
torusNormals [] = new Tuple3f[(SLICE + 1)],
temporary = new Tuple3f();
float x = 0,
z = 0,
sliceStep = 2f*FastTrig.PI/SLICE,
stackStep = 2f*FastTrig.PI/STACK,
sliceAngle = 0,
stackAngle = 0;
vertexData = createFloatBuffer((SLICE+1)*(STACK+1));
normalData = createFloatBuffer((SLICE+1)*(STACK+1));
numberOfIndices = SLICE*STACK*6;
indices = createIntBuffer(numberOfIndices);
for(int i = 0; i<(SLICE + 1); i++){
if(i == SLICE )
sliceAngle = 0;
x = FastTrig.sin(sliceAngle);
z = FastTrig.cos(sliceAngle);
torusVertices[i] = new Tuple3f(OUTER_RADIUS - INNER_RADIUS*x,
0 ,
INNER_RADIUS*z );
vertexData.put(torusVertices[i].x);
vertexData.put(torusVertices[i].y);
vertexData.put(torusVertices[i].z);
torusNormals[i] = new Tuple3f(-x,0,z);
normalData.put(-x);
normalData.put( 0);
normalData.put( z);
sliceAngle += sliceStep;
}
for(int a = 1, p = SLICE + 1; a < STACK + 1; a++){
stackAngle += stackStep;
if(a == STACK)
stackAngle = 0;
for(int i = 0; i< SLICE +1 ; i++, p++){
rotateTupleZ(temporary, torusVertices[i], stackAngle);
vertexData.put(temporary.x);
vertexData.put(temporary.y);
vertexData.put(temporary.z);
rotateTupleZ(temporary, torusNormals[i] , stackAngle);
normalData.put(temporary.x);
normalData.put(temporary.y);
normalData.put(temporary.z);
}
}
for(int j=0; j<STACK; j++)
for(int i=0; i<SLICE; i++){
indices.put(((j*SLICE+i)*2)*3+0, j*(SLICE+1)+i);
indices.put(((j*SLICE+i)*2)*3+1, (j+1)*(SLICE+1)+i);
indices.put(((j*SLICE+i)*2)*3+2, j*(SLICE+1)+i+1);
indices.put(((j*SLICE+i)*2+1)*3+0, j*(SLICE+1)+i+1);
indices.put(((j*SLICE+i)*2+1)*3+1, (j+1)*(SLICE+1)+i);
indices.put(((j*SLICE+i)*2+1)*3+2, (j+1)*(SLICE+1)+i+1);
}
indices.flip();
vertexData.flip();
normalData.flip();
}
public static final void drawTorus(int textureID,
float rotx, float roty ,
boolean reflectionMap){
int generationMode = reflectionMap ? GL13.GL_NORMAL_MAP : GL13.GL_REFLECTION_MAP;
GL11.glTexGeni(GL11.GL_S, GL11.GL_TEXTURE_GEN_MODE, generationMode);
GL11.glTexGeni(GL11.GL_T, GL11.GL_TEXTURE_GEN_MODE, generationMode);
GL11.glTexGeni(GL11.GL_R, GL11.GL_TEXTURE_GEN_MODE, generationMode);
GL11.glEnable(GL11.GL_TEXTURE_GEN_S);
GL11.glEnable(GL11.GL_TEXTURE_GEN_T);
GL11.glEnable(GL11.GL_TEXTURE_GEN_R);
GL11.glBindTexture(GL13.GL_TEXTURE_CUBE_MAP, textureID);
GL11.glEnable(GL13.GL_TEXTURE_CUBE_MAP);
GL11.glColor3f(1.0f,1.0f,10f);
GL11.glPushMatrix();
GL11.glTranslatef(0.0f, 0.0f, -2.5f);
GL11.glRotatef(rotx, 1, 0, 0);
GL11.glRotatef(roty, 0, 1, 0);
GL11.glVertexPointer(3, GL11.GL_FLOAT, vertexData);
GL11.glNormalPointer( GL11.GL_FLOAT, normalData);
GL11.glDrawElements (GL11.GL_TRIANGLES, indices);
GL11.glDisable(GL13.GL_TEXTURE_CUBE_MAP);
GL11.glDisable(GL11.GL_TEXTURE_GEN_S);
GL11.glDisable(GL11.GL_TEXTURE_GEN_T);
GL11.glDisable(GL11.GL_TEXTURE_GEN_R);
GL11.glPopMatrix();
}
private static void rotateTupleZ(Tuple3f t1, Tuple3f t2, float angle){
float sin = FastTrig.sin(angle),
cos = FastTrig.cos(angle);
t1.set( t2.x*cos - t2.y*sin, t2.x*sin + t2.y*cos, t2.z);
}
private static FloatBuffer createFloatBuffer(int numElements){
return ByteBuffer.allocateDirect(4*3*numElements).order(
ByteOrder.nativeOrder()).asFloatBuffer();
}
private static IntBuffer createIntBuffer(int numElements){
return ByteBuffer.allocateDirect(4*numElements).order(
ByteOrder.nativeOrder()).asIntBuffer();
}
}
^
Nothing gets drawn to the screen even though I’m positive all the right coordinates/normals are there (tried breaking it into pieces)
The following code I used in JoGL works perfectly though
package GeomUtils;
import net.java.games.jogl.GL;
import java.nio.FloatBuffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import Math.*;
public class Torus{
private static final int SLICE = 64,
STACK = 64;
private static float OUTER_RADIUS = 1.0f,
INNER_RADIUS = 0.5f;
private static int indices[] = null;
private static FloatBuffer vertexData = null,
normalData = null;
static{
Tuple3f torusVertices[] = new Tuple3f[(SLICE + 1)],
torusNormals [] = new Tuple3f[(SLICE + 1)],
temporary = new Tuple3f();
float x = 0,
z = 0,
sliceStep = 2f*FastTrig.PI/SLICE,
stackStep = 2f*FastTrig.PI/STACK,
sliceAngle = 0,
stackAngle = 0;
vertexData = createFloatBuffer((SLICE+1)*(STACK+1));
normalData = createFloatBuffer((SLICE+1)*(STACK+1));
indices = new int[SLICE*STACK*6];
for(int i = 0; i<(SLICE + 1); i++){
if(i == SLICE )
sliceAngle = 0;
x = FastTrig.sin(sliceAngle);
z = FastTrig.cos(sliceAngle);
torusVertices[i] = new Tuple3f(OUTER_RADIUS - INNER_RADIUS*x,
0 ,
INNER_RADIUS*z );
vertexData.put(torusVertices[i].x);
vertexData.put(torusVertices[i].y);
vertexData.put(torusVertices[i].z);
torusNormals[i] = new Tuple3f(-x,0,z);
normalData.put(-x);
normalData.put( 0);
normalData.put( z);
sliceAngle += sliceStep;
}
for(int a = 1, p = SLICE + 1; a < STACK + 1; a++){
stackAngle += stackStep;
if(a == STACK)
stackAngle = 0;
for(int i = 0; i< SLICE +1 ; i++, p++){
rotateTupleZ(temporary, torusVertices[i], stackAngle);
vertexData.put(temporary.x);
vertexData.put(temporary.y);
vertexData.put(temporary.z);
rotateTupleZ(temporary, torusNormals[i] , stackAngle);
normalData.put(temporary.x);
normalData.put(temporary.y);
normalData.put(temporary.z);
}
}
for(int j=0; j<STACK; j++)
for(int i=0; i<SLICE; i++){
indices[((j*SLICE+i)*2)*3+0] = j*(SLICE+1)+i;
indices[((j*SLICE+i)*2)*3+1] = (j+1)*(SLICE+1)+i;
indices[((j*SLICE+i)*2)*3+2] = j*(SLICE+1)+i+1;
indices[((j*SLICE+i)*2+1)*3+0]= j*(SLICE+1)+i+1;
indices[((j*SLICE+i)*2+1)*3+1]= (j+1)*(SLICE+1)+i;
indices[((j*SLICE+i)*2+1)*3+2]= (j+1)*(SLICE+1)+i+1;
}
}
public static final void drawTorus(GL gl , int textureID,
float rotx, float roty ,
boolean reflectionMap){
int generationMode = reflectionMap ? GL.GL_NORMAL_MAP_ARB : GL.GL_REFLECTION_MAP_ARB;
gl.glColor3f(1.0f, 1.0f, 1.0f);
gl.glTexGeni(GL.GL_S, GL.GL_TEXTURE_GEN_MODE, generationMode);
gl.glTexGeni(GL.GL_T, GL.GL_TEXTURE_GEN_MODE, generationMode);
gl.glTexGeni(GL.GL_R, GL.GL_TEXTURE_GEN_MODE, generationMode);
gl.glEnable(GL.GL_TEXTURE_GEN_S);
gl.glEnable(GL.GL_TEXTURE_GEN_T);
gl.glEnable(GL.GL_TEXTURE_GEN_R);
gl.glBindTexture(GL.GL_TEXTURE_CUBE_MAP_ARB, textureID);
gl.glEnable(GL.GL_TEXTURE_CUBE_MAP_ARB);
gl.glPushMatrix();
gl.glTranslatef(0.0f, 0.0f, -2.5f);
gl.glRotatef(rotx, 1, 0, 0);
gl.glRotatef(roty, 0, 1, 0);
gl.glVertexPointer(3, gl.GL_FLOAT, 0, vertexData);
gl.glNormalPointer( gl.GL_FLOAT, 0, normalData);
gl.glDrawElements (gl.GL_TRIANGLES,indices.length, gl.GL_UNSIGNED_INT, indices);
gl.glPopMatrix();
gl.glDisable(GL.GL_TEXTURE_CUBE_MAP_ARB);
gl.glDisable(GL.GL_TEXTURE_GEN_S);
gl.glDisable(GL.GL_TEXTURE_GEN_T);
gl.glDisable(GL.GL_TEXTURE_GEN_R);
}
private static void rotateTupleZ(Tuple3f t1, Tuple3f t2, float angle){
float sin = FastTrig.sin(angle),
cos = FastTrig.cos(angle);
t1.set( t2.x*cos - t2.y*sin, t2.x*sin + t2.y*cos, t2.z);
}
private static FloatBuffer createFloatBuffer(int numElements){
return ByteBuffer.allocateDirect(12*numElements).order(
ByteOrder.nativeOrder()).asFloatBuffer();
}
}
Elias is right, it should be 0, not GL.GL_FLOAT.
