mirror of
https://github.com/processing/processing4.git
synced 2026-01-29 19:31:16 +01:00
Some reorganization in the shader classes
This commit is contained in:
@@ -5601,12 +5601,6 @@ public class PGraphicsOpenGL extends PGraphics {
|
||||
|
||||
|
||||
protected class PolyShader extends PShader {
|
||||
// We need a reference to the renderer since a shader might
|
||||
// be called by different renderers within a single application
|
||||
// (the one corresponding to the main surface, or other offscreen
|
||||
// renderers).
|
||||
protected PGraphicsOpenGL renderer;
|
||||
|
||||
public PolyShader(PApplet parent) {
|
||||
super(parent);
|
||||
}
|
||||
@@ -5619,20 +5613,6 @@ public class PGraphicsOpenGL extends PGraphics {
|
||||
super(parent, vertURL, fragURL);
|
||||
}
|
||||
|
||||
public void setRenderer(PGraphicsOpenGL pg) {
|
||||
this.renderer = pg;
|
||||
}
|
||||
|
||||
public void loadAttributes() { }
|
||||
public void loadUniforms() { }
|
||||
|
||||
public void setAttribute(int loc, int vboId, int size, int type, boolean normalized, int stride, int offset) {
|
||||
if (-1 < loc) {
|
||||
pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, vboId);
|
||||
pgl.glVertexAttribPointer(loc, size, type, normalized, stride, offset);
|
||||
}
|
||||
}
|
||||
|
||||
public void setVertexAttribute(int vboId, int size, int type, int stride, int offset) { }
|
||||
public void setColorAttribute(int vboId, int size, int type, int stride, int offset) { }
|
||||
public void setNormalAttribute(int vboId, int size, int type, int stride, int offset) { }
|
||||
@@ -5686,9 +5666,9 @@ public class PGraphicsOpenGL extends PGraphics {
|
||||
if (-1 < inVertexLoc) pgl.glEnableVertexAttribArray(inVertexLoc);
|
||||
if (-1 < inColorLoc) pgl.glEnableVertexAttribArray(inColorLoc);
|
||||
|
||||
if (renderer != null) {
|
||||
renderer.updateGLProjmodelview();
|
||||
set4x4MatUniform(projmodelviewMatrixLoc, renderer.glProjmodelview);
|
||||
if (pgCurrent != null) {
|
||||
pgCurrent.updateGLProjmodelview();
|
||||
set4x4MatUniform(projmodelviewMatrixLoc, pgCurrent.glProjmodelview);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5804,24 +5784,24 @@ public class PGraphicsOpenGL extends PGraphics {
|
||||
if (-1 < inEmissiveLoc) pgl.glEnableVertexAttribArray(inEmissiveLoc);
|
||||
if (-1 < inShineLoc) pgl.glEnableVertexAttribArray(inShineLoc);
|
||||
|
||||
if (renderer != null) {
|
||||
renderer.updateGLProjmodelview();
|
||||
set4x4MatUniform(projmodelviewMatrixLoc, renderer.glProjmodelview);
|
||||
if (pgCurrent != null) {
|
||||
pgCurrent.updateGLProjmodelview();
|
||||
set4x4MatUniform(projmodelviewMatrixLoc, pgCurrent.glProjmodelview);
|
||||
|
||||
renderer.updateGLModelview();
|
||||
set4x4MatUniform(modelviewMatrixLoc, renderer.glModelview);
|
||||
pgCurrent.updateGLModelview();
|
||||
set4x4MatUniform(modelviewMatrixLoc, pgCurrent.glModelview);
|
||||
|
||||
renderer.updateGLNormal();
|
||||
set3x3MatUniform(normalMatrixLoc, renderer.glNormal);
|
||||
pgCurrent.updateGLNormal();
|
||||
set3x3MatUniform(normalMatrixLoc, pgCurrent.glNormal);
|
||||
|
||||
setIntUniform(lightCountLoc, renderer.lightCount);
|
||||
set4FloatVecUniform(lightPositionLoc, renderer.lightPosition);
|
||||
set3FloatVecUniform(lightNormalLoc, renderer.lightNormal);
|
||||
set3FloatVecUniform(lightAmbientLoc, renderer.lightAmbient);
|
||||
set3FloatVecUniform(lightDiffuseLoc, renderer.lightDiffuse);
|
||||
set3FloatVecUniform(lightSpecularLoc, renderer.lightSpecular);
|
||||
set3FloatVecUniform(lightFalloffCoefficientsLoc, renderer.lightFalloffCoefficients);
|
||||
set2FloatVecUniform(lightSpotParametersLoc, renderer.lightSpotParameters);
|
||||
setIntUniform(lightCountLoc, pgCurrent.lightCount);
|
||||
set4FloatVecUniform(lightPositionLoc, pgCurrent.lightPosition);
|
||||
set3FloatVecUniform(lightNormalLoc, pgCurrent.lightNormal);
|
||||
set3FloatVecUniform(lightAmbientLoc, pgCurrent.lightAmbient);
|
||||
set3FloatVecUniform(lightDiffuseLoc, pgCurrent.lightDiffuse);
|
||||
set3FloatVecUniform(lightSpecularLoc, pgCurrent.lightSpecular);
|
||||
set3FloatVecUniform(lightFalloffCoefficientsLoc, pgCurrent.lightFalloffCoefficients);
|
||||
set2FloatVecUniform(lightSpotParametersLoc, pgCurrent.lightSpotParameters);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10037,6 +10017,12 @@ public class PGraphicsOpenGL extends PGraphics {
|
||||
// previous group. Since the index groups are by definition disjoint,
|
||||
// these vertices need to be duplicated at the end of the corresponding
|
||||
// region in the vertex array.
|
||||
//
|
||||
// Also to keep in mind, the ordering of the indices affects performance
|
||||
// take a look at some of this references:
|
||||
// http://gameangst.com/?p=9
|
||||
// http://home.comcast.net/~tom_forsyth/papers/fast_vert_cache_opt.html
|
||||
// http://www.ludicon.com/castano/blog/2009/02/optimal-grid-rendering/
|
||||
void partitionRawIndices() {
|
||||
tess.polyIndexCheck(rawSize);
|
||||
int offset = tess.firstPolyIndex;
|
||||
|
||||
@@ -43,7 +43,14 @@ public class PShader {
|
||||
static public final int POINT = 5;
|
||||
|
||||
protected PApplet parent;
|
||||
protected PGraphicsOpenGL pg;
|
||||
// The main renderer associated to the parent PApplet.
|
||||
protected PGraphicsOpenGL pgMain;
|
||||
// We need a reference to the renderer since a shader might
|
||||
// be called by different renderers within a single application
|
||||
// (the one corresponding to the main surface, or other offscreen
|
||||
// renderers).
|
||||
protected PGraphicsOpenGL pgCurrent;
|
||||
|
||||
protected PGL pgl;
|
||||
protected PGL.Context context; // The context that created this shader.
|
||||
|
||||
@@ -62,9 +69,10 @@ public class PShader {
|
||||
|
||||
protected boolean active;
|
||||
|
||||
|
||||
public PShader() {
|
||||
parent = null;
|
||||
pg = null;
|
||||
pgMain = null;
|
||||
pgl = null;
|
||||
context = null;
|
||||
|
||||
@@ -80,14 +88,16 @@ public class PShader {
|
||||
active = false;
|
||||
}
|
||||
|
||||
|
||||
public PShader(PApplet parent) {
|
||||
this();
|
||||
this.parent = parent;
|
||||
pg = (PGraphicsOpenGL) parent.g;
|
||||
pgl = pg.pgl;
|
||||
pgMain = (PGraphicsOpenGL) parent.g;
|
||||
pgl = pgMain.pgl;
|
||||
context = pgl.createEmptyContext();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a shader program using the specified vertex and fragment
|
||||
* shaders.
|
||||
@@ -98,8 +108,8 @@ public class PShader {
|
||||
*/
|
||||
public PShader(PApplet parent, String vertFilename, String fragFilename) {
|
||||
this.parent = parent;
|
||||
pg = (PGraphicsOpenGL) parent.g;
|
||||
pgl = pg.pgl;
|
||||
pgMain = (PGraphicsOpenGL) parent.g;
|
||||
pgl = pgMain.pgl;
|
||||
|
||||
this.vertexURL = null;
|
||||
this.fragmentURL = null;
|
||||
@@ -111,10 +121,11 @@ public class PShader {
|
||||
fragmentShader = 0;
|
||||
}
|
||||
|
||||
|
||||
public PShader(PApplet parent, URL vertURL, URL fragURL) {
|
||||
this.parent = parent;
|
||||
pg = (PGraphicsOpenGL) parent.g;
|
||||
pgl = pg.pgl;
|
||||
pgMain = (PGraphicsOpenGL) parent.g;
|
||||
pgl = pgMain.pgl;
|
||||
|
||||
this.vertexURL = vertURL;
|
||||
this.fragmentURL = fragURL;
|
||||
@@ -126,38 +137,44 @@ public class PShader {
|
||||
fragmentShader = 0;
|
||||
}
|
||||
|
||||
|
||||
protected void finalize() throws Throwable {
|
||||
try {
|
||||
if (vertexShader != 0) {
|
||||
pg.finalizeGLSLVertShaderObject(vertexShader, context.code());
|
||||
pgMain.finalizeGLSLVertShaderObject(vertexShader, context.code());
|
||||
}
|
||||
if (fragmentShader != 0) {
|
||||
pg.finalizeGLSLFragShaderObject(fragmentShader, context.code());
|
||||
pgMain.finalizeGLSLFragShaderObject(fragmentShader, context.code());
|
||||
}
|
||||
if (programObject != 0) {
|
||||
pg.finalizeGLSLProgramObject(programObject, context.code());
|
||||
pgMain.finalizeGLSLProgramObject(programObject, context.code());
|
||||
}
|
||||
} finally {
|
||||
super.finalize();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void setVertexShader(String vertFilename) {
|
||||
this.vertexFilename = vertFilename;
|
||||
}
|
||||
|
||||
|
||||
public void setVertexShader(URL vertURL) {
|
||||
this.vertexURL = vertURL;
|
||||
}
|
||||
|
||||
|
||||
public void setFragmentShader(String fragFilename) {
|
||||
this.fragmentFilename = fragFilename;
|
||||
}
|
||||
|
||||
|
||||
public void setFragmentShader(URL fragURL) {
|
||||
this.fragmentURL = fragURL;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Starts the execution of the shader program.
|
||||
*/
|
||||
@@ -209,19 +226,27 @@ public class PShader {
|
||||
}
|
||||
|
||||
|
||||
public void setAttribute(int loc, int vboId, int size, int type, boolean normalized, int stride, int offset) {
|
||||
if (-1 < loc) {
|
||||
pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, vboId);
|
||||
pgl.glVertexAttribPointer(loc, size, type, normalized, stride, offset);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void setIntUniform(int loc, int x) {
|
||||
if (-1 < loc) {
|
||||
pgl.glUniform1i(loc, x);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void set1FloatUniform(int loc, float x) {
|
||||
if (-1 < loc) {
|
||||
pgl.glUniform1f(loc, x);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void set2FloatUniform(int loc, float x, float y) {
|
||||
if (-1 < loc) {
|
||||
@@ -293,6 +318,10 @@ public class PShader {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
// The individal attribute setters are not really needed,
|
||||
// read this:
|
||||
// http://stackoverflow.com/questions/7718976/what-is-glvertexattrib-versus-glvertexattribpointer-used-for
|
||||
public void set1FloatAttribute(int loc, float x) {
|
||||
if (-1 < loc) {
|
||||
pgl.glVertexAttrib1f(loc, x);
|
||||
@@ -319,11 +348,13 @@ public class PShader {
|
||||
pgl.glVertexAttrib4f(loc, x, y, z, w);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
protected void init() {
|
||||
if (programObject == 0 || contextIsOutdated()) {
|
||||
context = pgl.getCurrentContext();
|
||||
programObject = pg.createGLSLProgramObject(context.code());
|
||||
programObject = pgMain.createGLSLProgramObject(context.code());
|
||||
|
||||
boolean hasVert = false;
|
||||
if (vertexFilename != null) {
|
||||
@@ -383,9 +414,9 @@ public class PShader {
|
||||
protected boolean contextIsOutdated() {
|
||||
boolean outdated = !pgl.contextIsCurrent(context);
|
||||
if (outdated) {
|
||||
pg.removeGLSLProgramObject(programObject, context.code());
|
||||
pg.removeGLSLVertShaderObject(vertexShader, context.code());
|
||||
pg.removeGLSLFragShaderObject(fragmentShader, context.code());
|
||||
pgMain.removeGLSLProgramObject(programObject, context.code());
|
||||
pgMain.removeGLSLVertShaderObject(vertexShader, context.code());
|
||||
pgMain.removeGLSLFragShaderObject(fragmentShader, context.code());
|
||||
|
||||
programObject = 0;
|
||||
vertexShader = 0;
|
||||
@@ -404,6 +435,7 @@ public class PShader {
|
||||
vertexShaderSource = PApplet.join(parent.loadStrings(filename), "\n");
|
||||
return vertexShaderSource != null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Loads and compiles the vertex shader contained in the URL.
|
||||
@@ -420,6 +452,7 @@ public class PShader {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Loads and compiles the fragment shader contained in file.
|
||||
*
|
||||
@@ -430,6 +463,7 @@ public class PShader {
|
||||
return fragmentShaderSource != null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Loads and compiles the fragment shader contained in the URL.
|
||||
*
|
||||
@@ -445,11 +479,12 @@ public class PShader {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param shaderSource a string containing the shader's code
|
||||
*/
|
||||
protected boolean compileVertexShader() {
|
||||
vertexShader = pg.createGLSLVertShaderObject(context.code());
|
||||
vertexShader = pgMain.createGLSLVertShaderObject(context.code());
|
||||
|
||||
pgl.glShaderSource(vertexShader, vertexShaderSource);
|
||||
pgl.glCompileShader(vertexShader);
|
||||
@@ -469,7 +504,7 @@ public class PShader {
|
||||
* @param shaderSource a string containing the shader's code
|
||||
*/
|
||||
protected boolean compileFragmentShader() {
|
||||
fragmentShader = pg.createGLSLFragShaderObject(context.code());
|
||||
fragmentShader = pgMain.createGLSLFragShaderObject(context.code());
|
||||
|
||||
pgl.glShaderSource(fragmentShader, fragmentShaderSource);
|
||||
pgl.glCompileShader(fragmentShader);
|
||||
@@ -485,17 +520,27 @@ public class PShader {
|
||||
}
|
||||
|
||||
|
||||
protected void setRenderer(PGraphicsOpenGL pg) {
|
||||
pgCurrent = pg;
|
||||
}
|
||||
|
||||
protected void loadAttributes() { }
|
||||
|
||||
|
||||
protected void loadUniforms() { }
|
||||
|
||||
|
||||
protected void release() {
|
||||
if (vertexShader != 0) {
|
||||
pg.deleteGLSLVertShaderObject(vertexShader, context.code());
|
||||
pgMain.deleteGLSLVertShaderObject(vertexShader, context.code());
|
||||
vertexShader = 0;
|
||||
}
|
||||
if (fragmentShader != 0) {
|
||||
pg.deleteGLSLFragShaderObject(fragmentShader, context.code());
|
||||
pgMain.deleteGLSLFragShaderObject(fragmentShader, context.code());
|
||||
fragmentShader = 0;
|
||||
}
|
||||
if (programObject != 0) {
|
||||
pg.deleteGLSLProgramObject(programObject, context.code());
|
||||
pgMain.deleteGLSLProgramObject(programObject, context.code());
|
||||
programObject = 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user