Warning:
the process order are no matter for the scale and rotate , since scale and rotate both are linear transform . But not the translation ! the translation is a affine transform . So, the linear transform must be process first then the affine transform. It is very very important!
//clear viewport
gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
//begin and end draw
gl.glBegin(GL2.GL_LINE_LOOP);
gl.glEnd();
//set color
gl.glColor3f(r,g,b);
gl.glColor3d(r,g,b);
gl.glColor3fv(c.getColorComponents(null),0);
//clear color
gl.glClearColor(f,g,b,a);
//transforms
gl.glTranslatef(x,y,z);
gl.glRotatef(angle,x,y,z);
gl.glScalef(x,y,z);
gl.glTranslated(x,y,z);
gl.glRotated(angle,x,y,z);
gl.glScaled(x,y,z);
//basic getters
gl.glGetFloatv(propertyCodeNumber,destinationArray,offset);
gl.glGetDoublev(propertyCodeNumber,destinationArray,offset);
gl.glGetIntegerv(propertyCodeNumber,destinationArray,offset);
gl.glGetBooleanv(propertyCodeNumber,destinationArray,offset);
//example
float[] saveColor=new float[4];
gl.glGetFloatv(GL2.GL_CURRENT_COLOR,saveColor,0);
int[] viewport=new int[4];
gl.glGetIntegerv(GL2.GL_VIEWPORT,viewport,0); // return the viewport x,y,width,height
byte[] lit=new byte[1];
gl.glGetBooleanv(GL2.GL_LIGHTING,lit);
//return matrix
float[] transform=new float[16];
gl.glGetFloatv(GL2.GL_MODELVIEW,transform,0);
//use
gl.glLoadMatrixf(transform,0);
float[] shear=new float[]{1,0,0,0, 0,1,0,0, s,0,1,0, 0,0,0,1};
gl.glMultMatrix(shear,0);
//set gl at the initial coordinary
gl.glLoadIdentity();
//save and load gl matrix
gl.glPushMatrix();
gl.glPopMatrix();
//set matix mode
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glMatrixMode(GL2.GL_MODELVIEW);
//set view perspective mode
gl.glOrtho(-s,s,-s,s,-s,s);
// depth test
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glDisable(GL.GL_DEPTH_TEST);
//set the firm reshape ratio
public void reshape(GLAutoDrawable drawable,
int x, int y, int width, int height) {
GL gl = drawable.getGL();
double s = 1.5; // limits of cube that we want to view go from -s to s.
gl.glMatrixMode(GL.GL PROJECTION);
gl.glLoadIdentity(); // Start with the identity transform.
if (width > height) { // Expand x limits to match viewport aspect ratio.
double ratio = (double)width/height;
gl.glOrtho(-s*ratio, s*ratio, -s, s, -s, s);
}
else { // Expand y limits to match viewport aspect ratio.
double ratio = (double)height/width;
gl.glOrtho(-s, s, -s*ratio, s*ratio, -s, s);
}
gl.glMatrixMode(GL.GL MODELVIEW);
}
//set lighting
gl.glEnable(GL2.GL_LIGHTING);
gl.glEnable(GL2.GL_LIGHT0);
//set shade model
gl.glShadeModel(GL2.GL_SMOOTH);
gl.glShadeModel(Gl2.Gl_FLAT);
gl.glEnable(GL2.GL_MATERIAL_COLOR);
gl.glEnabel(GL2.GL_NORMALIZE);
gl.glLightModeli(GL2.GL_LIGHT_MODEL_TWO_SIDE,GL.GL_TRUE);
//display list
listID=gl.glGenLists(1);
gl.glNewList(listID,GL.GL_COMPILE);
gl.glNewList(listID,GL.GL_COMPILE_AND_EXECUTE);
gl.glEndList();
gl.glCallList(listID);
gl.glDeleteLists(listID,1);
gl.glFlush(); // Make sure all commands are sent to graphics card.
gl.glFinish(); // Wait for all commands to complete, before checking time.
//normal
gl.glNormal3f(x,y,z);
gl.glNormal3d(x,y,z);
//texture
Texture texture=TextureIO.newTexture(img,true);
Texture teture=Texture.newTexture(file,true);
Texture texture; // Most likely, an instance variable.
try {
URL textureURL; // URL class from package java.net.
textureURL = getClass().getClassLoader().getResource("texture/brick001.jpg");
texture = TextureIO.newTexture(textureURL, true, "jpg");
// The third param above is the extension from the file name;
// "jpg", "png", and probably "gif" files should be OK.
}
catch (Exception e) {
// Won’t get here if the file is properly packaged with the program!
e.printStackTrace();
}
//solve the filp problem
Texture texture;
try {
URL textureURL;
textureURL = getClass().getClassLoader().getResource(textureFileName);
BufferedImage img = ImageIO.read(textureURL); // read file into BufferedImage
ImageUtil.flipImageVertically(img);
texture = TextureIO.newTexture(img, true);
}
catch (Exception e) {
e.printStackTrace();
}
//use file to load image
Texture texture;
File f=new File(textureFileName);
BufferedImage img=ImageIO.read(f);
ImageUtil.flipImageVertically(img);
texture=AWTTextureIO.new Texture(gl.getGLProfile(),img,true);
texture.bind(gl);
texture.enable(gl);
texture.enable();
texture.disable();
texture.bind();
//repeat texture
tex.setTexParameteri(GL.GL_TEXTURE_WRAP_S,GL.GL_REPEAT);
tex.setTexParameteri(GL.GL_TEXTURE_WRAP_T,GL.GL_REPEAT);
// vertex
gl.glVertex2f(x,y);
gl.glVertex2d(x,y);
gl.glVertex3f(x,y,z);
gl.glVertex3d(x,y,z);
gl.glVertex4d(x,y,z,w);
float[] vert=new float[]{1,0,0, -1,2,2, 1,1,-3};
gl.glBegin(GL2.GL_POLYGON);
gl.glVertex3fv(vert,0);
gl.glVertex3fv(vert,3);
gl.glVertex3fv(vert,6);
gl.glEnd();
//points
gl.glBegin(GL2.GL_POINTS);
gl.glVertex3f(x,y,z);
gl.glEnable(GL2.GL_POINT_SMOOTH);
gl.glEnable(GL2.GL_BLEND); //you should remember to turn off the GL BLEND option when drawing polygons.
gl.glBlendFunc(GL2.GL_SRC_ALPHA,GL2.GL_ONE_MINUS_SRC_ALPHA);
//lines
gl.glBegin(GL2.GL_LINES);
gl.glBegin(GL2.GL_LINE_LOOP);
gl.glBegin(GL2.GL_LINE_STRIP);
gl.glEnable(GL2.GL_LINE_SMOOTH);
gl.glEnable(GL2.GL_LINE_STIPPLE);
gl.glLineStipple(multiplier,pattern); //pattern exp:(short)0x0F33 represents the binary number 0000111100110011
//polygons
gl.glFrontFace(GL2.GL_CW);
gl.glEnable(GL2.GL_CULL_FACE); // cull the back face;
gl.glPolygonMode(GL2.GL_FRONT_AND_BACK,GL2.GL_FILL);
gl.glPolygonMode(GL2.GL_FRONT_AND_BACK,GL2.GL_LINE);
gl.glPolygonMode(GL2.GL_FRONT_AND_BACK,GL2.GL_POINT);
gl.glEnable(GL2.GL_POLYGON_OFFSET_FILL);
gl.glPolygonOffset(1,1);
//triangles
gl.glBegin(GL2.GL_TRIANGLES);
gl.glBegin(GL2.GL_TRIANGLE_STRIP);
gl.glBegin(GL2.GL_TRIANGLE_FAN);
//quadrilaterals
gl.glBegin(GL2.GL_QUADS);
gl.glBegin(GL2.GL_QUAD_STRIPS);
//index face set
float[] vertexList={1,0,1, 1,0,-1, -1,0,-1, -1,0,1, 0,1,0};
int[][] faceList={{4,3,0},{4,0,1},{4,1,2},{4,2,3},{0,3,2},{0,2,1}}
static void drawIFS(GL2 gl,float[] vertexList,int[][] faceList){
for(int i=0;i<faceList.length;i++){
gl.glBegin(GL2.GL_POLYGON);
int[] faceData=faceList[i];
float[] normal=computerUnitNormalForPolygon(vertexList,faceData);
gl.glNormal3fv(noraml,0);
for(int j=0;j<faceData.length;j++){
int vertexIndex=faceData[j];
gl.glVertex3fv(vertexList,vertexIndex*3);
}
}
}
//both one dimension test
float[] vertexList={1,0,1, 1,0,-1, -1,0,-1, -1,0,1, 0,1,0};
int[] faceList={4,3,0, 4,0,1, 4,1,2, 4,2,3, 0,3,2, 0,2,1};
static void drawIFS(GL2 gl,float[] vertexList, int[] faceList){
for(int i=0;i<faceList.length/3){
gl.glBegin(GL2.GL_POLYGON);
float[] normal=computerUnitNormalForPolygon(vertexList,faceList,i)
gl.glVertex3fv(vertexList,faceList[i*3]*3);
gl.glVertex3fv(vertexList,faceList[i*3+1]*3);
gl.glVertex3fv(vertexList,faceList[i*3+2]*3);
}
}
//GLBuffers
gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL2.GL_NORMAL_ARRAY);
gl.glEnableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
GLBuffers.newDirectIntBuffer(new int[]{});
FloatBuffer fb=BufferUtil.newFloatBUffer(1);
fb.put(1.5f);
fb.get(0);
fb.position(int index);
fb.rewind(); //position to zero;
float[] vert={1.5,0,0, 0,1.5,0, 0.5,0.5,-1};
fb.put(vert,0,9); //start at 0 , 9 numbers
public void glVertexPointer(int size,int type,int stride,Buffer buffer)
void glDrawArrays(int mode,int first,int count);
//example
private FloatBuffer rectVertices;
rectVertices=BufferUtil.new FloatBuffer(8);
float[] data={-1,-1, 1,-1, 1,1, -1,1};
rectVertices.put(data,0,8);
rectVertices.rewind();
gl.glVertexPointer(2,GL2.GL_FLOAT,0,rectVertices);
gl.glEnableClientStates(GL2.GL_VERTEX_ARRAY);
gl.glNormal3f(0,0,1);
gl.glDrawArrays(GL2.GL_POLYGON,0,4);
gl.glDisableClientStates(GL.GL_VERTEX_ARRAY);
//with VBOs
//init get 4 buffer ID corresponding 4 floatBuffers
int[] bufferID=new int[4];
gl.glGenBuffers(4,bufferID,0);
gl.glBindBuffer(GL2.GL_ARRAY_BUFFER,bufferID[0]);
gl.glBufferData(GL2.GL_ARRAY_BUFFER,vertexCont*4,floatBuffer,GL2.GL_STATIC_DRAW);
(public void glBufferData(int target, int size, Buffer data, int usage) );
//usage types
GL2.GL_STATIC_DRAW;
GL2.GL_STREAM_DRAW;
GL2.GL_DYNAMIC_DRAW;
//draw using the bufferID
gl.glBindBUffer(GL2.GL_ARRAY_BUFFER,bufferID[0]);
gl.glVertexPointer(3,GL2.GL_FLOAT,0,0);
gl.glNormalPointer(3,GL2.GL_FLOAT,0,0);
gl.glBindBUffer(GL2.GL_ARRAY_BUFFER,bufferID[1]);
gl.glTexCoordPointer(2,GL2.GL_FLOAT,0,0);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER,0); // leave no VBO bound.
gl.glEnableClientState(GL.GL VERTEX ARRAY);
gl.glEnableClientState(GL.GL NORMAL ARRAY);
gl.glEnableClientState(GL.GL TEXTURE COORD ARRAY);
sphereTexture.enable();
gl.glDrawArrays(GL.GL POINTS, 0, spherePointCount); // Generate the points!
gl.glDisableClientState(GL.GL VERTEX ARRAY);
gl.glDisableClientState(GL.GL NORMAL ARRAY);
gl.glDisableClientState(GL.GL TEXTURE COORD ARRAY);
sphereTexture.disable();
//alternative method
public void glVertexPointer(int size, int type, int stride, long vboOffset)
public void glNormalPointer(int type, int stride, long vboOffset)
public void glTexCoordPointer(int size, int type, int stride, long vboOffset)
//drawElement2
gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER,icosphereIndexID);
gl.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER,indexCount*4,icosphereIndexBuffer,GL2.GL_STATIC_DRAW);
gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER,icosphereIndexID);
gl.glDrawElements(GL2.GL_TRIANGLES,indexCount,GL2.GL_UNSIGNED_INT,0);
//samle draw a pyramid
// Declare variables to hold the data, probably as instance variables:
FloatBuffer vertexBuffer;
IntBuffer faceBuffer;
// Create the Java nio buffers, and fill them with data, perhaps in init():
vertexBuffer = BufferUtil.newFloatBuffer(15);
faceBuffer = BufferUtil.newIntBuffer(16);
float[] vertexList = { 1,0,1, 1,0,-1, -1,0,-1, -1,0,1, 0,1,0 };
int[] faceList = { 4,3,0, 4,0,1, 4,1,2, 4,2,3, 0,3,2,1 };
vertexBuffer.put(vertexList, 0, 15);
vertexBuffer.rewind();
faceBuffer.put(faceList, 0, 16); // No need to rewind; position is set later.
// Set up for using a vertex array, in init() or display():
gl.glEnableClientState(GL.GL VERTEX ARRAY);
gl.glVertexPointer(3, GL.GL FLOAT, 0, vertexBuffer);
// Do the actual drawing, one face at a time. Set the position in
// the faceBuffer to indicate the start of the data in each case:
faceBuffer.position(0);
gl.glNormal3f(0, 0.707f, 0.707f);
gl.glDrawElements(GL.GL POLYGON, 3, GL.GL UNSIGNED INT, faceBuffer);
faceBuffer.position(3);
gl.glNormal3f(0.707f, 0.707f, 0);
gl.glDrawElements(GL.GL POLYGON, 3, GL.GL UNSIGNED INT, faceBuffer);
faceBuffer.position(6);
gl.glNormal3f(0, 0.707f, -0.707f);
gl.glDrawElements(GL.GL POLYGON, 3, GL.GL UNSIGNED INT, faceBuffer);
faceBuffer.position(9);
gl.glNormal3f(-0.707f, 0.707f, 0);
gl.glDrawElements(GL.GL POLYGON, 3, GL.GL UNSIGNED INT, faceBuffer);
faceBuffer.position(12);
gl.glNormal3f(0, -1, 0);
gl.glDrawElements(GL.GL POLYGON, 4, GL.GL UNSIGNED INT, faceBuffer);
//light & material control
public void glMateriali(int face,int propertyName,int propertyValue);
public void glMaterialfv(int face,int propertyName,float[] propertyValue,int offset);
gl.glLightModeli(GL2.GL_LIGHT_MODEL_TWO_SIDE,GL2.GL_TRUE); //gobal light model
gl.glMaterialfv(GL2.GL_FRONT,GL2.GL_SPECULAR,new float[]{0.75,0.75,0.75,1},0);
gl.glMaterialfv(Gl2.GL_FRONT,GL2.GL_AMBIENT_AND_DIFFUSE,new float[]{0.75f,0.75f,0,1},0);
gl.glMateriali(GL2.GL_FRONT,GL2.GL_SHINESS,i*16); //shineness default is 0-128;
//to say which material properties should be tied to glColor.Since color material will replace the sp,di,am,em
gl.glEnable(GL2.GL_COLOR_MATERIAL);
gl.glColorMaterial(GL2.GL_FRONT_AND_BACK,GL2.GL_DIFFUSE);
gl.glEnableClientState(GL2.GL_COLOR_MATERIAL); //enable buffer drawing
gl.glColorPointer();
float[] bluish={0.3f,0.3f,0.7f,1};
gl.glLightfv(GL2.GL_LIGHT1,GL2.GL_DIFFUSE,bluish,0);
gl.glLightfv(GL2.GL_LIGHT1.GL2.GL_SPECULAR,bluish,0);
gl.glLightfv(GL2.GL_LIGHT1,GL2.GL_AMBIENT,new float[]{0,0,0.1f,0},0);
gl.glLightfv(GL2.GL_LIGHT1,GL2.GL_POSITION,new float[]{1,2,3,1},0);
//equals:
gl.glTranslatef(1,2,3);
gl.glLightfv(GL2.GL_LIGHT1,GL2.GL_POSITION,new float[]{0,0,0,1},0); //w=0, use directional light instead of position light
gl.glLightfv(GL2.GL_LIGHT1,GL2.GL_POSITION,new float[]{10,15,5},0);
gl.glLighti(GL2.GL_LIGHT1,GL2.GL_SPOT_CUTOFF,30); //set a spot light with angle 30 degree
gl.glLightfv(GL2.GL_LIGHT1,GL2.GL_SPOT_DIRECTION,new float[]{-10,-15,-5},0);
gl.glLightModelfv(GL2.GL_LIGHT_MODEL_AMBIENT,new float[]{0.2f,0.2f,0,1},0);
gl.glLightModeli(GL2.GL_LIGHT_MODEL_TWO_SIDE,GL2.GL_TRUE);
gl.glLightModeli(GL2.GL_LIGHT_MODEL_LOCAL_VIEWER,GL2.GL_TRUE);
if(gl.isExtensionAvailable("GL_VERSION_1_2"){
gl.glLightModeli(GL2.GL_LIGHT_MODEL_COLOR_CONTROL,GL2.GL_SEPERATE_SPECULAR_COLOR);
//light and material in the scene;
public void glPushAttrib(int mask);
public void glPopAttrib();
gl.glPushAttrib(GL2.GL_LIGHTING_BIT);
gl.glPushAttrib(GL2.GL_LIGHTING_BIT|GL2.GL_CURRENT_BIT); //current bit means the current color;
//other bits:
GL2.GL_POLYGON_BIT;
GL2.GL_LINE_BIT;
GL2.GL_POINT_BIT;
GL2.GL_TEXTURE_BIT;
GL2.GL_ENABLE_BIT;
gl.glPushClientAttrib(GL2.GL_CLIENT_VERTEX_ARRAY_BIT);
gl.glPopClientAttrib();
//texture control
gl.glEnable(GL2.GL_TEXTURE_1D);
gl.glEnable(GL2.GL_TEXTURE_2D);
gl.glEnable(GL2.GL_TEXTURE_3D);
gl.glTexParameteri(GL2.GL_TEXTURE_2D,GL2.GL_TEXTURE_WRAP_S,GL2.GL_REPEAT);
//equals to
tex.setTexParameteri(GL2.GL_TEXTURE_WRAP_S,GL2.GL_REPEAT);