Jump to content

Simple LWJGL Blending

- - - - -

  • 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
This example is translated from the OpenGL Redbook example 6.2 from C to Java.
There's a few extra options I added like changing the blending equations.

Attached File  Java - 3D ProgrammingsrcredbookblendingBlendingTriangles.png   67.26K   48 downloads


/*

 * Legend -------------------------------

 * Key A = move leftTriangle left

 * Key D = move leftTriangle right

 * Key LEFT_ARROW = move rightTriangle left

 * Key RIGHT_ARROW = move rightTriangle right

 * Key SPACE = switches from drawing triangles on top of eachother 

 * Key B - changes the blending equation

 */

package redbook.blending;


import org.lwjgl.LWJGLException;

import org.lwjgl.input.Keyboard;

import org.lwjgl.opengl.Display;

import org.lwjgl.opengl.DisplayMode;

import org.lwjgl.opengl.GL14;

import org.lwjgl.util.glu.GLU;


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


public class BlendingTriangles {

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

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

	private static final String WINDOW_TITLE = "Blending Triangles 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;

	

	//---------- Variables added for Blending Triangles test --------------//

	private Triangle triangleLeft, triangleRight;

	private final float VELOCITY = .2f;

	private final float VELOCITY_ALPHA = .05f;

	private boolean leftFirst = false;

	private boolean keySpaceDown = false;

	private boolean keyBlendingDown = false;

	private int blendingEquationIndex;

	private final int[] BLENDING_EQUATIONS = {

								GL14.GL_FUNC_ADD,

								GL14.GL_FUNC_SUBTRACT,

								GL14.GL_FUNC_REVERSE_SUBTRACT,

								GL14.GL_MIN,

								GL14.GL_MAX};

	//---------- End - Variables added for Blending Triangles Test ------------//


	

	public static void main(String[] args) {

		BlendingTriangles test = new BlendingTriangles();

		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);


		if(leftFirst) {

			drawTriangle(triangleLeft);

			drawTriangle(triangleRight);

		} else {

			drawTriangle(triangleRight);

			drawTriangle(triangleLeft);

		}


	}

	

	//------------Method added for Blending Triangles Test----------//

	private void drawTriangle(Triangle triangle) {

		glBegin(GL_TRIANGLES); {

			glColor4f(triangle.r, triangle.g, triangle.b, triangle.a);

			glVertex3f(triangle.x, triangle.y, triangle.z);

			glVertex3f(triangle.x+2, triangle.y, triangle.z);

			glVertex3f(triangle.x+1, triangle.y+1, triangle.z);

		}glEnd();

	}

	//------------End - Method added for Blending Triangles 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;

		}

		

		//------------Moves triangles left or right--------------//

		if( Keyboard.isKeyDown(Keyboard.KEY_A)) {

			triangleLeft.x -= VELOCITY;

		}

		if( Keyboard.isKeyDown(Keyboard.KEY_D)) {

			triangleLeft.x += VELOCITY;

		}

		if( Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {

			triangleRight.x -= VELOCITY;

		}

		if( Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {

			triangleRight.x += VELOCITY;

		}

		//------------End - Moves triangles left or right--------------//

		

		//------------Changes triangles alpha values--------------//

		if( Keyboard.isKeyDown(Keyboard.KEY_W)) {

			triangleLeft.a += VELOCITY_ALPHA;

		}

		if( Keyboard.isKeyDown(Keyboard.KEY_S)) {

			triangleLeft.a -= VELOCITY_ALPHA;

		}

		if( Keyboard.isKeyDown(Keyboard.KEY_UP)) {

			triangleRight.a += VELOCITY_ALPHA;

		}

		if( Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {

			triangleRight.a -= VELOCITY_ALPHA;

		}

		//------------End - Changes triangles alpha values--------------//

		

		//------------Switches between drawing triangleLeft over triangleRight--------------//

		if( Keyboard.isKeyDown(Keyboard.KEY_SPACE) && !keySpaceDown) {

			leftFirst = !leftFirst;

			keySpaceDown = true;

			log("leftFirst=" + leftFirst);

		}

		if( !Keyboard.isKeyDown(Keyboard.KEY_SPACE) && keySpaceDown) {

			keySpaceDown = false;

		}

		//------------End - Switches between drawing triangleLeft over triangleRight--------------//

		

		//------------Switches Blending Modes--------------//

		if( Keyboard.isKeyDown(Keyboard.KEY_B) && !keyBlendingDown) {

			blendingEquationIndex = (blendingEquationIndex==BLENDING_EQUATIONS.length-1)?0:blendingEquationIndex+1;

			GL14.glBlendEquation(BLENDING_EQUATIONS[blendingEquationIndex]);

			keyBlendingDown = true;

			log("blendingEquationIndex=" + blendingEquationIndex);

		}

		if( !Keyboard.isKeyDown(Keyboard.KEY_B) && keyBlendingDown) {

			keyBlendingDown = false;

		}

		//------------End - Switches Blending Modes--------------//

	}

	

	private void log(String s) {

		System.out.println(s);

	}

	

	private void init() {

		createWindow();

		initGL();

		initTriangles();

		isRunning = true;

	}

	

	//------------Method added for Blending Triangles Test----------//

	private void initTriangles() {

		triangleLeft = new Triangle();

		triangleRight = new Triangle();

		triangleLeft.x = -2f;

		triangleRight.x = 2f;

		

		triangleLeft.r = 1.0f;

		triangleLeft.g = 1.0f;

		triangleLeft.b = 0.0f;

		triangleLeft.a = 0.75f;

		

		triangleRight.r = 0.0f;

		triangleRight.g = 1.0f;

		triangleRight.b = 1.0f;

		triangleRight.a = 0.75f;

	}

	//------------End - Method added for Blending Triangles 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); 

		

		//---------Code added to enable and set up blending function----------//

		glEnable(GL_BLEND);

		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

		GL14.glBlendEquation( BLENDING_EQUATIONS[blendingEquationIndex]);

		glShadeModel(GL_FLAT);

		//---------End - Code added to enable and set up blending function----------//

	}

	

	//----------Class added for Triangle Blending Test -----------//

	private class Triangle {

		float x, y, z; //refers to bottom left corner of a rightside up triangle

		float r, g, b, a; //red, green, blue, alpha

	}

	//----------End - Class added for Triangle Blending Test -----------//

}






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users