├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── examples ├── src │ └── main │ │ ├── resources │ │ └── texture.png │ │ ├── webapp │ │ ├── index.html │ │ └── WEB-INF │ │ │ └── web.xml │ │ └── java │ │ └── com │ │ └── shc │ │ └── webgl4j │ │ └── examples │ │ ├── client.gwt.xml │ │ └── client │ │ ├── Example.java │ │ ├── TriangleExample.java │ │ ├── RectangleExample.java │ │ ├── TextureExample.java │ │ ├── CubeExample.java │ │ ├── WebGL4J.java │ │ └── TexturedCubeExample.java └── build.gradle ├── .gitignore ├── webgl4j ├── src │ └── main │ │ └── java │ │ └── com │ │ └── shc │ │ └── webgl4j │ │ ├── client.gwt.xml │ │ └── client │ │ ├── EXT_frag_depth.java │ │ ├── OES_texture_float.java │ │ ├── EXT_shader_texture_lod.java │ │ ├── OES_element_index_uint.java │ │ ├── EXT_blend_minmax.java │ │ ├── OES_texture_float_linear.java │ │ ├── WEBGL_depth_texture.java │ │ ├── OES_texture_half_float.java │ │ ├── OES_texture_half_float_linear.java │ │ ├── OES_standard_derivatives.java │ │ ├── WEBGL_debug_renderer_info.java │ │ ├── EXT_texture_filter_anisotropic.java │ │ ├── WEBGL_compressed_texture_s3tc.java │ │ ├── WEBGL_debug_shaders.java │ │ ├── WEBGL_lose_context.java │ │ ├── ANGLE_instanced_arrays.java │ │ ├── OES_vertex_array_object.java │ │ ├── TimeUtil.java │ │ ├── WEBGL_draw_buffers.java │ │ ├── WebGLObjectMap.java │ │ └── WebGLContext.java └── build.gradle ├── LICENCE ├── gradlew.bat ├── gradlew └── README.md /settings.gradle: -------------------------------------------------------------------------------- 1 | include 'webgl4j', 'examples' 2 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sriharshachilakapati/WebGL4J/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /examples/src/main/resources/texture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sriharshachilakapati/WebGL4J/HEAD/examples/src/main/resources/texture.png -------------------------------------------------------------------------------- /examples/src/main/webapp/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 |Provides access to a high performance timer. This class creates a polyfill function on first access that uses the 29 | * following APIs in the priority of given order.
30 | * 31 | *32 | * {@code window.performance.now()} → {@code window.performance.webkitNow()} → {@code Date.now()} 33 | *
34 | * 35 | *The first two are high performance timers, which provide timestamps with microsecond precision. The last one is a 36 | * low performance timer and is used as a fallback when the first two are not available. At first we check if the 37 | * {@code window.performance.now()} is supported or not. If not then a check is performed for {@code window.performance 38 | * .webkitNow()} and finally if everything fails we use {@code Date.now()} as the last option.
39 | * 40 | * @author Sri Harsha Chilakapati 41 | */ 42 | public final class TimeUtil 43 | { 44 | private static Implementation implementation = Implementation.HIGH_PERFORMANCE; 45 | 46 | /** 47 | * Prevent instantiation 48 | */ 49 | private TimeUtil() 50 | { 51 | } 52 | 53 | private static native boolean isInitialized() /*-{ 54 | return !!$wnd['currentMillis']; 55 | }-*/; 56 | 57 | private static native void initialize() /*-{ 58 | var currentMillis; 59 | if ($wnd.performance.now) 60 | { 61 | @com.shc.webgl4j.client.TimeUtil::implementation 62 | = @com.shc.webgl4j.client.TimeUtil.Implementation::HIGH_PERFORMANCE; 63 | 64 | currentMillis = function (){ return $wnd.performance.now(); } 65 | } 66 | else if ($wnd.performance.webkitNow) 67 | { 68 | @com.shc.webgl4j.client.TimeUtil::implementation 69 | = @com.shc.webgl4j.client.TimeUtil.Implementation::HIGH_PERFORMANCE_WEBKIT; 70 | 71 | currentMillis = function (){ return $wnd.performance.webkitNow(); } 72 | } 73 | else 74 | { 75 | @com.shc.webgl4j.client.TimeUtil::implementation 76 | = @com.shc.webgl4j.client.TimeUtil.Implementation::LOW_PERFORMANCE; 77 | 78 | currentMillis = function (){ return Date.now(); } 79 | } 80 | 81 | $wnd.currentMillis = currentMillis; 82 | }-*/; 83 | 84 | private static native double nGetTimeStamp() /*-{ 85 | return $wnd.currentMillis(); 86 | }-*/; 87 | 88 | /** 89 | * Returns the current time stamp in the unit of seconds. This method does multiply the current milli seconds by 90 | * {@literal 0.001} to convert them into seconds. 91 | * 92 | * @return The current time stamp in the unit of seconds. 93 | */ 94 | public static double currentSeconds() 95 | { 96 | return currentMillis() * 0.001; 97 | } 98 | 99 | /** 100 | * Returns the current time stamp in the unit of milli seconds. This method simply returns the value returned by the 101 | * underlying high performance timer which by default returns milli seconds. 102 | * 103 | * @return The current time stamp in the unit of milli seconds. 104 | */ 105 | public static double currentMillis() 106 | { 107 | if (!isInitialized()) 108 | initialize(); 109 | 110 | return nGetTimeStamp(); 111 | } 112 | 113 | /** 114 | * Returns the current time stamp in the unit of micro seconds. This method does multiply the current milli seconds 115 | * by {@literal 1000} to convert them into micro seconds. 116 | * 117 | * @return The current time stamp in the unit of micro seconds. 118 | */ 119 | public static double currentMicros() 120 | { 121 | return currentMillis() * 1000; 122 | } 123 | 124 | /** 125 | * Returns the current time stamp in the unit of nano seconds. This method does multiply the current milli seconds 126 | * by {@literal 1000000} to convert them into nano seconds. This however will not be so accurate as the browsers as 127 | * of now only provide timers to the microsecond resolution. 128 | * 129 | * @return The current time stamp in the unit of nano seconds. 130 | */ 131 | public static double currentNanos() 132 | { 133 | return currentMicros() * 1000; 134 | } 135 | 136 | /** 137 | * Returns the implementation that this polyfill uses. It might be {@code window.performance.now()} in the best case 138 | * or {@code window.performance.webkitNow()} in the average case. In the worst case if both those timers are not 139 | * available, that is we are using {@code Date.now()} it returns {@link Implementation#LOW_PERFORMANCE}. 140 | * 141 | * @return Returns an enum describing which implementation that this timer polyfill is using. 142 | */ 143 | public static Implementation getImplementation() 144 | { 145 | if (!isInitialized()) 146 | initialize(); 147 | 148 | return implementation; 149 | } 150 | 151 | /** 152 | * An enumeration used to describe the method this polyfill is using to report time stamps. 153 | */ 154 | public enum Implementation 155 | { 156 | /** 157 | * Corresponds to {@code window.performance.now()} 158 | */ 159 | HIGH_PERFORMANCE, 160 | 161 | /** 162 | * Corresponds to {@code window.performance.webkitNow()} 163 | */ 164 | HIGH_PERFORMANCE_WEBKIT, 165 | 166 | /** 167 | * Corresponds to {@code Date.now()} 168 | */ 169 | LOW_PERFORMANCE 170 | } 171 | } 172 | -------------------------------------------------------------------------------- /webgl4j/src/main/java/com/shc/webgl4j/client/WEBGL_draw_buffers.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2014-2015 Sri Harsha Chilakapati 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package com.shc.webgl4j.client; 26 | 27 | import com.google.gwt.core.client.JsArrayInteger; 28 | import com.google.gwt.typedarrays.shared.Int32Array; 29 | 30 | /** 31 | * @author Sri Harsha Chilakapati 32 | */ 33 | public final class WEBGL_draw_buffers 34 | { 35 | public static final int GL_COLOR_ATTACHMENT0_WEBGL = 0x8CE0; 36 | public static final int GL_COLOR_ATTACHMENT1_WEBGL = 0x8CE1; 37 | public static final int GL_COLOR_ATTACHMENT2_WEBGL = 0x8CE2; 38 | public static final int GL_COLOR_ATTACHMENT3_WEBGL = 0x8CE3; 39 | public static final int GL_COLOR_ATTACHMENT4_WEBGL = 0x8CE4; 40 | public static final int GL_COLOR_ATTACHMENT5_WEBGL = 0x8CE5; 41 | public static final int GL_COLOR_ATTACHMENT6_WEBGL = 0x8CE6; 42 | public static final int GL_COLOR_ATTACHMENT7_WEBGL = 0x8CE7; 43 | public static final int GL_COLOR_ATTACHMENT8_WEBGL = 0x8CE8; 44 | public static final int GL_COLOR_ATTACHMENT9_WEBGL = 0x8CE9; 45 | public static final int GL_COLOR_ATTACHMENT10_WEBGL = 0x8CEA; 46 | public static final int GL_COLOR_ATTACHMENT11_WEBGL = 0x8CEB; 47 | public static final int GL_COLOR_ATTACHMENT12_WEBGL = 0x8CEC; 48 | public static final int GL_COLOR_ATTACHMENT13_WEBGL = 0x8CED; 49 | public static final int GL_COLOR_ATTACHMENT14_WEBGL = 0x8CEE; 50 | public static final int GL_COLOR_ATTACHMENT15_WEBGL = 0x8CEF; 51 | 52 | public static final int GL_DRAW_BUFFER0_WEBGL = 0x8825; 53 | public static final int GL_DRAW_BUFFER1_WEBGL = 0x8826; 54 | public static final int GL_DRAW_BUFFER2_WEBGL = 0x8827; 55 | public static final int GL_DRAW_BUFFER3_WEBGL = 0x8828; 56 | public static final int GL_DRAW_BUFFER4_WEBGL = 0x8829; 57 | public static final int GL_DRAW_BUFFER5_WEBGL = 0x882A; 58 | public static final int GL_DRAW_BUFFER6_WEBGL = 0x882B; 59 | public static final int GL_DRAW_BUFFER7_WEBGL = 0x882C; 60 | public static final int GL_DRAW_BUFFER8_WEBGL = 0x882D; 61 | public static final int GL_DRAW_BUFFER9_WEBGL = 0x882E; 62 | public static final int GL_DRAW_BUFFER10_WEBGL = 0x882F; 63 | public static final int GL_DRAW_BUFFER11_WEBGL = 0x8830; 64 | public static final int GL_DRAW_BUFFER12_WEBGL = 0x8831; 65 | public static final int GL_DRAW_BUFFER13_WEBGL = 0x8832; 66 | public static final int GL_DRAW_BUFFER14_WEBGL = 0x8833; 67 | public static final int GL_DRAW_BUFFER15_WEBGL = 0x8834; 68 | 69 | public static final int GL_MAX_COLOR_ATTACHMENTS_WEBGL = 0x8CDF; 70 | public static final int GL_MAX_DRAW_BUFFERS_WEBGL = 0x8824; 71 | 72 | /* Prevent instantiation */ 73 | private WEBGL_draw_buffers() 74 | { 75 | } 76 | 77 | public static boolean isSupported() 78 | { 79 | if (!WebGL10.isSupported()) 80 | return false; 81 | 82 | if (!WebGL10.isContextCompatible()) 83 | throw new IllegalStateException("You must have a WebGL context >= 1.0 to check if extension is supported."); 84 | 85 | for (String supportedExtension : WebGL10.glGetSupportedExtensions()) 86 | { 87 | switch (supportedExtension) 88 | { 89 | case "WEBGL_draw_buffers": 90 | case "O_WEBGL_draw_buffers": 91 | case "IE_WEBGL_draw_buffers": 92 | case "MOZ_WEBGL_draw_buffers": 93 | case "WEBKIT_WEBGL_draw_buffers": 94 | return true; 95 | } 96 | } 97 | 98 | return false; 99 | } 100 | 101 | public static void enableExtension() 102 | { 103 | if (!WebGL10.isContextCompatible()) 104 | throw new IllegalStateException("You must have a WebGL context >= 1.0 to enable this extension."); 105 | 106 | if (!isSupported()) 107 | throw new RuntimeException("This browser does not support the WEBGL_draw_buffers extension."); 108 | 109 | if (!isExtensionEnabled()) 110 | nEnableExtension(); 111 | } 112 | 113 | private static void checkExtension() 114 | { 115 | if (!WebGL10.isContextCompatible()) 116 | throw new RuntimeException("You must create a WebGL context before accessing extension methods."); 117 | 118 | if (!isExtensionEnabled()) 119 | throw new IllegalStateException("Extension must be enabled before using any members."); 120 | } 121 | 122 | public static void glDrawBuffersWEBGL(JsArrayInteger buffers) 123 | { 124 | checkExtension(); 125 | nglDrawBuffersWEBGL(buffers); 126 | } 127 | 128 | public static void glDrawBuffersWEBGL(int... buffers) 129 | { 130 | JsArrayInteger array = JsArrayInteger.createArray(buffers.length).cast(); 131 | 132 | for (int buffer : buffers) 133 | array.push(buffer); 134 | 135 | glDrawBuffersWEBGL(array); 136 | } 137 | 138 | public static void glDrawBuffersWEBGL(Int32Array buffers) 139 | { 140 | JsArrayInteger array = JsArrayInteger.createArray(buffers.length()).cast(); 141 | 142 | for (int i = 0; i < buffers.length(); i++) 143 | array.push(buffers.get(i)); 144 | 145 | glDrawBuffersWEBGL(array); 146 | } 147 | 148 | public static native boolean isExtensionEnabled() /*-{ 149 | return typeof ($wnd.gl.wdb_ext) !== 'undefined'; 150 | }-*/; 151 | 152 | private static native void nEnableExtension() /*-{ 153 | $wnd.gl.wdb_ext = $wnd.gl.getExtension('WEBGL_draw_buffers') || 154 | $wnd.gl.getExtension('O_WEBGL_draw_buffers') || 155 | $wnd.gl.getExtension('IE_WEBGL_draw_buffers') || 156 | $wnd.gl.getExtension('MOZ_WEBGL_draw_buffers') || 157 | $wnd.gl.getExtension('WEBKIT_WEBGL_draw_buffers'); 158 | }-*/; 159 | 160 | private static native void nglDrawBuffersWEBGL(JsArrayInteger buffers) /*-{ 161 | $wnd.gl.wdb_ext.drawBuffersWEBGL(buffers); 162 | }-*/; 163 | } 164 | -------------------------------------------------------------------------------- /examples/src/main/java/com/shc/webgl4j/examples/client/TextureExample.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2014-2015 Sri Harsha Chilakapati 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package com.shc.webgl4j.examples.client; 26 | 27 | import com.google.gwt.canvas.client.Canvas; 28 | import com.google.gwt.user.client.ui.Image; 29 | import com.google.gwt.user.client.ui.RootPanel; 30 | import org.joml.Matrix4f; 31 | 32 | import static com.shc.webgl4j.client.WebGL10.*; 33 | 34 | /** 35 | * @author Sri Harsha Chilakapati 36 | */ 37 | public class TextureExample extends Example 38 | { 39 | private int programID; 40 | private Matrix4f projView; 41 | private float angle = 0; 42 | 43 | public TextureExample(Canvas canvas) 44 | { 45 | super(canvas); 46 | } 47 | 48 | @Override 49 | public void init() 50 | { 51 | glClearColor(0, 0, 0, 1); 52 | 53 | // The vertex shader source 54 | String vsSource = "precision mediump float; \n" + 55 | " \n" + 56 | "uniform mat4 proj; \n" + 57 | " \n" + 58 | "attribute vec2 position; \n" + 59 | "attribute vec2 texCoords; \n" + 60 | " \n" + 61 | "varying vec2 vTexCoords; \n" + 62 | " \n" + 63 | "void main() \n" + 64 | "{ \n" + 65 | " vTexCoords = texCoords; \n" + 66 | " gl_Position = proj * vec4(position, 0.0, 1.0); \n" + 67 | "}"; 68 | 69 | // The fragment shader source 70 | String fsSource = "precision mediump float; \n" + 71 | " \n" + 72 | "uniform sampler2D tex; \n" + 73 | " \n" + 74 | "varying vec2 vTexCoords; \n" + 75 | " \n" + 76 | "void main() \n" + 77 | "{ \n" + 78 | " gl_FragColor = texture2D(tex, vTexCoords); \n" + 79 | "}"; 80 | 81 | // Create the vertex shader 82 | int vsShaderID = glCreateShader(GL_VERTEX_SHADER); 83 | glShaderSource(vsShaderID, vsSource); 84 | glCompileShader(vsShaderID); 85 | 86 | // Create the fragment shader 87 | int fsShaderID = glCreateShader(GL_FRAGMENT_SHADER); 88 | glShaderSource(fsShaderID, fsSource); 89 | glCompileShader(fsShaderID); 90 | 91 | // Create the program 92 | programID = glCreateProgram(); 93 | glAttachShader(programID, vsShaderID); 94 | glAttachShader(programID, fsShaderID); 95 | glLinkProgram(programID); 96 | glUseProgram(programID); 97 | 98 | // Create the positions VBO 99 | float[] vertices = 100 | { 101 | -0.8f, +0.8f, 102 | +0.8f, +0.8f, 103 | -0.8f, -0.8f, 104 | +0.8f, +0.8f, 105 | +0.8f, -0.8f, 106 | -0.8f, -0.8f 107 | }; 108 | 109 | int vboPosID = glCreateBuffer(); 110 | glBindBuffer(GL_ARRAY_BUFFER, vboPosID); 111 | glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW); 112 | 113 | int positionLoc = glGetAttribLocation(programID, "position"); 114 | glVertexAttribPointer(positionLoc, 2, GL_FLOAT, false, 0, 0); 115 | glEnableVertexAttribArray(positionLoc); 116 | 117 | // Create the texcoords VBO 118 | float[] texCoords = 119 | { 120 | 0, 0, 121 | 1, 0, 122 | 0, 1, 123 | 1, 0, 124 | 1, 1, 125 | 0, 1 126 | }; 127 | 128 | int vboTexID = glCreateBuffer(); 129 | glBindBuffer(GL_ARRAY_BUFFER, vboTexID); 130 | glBufferData(GL_ARRAY_BUFFER, texCoords, GL_STATIC_DRAW); 131 | 132 | int texcoordLoc = glGetAttribLocation(programID, "texCoords"); 133 | glVertexAttribPointer(texcoordLoc, 2, GL_FLOAT, false, 0, 0); 134 | glEnableVertexAttribArray(texcoordLoc); 135 | 136 | final Image image = new Image("texture.png"); 137 | image.addLoadHandler(event -> 138 | { 139 | // Make current again, this is asynchronous event 140 | context.makeCurrent(); 141 | int texID = glCreateTexture(); 142 | glBindTexture(GL_TEXTURE_2D, texID); 143 | 144 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, image); 145 | 146 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 147 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 148 | 149 | glUniform1i(glGetUniformLocation(programID, "tex"), 0); 150 | }); 151 | image.setVisible(false); 152 | RootPanel.get().add(image); 153 | 154 | projView = new Matrix4f(); 155 | } 156 | 157 | @Override 158 | public void render() 159 | { 160 | angle++; 161 | 162 | projView.setPerspective((float) Math.toRadians(70), 640f / 480f, 0.1f, 100) 163 | .translate(0, 0, -2) 164 | .rotateY((float) Math.toRadians(angle)) 165 | .rotateZ((float) Math.toRadians(angle)); 166 | 167 | glUniformMatrix4fv(glGetUniformLocation(programID, "proj"), false, projView.get(new float[16])); 168 | 169 | glClear(GL_COLOR_BUFFER_BIT); 170 | glDrawArrays(GL_TRIANGLES, 0, 6); 171 | } 172 | } 173 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |