Jump to content

LWJGL Fog

- - - - -

  • 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
Example 6-7 from the red book translated into LWJGL
I excluded the part about changing the fog modes though. It can easily be added as an exercise.


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


import java.nio.FloatBuffer;


import org.lwjgl.BufferUtils;

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 org.lwjgl.util.glu.Sphere;


public class FogDemo {

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

	private static final DisplayMode DISPLAY_MODE = new DisplayMode(WIDTH,

			HEIGHT);

	private static final String WINDOW_TITLE = "Fog 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 = -4f;

	

	/*------- Variables added for Fog Test -----------*/

	private final FloatBuffer position = BufferUtils.createFloatBuffer(4);

	private int fogMode;

	/*------- End Variables added for Fog Test -----------*/

	

	public static void main(String[] args) {

		FogDemo test = new FogDemo();

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

		renderSphere(-2f, -0.5f, -1f);

		renderSphere(-1f, -0.5f, -2f);

		renderSphere(-0f, -0.5f, -3f);

		renderSphere(1f, -0.5f, -4f);

		renderSphere(2f, -0.5f, -5f);


	}

	

	private void renderSphere(float x, float y, float z) {

		glPushMatrix();

		glTranslatef(x, y, z);

		Sphere s = new Sphere();

		s.draw(0.4f, 16, 16);

		glPopMatrix();

	}


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

		isRunning = true;

	}


	private void createWindow() {

		try {

			Display.setDisplayMode(DISPLAY_MODE);

			Display.setTitle(WINDOW_TITLE);

			Display.create();

		} catch (LWJGLException e) {

			e.printStackTrace();

		}

	}


	private void initGL() {

		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 for Fog --------*/

		position.put(0.5f).put(0.5f).put(5.0f).put(0.0f).flip();

		glLight(GL_LIGHT0, GL_POSITION, position);

		

		glEnable(GL_LIGHTING);

		glEnable(GL_LIGHT0); {

			FloatBuffer mat = BufferUtils.createFloatBuffer(4);

			mat.put(0.1745f).put(0.01175f).put(0.01175f).put(1.0f).flip();

			glMaterial(GL_FRONT, GL_AMBIENT, mat);

			mat.clear();

			mat.put(0.61424f).put(0.04136f).put(0.04136f).put(1.0f).flip();

			glMaterial(GL_FRONT, GL_DIFFUSE, mat);

			mat.clear();

			mat.put(0.727811f).put(0.62959f).put(0.62959f).put(1.0f).flip();

			glMaterial(GL_FRONT, GL_SPECULAR, mat);

			glMateriali(GL_FRONT, GL_SHININESS, (int)(0.6*128));

		}

		

		

		glEnable(GL_FOG); {

			FloatBuffer fogColor = BufferUtils.createFloatBuffer(4);

			fogColor.put(0.5f).put(0.5f).put(0.5f).put(1.0f).flip();

			

			fogMode = GL_EXP;

			glFogi(GL_FOG_MODE, fogMode);

			glFog(GL_FOG_COLOR, fogColor);

			glFogf(GL_FOG_DENSITY, 0.35f);

			glHint(GL_FOG_HINT, GL_DONT_CARE);

			glFogf(GL_FOG_START, 1.0f);

			glFogf(GL_FOG_END, 5.0f);

		}

		

		glClearColor(0.5f, 0.5f, 0.5f, 1.0f);

		

		/*------- End code added for Fog --------*/

	}


}

Attached File  Spheres with Fog.png   70.28K   61 downloads

Edited by lethalwire, 18 December 2011 - 08:12 PM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users