3D Views: Source Code

Page 6

Prepare Shaders

This Java code is modified from Learn OpenGL ES code posted on GitHub. The WebGL tutorials can be found at http://www.learnopengles.com.

Introduction Java Code Summary

Introduction

The ShaderHelper Java class compiles the vertex and fragment shaders then links them.

Java Code

package com.seventhundersoftware.cubeview.common;

import android.opengl.GLES20;
import android.util.Log;

public class ShaderHelper
{
private static final String TAG = "ShaderHelper";
	
/** 
* Helper function to compile a shader.
* 
* @param shaderType The shader type.
* @param shaderSource The shader source code.
* @return An OpenGL handle to the shader.
*/
public static int compileShader(final int shaderType, final String shaderSource) 
{
int shaderHandle = GLES20.glCreateShader(shaderType);

if (shaderHandle != 0) 
{
// Pass in the shader source.
GLES20.glShaderSource(shaderHandle, shaderSource);

// Compile the shader.
GLES20.glCompileShader(shaderHandle);

// Get the compilation status.
final int[] compileStatus = new int[1];
GLES20.glGetShaderiv(shaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus, 0);

// If the compilation failed, delete the shader.
if (compileStatus[0] == 0) 
{
Log.e(TAG, "Error compiling shader: " + GLES20.glGetShaderInfoLog(shaderHandle));
GLES20.glDeleteShader(shaderHandle);
shaderHandle = 0;
}
}

if (shaderHandle == 0)
{			
throw new RuntimeException("Error creating shader.");
}
		
return shaderHandle;
}
	
/**
* Helper function to compile and link a program.
* 
* @param vertexShaderHandle An OpenGL handle to an already-compiled vertex shader.
* @param fragmentShaderHandle An OpenGL handle to an already-compiled fragment shader.
* @param attributes Attributes that need to be bound to the program.
* @return An OpenGL handle to the program.
*/
public static int createAndLinkProgram(final int vertexShaderHandle, final int fragmentShaderHandle, final String[] attributes) 
{
int programHandle = GLES20.glCreateProgram();
		
if (programHandle != 0) 
{
// Bind the vertex shader to the program.
GLES20.glAttachShader(programHandle, vertexShaderHandle);			

// Bind the fragment shader to the program.
GLES20.glAttachShader(programHandle, fragmentShaderHandle);
			
// Bind attributes
if (attributes != null)
{
final int size = attributes.length;
for (int i = 0; i < size; i++)
{
GLES20.glBindAttribLocation(programHandle, i, attributes[i]);
}						
}
			
// Link the two shaders together into a program.
GLES20.glLinkProgram(programHandle);

// Get the link status.
final int[] linkStatus = new int[1];
GLES20.glGetProgramiv(programHandle, GLES20.GL_LINK_STATUS, linkStatus, 0);

// If the link failed, delete the program.
if (linkStatus[0] == 0) 
{				
Log.e(TAG, "Error compiling program: " + GLES20.glGetProgramInfoLog(programHandle));
GLES20.glDeleteProgram(programHandle);
	programHandle = 0;
}
}
		
if (programHandle == 0)
{
throw new RuntimeException("Error creating program.")
}
		
return programHandle;

}
}

Summary

The ShaderHelper Java class compiles the vertex and fragment shaders then links them.

Java & OpenGL ES

This set of pages include Android Java, combined with OpenGL ES, to display a set of simple three dimensional views. The views apply a unique, simple concept to render 3D backgrounds, without Skyboxes or time consuming shader switching. Each view's contained in one graphic, with room for other sprites and meshes.

Java and OpenGL ES source code was ported from WebGL with only minor changes. The book, 3D Scenes: Learn WebGL Book 3, explains implementation and graphics preparation for the WebGL app, in detail.

Tags

Free source code, free Java, free OpenGL ES, 3D programming, learn to code,

Ads >
Create 3D Games: Learn WebGL Book 2 Simple Shaders: Learn WebGL Book 4
3D Programming for Beginners: Learn WebGL Book 1
Web Graphics & Illustration Architectural Rendering 3D Architectural Web Animation
Web Development Python & MySQL Send Email Complete Sitemap About Search 3D