View Javadoc
1   package org.newdawn.slick.opengl.renderer;
2   
3   import java.nio.ByteBuffer;
4   import java.nio.DoubleBuffer;
5   import java.nio.FloatBuffer;
6   import java.nio.IntBuffer;
7   
8   import org.lwjgl.opengl.EXTSecondaryColor;
9   import org.lwjgl.opengl.GL11;
10  import org.lwjgl.opengl.GLContext;
11  
12  import javax.annotation.Nonnull;
13  
14  /**
15   * The default OpenGL renderer, uses immediate mode for everything
16   * 
17   * @author kevin
18   */
19  public class ImmediateModeOGLRenderer implements SGL {
20      /** The width of the display */
21      private int width;
22      /** The height of the display */
23      private int height;
24      /** The current colour */
25      @Nonnull
26      private final float[] current = new float[] {1,1,1,1};
27      /** The global colour scale */
28      float alphaScale = 1;
29  
30      /**
31       * Initializes default states (texturing, shade model, disable depth test, etc).
32       * @see org.newdawn.slick.opengl.renderer.SGL#initDisplay(int, int)
33       */
34      public void initDisplay(int width, int height) {
35          this.width = width;
36          this.height = height;
37  
38          //String extensions = GL11.glGetString(GL11.GL_EXTENSIONS);
39  
40          GL11.glEnable(GL11.GL_TEXTURE_2D);
41          GL11.glShadeModel(GL11.GL_SMOOTH);
42          GL11.glDisable(GL11.GL_DEPTH_TEST);
43          GL11.glDisable(GL11.GL_LIGHTING);
44          
45          GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
46          GL11.glClearDepth(1);                                       
47          
48          GL11.glEnable(GL11.GL_BLEND);
49          GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
50          
51          GL11.glViewport(0,0,width,height);
52          GL11.glMatrixMode(GL11.GL_MODELVIEW);
53      }
54  
55      /**
56       * Re-initializes the orthographic display to the given size.
57       * @see org.newdawn.slick.opengl.renderer.SGL#enterOrtho(int, int)
58       */
59      public void enterOrtho(int xsize, int ysize) {
60          this.width = xsize;
61          this.height = ysize;
62          GL11.glMatrixMode(GL11.GL_PROJECTION);
63          GL11.glLoadIdentity();
64          GL11.glOrtho(0, width, height, 0, 1, -1);
65          GL11.glMatrixMode(GL11.GL_MODELVIEW);
66          GL11.glLoadIdentity();
67          GL11.glViewport(0,0,width,height);
68      }
69  
70      /**
71       * @see org.newdawn.slick.opengl.renderer.SGL#glBegin(int)
72       */
73      public void glBegin(int geomType) {
74          GL11.glBegin(geomType);
75      }
76  
77      /**
78       * @see org.newdawn.slick.opengl.renderer.SGL#glBindTexture(int, int)
79       */
80      public void glBindTexture(int target, int id) {
81          GL11.glBindTexture(target, id);
82      }
83  
84      /**
85       * @see org.newdawn.slick.opengl.renderer.SGL#glBlendFunc(int, int)
86       */
87      public void glBlendFunc(int src, int dest) {
88          GL11.glBlendFunc(src, dest);
89      }
90  
91      /**
92       * @see org.newdawn.slick.opengl.renderer.SGL#glCallList(int)
93       */
94      public void glCallList(int id) {
95          GL11.glCallList(id);
96      }
97  
98      /**
99       * @see org.newdawn.slick.opengl.renderer.SGL#glClear(int)
100      */
101     public void glClear(int value) {
102         GL11.glClear(value);
103     }
104 
105     /**
106      * @see org.newdawn.slick.opengl.renderer.SGL#glClearColor(float, float, float, float)
107      */
108     public void glClearColor(float red, float green, float blue, float alpha) {
109         GL11.glClearColor(red, green, blue, alpha);
110     }
111 
112     /**
113      * @see org.newdawn.slick.opengl.renderer.SGL#glClipPlane(int, java.nio.DoubleBuffer)
114      */
115     public void glClipPlane(int plane, DoubleBuffer buffer) {
116         GL11.glClipPlane(plane, buffer);
117     }
118 
119     /**
120      * @see org.newdawn.slick.opengl.renderer.SGL#glColor4f(float, float, float, float)
121      */
122     public void glColor4f(float r, float g, float b, float a) {
123         a *= alphaScale;
124 
125         current[0] = r;
126         current[1] = g;
127         current[2] = b;
128         current[3] = a;
129 
130         GL11.glColor4f(r, g, b, a);
131     }
132 
133     /**
134      * @see org.newdawn.slick.opengl.renderer.SGL#glColorMask(boolean, boolean, boolean, boolean)
135      */
136     public void glColorMask(boolean red, boolean green, boolean blue, boolean alpha) {
137         GL11.glColorMask(red, green, blue, alpha);
138     }
139 
140     /**
141      * @see org.newdawn.slick.opengl.renderer.SGL#glCopyTexImage2D(int, int, int, int, int, int, int, int)
142      */
143     public void glCopyTexImage2D(int target, int level, int internalFormat, int x, int y, int width, int height, int border) {
144         GL11.glCopyTexImage2D(target, level, internalFormat, x, y, width, height, border);
145     }
146 
147     /**
148      * @see org.newdawn.slick.opengl.renderer.SGL#glDeleteTextures(java.nio.IntBuffer)
149      */
150     public void glDeleteTextures(IntBuffer buffer) {
151         GL11.glDeleteTextures(buffer);
152     }
153 
154     /**
155      * @see org.newdawn.slick.opengl.renderer.SGL#glDisable(int)
156      */
157     public void glDisable(int item) {
158         GL11.glDisable(item);
159     }
160 
161     /**
162      * @see org.newdawn.slick.opengl.renderer.SGL#glEnable(int)
163      */
164     public void glEnable(int item) {
165         GL11.glEnable(item);
166     }
167 
168     /**
169      * @see org.newdawn.slick.opengl.renderer.SGL#glEnd()
170      */
171     public void glEnd() {
172         GL11.glEnd();
173     }
174 
175     /**
176      * @see org.newdawn.slick.opengl.renderer.SGL#glEndList()
177      */
178     public void glEndList() {
179         GL11.glEndList();
180     }
181 
182     /**
183      * @see org.newdawn.slick.opengl.renderer.SGL#glGenLists(int)
184      */
185     public int glGenLists(int count) {
186         return GL11.glGenLists(count);
187     }
188 
189     /**
190      * @see org.newdawn.slick.opengl.renderer.SGL#glGetFloat(int, java.nio.FloatBuffer)
191      */
192     public void glGetFloat(int id, FloatBuffer ret) {
193         GL11.glGetFloat(id, ret);
194     }
195 
196     /**
197      * @see org.newdawn.slick.opengl.renderer.SGL#glGetInteger(int, java.nio.IntBuffer)
198      */
199     public void glGetInteger(int id, IntBuffer ret) {
200         GL11.glGetInteger(id, ret);
201     }
202 
203     /**
204      * @see org.newdawn.slick.opengl.renderer.SGL#glGetTexImage(int, int, int, int, java.nio.ByteBuffer)
205      */
206     public void glGetTexImage(int target, int level, int format, int type, ByteBuffer pixels) {
207         GL11.glGetTexImage(target, level, format, type, pixels);
208     }
209 
210     /**
211      * @see org.newdawn.slick.opengl.renderer.SGL#glLineWidth(float)
212      */
213     public void glLineWidth(float width) {
214         GL11.glLineWidth(width);
215     }
216 
217     /**
218      * @see org.newdawn.slick.opengl.renderer.SGL#glLoadIdentity()
219      */
220     public void glLoadIdentity() {
221         GL11.glLoadIdentity();
222     }
223 
224     /**
225      * @see org.newdawn.slick.opengl.renderer.SGL#glNewList(int, int)
226      */
227     public void glNewList(int id, int option) {
228         GL11.glNewList(id, option);
229     }
230 
231     /**
232      * @see org.newdawn.slick.opengl.renderer.SGL#glPointSize(float)
233      */
234     public void glPointSize(float size) {
235         GL11.glPointSize(size);
236     }
237 
238     /**
239      * @see org.newdawn.slick.opengl.renderer.SGL#glPopMatrix()
240      */
241     public void glPopMatrix() {
242         GL11.glPopMatrix();
243     }
244 
245     /**
246      * @see org.newdawn.slick.opengl.renderer.SGL#glPushMatrix()
247      */
248     public void glPushMatrix() {
249         GL11.glPushMatrix();
250     }
251 
252     /**
253      * @see org.newdawn.slick.opengl.renderer.SGL#glReadPixels(int, int, int, int, int, int, java.nio.ByteBuffer)
254      */
255     public void glReadPixels(int x, int y, int width, int height, int format, int type, ByteBuffer pixels) {
256         GL11.glReadPixels(x, y, width, height, format, type, pixels);
257     }
258 
259     /**
260      * @see org.newdawn.slick.opengl.renderer.SGL#glRotatef(float, float, float, float)
261      */
262     public void glRotatef(float angle, float x, float y, float z) {
263         GL11.glRotatef(angle, x, y, z);
264     }
265 
266     /**
267      * @see org.newdawn.slick.opengl.renderer.SGL#glScalef(float, float, float)
268      */
269     public void glScalef(float x, float y, float z) {
270         GL11.glScalef(x, y, z);
271     }
272 
273     /**
274      * @see org.newdawn.slick.opengl.renderer.SGL#glScissor(int, int, int, int)
275      */
276     public void glScissor(int x, int y, int width, int height) {
277         GL11.glScissor(x, y, width, height);
278     }
279 
280     /**
281      * @see org.newdawn.slick.opengl.renderer.SGL#glTexCoord2f(float, float)
282      */
283     public void glTexCoord2f(float u, float v) {
284         GL11.glTexCoord2f(u, v);
285     }
286 
287     /**
288      * @see org.newdawn.slick.opengl.renderer.SGL#glTexEnvi(int, int, int)
289      */
290     public void glTexEnvi(int target, int mode, int value) {
291         GL11.glTexEnvi(target, mode, value);
292     }
293 
294     /**
295      * @see org.newdawn.slick.opengl.renderer.SGL#glTranslatef(float, float, float)
296      */
297     public void glTranslatef(float x, float y, float z) {
298         GL11.glTranslatef(x, y, z);
299     }
300 
301     /**
302      * @see org.newdawn.slick.opengl.renderer.SGL#glVertex2f(float, float)
303      */
304     public void glVertex2f(float x, float y) {
305         GL11.glVertex2f(x, y);
306     }
307 
308     /**
309      * @see org.newdawn.slick.opengl.renderer.SGL#glVertex3f(float, float, float)
310      */
311     public void glVertex3f(float x, float y, float z) {
312         GL11.glVertex3f(x, y, z);
313     }
314 
315     /**
316      * @see org.newdawn.slick.opengl.renderer.SGL#flush()
317      */
318     public void flush() {
319     }
320 
321     /**
322      * @see org.newdawn.slick.opengl.renderer.SGL#glTexParameteri(int, int, int)
323      */
324     public void glTexParameteri(int target, int param, int value) {
325         GL11.glTexParameteri(target, param, value);
326     }
327 
328     /**
329      * @see org.newdawn.slick.opengl.renderer.SGL#getCurrentColor()
330      */
331     @Nonnull
332     public float[] getCurrentColor() {
333         return current;
334     }
335 
336     /**
337      * @see org.newdawn.slick.opengl.renderer.SGL#glDeleteLists(int, int)
338      */
339     public void glDeleteLists(int list, int count) {
340         GL11.glDeleteLists(list, count);
341     }
342 
343     /**
344      * @see org.newdawn.slick.opengl.renderer.SGL#glClearDepth(float)
345      */
346     public void glClearDepth(float value) {
347         GL11.glClearDepth(value);
348     }
349 
350     /**
351      * @see org.newdawn.slick.opengl.renderer.SGL#glDepthFunc(int)
352      */
353     public void glDepthFunc(int func) {
354         GL11.glDepthFunc(func);
355     }
356 
357     /**
358      * @see org.newdawn.slick.opengl.renderer.SGL#glDepthMask(boolean)
359      */
360     public void glDepthMask(boolean mask) {
361         GL11.glDepthMask(mask);
362     }
363 
364     /**
365      * @see org.newdawn.slick.opengl.renderer.SGL#setGlobalAlphaScale(float)
366      */
367     public void setGlobalAlphaScale(float alphaScale) {
368         this.alphaScale = alphaScale;
369     }
370 
371     /**
372      * @see org.newdawn.slick.opengl.renderer.SGL#glLoadMatrix(java.nio.FloatBuffer)
373      */
374     public void glLoadMatrix(FloatBuffer buffer) {
375         GL11.glLoadMatrix(buffer);
376     }
377 
378     /*
379      * (non-Javadoc)
380      * @see org.newdawn.slick.opengl.renderer.SGL#glGenTextures(java.nio.IntBuffer)
381      */
382     public void glGenTextures(IntBuffer ids) {
383         GL11.glGenTextures(ids);
384     }
385 
386     /*
387      * (non-Javadoc)
388      * @see org.newdawn.slick.opengl.renderer.SGL#glGetError()
389      */
390     public void glGetError() {
391         GL11.glGetError();
392     }
393 
394     /*
395      * (non-Javadoc)
396      * @see org.newdawn.slick.opengl.renderer.SGL#glTexImage2D(int, int, int, int, int, int, int, int, java.nio.ByteBuffer)
397      */
398     public void glTexImage2D(int target, int i, int dstPixelFormat,
399             int width, int height, int j, int srcPixelFormat,
400             int glUnsignedByte, ByteBuffer textureBuffer) {
401         GL11.glTexImage2D(target, i, dstPixelFormat, width, height, j, srcPixelFormat,glUnsignedByte,textureBuffer);
402     }
403 
404     public void glTexSubImage2D(int glTexture2d, int i, int pageX, int pageY,
405             int width, int height, int glBgra, int glUnsignedByte,
406             ByteBuffer scratchByteBuffer) {
407         GL11.glTexSubImage2D(glTexture2d, i, pageX, pageY, width, height, glBgra, glUnsignedByte, scratchByteBuffer);
408     }
409 
410     /*
411      * (non-Javadoc)
412      * @see org.newdawn.slick.opengl.renderer.SGL#canTextureMirrorClamp()
413      */
414     public boolean canTextureMirrorClamp() {
415         return GLContext.getCapabilities().GL_EXT_texture_mirror_clamp;
416     }
417 
418     /*
419      * (non-Javadoc)
420      * @see org.newdawn.slick.opengl.renderer.SGL#canSecondaryColor()
421      */
422     public boolean canSecondaryColor() {
423         return GLContext.getCapabilities().GL_EXT_secondary_color;
424     }
425 
426     /*
427      * (non-Javadoc)
428      * @see org.newdawn.slick.opengl.renderer.SGL#glSecondaryColor3ubEXT(byte, byte, byte)
429      */
430     public void glSecondaryColor3ubEXT(byte b, byte c, byte d) {
431         EXTSecondaryColor.glSecondaryColor3ubEXT(b,c,d);
432     }
433 }