This example shows how to create extremely realistic reflections using the stencil buffer, clipping, and multi-texturing.

This is the Java port of the one of the NeHe OpenGL tutorials.

You can get complete IntelliJ IDEA project structure (all source, resources, build script, …) by downloading the source distribution from here.

The original post of the programmer who ported the examples can be found here.

 package demos.nehe.lesson26;

import demos.common.GLDisplay;

 * @author Pepijn Van Eeckhoudt
public class Lesson26 {
    public static void main(String[] args) {
        GLDisplay neheGLDisplay = GLDisplay.createGLDisplay("Lesson 26: Reflections");
        Renderer renderer = new Renderer();
        InputHandler inputHandler = new InputHandler(renderer, neheGLDisplay);

package demos.nehe.lesson26;

import demos.common.GLDisplay;

import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

class InputHandler extends KeyAdapter {
    private Renderer renderer;

    public InputHandler(Renderer renderer, GLDisplay glDisplay) {
        this.renderer = renderer;
                KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, 0), "Move ball up");
                KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, 0), "Move ball down");
                KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), "Zoom in");
                KeyStroke.getKeyStroke(KeyEvent.VK_Z, 0), "Zoom out");
                KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), 
                "Decrease X-axis rotation speed");
                KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), 
                "Increase X-axis rotation speed");
                KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), 
                "Decrease Y-axis rotation speed");
                KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), 
                "Increase Y-axis rotation speed");

    public void keyPressed(KeyEvent e) {
        processKeyEvent(e, true);

    public void keyReleased(KeyEvent e) {
        processKeyEvent(e, false);

    private void processKeyEvent(KeyEvent e, boolean pressed) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_A:
            case KeyEvent.VK_Z:
            case KeyEvent.VK_PAGE_UP:
            case KeyEvent.VK_PAGE_DOWN:
            case KeyEvent.VK_UP:
            case KeyEvent.VK_DOWN:
            case KeyEvent.VK_RIGHT:
            case KeyEvent.VK_LEFT:

package demos.nehe.lesson26;

import demos.common.TextureReader;


class Renderer implements GLEventListener {
    private float xrot = 0f;        // X Rotation
    private float yrot = 0f;        // Y Rotation
    private float xspeed = 0f;        // X Rotation Speed
    private boolean increaseXspeed;
    private boolean decreaseXspeed;

    private float yspeed = 0f;        // Y Rotation Speed
    private boolean increaseYspeed;
    private boolean decreaseYspeed;

    private float zoom = -7.0f;        // Depth Into The Screen
    private boolean zoomIn;
    private boolean zoomOut;

    private float height = 2.0f;      // Height Of Ball From Floor
    private boolean increaseHeight;
    private boolean decreaseHeight;

    private int textures[] = new int[3];    // Storage For 6 Textures (Modified)

    private float lightAmbient[] = {0.2f, 0.2f, 0.2f};  // Ambient Light is 20% white
    private float lightDiffuse[] = {1.0f, 1.0f, 1.0f};  // Diffuse Light is white
    // Position is somewhat in front of screen
    private float lightPosition[] = {0.0f, 0.0f, 2.0f};  

    private GLUquadric q;  // Storage For Our Quadratic Objects
    private GLU glu = new GLU();

    public Renderer() {

    public float getZoom() {
        return zoom;

    public void zoomIn(boolean zoom) {
        zoomIn = zoom;

    public void zoomOut(boolean zoom) {
        zoomOut = zoom;

    public void increaseHeight(boolean increase) {
        increaseHeight = increase;

    public void decreaseHeight(boolean decrease) {
        decreaseHeight = decrease;

    public void increaseXspeed(boolean increase) {
        increaseXspeed = increase;

    public void decreaseXspeed(boolean decrease) {
        decreaseXspeed = decrease;

    public void increaseYspeed(boolean increase) {
        increaseYspeed = increase;

    public void decreaseYspeed(boolean decrease) {
        decreaseYspeed = decrease;

    public void loadGLTextures(GL gl) throws IOException {
        String[] textureNames = new String[]{

        gl.glGenTextures(3, textures, 0);  // Create The Texture
        for (int loop = 0; loop < 3; loop++)  // Loop Through 5 Textures
            String textureName = textureNames[loop];
            TextureReader.Texture texture = TextureReader.readTexture(textureName);
            gl.glBindTexture(GL.GL_TEXTURE_2D, textures[loop]);
            gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB8, texture.getWidth(), 
                    texture.getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, 
            gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, 
            gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, 

    public void init(GLAutoDrawable glDrawable) {
        GL gl = glDrawable.getGL();

        try {
            loadGLTextures(gl);  // If Loading The Textures Failed
        } catch (IOException e) {
            throw new RuntimeException(e);
        gl.glShadeModel(GL.GL_SMOOTH);  // Enable Smooth Shading
        gl.glClearColor(0.2f, 0.5f, 1.0f, 1.0f);  // Background
        gl.glClearDepth(1.0f);  // Depth Buffer Setup
        gl.glClearStencil(0);  // Clear The Stencil Buffer To 0
        gl.glEnable(GL.GL_DEPTH_TEST);  // Enables Depth Testing
        gl.glDepthFunc(GL.GL_LEQUAL);  // The Type Of Depth Testing To Do
        // Really Nice Perspective Calculations
        gl.glEnable(GL.GL_TEXTURE_2D);  // Enable 2D Texture Mapping

        // Set The Ambient Lighting For Light0
        gl.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, lightAmbient, 0);      
        // Set The Diffuse Lighting For Light0
        gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, lightDiffuse, 0);      
        // Set The Position For Light0
        gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, lightPosition, 0);    

        gl.glEnable(GL.GL_LIGHT0);  // Enable Light 0
        gl.glEnable(GL.GL_LIGHTING);  // Enable Lighting

        q = glu.gluNewQuadric();    // Create A New Quadratic
        glu.gluQuadricNormals(q, GL.GL_SMOOTH);  // Generate Smooth Normals For The Quad
        glu.gluQuadricTexture(q, true);    // Enable Texture Coords For The Quad

        // Set Up Sphere Mapping
        // Set Up Sphere Mapping

    private void DrawObject(GL gl, GLU glu)      // Draw Our Ball
        gl.glColor3f(1.0f, 1.0f, 1.0f);      // Set Color To White
        gl.glBindTexture(GL.GL_TEXTURE_2D, textures[1]);// Select Texture 2 (1)
        glu.gluSphere(q, 0.35f, 32, 16);    // Draw First Sphere

        gl.glBindTexture(GL.GL_TEXTURE_2D, textures[2]);// Select Texture 3 (2)
        gl.glColor4f(1.0f, 1.0f, 1.0f, 0.4f);  // Set Color To White With 40% Alpha
        gl.glEnable(GL.GL_BLEND);      // Enable Blending
        // Set Blending Mode To Mix Based On SRC Alpha
        gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE);  
        gl.glEnable(GL.GL_TEXTURE_GEN_S);  // Enable Sphere Mapping
        gl.glEnable(GL.GL_TEXTURE_GEN_T);  // Enable Sphere Mapping

        glu.gluSphere(q, 0.35f, 32, 16); // Draw Another Sphere Using New Texture
        // Textures Will Mix Creating A MultiTexture Effect (Reflection)
        gl.glDisable(GL.GL_TEXTURE_GEN_S);  // Disable Sphere Mapping
        gl.glDisable(GL.GL_TEXTURE_GEN_T);  // Disable Sphere Mapping
        gl.glDisable(GL.GL_BLEND);    // Disable Blending

    private void DrawFloor(GL gl)  // Draws The Floor
        gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0]); // Select Texture 1 (0)
        gl.glBegin(GL.GL_QUADS);                // Begin Drawing A Quad
        gl.glNormal3f(0.0f, 1.0f, 0.0f);  // Normal Pointing Up
        gl.glTexCoord2f(0.0f, 1.0f);    // Bottom Left Of Texture
        gl.glVertex3f(-2.0f, 0.0f, 2.0f);  // Bottom Left Corner Of Floor

        gl.glTexCoord2f(0.0f, 0.0f);    // Top Left Of Texture
        gl.glVertex3f(-2.0f, 0.0f, -2.0f);  // Top Left Corner Of Floor

        gl.glTexCoord2f(1.0f, 0.0f);    // Top Right Of Texture
        gl.glVertex3f(2.0f, 0.0f, -2.0f);  // Top Right Corner Of Floor

        gl.glTexCoord2f(1.0f, 1.0f);    // Bottom Right Of Texture
        gl.glVertex3f(2.0f, 0.0f, 2.0f);  // Bottom Right Corner Of Floor
        gl.glEnd();        // Done Drawing The Quad

    private void update() {
        if (zoomIn)
            zoom += 0.05f;
        if (zoomOut)
            zoom -= 0.05f;
        if (increaseHeight)
            height += 0.03f;
        if (decreaseHeight)
            height -= 0.03f;
        if (increaseXspeed)
            xspeed += 0.08f;
        if (decreaseXspeed)
            xspeed -= 0.08f;
        if (increaseYspeed)
            yspeed += 0.08f;
        if (decreaseYspeed)
            yspeed -= 0.08f;

    public void display(GLAutoDrawable glDrawable) {
        GL gl = glDrawable.getGL();

        // Clear Screen, Depth Buffer & Stencil Buffer

        // Clip Plane Equations
        // Plane Equation To Use For The Reflected Objects
        double eqr[] = {0.0f, -1.0f, 0.0f, 0.0f};

        gl.glLoadIdentity();    // Reset The Modelview Matrix
        // Zoom And Raise Camera Above The Floor (Up 0.6 Units)
        gl.glTranslatef(0.0f, -0.6f, zoom);  
        gl.glColorMask(false, false, false, false);  // Set Color Mask
        // Enable Stencil Buffer For "marking" The Floor
        // Always Passes, 1 Bit Plane, 1 As Mask
        gl.glStencilFunc(GL.GL_ALWAYS, 1, 1);  
        // We Set The Stencil Buffer To 1 Where We Draw Any Polygon
        gl.glStencilOp(GL.GL_KEEP, GL.GL_KEEP, GL.GL_REPLACE);      
        // Keep If Test Fails, Keep If Test Passes But Buffer Test Fails
        // Replace If Test Passes
        gl.glDisable(GL.GL_DEPTH_TEST);    // Disable Depth Testing
        DrawFloor(gl);    // Draw The Floor (Draws To The Stencil Buffer)
        // We Only Want To Mark It In The Stencil Buffer
        gl.glEnable(GL.GL_DEPTH_TEST);  // Enable Depth Testing
        gl.glColorMask(true, true, true, true);  // Set Color Mask to TRUE, TRUE, TRUE, TRUE
        gl.glStencilFunc(GL.GL_EQUAL, 1, 1);  // We Draw Only Where The Stencil Is 1
        // (I.E. Where The Floor Was Drawn)
        // Don't Change The Stencil Buffer
        gl.glStencilOp(GL.GL_KEEP, GL.GL_KEEP, GL.GL_KEEP);  
        gl.glEnable(GL.GL_CLIP_PLANE0);  // Enable Clip Plane For Removing Artifacts
        // (When The Object Crosses The Floor)
        gl.glClipPlane(GL.GL_CLIP_PLANE0, eqr, 0);  // Equation For Reflected Objects
        gl.glPushMatrix();        // Push The Matrix Onto The Stack
        gl.glScalef(1.0f, -1.0f, 1.0f);    // Mirror Y Axis
        gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, lightPosition, 0);  // Set Up Light0
        gl.glTranslatef(0.0f, height, 0.0f);  // Position The Object
        gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);  // Rotate Local Coordinate System On X Axis
        gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);  // Rotate Local Coordinate System On Y Axis
        DrawObject(gl, glu);      // Draw The Sphere (Reflection)
        gl.glPopMatrix();      // Pop The Matrix Off The Stack
        gl.glDisable(GL.GL_CLIP_PLANE0);  // Disable Clip Plane For Drawing The Floor
        gl.glDisable(GL.GL_STENCIL_TEST);  // We Don't Need The Stencil Buffer Any More (Disable)
        gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, lightPosition, 0);  // Set Up Light0 Position
        gl.glEnable(GL.GL_BLEND);    // Enable Blending (Otherwise The Reflected Object Wont Show)
        gl.glDisable(GL.GL_LIGHTING);    // Since We Use Blending, We Disable Lighting
        gl.glColor4f(1.0f, 1.0f, 1.0f, 0.8f);  // Set Color To White With 80% Alpha
        // Blending Based On Source Alpha And 1 Minus Dest Alpha
        gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);  
        DrawFloor(gl);    // Draw The Floor To The Screen
        gl.glEnable(GL.GL_LIGHTING);  // Enable Lighting
        gl.glDisable(GL.GL_BLEND);  // Disable Blending
        gl.glTranslatef(0.0f, height, 0.0f);  // Position The Ball At Proper Height
        gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);  // Rotate On The X Axis
        gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);  // Rotate On The Y Axis
        DrawObject(gl, glu);  // Draw The Ball
        xrot += xspeed;    // Update X Rotation Angle By xrotspeed
        yrot += yspeed;    // Update Y Rotation Angle By yrotspeed

    public void reshape(GLAutoDrawable glDrawable, int x, int y, int w, int h) {
        if (h == 0) h = 1;
        GL gl = glDrawable.getGL();

        // Reset The Current Viewport And Perspective Transformation
        gl.glViewport(0, 0, w, h);     
        gl.glMatrixMode(GL.GL_PROJECTION);  // Select The Projection Matrix
        gl.glLoadIdentity();                // Reset The Projection Matrix
        // Calculate The Aspect Ratio Of The Window
        glu.gluPerspective(45.0f, (float) w / (float) h, 0.1f, 100.0f);  
        gl.glMatrixMode(GL.GL_MODELVIEW); // Select The Modelview Matrix
        gl.glLoadIdentity();              // Reset The ModalView Matrix

    public void displayChanged(GLAutoDrawable glDrawable, boolean b, boolean b1) {