Introduction to BigTexture BigTexture helps you to handle very big textures (> 512x512) like landscapes. Normally OpenGL can only load images whose width and height are 64, 128, 256 or 512. BigTexture can load images of any size. If the images are too big they will automatically be split into smaller tiles. The second advantage of BigTexture is that it loads only the currently visible tiles into graphics card memory, so it is able to handle images that are too big to fit into your graphics card memory. How to use BigTexture 1. Initializing OpenGL:
058 OpenGLUtils.initFullscreen("JWorms");
2. Loading the texture:
034 BigTexture textureLandscape = null;
065 textureLandscape = new BigTexture("tutorial/landscape.png", BigTexture.FORMAT_RGB, false);
3. Generating the textures (loads the textures into OpenGL memory)
075 OpenGLTextureInternal.glGenTextures();
4. Draw the texture within our main loop
082 // close with Alt+F4 or Escape
083 while(!OpenGLUtils.isCloseRequested() && !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
084 Mouse.poll();
085 Keyboard.poll();
156 // to draw the map we don't need blend or alpha test
157 GL.glDisable(GL.GL_BLEND);
158 GL.glDisable(GL.GL_ALPHA_TEST);
159
160 textureLandscape.draw(position.x, position.y);
161
162 GL.glEnable(GL.GL_BLEND);
099 // update internal window state
100 Window.update();
101 // swap buffers
102 Window.paint();
105 }
5. Destroy OpenGL
108 OpenGLUtils.destroy();
Download and installation Full source code of BigTextureTutorial.java.
001
002 package tutorial;
003
004 import jworms.openGL.BitmapFont;
005 import jworms.openGL.OpenGLUtils;
006 import jworms.openGL.OpenGLTextureInternal;
007 import jworms.openGL.BigTexture;
008 import jworms.openAL.OpenALUtils;
009 import jworms.openAL.OpenALWave;
010 import jworms.openAL.OpenALListener;
011 import jworms.openAL.OpenALSource;
012
013 import java.awt.*;
014
015 import org.lwjgl.input.Mouse;
016 import org.lwjgl.input.Keyboard;
017 import org.lwjgl.opengl.Window;
018 import org.lwjgl.opengl.GL;
019 import org.lwjgl.Display;
020 import org.lwjgl.Sys;
021 import org.lwjgl.openal.AL;
022
023 /**
024 * Demonstrates the usage of jworms.openGL.BigTexture and jworms.openAL.*
025 *
026 * usage: java BigTextureTutorial
027 * (close with Alt+F4)
028 */
029 public class BigTextureTutorial{
030 // bitmap font to display the frame rate
031 private BitmapFont font = null;
032
033 // landscape texture (big texture)
034 BigTexture textureLandscape = null;
035
036 // Sound source.
037 private OpenALSource soundSource = new OpenALSource();
038 // Hit sound
039 private OpenALWave soundHit = null;
040
041 // upper left corner of the texture on the screen
042 private Point position = new Point();
043 // motion of the texture
044 private Point motion = new Point(-7, -5);
045
046 // frames per second
047 private long framesPerSecond = 0;
048
049 public static void main(String[] arguments){
050 new BigTextureTutorial();
051 }
052
053 /**
054 * Initialized OpenGL fullscreen and renders a big landscape texture.
055 */
056 public BigTextureTutorial(){
057 // initialize OpenGL
058 OpenGLUtils.initFullscreen("JWorms");
059 // initialize OpenAL
060 OpenALUtils.alInit();
061
062 // load the landscape textur, font and sound
063 try{
064 font = new BitmapFont("images/fonts/font.dat");
065 textureLandscape = new BigTexture("tutorial/landscape.png",
066 BigTexture.FORMAT_RGB, false);
067
068 soundHit = OpenALWave.create("tutorial/hit.wav");
069 } catch(Exception e){
070 System.out.println("Failed starting game due to ");
071 e.printStackTrace();
072 System.exit(-1);
073 }
074
075 // generate all loaded textures and sounds
076 OpenGLTextureInternal.glGenTextures();
077 OpenALWave.alGenWaves();
078
079 // update the frames counter every second
080 long framesCounter = 0;
081 double lastTime = getTime();
082
083 // close with Alt+F4 or Escape
084 while(!OpenGLUtils.isCloseRequested() && !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
085 Mouse.poll();
086 Keyboard.poll();
087
088 motion();
089 // render the landscape
090 render();
091
092 // update frames counter every second (uses high resolution timer of LWJGL)
093 if(getTime() - lastTime > 1){
094 framesPerSecond = Math.round(framesCounter / (getTime() - lastTime));
095
096 framesCounter = 0;
097 lastTime = getTime();
098 }
099
100 // update internal window state
101 Window.update();
102 // swap buffers
103 Window.paint();
104
105 framesCounter++;
106 }
107
108 // dispose OpenGL
109 OpenGLUtils.destroy();
110 // dispose OpenAL
111 OpenALUtils.alExit();
112 }
113
114 /**
115 * Returns the time of the hres timer (high resolution time) in seconds.
116 *
117 * @return time in seconds
118 */
119 private double getTime(){
120 return ((double) Sys.getTime()) / Sys.getTimerResolution();
121 }
122
123 /**
124 * Lets the texture scroll. (just for fun)
125 */
126 private void motion(){
127 position.x += motion.x;
128 position.y += motion.y;
129
130 // mirroring motion
131 if(position.x < Display.getWidth() - textureLandscape.getImage().getWidth()){
132 position.x = Display.getWidth() - textureLandscape.getImage().getWidth();
133 motion.x = -motion.x;
134 playSound();
135 }
136 if(position.x > 0){
137 position.x = 0;
138 motion.x = -motion.x;
139 playSound();
140 }
141 if(position.y < Display.getHeight() - textureLandscape.getImage().getHeight()){
142 position.y = Display.getHeight() - textureLandscape.getImage().getHeight();
143 motion.y = -motion.y;
144 playSound();
145 }
146 if(position.y > 0){
147 position.y = 0;
148 motion.y = -motion.y;
149 playSound();
150 }
151 }
152
153 /**
154 * Renders the big texture with jworms.OpenGL and LWJGL OpenGL.
155 */
156 private void render(){
157 // to draw the map we don't need blend or alpha test
158 GL.glDisable(GL.GL_BLEND);
159 GL.glDisable(GL.GL_ALPHA_TEST);
160
161 textureLandscape.draw(position.x, position.y);
162
163 GL.glEnable(GL.GL_BLEND);
164
165 // show frames per second
166 font.drawString("frames per second: " + framesPerSecond, 20, 20);
167
168 // update openAL listener position
169 OpenALListener.setPos(position.x + Display.getWidth() / 2,
170 position.y + Display.getHeight() / 2, 100);
171 }
172
173 /**
174 * Plays a sound with jworms.OpenAL and LWJGL OpenAL.
175 */
176 private void playSound(){
177 if(soundSource.bind()){
178 soundSource.stop();
179 soundSource.setPos(position.x, position.y, 0);
180 soundSource.setPropf(AL.AL_GAIN, 1);
181 soundSource.play(soundHit);
182 }
183 }
184 }
|
Java2html
|
|