Home | History | Annotate | Download | only in client
      1 /*******************************************************************************
      2  * Copyright 2011 See AUTHORS file.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *   http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  ******************************************************************************/
     16 
     17 package com.google.gwt.webgl.client;
     18 
     19 import static com.google.gwt.webgl.client.WebGLRenderingContext.*;
     20 
     21 public class WebGLUtil {
     22 
     23 	public static float[] createPerspectiveMatrix (int fieldOfViewVertical, float aspectRatio, float minimumClearance,
     24 		float maximumClearance) {
     25 		double fieldOfViewInRad = fieldOfViewVertical * Math.PI / 180.0;
     26 		return new float[] {(float)(Math.tan(fieldOfViewInRad) / aspectRatio), 0, 0, 0, 0,
     27 			(float)(1 / Math.tan(fieldOfViewVertical * Math.PI / 180.0)), 0, 0, 0, 0,
     28 			(minimumClearance + maximumClearance) / (minimumClearance - maximumClearance), -1, 0, 0,
     29 			2 * minimumClearance * maximumClearance / (minimumClearance - maximumClearance), 0};
     30 	}
     31 
     32 	public static WebGLProgram createShaderProgram (WebGLRenderingContext gl, String vertexSource, String fragmentSource) {
     33 		WebGLShader vertexShader = getShader(gl, VERTEX_SHADER, vertexSource);
     34 		WebGLShader fragmentShader = getShader(gl, FRAGMENT_SHADER, fragmentSource);
     35 
     36 		WebGLProgram shaderProgram = gl.createProgram();
     37 		gl.attachShader(shaderProgram, fragmentShader);
     38 		gl.attachShader(shaderProgram, vertexShader);
     39 		gl.linkProgram(shaderProgram);
     40 
     41 		if (!gl.getProgramParameterb(shaderProgram, LINK_STATUS)) {
     42 			throw new RuntimeException("Could not initialize shaders");
     43 		}
     44 
     45 		return shaderProgram;
     46 	}
     47 
     48 	private static WebGLShader getShader (WebGLRenderingContext gl, int shaderType, String source) {
     49 		WebGLShader shader = gl.createShader(shaderType);
     50 		gl.shaderSource(shader, source);
     51 		gl.compileShader(shader);
     52 		if (!gl.getShaderParameterb(shader, COMPILE_STATUS)) {
     53 			throw new RuntimeException(gl.getShaderInfoLog(shader));
     54 		}
     55 		return shader;
     56 	}
     57 }
     58