Hello, the entire code can be seeing here:
import java.awt.*;
import java.awt.event.*;
import javax.media.opengl.*;
import com.sun.opengl.util.*;
import com.sun.opengl.util.gl2.GLUT;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;
import javax.media.opengl.glu.GLUnurbs;
import javax.media.opengl.glu.gl2.GLUgl2;
/**
* Nurbc.java
* OpenGL SuperBible
*
* @author Richard S. Wright Jr. (Original C++ Code)
* @author Claudio Eduardo Goes (Coded to Java) <P>
*/
public class Nurbc implements GLEventListener {
// NURBS object pointer
GLUnurbs pNurb = null;
// The number of control points for this curve
int nNumPoints = 4; // 4 X 4
// Mesh extends four units -6 to +6 along x and y axis
// Lies in Z plane
// u v (x,y,z)
float ctrlPoints[] = { -6.0f, -6.0f, 0.0f,
2.0f, -2.0f, 8.0f,
2.0f, 6.0f, 0.0f,
6.0f, 6.0f, 0.0f};
// Knot sequence for the NURB
float Knots[] = {0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f};
static GLCanvas canvas;
public static void main(String[] args) {
Frame frame = new Frame("NURBS Curve");
canvas = new GLCanvas();
canvas.addGLEventListener(new Nurbc());
frame.add(canvas);
frame.setSize(800, 600);
final Animator animator = new Animator(canvas);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// Run this on another thread than the AWT event queue to
// make sure the call to Animator.stop() completes before
// exiting
new Thread(new Runnable() {
@Override
public void run() {
animator.stop();
System.exit(0);
}
}).start();
}
});
frame.setVisible(true);
animator.start();
canvas.requestFocusInWindow();
}
public void init(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2(); //JOGL Version 2.0
System.err.println("INIT GL IS: " + gl.getClass().getName());
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL2 gl = drawable.getGL().getGL2(); //JOGL Version 2.0
changeSize(gl, width, height);
}
public void display(GLAutoDrawable drawable) {
GLUT glut = new GLUT();//JOGL Version 2.0
GL2 gl = drawable.getGL().getGL2(); //JOGL Version 2.0
renderScene(gl);
// Flush drawing commands
//drawable.swapBuffers(); //glutSwapBuffers();
setupRC(gl);
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {}
// Called to draw the control points in Red over the NURB
void drawPoints(GL2 gl)
{
int i;
// Large Red Points
gl.glPointSize(5.0f);
gl.glColor3ub((byte)255,(byte)0,(byte)0);
// Draw all the points in the array
int cont=0;
gl.glBegin(GL2.GL_POINTS);
for(i = 0; i < 4; i++) {
gl.glVertex3fv(ctrlPoints,cont);
cont=cont+3;
}
gl.glEnd();
}
// Called to draw scene
void renderScene(GL2 gl)
{
GLUgl2 glu = new GLUgl2();
// Draw in Blue
gl.glColor3ub((byte)0,(byte)0,(byte)220);
// Clear the window with current clearing color
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
// Save the modelview matrix stack
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glPushMatrix();
// Rotate the mesh around to make it easier to see
gl.glRotatef(330.0f, 1.0f,0.0f,0.0f);
// Render the NURB
// Begin the NURB definition
glu.gluBeginCurve(pNurb);
// Evaluate the surface
glu.gluNurbsCurve(pNurb,
8, Knots,
3,
ctrlPoints,
4,
GL2.GL_MAP1_VERTEX_3);
// Done with surface
glu.gluEndCurve(pNurb);
// Show the control points
drawPoints(gl);
// Restore the modelview matrix
gl.glPopMatrix();
// Dispalay the image
//glutSwapBuffers();
}
// This function does any needed initialization on the rendering
// context. Here it sets up and initializes the lighting for
// the scene.
void setupRC(GL2 gl)
{
GLUgl2 glu = new GLUgl2();
// Clear Window to white
gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f );
// Setup the Nurbs object
pNurb = glu.gluNewNurbsRenderer();
glu.gluNurbsProperty(pNurb, GLUgl2.GLU_SAMPLING_TOLERANCE, 25.0f);
glu.gluNurbsProperty(pNurb, GLUgl2.GLU_DISPLAY_MODE, (float)GLU.GLU_FILL);
}
void changeSize(GL2 gl, int w, int h)
{
GLU glu = new GLU();
// Prevent a divide by zero
if(h == 0)
h = 1;
// Set Viewport to window dimensions
gl.glViewport(0, 0, w, h);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
// Perspective view
glu.gluPerspective (45.0f, (double)w/(double)h, 1.0, 40.0f);
// Modelview matrix reset
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
// Viewing transformation, position for better view
gl.glTranslatef (0.0f, 0.0f, -20.0f);
}
public void dispose(GLAutoDrawable arg0) {}
}
Best Regards
Claudio Eduardo Goes