lwjglapp.Lights.java Source code

Java tutorial

Introduction

Here is the source code for lwjglapp.Lights.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lwjglapp;

import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;

/**
 *
 * @author mati
 */
class Lights {

    private FloatBuffer matSpecular;
    private FloatBuffer lightPosition;
    private FloatBuffer whiteLight;
    private FloatBuffer lModelAmbient;

    public Lights() {
        initArrays();
        initGL();
    }

    private void initArrays() {
        matSpecular = BufferUtils.createFloatBuffer(4);
        matSpecular.put(1.0f).put(1.0f).put(1.0f).put(1.0f).flip();

        lightPosition = BufferUtils.createFloatBuffer(4);
        lightPosition.put(5.0f).put(-5.0f).put(10.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();
    }

    private void initGL() {
        GL11.glShadeModel(GL11.GL_SMOOTH);
        GL11.glMaterial(GL11.GL_FRONT, GL11.GL_SPECULAR, matSpecular); // sets specular material color
        GL11.glMaterialf(GL11.GL_FRONT, GL11.GL_SHININESS, 50.0f); // sets shininess

        GL11.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, lightPosition); // sets light position
        GL11.glLight(GL11.GL_LIGHT0, GL11.GL_SPECULAR, whiteLight); // sets specular light to white
        GL11.glLight(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, whiteLight); // sets diffuse light to white
        GL11.glLightModel(GL11.GL_LIGHT_MODEL_AMBIENT, lModelAmbient); // global ambient light 

        GL11.glEnable(GL11.GL_LIGHTING); // enables lighting
        GL11.glEnable(GL11.GL_LIGHT0); // enables light0

        GL11.glEnable(GL11.GL_COLOR_MATERIAL); // enables opengl to use glColor3f to define material color
        GL11.glColorMaterial(GL11.GL_FRONT, GL11.GL_AMBIENT_AND_DIFFUSE); // tell opengl glColor3f effects the ambient and diffuse properties of material
    }
}