Jump to content

LWJGL Outline Polygons

- - - - -

  • Please log in to reply
No replies to this topic

#1
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Converted from Example 6-11 from the redbook.



import org.lwjgl.LWJGLException;

import org.lwjgl.input.Keyboard;

import org.lwjgl.opengl.Display;

import org.lwjgl.opengl.DisplayMode;

import org.lwjgl.util.glu.GLU;


import static org.lwjgl.opengl.GL11.*;


public class PolygonOffsetTest {

	private static final int WIDTH = 800, HEIGHT = 600;

	private static final DisplayMode DISPLAY_MODE = new DisplayMode(WIDTH, HEIGHT);

	private static final String WINDOW_TITLE = "Polygon Offset Test";

	private static final int FPS = 70;	//frames per second

	

	private boolean isRunning;	//variable to tell if program is running or not

	private float zTranslation = -12f;


	/*------- Added for Polygon Offset Test -------*/

	private int boxPointer;

	private float degree;

	/*------- End - Added for Polygon Offset Test -------*/

	

	public static void main(String[] args) {

		PolygonOffsetTest test = new PolygonOffsetTest();

		test.run();

	}


	private void run() {

		try {

			init();

			while( isRunning ) {

				getInput();	//read input

				render(); 	//render graphics

				Display.sync(FPS);	//sync to fps

				Display.update();	//update the view/screen

			}

		} catch (Exception e) {

			e.printStackTrace();

			System.exit(1);

		}

	}

	

	private void render() {

		glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

		glLoadIdentity(); 	

		glTranslatef(0.0f, 0.0f, zTranslation); /*Zoom into page by zTranslation units */

		glRotatef(15.0f, 1.0f, 0.0f, 0.0f);		/* Rotate 15 degrees over the x axis */

		glRotatef(degree++%360, 0.0f, 1.0f, 0.0f);	/* Rotate continuously */

		

		/*------- Added for Polygon Offset Test -------*/

		glLineWidth(1.0f); /*Changes the width of the outline*/

		

		

		glEnable(GL_LIGHT0); 

		glEnable(GL_LIGHTING);

		glPolygonOffset(1.0f, 1.0f); 

		

		glEnable(GL_POLYGON_OFFSET_FILL);

		glCallList(boxPointer);

		glDisable(GL_POLYGON_OFFSET_FILL);

		

		glDisable(GL_LIGHTING);

		glDisable(GL_LIGHT0);

		glColor3f(1.0f, 0.0f, 0.0f);

		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

		glCallList(boxPointer);

		glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

		/*------- End - Added for Polygon Offset Test -------*/

	}

	

	

	private void getInput() {

		if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {	// if user presses escape key

			isRunning = false;

		}

		if( Display.isCloseRequested()) {	// if user closes window

			isRunning = false;

		}

	}

	

	private void init() {

		createWindow();

		initGL();

		/*------- Added for Polygon Offset Test -------*/

		createDisplayList();

		/*------- End - Added for Polygon Offset Test -------*/

		isRunning = true;

	}

	

	/*------- Added for Polygon Offset Test -------*/

	/* createDisplayList(): creates a display list that draws a cube

	 * 

	 */

	private void createDisplayList() {

		boxPointer = glGenLists(1);

		glNewList(boxPointer, GL_COMPILE);

		glBegin(GL_QUADS); {

			/* --------- Top Polygon of Cube --------*/

			glNormal3f(0.0f, 1.0f, 0.0f);

			glColor3f(0.16f, 0.442f, 0.442f);

			glVertex3f(1.0f, 1.0f, -1.0f); 		//top right 

			glVertex3f(-1.0f, 1.0f, -1.0f); 	//top left 

			glVertex3f(-1.0f, 1.0f, 1.0f);		//bottom left

			glVertex3f(1.0f, 1.0f, 1.0f);		//bottom right

			

			/* --------- Bottom Polygon of Cube --------*/

			glNormal3f(0.0f, -1.0f, 0.0f);

			glVertex3f(1.0f, -1.0f, 1.0f); 		//top right 

			glVertex3f(-1.0f, -1.0f, 1.0f); 	//top left 

			glVertex3f(-1.0f, -1.0f, -1.0f);		//bottom left

			glVertex3f(1.0f, -1.0f, -1.0f);		//bottom right

			

			/* --------- Front Polygon of Cube --------*/

			glNormal3f(0.0f, 0.0f, 1.0f);

			glVertex3f(1.0f, 1.0f, 1.0f); 		//top right 

			glVertex3f(-1.0f, 1.0f, 1.0f); 		//top left 

			glVertex3f(-1.0f, -1.0f, 1.0f);		//bottom left

			glVertex3f(1.0f, -1.0f, 1.0f);		//bottom right

			

			/* --------- Left Polygon of Cube --------*/

			glNormal3f(-1.0f, 0.0f, 0.0f);

			glVertex3f(-1.0f, 1.0f, 1.0f); 		//top right 

			glVertex3f(-1.0f, 1.0f, -1.0f); 	//top left 

			glVertex3f(-1.0f, -1.0f, -1.0f);	//bottom left

			glVertex3f(-1.0f, -1.0f, 1.0f);		//bottom right

			

			/* --------- Right Polygon of Cube --------*/

			glNormal3f(1.0f, 0.0f, 0.0f);

			glVertex3f(1.0f, 1.0f, -1.0f); 		//top right 

			glVertex3f(1.0f, 1.0f, 1.0f); 	//top left 

			glVertex3f(1.0f, -1.0f, 1.0f);	//bottom left

			glVertex3f(1.0f, -1.0f, -1.0f);		//bottom right

			

			/* --------- Back Polygon of Cube --------*/

			glNormal3f(0.0f, 0.0f, -1.0f);

			glVertex3f(-1.0f, 1.0f, -1.0f); 	//top right 

			glVertex3f(1.0f, 1.0f, -1.0f); 		//top left 

			glVertex3f(1.0f, -1.0f, -1.0f);		//bottom left

			glVertex3f(-1.0f, -1.0f, -1.0f);	//bottom right

			

		} glEnd();

		glEndList();

	}

	/*------- End - Added for Polygon Offset Test -------*/

	

	private void createWindow() {

		try {

			Display.setDisplayMode(DISPLAY_MODE);

			Display.setTitle(WINDOW_TITLE);

			Display.create();

		} catch (LWJGLException e){

			e.printStackTrace();

		}

	}

	

	private void initGL() {

		glClearColor(0.5f, 0.5f, 0.5f, 0.0f); // sets background to grey

		glClearDepth(1.0f); // clear depth buffer

		glEnable(GL_DEPTH_TEST); // Enables depth testing

		glDepthFunc(GL_LEQUAL); // sets the type of test to use for depth testing

		glMatrixMode(GL_PROJECTION); // sets the matrix mode to project

		

		float fovy = 45.0f;

		float aspect = DISPLAY_MODE.getWidth() / (float)DISPLAY_MODE.getHeight();

		float zNear = 0.1f;

		float zFar = 100.0f;

		GLU.gluPerspective(fovy, aspect, zNear, zFar);

		

		glMatrixMode(GL_MODELVIEW);

		

		glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); 

	}

}

Attached File  polygonOffset2.png   45.11K   38 downloads

Attached Files






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users