If you think the Android project Schooner-3D listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright 2012 Dan Mercer/*www.java2s.com*/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/package com.supermercerbros.gameengine;
import com.supermercerbros.gameengine.engine.shaders.Material;
import com.supermercerbros.gameengine.objects.GameObject;
import com.supermercerbros.gameengine.shaders.ProgramSource;
publicclass TestMaterials {
publicstaticfinalint FRAMES_TO_LOG = 0;
/**
* Flat white polys, no transformations
*/publicstaticclass MinimalMaterial extends Material {
privatestaticfinal String VERT_VARS =
"attribute vec3 a_pos;\n";
privatestaticfinal String VERT_MAIN =
"gl_Position = vec4(a_pos, 1.0);\n";
privatestaticfinal String FRAG_MAIN =
"gl_FragColor = vec4(1.0);\n";
privatestaticfinalint STRIDE = 3;
@Override
publicvoid onLoadObject(GameObject obj, float[] vbo, int vertCount) {
loadArrayToVbo(obj.verts, vbo, 3, vertCount);
}
@Override
publicvoid onAttachAttribs() {
attachAttrib(a_pos, 3);
}
@Override
publicvoid makeProgram() {
ProgramSource prog = new ProgramSource(null, null, VERT_VARS, VERT_MAIN, null, null, FRAG_MAIN);
setProgram(prog, STRIDE);
}
}
publicstaticclass ShadelessMaterial extends Material {
privatestaticfinal String VERT_VARS =
"uniform mat4 u_viewProj;\n" +
"uniform mat4 u_model;\n" +
"attribute vec3 a_pos;\n";
privatestaticfinal String VERT_MAIN =
"mat4 transform = u_viewProj * u_model;\n" +
"gl_Position = transform * vec4(a_pos, 1.0);\n";
privatestaticfinal String FRAG_MAIN =
"gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n";
privatestaticfinalint STRIDE = 3;
@Override
protectedvoid onLoadObject(GameObject obj, float[] vbo, int vertCount) {
loadArrayToVbo(obj.verts, vbo, 3, vertCount);
}
@Override
protectedvoid onAttachAttribs() {
attachAttrib(a_pos, 3);
}
@Override
publicvoid makeProgram() {
ProgramSource prog = new ProgramSource(null, null, VERT_VARS, VERT_MAIN, null, null, FRAG_MAIN);
setProgram(prog, STRIDE);
}
}
/**
* Vertex-shaded polys
*/publicstaticclass ShadedMaterial extends Material {
privatestaticfinal String VERT_VARS =
"uniform mat4 u_viewProj;\n" +
"uniform vec3 u_lightVec;\n" +
"uniform mat4 u_model;\n" +
"attribute vec3 a_pos;\n" +
"attribute vec3 a_normal;\n";
privatestaticfinal String VERT_MAIN =
"mat4 transform = u_viewProj * u_model;\n" +
"gl_Position = transform * vec4(a_pos, 1.0);\n" +
"vec3 normal = (transform * vec4(a_normal, 0.0)).xyz;\n" +
"v_brightness = (dot(normalize(normal), u_lightVec) + 1.0) / 2.0;\n";
privatestaticfinal String VARYINGS =
"varying float v_brightness;";
privatestaticfinal String FRAG_VARS =
"uniform vec3 u_lightColor;\n";
privatestaticfinal String FRAG_MAIN =
"gl_FragColor = vec4(min(u_lightColor * v_brightness + 0.2, vec3(1.0)), 1.0);\n";
privatestaticfinalint STRIDE = 6;
@Override
publicvoid onLoadObject(GameObject obj, float[] vbo, int vertCount) {
loadArrayToVbo(obj.verts, vbo, 3, vertCount);
loadArrayToVbo(obj.normals, vbo, 3, vertCount);
}
@Override
publicvoid onAttachAttribs() {
attachAttrib(a_pos, 3);
attachAttrib(a_normal, 3);
}
@Override
publicvoid makeProgram() {
ProgramSource prog = new ProgramSource(VARYINGS, null, VERT_VARS, VERT_MAIN, null, FRAG_VARS, FRAG_MAIN);
setProgram(prog, STRIDE);
}
}
}