Mesh replaces Polygon
[org.ibex.core.git] / src / org / ibex / plat / OpenGL.java
1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [LGPL]
2 // Author: Brian Alliet
3
4 package org.ibex.plat;
5 import org.ibex.js.*;
6 import org.ibex.util.*;
7 import org.ibex.graphics.*;
8 import org.ibex.core.*;
9 import org.ibex.net.*;
10
11 abstract class OpenGL {
12     static final boolean pretendToBeACrappyVideoCard = false;
13     boolean rectangularTextures;
14     boolean hasRectangularTextures() { return rectangularTextures; }
15     int maxTexSize;
16     int maxRectTexSize;
17     float glVersion;
18     String version;
19     String renderer;
20     String vendor;
21     
22     private native void natInit();
23     
24     private static float parseVersion(String s) {
25         int end = s.indexOf(' ');
26         if(end < 0) end = s.length();
27         try {
28             return Float.parseFloat(s.substring(0,end));
29         } catch(NumberFormatException e) {
30             return 1.1f; // just a guess
31         }
32     }
33     
34     // This MUST be called after OpenGL is instansiated (and activateSharedContext is functioning)
35     public void init() throws NotSupportedException {
36         natInit();
37         glVersion = parseVersion(version);
38         if(glVersion < 1.1) throw new NotSupportedException("OpenGL 1.1 or greater is required. (you have: " + version +" - " + glVersion + ")");
39         if(pretendToBeACrappyVideoCard) {
40             maxTexSize = 512;
41             maxRectTexSize = 0;
42             rectangularTextures = false;
43         }
44         Log.diag(this,"Renderer: " + renderer);
45         Log.diag(this,"Version: " + version);
46         Log.diag(this,"Vendor: " + vendor);
47         Log.diag(this,"Rectangular textures: " + (rectangularTextures ? "supported" : "unsupported"));
48         Log.diag(this,"Max texture size: " + maxTexSize);
49         Log.diag(this,"Max rectangular texture size: " + maxRectTexSize);
50     }
51     
52     protected abstract void activateSharedContext();
53
54     public static class NotSupportedException extends Exception {
55         public NotSupportedException(String s) { super(s); }
56     }
57
58     public static abstract class GLPixelBuffer implements PixelBuffer {
59         protected int width;
60         protected int height;
61         public void fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int argb) { throw new Error("not implemented"); }
62         public void drawPicture(Picture p, Affine a, Mesh h) { throw new Error("drawPicture() not implemented"); }
63         public void drawGlyph(Font.Glyph source,Affine a,Mesh h,int rgb,int bg) { throw new Error("drawGlyph() not implemented"); }
64         public int getWidth() { return width; }
65         public int getHeight() { return height; }
66         
67         private boolean glScissorEnabled = false;
68         
69         // FIXME: HUGE HACK!
70         public GLPixelBuffer() { width = 500; height = 500; }
71
72         public GLPixelBuffer(int width, int height) {
73             this.width = width;
74             this.height = height;
75         }
76         
77         // This should activate the drawing context and prepare the double buffer for drawing
78         protected abstract void activateContext();
79         
80         protected static native void drawableInit(int w, int h);
81         
82         private static native void setColor(int color);
83         
84         public native void setClip(int x, int y, int x2, int y2);
85         public native void resetClip();
86         public native void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int color);
87         public void drawLine(int x1, int y1, int x2, int y2, int color) {
88             // FIXME
89             fillTrapezoid(x1, x1, y1, x2, x2, y2, color);
90         }
91
92         public void stroke(Mesh p, int color) { /*p.stroke(this, color);*/ }
93         public native void natFill(Mesh p, int color);
94         public void fill(Mesh p, Paint paint) { natFill(p, ((Paint.SingleColorPaint)paint).color); }
95
96         public void drawGlyph(org.ibex.graphics.Font.Glyph source, int dx, int dy,
97                               int cx1, int cy1, int cx2, int cy2, int rgb, int pc) {
98             drawPicture_(((org.ibex.plat.Platform.DefaultGlyph)source).getPicture(), dx, dy, cx1, cy1, cx2, cy2, rgb);
99         }
100         public void drawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2) {
101             drawPicture_(source, dx, dy, cx1, cy1, cx2, cy2, 0xffffffff);
102         }
103
104         private void drawPicture_(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int color) {
105             activateContext();
106             setColor(color);
107             GLPicture p = getInnerPicture(source, gl);
108             p.draw(dx,dy,cx1,cy1,cx2,cy2);
109         }
110     }
111
112     // FIXME ugly
113     public static OpenGL gl = null;
114     public OpenGL() { gl = this; }
115     
116     public final static int roundToPowerOf2(int n) {
117         if(((n-1)&n)==0) return n;
118         for(int x=2;x!=0;x<<=1) if(n < x) return x;
119         return 0;
120     }
121     
122     private static GLPicture getInnerPicture(Picture p, OpenGL gl) {
123         OpenGLPicture oglp = (OpenGLPicture)p;
124         if (!oglp.isLoaded || oglp.realPicture != null) return oglp.realPicture;
125         if (gl.rectangularTextures && p.width <= gl.maxRectTexSize && p.height <= gl.maxRectTexSize) 
126             oglp.realPicture = new RectGLPicture(p.data,p.width,p.height,oglp.alphaOnly,gl);
127         else if (p.width <= gl.maxTexSize && p.height <= gl.maxTexSize)
128             oglp.realPicture = new SquareGLPicture(p.data,p.width,p.height,oglp.alphaOnly,gl);
129         else
130             oglp.realPicture = new MosaicGLPicture(p.data,p.width,p.height,oglp.alphaOnly,gl);
131         p.data = null;
132         return oglp.realPicture;
133     }
134     
135     public Picture _createPicture(JS r, boolean alphaOnly) { return new OpenGLPicture(r, false); }
136
137     public static class OpenGLPicture extends Picture {
138         public OpenGLPicture(JS r, boolean a) { super(r); alphaOnly = a; }
139         boolean alphaOnly;
140         GLPicture realPicture = null;
141     }
142
143     public Font.Glyph _createGlyph(org.ibex.graphics.Font f, char c) { return new org.ibex.plat.Platform.DefaultGlyph(f, c); }
144     
145     private native void natDeleteTexture(int tex);
146     public void deleteTexture(final int tex) {
147         // CHECKME: Is this safe to do from finalize()?
148         // natDeleteTexture MUST be run from the message queue thread
149         Platform.Scheduler.add(new Callable() { public Object run(Object o) { natDeleteTexture(tex); return null; }});
150     }
151     
152     private static abstract class GLPicture {
153         protected int width;
154         protected int height;
155         
156         public final int getWidth() { return width; }
157         public final int getHeight() { return height; }
158                 
159         public GLPicture(int w, int h) {
160             this.width = w;
161             this.height = h;
162         }
163         
164         public abstract void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2);
165         protected abstract void finalize();
166     }
167     
168     private static class RectGLPicture extends GLPicture {
169         private OpenGL gl;
170         public int textureName;
171                 
172         public native void natInit(Object data, boolean alphaOnly);
173         
174         public RectGLPicture(Object data,int w, int h, boolean alphaOnly, OpenGL gl) {
175             super(w,h);
176             this.gl = gl;
177             natInit(data,alphaOnly);
178         }
179     
180         public native void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2);  
181         protected void finalize() { gl.deleteTexture(textureName); }
182     }
183     
184     private static class SquareGLPicture extends GLPicture {
185         private int texHeight;
186         private int texWidth;
187         private OpenGL gl;
188         public int textureName;
189                 
190         public native void natInit(Object data, boolean alphaOnly);
191                 
192         public SquareGLPicture(Object data,int w, int h, boolean alphaOnly, OpenGL gl) {
193             super(w,h);
194             this.gl = gl;
195             if(w > 0x7fffffff || h > 0x7fffffff) throw new Error("insane texture size: " + w + "x" + h);
196             texHeight = roundToPowerOf2(h);
197             texWidth = roundToPowerOf2(w);
198             natInit(data,alphaOnly);
199         }
200         
201         public native void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2);  
202         protected void finalize() { gl.deleteTexture(textureName); }
203     }
204     
205     private static class MosaicGLPicture extends GLPicture {
206         int psize;
207         GLPicture[][] pics;
208         
209         private static float wastedSpace(int w,int h,int size) {
210             int w2 = size * ((w+size-1)/size);
211             int h2 = size * ((h+size-1)/size);
212             return 1.0f-(float)(w*h)/(float)(w2*h2);
213         }
214         
215         private static Object subData(Object data, int x, int y, int w, int h, int rowSize) {
216             if(data instanceof byte[]) return subData((byte[])data,x,y,w,h,rowSize);
217             if(data instanceof int[]) return subData((int[])data,x,y,w,h,rowSize);
218             throw new Error("not reached");
219         }
220         
221         private static int[] subData(int[] data, int x, int y, int w, int h, int rowSize) {
222             int[] sub = new int[w*h];
223             int row = y;
224             for(int i=0;i<h;i++,row++)
225                 for(int j=0;j<w;j++)
226                     sub[i*w+j] = data[row*rowSize+j+x];
227             return sub;
228         }
229         
230         private static byte[] subData(byte[] data, int x, int y, int w, int h, int rowSize) {
231             byte[] sub = new byte[w*h];
232             int row = y;
233             for(int i=0;i<h;i++,row++)
234                 for(int j=0;j<w;j++)
235                     sub[i*w+j] = data[row*rowSize+j+x];
236             return sub;
237         }
238         
239         public MosaicGLPicture(Object data,int w, int h, boolean alphaOnly, OpenGL gl) {
240             super(w,h);
241             psize = gl.maxTexSize;
242             while(wastedSpace(w,h,psize) > 0.40) psize/=2;
243             Log.info(this,"Using psize: " + psize + " for " + w + "x" + h + " image (wasted space: " + wastedSpace(w,h,psize));
244             
245             int rows = (h+psize-1)/psize;
246             int cols = (w+psize-1)/psize;
247             int tmp,tmp2;
248             
249             pics = new GLPicture[rows][cols];
250             for(int i=0;i<rows-1;i++)
251                 for(int j=0;j<cols-1;j++)
252                     pics[i][j] = new SquareGLPicture(subData(data,j*psize,i*psize,psize,psize,w),psize,psize,alphaOnly,gl);
253             tmp = (rows-1)*psize;
254             for(int i=0;i<cols-1;i++)
255                 pics[rows-1][i] = new SquareGLPicture(subData(data,i*psize,tmp,psize,h-tmp,w),psize,h-tmp,alphaOnly,gl);
256             tmp2 = (cols-1)*psize;
257             for(int i=0;i<rows-1;i++)
258                 pics[i][cols-1] = new SquareGLPicture(subData(data,tmp2,i*psize,w-tmp2,psize,w),w-tmp2,psize,alphaOnly,gl);
259             pics[rows-1][cols-1] = new SquareGLPicture(subData(data,tmp2,tmp,w-tmp2,h-tmp,w),w-tmp2,h-tmp,alphaOnly,gl);
260         }
261         protected void finalize() {  }
262         
263         private static final int max(int a, int b) { return a > b ? a : b; }
264         private static final int min(int a, int b) { return a < b ? a : b; }
265         
266         public void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2) {
267             // *{x,y}{1,2} key: d=dest s=src, p=bounds of this picture, i=intersection of s and p, pd = dest of this pic
268             for(int i=0;i<pics.length;i++) {
269                 for(int j=0;j<pics[i].length;j++) {
270                     int px1 = j*psize + dx;
271                     int py1 = i*psize + dy;
272                     pics[i][j].draw(px1, py1, cx1, cy1, cx2, cy2);
273                 }
274             }
275         }
276     }
277 }