Perspective projection problem

Hi,

I’m trying to prepare my project for the Graphics course: drawing a superellipsoid and supertoroid in wireframe form. The problem is, I could not see anything on my canvas although I’m drawing something. I suspect the problem is the perspective projection I’m using, but I could not figure it out. Any help?

Thanks :slight_smile:


import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;

import javax.swing.JFrame;

public class Canvas extends JFrame implements GLEventListener 
{
	private static final long serialVersionUID = 1L;

	private static final int CANVAS_WIDTH = 800;
	private static final int CANVAS_HEIGHT = 600;
	
	// Dimension of the transformation canvas
	private static final Dimension DEFAULT_CANVAS_SIZE = 
		new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT);
	
	// Canvas to add to the frame
	private GLCanvas canvas;
	
	// Constructor
	public Canvas() 
	{
		super("CS 465 - Programming Assignment 2");
		
		GLCapabilities capabilities = new GLCapabilities();
		capabilities.setDoubleBuffered(true);
		
		canvas = new GLCanvas(capabilities);
		canvas.addGLEventListener(this);
		canvas.setSize(DEFAULT_CANVAS_SIZE);
        
        // Add required listeners
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        
        this.add(canvas, BorderLayout.CENTER);
	}
	
	public void init(GLAutoDrawable drawable) 
	{
		final GL gl = drawable.getGL();
		final GLU glu = new GLU();
		
		// Set canvas color to white
		gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
		gl.glClearDepth(1.0f);
		gl.glEnable(GL.GL_DEPTH_TEST);
		gl.glDepthFunc(GL.GL_LEQUAL);
		gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
		
		gl.glMatrixMode(GL.GL_PROJECTION);
		gl.glLoadIdentity();
		
		glu.gluPerspective(45.0f, 1, 0.1f, 100.0f);
		
		gl.glMatrixMode(GL.GL_MODELVIEW);
		gl.glLoadIdentity();
		
		gl.glViewport(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
	}

	public void display(GLAutoDrawable drawable) 
	{
		final GL gl = drawable.getGL();
		
		SuperEllipsoid test = new SuperEllipsoid(1, 1, 1, 1, 1, 50, 50);
		test.draw(gl);
	}

	// Called when the display window is resized
	public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) 
	{
		// do nothing...
	}

	// Called when the display mode has changed
	public void displayChanged(GLAutoDrawable drawable, boolean w, boolean arg2) 
	{
		// do nothing...
	}
	
	public static void main(String[] args)
	{
		Canvas canvas = new Canvas();
		canvas.pack();
		canvas.setResizable(false);
		canvas.setVisible(true);
	}
}

The draw code I’m using…


public void draw(GL gl) {
		float U, dU, V, dV;
		float S, dS, T, dT;
		
		dU = (u2 - u1) / (float) uSeg;
		dV = (v2 - v1) / (float) vSeg;
		dS = (s2 - s1) / (float) uSeg;
		dT = (t2 - t1) / (float) vSeg;
		
		U = u1;
		S = s1;
		
		gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE);
		
		gl.glBegin(GL.GL_QUADS);
		
		for (int y = 0; y < uSeg; y++) {
			V = v1;
			T = t1;
			
			for (int x = 0; x < vSeg; x++) {
				// Vertex 1
				EllipsoidNormal tmp = sqEllipsoid(1, 1, 1, U, V, n, e);
				gl.glNormal3f(tmp.nx, tmp.ny, tmp.nz);
				gl.glTexCoord2f(S, T);
				gl.glVertex3f(tmp.x, tmp.y, tmp.z);
				
				// Vertex 2
				tmp = sqEllipsoid(1, 1, 1, U + dU, V, n, e);
				gl.glNormal3f(tmp.nx, tmp.ny, tmp.nz);
				gl.glTexCoord2f(S + dS, T);
				gl.glVertex3f(tmp.x, tmp.y, tmp.z);
				
				// Vertex 3
				tmp = sqEllipsoid(1, 1, 1, U + dU, V + dV, n, e);
				gl.glNormal3f(tmp.nx, tmp.ny, tmp.nz);
				gl.glTexCoord2f(S + dS, T + dT);
				gl.glVertex3f(tmp.x, tmp.y, tmp.z);
				
				// Vertex 4
				tmp = sqEllipsoid(1, 1, 1, U, V + dV, n, e);
				gl.glNormal3f(tmp.nx, tmp.ny, tmp.nz);
				gl.glTexCoord2f(S, T + dT);
				gl.glVertex3f(tmp.x, tmp.y, tmp.z);
				
				// Update variables for next loop */
		        V += dV;
		        T += dT;
			}
			
			// Update variables for next loop */
		    S += dS;
		    U += dU;
		}
		
		gl.glEnd();	
	}

Hi,

You should add glClear() before drawing.

gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
test.draw(gl);

and, If object is near,
you should move object in the distance.

gl.glLoadIdentity();
gl.glTranslatef(0.0f,0.0f,-3.0f);
//drawing

And gluPerspective is

float asp = (float)WIDTH / (float)HEIGHT;
glu.gluPerspective(45.0f, asp, 0.1f, 100.0f);