Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

LWJGL Fog Coordinates

setpreferredsize

  • Please log in to reply
No replies to this topic

#1 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 766 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:PHP

Posted 18 December 2011 - 08:15 PM

*Edit: Added a simple controller*

Example 6-9 from the red book written in LWJGL:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.nio.FloatBuffer;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

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 static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL14.*;
import static org.lwjgl.opengl.GL15.*;

public class FogCoord {
	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 Coordinates Test";
	private static final int FPS = 70;	//frames per second
	
	private boolean isRunning;	//variable to tell if program is running or not
	
	/* ------- Code added for Fog Coord --------*/
	private float f1, f2, f3;
	/* ------- End Code added for Fog Coord --------*/
	
	/*---------Added for Controller purposes -----------*/
	private JFrame frame;
	private JPanel f1Panel, f2Panel, f3Panel, zoomPanel;
	private JSlider f1Slider, f2Slider, f3Slider, zoomSlider;
	private static final int MIN = -20;
	private static final int MAX = 20;
	private float zoom = 0.0f;
	/*---------End - Added for Controller purposes -----------*/

	
	public static void main(String[] args) {
		FogCoord test = new FogCoord();
		test.run();
	}
	
	/* -------- Constructor sets up GUI --------*/
	public FogCoord() {
		
		frame = new JFrame(WINDOW_TITLE + " Controller");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel pane = new JPanel();
		pane.setPreferredSize(new Dimension(600,350));
		
		f1Panel = new JPanel(new BorderLayout());
		f1Panel.setBorder(BorderFactory.createTitledBorder("Vertex f1"));
		f1Panel.setPreferredSize( new Dimension(150, 240));
		f1Slider = new JSlider(JSlider.VERTICAL, MIN, MAX, 1);
		f1Slider.setMajorTickSpacing(10);
		f1Slider.setMinorTickSpacing(1);
		f1Slider.setPaintTicks(true);
		f1Slider.setPaintLabels(true);
		f1Slider.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent e) {
				JSlider source = (JSlider)e.getSource();
				if (source.getValueIsAdjusting()) {
			        int f = source.getValue();
			        f1 = f;
			    }
			}
			
		});
		f1Panel.add(f1Slider);
		
		f2Panel = new JPanel();
		f2Panel.setBorder(BorderFactory.createTitledBorder("Vertex f2"));
		f2Panel.setPreferredSize( new Dimension(150, 240));
		f2Slider = new JSlider(JSlider.VERTICAL, MIN, MAX, 5);
		f2Slider.setMajorTickSpacing(10);
		f2Slider.setMinorTickSpacing(1);
		f2Slider.setPaintTicks(true);
		f2Slider.setPaintLabels(true);
		f2Slider.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent e) {
				JSlider source = (JSlider)e.getSource();
				if (source.getValueIsAdjusting()) {
			        int f = source.getValue();
			        f2 = f;
			    }
			}
			
		});
		f2Panel.add(f2Slider);
		
		f3Panel = new JPanel();
		f3Panel.setBorder(BorderFactory.createTitledBorder("Vertex f3"));
		f3Panel.setPreferredSize( new Dimension(150, 240));
		f3Slider = new JSlider(JSlider.VERTICAL, MIN, MAX, 10);
		f3Slider.setMajorTickSpacing(10);
		f3Slider.setMinorTickSpacing(1);
		f3Slider.setPaintTicks(true);
		f3Slider.setPaintLabels(true);
		f3Slider.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent e) {
				JSlider source = (JSlider)e.getSource();
				if (source.getValueIsAdjusting()) {
			        int f = source.getValue();
			        f3 = f;
			    }
			}
			
		});
		f3Panel.add(f3Slider);
		
		zoomPanel = new JPanel();
		zoomPanel.setPreferredSize(new Dimension(550, 100));
		zoomPanel.setBorder(BorderFactory.createTitledBorder("Other Options"));
		zoomSlider = new JSlider(JSlider.HORIZONTAL, -100, 100, 0);
		zoomSlider.setMajorTickSpacing(50);
		zoomSlider.setMinorTickSpacing(25);
		zoomSlider.setPaintTicks(true);
		zoomSlider.setPaintLabels(true);
		zoomSlider.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent e) {
				JSlider source = (JSlider)e.getSource();
				if (source.getValueIsAdjusting()) {
			        int f = source.getValue();
			        zoom = (float) (f/10.0);
			    }
			}
			
		});
	
		
		zoomPanel.add(zoomSlider);
		zoomPanel.add(new JLabel("Change from GL_FRAGMENT_DEPTH and GL_FOG_COORD by pressing 4 and 5 respectively."));
	
		

		pane.add(f1Panel, BorderLayout.WEST);
		pane.add(f2Panel, BorderLayout.CENTER);
		pane.add(f3Panel, BorderLayout.EAST);
		pane.add(zoomPanel, BorderLayout.SOUTH);
		
		frame.setContentPane(pane);
		frame.pack();
		frame.setVisible(true);
	}

	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, zoom);
		
		glColor3f(1.0f, 0.75f, 0.0f);
		glBegin(GL_TRIANGLES); {
			glFogCoordf(f1);
			glVertex3f(2.0f, -2.0f, 0.0f);
			glFogCoordf(f2);
			glVertex3f(-2.0f, 0.0f, -5.0f);
			glFogCoordf(f3);
			glVertex3f(0.0f, 2.0f, -10.0f);
		} glEnd();

	}
	
	
	private void getInput() {
		
		if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {	// if user presses escape key
			isRunning = false;
			frame.dispose();
		}
		if( Display.isCloseRequested()) {	// if user closes window
			isRunning = false;
			frame.dispose();
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_4)){
			glFogi(GL_FOG_COORD_SRC, GL_FRAGMENT_DEPTH);
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_5)){
			glFogi(GL_FOG_COORD_SRC, GL_FOG_COORD);
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_1)){
			f1 += 0.25f;
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_2)){
			f2 += 0.25f;
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_3)){
			f3 += 0.25f;
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_8)){
			f1 -= 0.25f;
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_9)){
			f2 -= 0.25f;
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_0)){
			f3 -= 0.25f;
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_UP)){
			glTranslatef(0f,0f,zoom-=0.25f);
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_DOWN)){
			glTranslatef(0f,0f,zoom+=0.25f);
		}

	}
	
	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); 
		
		/* ------- Code added for Fog Coord --------*/
		FloatBuffer fogColor = BufferUtils.createFloatBuffer(4);
		fogColor.put(0f).put(0.25f).put(0.25f).put(1.0f).flip();
		
		f1 = 1.0f;
		f2 = 5.0f;
		f3 = 10.0f;
		
		glEnable(GL_FOG); {
			glFogi(GL_FOG_MODE, GL_EXP);
			glFog(GL_FOG_COLOR, fogColor);
			glFogf(GL_FOG_DENSITY, 0.25f);
			glHint(GL_FOG_HINT, GL_DONT_CARE);
			glFogi(GL_FOG_COORD_SRC, GL_FOG_COORD);
			glClearColor(0.0f, 0.25f, 0.25f, 1.0f);
		}
		
		/* ------- End - Code added for Fog Coord --------*/
	}
	
	
}
fog Coordinates 3.png

Edited by lethalwire, 18 December 2011 - 09:24 PM.

  • 0





Also tagged with one or more of these keywords: setpreferredsize

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download