Moving to GLES2 on Android. Not working yet.

This commit is contained in:
codeanticode
2012-01-30 20:52:20 +00:00
parent 056df405a7
commit 5f29531a46
10 changed files with 995 additions and 808 deletions
@@ -0,0 +1,28 @@
/*
Part of the Processing project - http://processing.org
Copyright (c) 2011 Andres Colubri
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
precision mediump float;
varying vec4 vertColor;
void main() {
gl_FragColor = vertColor;
}
@@ -0,0 +1,36 @@
/*
Part of the Processing project - http://processing.org
Copyright (c) 2011 Andres Colubri
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
uniform mat4 modelviewMatrix;
uniform mat4 projectionMatrix;
attribute vec4 inPosition;
attribute vec4 inColor;
attribute vec3 inNormal;
attribute vec2 inTexcoord;
varying vec4 vertColor;
void main() {
gl_Position = projectionMatrix * modelviewMatrix * inPosition;
vertColor = inColor;
}
@@ -19,6 +19,10 @@
Boston, MA 02111-1307 USA
*/
precision mediump float;
varying vec4 vertColor;
void main() {
gl_FragColor = gl_Color;
}
gl_FragColor = vertColor;
}
@@ -21,12 +21,18 @@
Boston, MA 02111-1307 USA
*/
attribute vec4 attribs;
uniform vec4 viewport;
uniform mat4 modelviewMatrix;
uniform mat4 projectionMatrix;
uniform vec4 viewport;
uniform int perspective;
uniform vec4 eye;
uniform int lights;
attribute vec4 inVertex;
attribute vec4 inColor;
attribute vec3 inNormal;
attribute vec4 inDirWidth;
varying vec4 vertColor;
vec3 clipToWindow(vec4 clip, vec4 viewport) {
vec3 post_div = clip.xyz / clip.w;
@@ -39,69 +45,23 @@ vec4 windowToClipVector(vec2 window, vec4 viewport, float clip_w) {
return vec4(xypos, 0.0, 0.0) * clip_w;
}
// From the "Directional Lights I & II" tutorials from lighthouse 3D:
// http://www.lighthouse3d.com/tutorials/glsl-tutorial/directional-lights-i/
vec4 calculateLight(int i) {
// Per-vertex diffuse and ambient lighting.
vec3 normal, lightDir;
vec4 diffuse, ambient, specular;
float NdotL, NdotHV;
// first transform the normal into eye space and normalize the result.
normal = normalize(gl_NormalMatrix * gl_Normal);
// now normalize the light's direction. Note that according to the
// OpenGL specification, the light is stored in eye space. Also since
// we're talking about a directional light, the position field is actually
// direction
lightDir = normalize(vec3(gl_LightSource[i].position));
// compute the cos of the angle between the normal and lights direction.
// The light is directional so the direction is constant for every vertex.
// Since these two are normalized the cosine is the dot product. We also
// need to clamp the result to the [0,1] range.
NdotL = max(dot(normal, lightDir), 0.0);
// Compute the diffuse term. Ambient and diffuse material components
// are stored in the vertex color, since processing uses GL_COLOR_MATERIAL
diffuse = gl_Color * gl_LightSource[i].diffuse;
// Compute the ambient and globalAmbient terms
ambient = gl_Color * gl_LightSource[i].ambient;
// compute the specular term if NdotL is larger than zero
if (NdotL > 0.0) {
// normalize the half-vector, and then compute the
// cosine (dot product) with the normal
NdotHV = max(dot(normal, gl_LightSource[i].halfVector.xyz), 0.0);
specular = gl_FrontMaterial.specular * gl_LightSource[i].specular * pow(NdotHV, gl_FrontMaterial.shininess);
}
return NdotL * diffuse + ambient;
}
void main() {
vec4 pos_p = gl_Vertex;
vec4 pos_q = vec4(attribs.xyz, 1);
vec4 v_p = gl_ModelViewMatrix * pos_p;
// Moving vertices slightly toward the camera
// to avoid depth-fighting with the polys.
// Discussed here:
// http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=252848
//v_p.xyz = v_p.xyz * 0.99;
vec4 pos_p = inVertex;
vec4 pos_q = vec4(inDirWidth.xyz, 1);
vec4 v_p = modelviewMatrix * pos_p;
vec4 clip_p = projectionMatrix * v_p;
vec4 v_q = modelviewMatrix * pos_q;
vec4 clip_q = projectionMatrix * v_q;
vec4 clip_p = gl_ProjectionMatrix * v_p;
vec4 v_q = gl_ModelViewMatrix * pos_q;
//v_q.xyz = v_q.xyz * 0.99;
vec4 clip_q = gl_ProjectionMatrix * v_q;
vec3 window_p = clipToWindow(clip_p, viewport);
vec3 window_q = clipToWindow(clip_q, viewport);
vec3 tangent = window_q - window_p;
float segment_length = length(tangent.xy);
vec2 perp = normalize(vec2(-tangent.y, tangent.x));
float thickness = attribs.w;
float thickness = inDirWidth.w;
vec2 window_offset = perp * thickness;
if (0 < perspective) {
@@ -116,15 +76,5 @@ void main() {
gl_Position = clip_p + offset_p;
}
vec4 color = vec4(0, 0, 0, 0);
vec4 globalAmbient = gl_Color * gl_LightModel.ambient;
if (lights == 0) {
color = gl_Color;
}
for (int i = 0; i < lights; i++) {
vec4 light = calculateLight(i);
color += light;
}
color = clamp(color, 0.0, 1.0);
gl_FrontColor = color;
vertColor = inColor;
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -220,6 +220,10 @@ public class PShader {
pgl.setIntUniform(loc, unit);
}
}
public void setTexUniform(int loc, int unit) {
pgl.setIntUniform(loc, unit);
}
/**
* Sets the texture uniform with the unit of tex is attached to
@@ -256,6 +260,10 @@ public class PShader {
}
}
public void setIntUniform(int loc, int x) {
pgl.setIntUniform(loc, x);
}
/**
* Sets the float uniform with name to the given value
*
@@ -269,6 +277,10 @@ public class PShader {
}
}
public void setFloatUniform(int loc, float x) {
pgl.setFloatUniform(loc, x);
}
/**
* Sets the vec2 uniform with name to the given values.
*
@@ -283,6 +295,10 @@ public class PShader {
}
}
public void setVecUniform(int loc, float x, float y) {
pgl.setFloatUniform(loc, x, y);
}
/**
* Sets the vec3 uniform with name to the given values.
*
@@ -297,6 +313,10 @@ public class PShader {
pgl.setFloatUniform(loc, x, y, z);
}
}
public void setVecUniform(int loc, float x, float y, float z) {
pgl.setFloatUniform(loc, x, y, z);
}
/**
* Sets the vec4 uniform with name to the given values.
@@ -314,6 +334,10 @@ public class PShader {
}
}
public void setVecUniform(int loc, float x, float y, float z, float w) {
pgl.setFloatUniform(loc, x, y, z, w);
}
/**
* Sets the mat2 uniform with name to the given values
*
@@ -330,6 +354,12 @@ public class PShader {
}
}
public void setMatUniform(int loc, float m00, float m01,
float m10, float m11) {
pgl.setMatUniform(loc, m00, m01,
m10, m11);
}
/**
* Sets the mat3 uniform with name to the given values
*
@@ -347,6 +377,14 @@ public class PShader {
m20, m21, m22);
}
}
public void setMatUniform(int loc, float m00, float m01, float m02,
float m10, float m11, float m12,
float m20, float m21, float m22) {
pgl.setMatUniform(loc, m00, m01, m02,
m10, m11, m12,
m20, m21, m22);
}
/**
* Sets the mat3 uniform with name to the given values
@@ -368,6 +406,16 @@ public class PShader {
}
}
public void setMatUniform(int loc, float m00, float m01, float m02, float m03,
float m10, float m11, float m12, float m13,
float m20, float m21, float m22, float m23,
float m30, float m31, float m32, float m33) {
pgl.setMatUniform(loc, m00, m01, m02, m03,
m10, m11, m12, m13,
m20, m21, m22, m23,
m30, m31, m32, m33);
}
/**
* Sets the float attribute with name to the given value
*
@@ -381,6 +429,10 @@ public class PShader {
}
}
public void setFloatAttribute(int loc, float x) {
pgl.setFloatAttrib(loc, x);
}
/**
* Sets the vec2 attribute with name to the given values
*
@@ -395,6 +447,10 @@ public class PShader {
}
}
public void setVecAttribute(int loc, float x, float y) {
pgl.setFloatAttrib(loc, x, y);
}
/**
* Sets the vec3 attribute with name to the given values
*
@@ -409,6 +465,10 @@ public class PShader {
pgl.setFloatAttrib(loc, x, y, z);
}
}
public void setVecAttribute(int loc, float x, float y, float z) {
pgl.setFloatAttrib(loc, x, y, z);
}
/**
* Sets the vec4 attribute with name to the given values
@@ -426,6 +486,10 @@ public class PShader {
}
}
public void setVecAttribute(int loc, float x, float y, float z, float w) {
pgl.setFloatAttrib(loc, x, y, z, w);
}
/**
* @param shaderSource a string containing the shader's code
* @param filename the shader's filename, used to print error log information
+52 -40
View File
@@ -3409,9 +3409,10 @@ public class PShape3D extends PShape {
protected void renderPoints() {
pg.startPointShader();
pgl.enableVertexArrays();
pgl.enableColorArrays();
pgl.enableNormalArrays();
pg.enablePointVertex();
pg.enablePointColor();
pg.enablePointNormal();
pg.enablePointSize();
for (int i = 0; i < pointIndexData.size(); i++) {
IndexData index = (IndexData)pointIndexData.get(i);
@@ -3420,15 +3421,16 @@ public class PShape3D extends PShape {
int size = index.size;
pgl.bindVertexBuffer(root.glPointVertexBufferID);
pgl.setVertexFormat(3, first);
pg.setPointVertexFormat(3, first);
pgl.bindVertexBuffer(root.glPointColorBufferID);
pgl.setColorFormat(4, first);
pg.setPointColorFormat(4, first);
pgl.bindVertexBuffer(root.glPointNormalBufferID);
pgl.setNormalFormat(3, first);
pg.setPointNormalFormat(4, first);
pg.setupPointShader(root.glPointAttribBufferID);
pgl.bindVertexBuffer(root.glPointAttribBufferID);
pg.setPointSizeFormat(2, first);
pgl.bindIndexBuffer(root.glPointIndexBufferID);
pgl.renderIndexBuffer(offset, size);
@@ -3437,10 +3439,11 @@ public class PShape3D extends PShape {
pgl.unbindVertexBuffer();
}
pgl.disableVertexArrays();
pgl.disableColorArrays();
pgl.disableNormalArrays();
pg.disablePointVertex();
pg.disablePointColor();
pg.disablePointNormal();
pg.disablePointSize();
pg.stopPointShader();
}
@@ -3448,9 +3451,10 @@ public class PShape3D extends PShape {
protected void renderLines() {
pg.startLineShader();
pgl.enableVertexArrays();
pgl.enableColorArrays();
pgl.enableNormalArrays();
pg.enableLineVertex();
pg.enableLineColor();
pg.enableLineNormal();
pg.enableLineDirWidth();
for (int i = 0; i < lineIndexData.size(); i++) {
IndexData index = (IndexData)lineIndexData.get(i);
@@ -3459,15 +3463,16 @@ public class PShape3D extends PShape {
int size = index.size;
pgl.bindVertexBuffer(root.glLineVertexBufferID);
pgl.setVertexFormat(3, first);
pg.setLineVertexFormat(3, first);
pgl.bindVertexBuffer(root.glLineColorBufferID);
pgl.setColorFormat(4, first);
pg.setLineColorFormat(4, first);
pgl.bindVertexBuffer(root.glLineNormalBufferID);
pgl.setNormalFormat(3, first);
pg.setupLineShader(root.glLineAttribBufferID);
pg.setLineNormalFormat(3, first);
pgl.bindVertexBuffer(root.glLineAttribBufferID);
pg.setLineDirWidthFormat(2, 0);
pgl.bindIndexBuffer(root.glLineIndexBufferID);
pgl.renderIndexBuffer(offset, size);
@@ -3476,19 +3481,24 @@ public class PShape3D extends PShape {
pgl.unbindVertexBuffer();
}
pgl.disableVertexArrays();
pgl.disableColorArrays();
pgl.disableNormalArrays();
pg.disableLineVertex();
pg.disableLineColor();
pg.disableLineNormal();
pg.disableLineDirWidth();
pg.stopLineShader();
pg.stopLineShader();
}
protected void renderFill(PImage textureImage) {
pgl.enableVertexArrays();
pgl.enableColorArrays();
pgl.enableNormalArrays();
pgl.enableTexCoordArrays();
protected void renderFill(PImage textureImage) {
pg.startFillShader();
pg.startFillShader();
pg.enableFillVertex();
pg.enableFillColor();
pg.enableFillNormal();
pg.enableFillTexCoord();
for (int i = 0; i < fillIndexData.size(); i++) {
IndexData index = (IndexData)fillIndexData.get(i);
@@ -3497,23 +3507,23 @@ public class PShape3D extends PShape {
int size = index.size;
pgl.bindVertexBuffer(root.glFillVertexBufferID);
pgl.setVertexFormat(3, first);
pg.setFillVertexFormat(3, first);
pgl.bindVertexBuffer(root.glFillColorBufferID);
pgl.setColorFormat(4, first);
pg.setFillColorFormat(4, first);
pgl.bindVertexBuffer(root.glFillNormalBufferID);
pgl.setNormalFormat(3, first);
pgl.bindVertexBuffer(root.glFillNormalBufferID);
pg.setFillNormalFormat(3, first);
pgl.bindVertexBuffer(root.glFillTexCoordBufferID);
pgl.setTexCoordFormat(2, first);
pgl.bindVertexBuffer(root.glFillTexCoordBufferID);
pg.setFillTexCoordFormat(2, first);
PTexture tex = null;
if (textureImage != null) {
pgl.setActiveTexUnit(0);
tex = pg.getTexture(textureImage);
if (tex != null) {
pgl.enableTexturing(tex.glTarget);
pgl.setActiveTexUnit(0);
pgl.enableTexturing(tex.glTarget);
pgl.bindTexture(tex.glTarget, tex.glID);
}
}
@@ -3530,10 +3540,12 @@ public class PShape3D extends PShape {
pgl.unbindVertexBuffer();
}
pgl.disableVertexArrays();
pgl.disableColorArrays();
pgl.disableNormalArrays();
pgl.disableTexCoordArrays();
pg.disableFillVertex();
pg.disableFillColor();
pg.disableFillNormal();
pg.disableFillTexCoord();
pg.stopFillShader();
}
@@ -19,6 +19,10 @@
Boston, MA 02111-1307 USA
*/
void main() {
gl_FragColor = gl_Color;
precision mediump float;
varying vec4 vertColor;
void main() {
gl_FragColor = vertColor;
}
@@ -19,66 +19,20 @@
Boston, MA 02111-1307 USA
*/
attribute vec2 vertDisp;
uniform mat4 modelviewMatrix;
uniform mat4 projectionMatrix;
uniform vec4 eye;
uniform int lights;
attribute vec4 inVertex;
attribute vec4 inColor;
attribute vec3 inNormal;
attribute vec2 inSize;
// From the "Directional Lights I & II" tutorials from lighthouse 3D:
// http://www.lighthouse3d.com/tutorials/glsl-tutorial/directional-lights-i/
vec4 calculateLight(int i) {
// Per-vertex diffuse and ambient lighting.
vec3 normal, lightDir;
vec4 diffuse, ambient, specular;
float NdotL, NdotHV;
// first transform the normal into eye space and normalize the result.
normal = normalize(gl_NormalMatrix * gl_Normal);
// now normalize the light's direction. Note that according to the
// OpenGL specification, the light is stored in eye space. Also since
// we're talking about a directional light, the position field is actually
// direction
lightDir = normalize(vec3(gl_LightSource[i].position));
// compute the cos of the angle between the normal and lights direction.
// The light is directional so the direction is constant for every vertex.
// Since these two are normalized the cosine is the dot product. We also
// need to clamp the result to the [0,1] range.
NdotL = max(dot(normal, lightDir), 0.0);
// Compute the diffuse term. Ambient and diffuse material components
// are stored in the vertex color, since processing uses GL_COLOR_MATERIAL
diffuse = gl_Color * gl_LightSource[i].diffuse;
// Compute the ambient and globalAmbient terms
ambient = gl_Color * gl_LightSource[i].ambient;
// compute the specular term if NdotL is larger than zero
if (NdotL > 0.0) {
// normalize the half-vector, and then compute the
// cosine (dot product) with the normal
NdotHV = max(dot(normal, gl_LightSource[i].halfVector.xyz), 0.0);
specular = gl_FrontMaterial.specular * gl_LightSource[i].specular * pow(NdotHV, gl_FrontMaterial.shininess);
}
return NdotL * diffuse + ambient;
}
varying vec4 vertColor;
void main() {
vec4 pos = gl_ModelViewMatrix * gl_Vertex;
pos.xy += vertDisp.xy;
gl_Position = gl_ProjectionMatrix * pos;
vec4 pos = modelviewMatrix * inVertex;
pos.xy += inSize.xy;
gl_Position = projectionMatrix * pos;
vec4 color = vec4(0, 0, 0, 0);
vec4 globalAmbient = gl_Color * gl_LightModel.ambient;
if (lights == 0) {
color = gl_Color;
}
for (int i = 0; i < lights; i++) {
vec4 light = calculateLight(i);
color += light;
}
color = clamp(color, 0.0, 1.0);
gl_FrontColor = color;
vertColor = inColor;
}