Jump to content




Recent Status Updates

  • Photo
      18 Aug
    KodeKool

    When faced with a wall of errors and no hope to fix them, remember the following "Programs always do what you tell them to, and seldom what you want them to, but eventually you'll run out of things that can go wrong and it'll just work. and that's the secret to good programming."

    Show comments (2)
View All Updates

Developed by Kemal Taskin
Photo
- - - - -

Simple LWJGL Lighting


  • Please log in to reply
5 replies to this topic

#1 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 762 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

Posted 03 October 2011 - 03:03 PM

Here's a simple translation to LWJGL of the first lighting program from the OpenGL redbook.

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;

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

public class LightingTest1 {
	private static final int WIDTH = 800, HEIGHT = 600;
	private static final DisplayMode DISPLAY_MODE = new DisplayMode(WIDTH, HEIGHT);
	private static final String WINDOW_TITLE = "Lighting 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 Lighting Test -----------//
	private FloatBuffer matSpecular;
	private FloatBuffer lightPosition;
	private FloatBuffer whiteLight; 
	private FloatBuffer lModelAmbient;
	//----------- END: Variables added for Lighting Test -----------//
	
	public static void main(String[] args) {
		LightingTest1 test = new LightingTest1();
		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);
		
		glColor3f(0.1f, 0.4f, 0.9f);
		Sphere s = new Sphere();
		s.draw(1.0f, 20, 16);
	}
	
	
	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() {
		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); 
		
		//----------- Variables & method calls added for Lighting Test -----------//
		initLightArrays();
		glShadeModel(GL_SMOOTH);
		glMaterial(GL_FRONT, GL_SPECULAR, matSpecular);				// sets specular material color
		glMaterialf(GL_FRONT, GL_SHININESS, 50.0f);					// sets shininess
		
		glLight(GL_LIGHT0, GL_POSITION, lightPosition);				// sets light position
		glLight(GL_LIGHT0, GL_SPECULAR, whiteLight);				// sets specular light to white
		glLight(GL_LIGHT0, GL_DIFFUSE, whiteLight);					// sets diffuse light to white
		glLightModel(GL_LIGHT_MODEL_AMBIENT, lModelAmbient);		// global ambient light 
		
		glEnable(GL_LIGHTING);										// enables lighting
		glEnable(GL_LIGHT0);										// enables light0
		
		glEnable(GL_COLOR_MATERIAL);								// enables opengl to use glColor3f to define material color
		glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);			// tell opengl glColor3f effects the ambient and diffuse properties of material
		//----------- END: Variables & method calls added for Lighting Test -----------//
	}
	

	//------- Added for Lighting Test----------//
	private void initLightArrays() {
		matSpecular = BufferUtils.createFloatBuffer(4);
		matSpecular.put(1.0f).put(1.0f).put(1.0f).put(1.0f).flip();
		
		lightPosition = BufferUtils.createFloatBuffer(4);
		lightPosition.put(1.0f).put(1.0f).put(1.0f).put(0.0f).flip();
		
		whiteLight = BufferUtils.createFloatBuffer(4);
		whiteLight.put(1.0f).put(1.0f).put(1.0f).put(1.0f).flip();
		
		lModelAmbient = BufferUtils.createFloatBuffer(4);
		lModelAmbient.put(0.5f).put(0.5f).put(0.5f).put(1.0f).flip();
	}
}

Java - 3D ProgrammingsrcredbooklightingLightingTest1.png
  • 0

#2 floorman

floorman

    CC Regular

  • Member
  • PipPipPip
  • 29 posts

Posted 04 December 2011 - 04:12 PM

Thank you so much for this!

I have only one error, the draw method is not being found...I take it this is in the Sphere Class (either that or Sphere comes with LWJGL/OpenGL) but I am not getting it either way...

Do you have a Sphere class that you're not showing us? :confused:

Looking forward to your response...
  • 0

#3 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 762 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

Posted 04 December 2011 - 06:16 PM

The sphere class comes with the lwjgl under this package:
import org.lwjgl.util.glu.Sphere;
Sphere (LWJGL API)
  • 0

#4 floorman

floorman

    CC Regular

  • Member
  • PipPipPip
  • 29 posts

Posted 05 December 2011 - 09:49 AM

The sphere class comes with the lwjgl under this package:
import org.lwjgl.util.glu.Sphere;
Sphere (LWJGL API)


Thanks you very much!

Turns out I hadn't added an import. :) , Silly me.

Anyways, you got anymore of these LWJGL ports or something? I would so love to learn more. Do you have a place where you learnt this? Any places I could learn LWJGL (besides their IRC as I go go on there a lot).

Again, thanks very much for this!
  • 0

#5 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 762 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

Posted 05 December 2011 - 10:30 AM

It's kind of hard to find a resource with lots of links/tutorials/etc on LWJGL. A strong background in OpenGL in general helps a lot.

I use:
Java-Gaming.org - home of the largest java game developer community
http://nehe.gamedev.net/
Main Page - LWJGL
OpenGL Programming Guide (Addison-Wesley Publishing Company): Table of Contents

I've got some more, but they're very simple (like this example) because I also am learning the LWJGL. I have postponed learning it though because I'm currently working on college work. :\

---------- Post added at 12:30 PM ---------- Previous post was at 12:30 PM ----------

BTW, where is the IRC channel?
  • 0

#6 floorman

floorman

    CC Regular

  • Member
  • PipPipPip
  • 29 posts

Posted 05 December 2011 - 11:27 AM

#LWJGL on Freenode.

And thanks you for all of the links! :D You're awesome.
  • 0