From d67c85ef222e44d62f3679695b1860c7ccc9bc27 Mon Sep 17 00:00:00 2001 From: arturo Date: Mon, 12 Nov 2012 19:29:45 +0100 Subject: [PATCH] gles2: fixed vbo texture coordinates + custom shaders working --- libs/openFrameworks/gl/ofGLES2Renderer.cpp | 196 ++++++++++++++++----- libs/openFrameworks/gl/ofGLES2Renderer.h | 16 +- libs/openFrameworks/gl/ofGLUtils.cpp | 67 +++++++ libs/openFrameworks/gl/ofGLUtils.h | 12 ++ libs/openFrameworks/gl/ofShader.cpp | 74 +++++++- libs/openFrameworks/gl/ofShader.h | 9 +- libs/openFrameworks/gl/ofTexture.cpp | 1 + libs/openFrameworks/gl/ofVbo.cpp | 32 +--- 8 files changed, 330 insertions(+), 77 deletions(-) diff --git a/libs/openFrameworks/gl/ofGLES2Renderer.cpp b/libs/openFrameworks/gl/ofGLES2Renderer.cpp index d581fdd8d43..9e6a073479f 100644 --- a/libs/openFrameworks/gl/ofGLES2Renderer.cpp +++ b/libs/openFrameworks/gl/ofGLES2Renderer.cpp @@ -76,6 +76,11 @@ ofGLES2Renderer::ofGLES2Renderer(string vertexShader, string fragmentShader, boo vertexFile = vertexShader; fragmentFile = fragmentShader; + + verticesEnabled = true; + colorsEnabled = false; + texCoordsEnabled = false; + normalsEnabled = false; } ofGLES2Renderer::~ofGLES2Renderer() { @@ -83,12 +88,12 @@ ofGLES2Renderer::~ofGLES2Renderer() { //---------------------------------------------------------- void ofGLES2Renderer::startRender() { - currentShader.begin(); + defaultShader.begin(); } //---------------------------------------------------------- void ofGLES2Renderer::finishRender() { - currentShader.end(); + defaultShader.end(); modelViewStack.empty(); projectionStack.empty(); } @@ -98,23 +103,24 @@ bool ofGLES2Renderer::setup() { bool ret; if(vertexFile!=""){ ofLogNotice() << "GLES2 loading vertex shader from " + vertexFile; - ret = currentShader.setupShaderFromFile(GL_VERTEX_SHADER,vertexFile); + ret = defaultShader.setupShaderFromFile(GL_VERTEX_SHADER,vertexFile); }else{ ofLogNotice() << "GLES2 loading vertex shader from default source"; - ret = currentShader.setupShaderFromSource(GL_VERTEX_SHADER,defaultVertexShader); + ret = defaultShader.setupShaderFromSource(GL_VERTEX_SHADER,defaultVertexShader); } if(ret){ if(fragmentFile!=""){ ofLogNotice() << "GLES2 loading fragment shader from " + fragmentFile; - ret = currentShader.setupShaderFromFile(GL_FRAGMENT_SHADER,fragmentFile); + ret = defaultShader.setupShaderFromFile(GL_FRAGMENT_SHADER,fragmentFile); }else{ ofLogNotice() << "GLES2 loading fragment shader from default source"; - ret = currentShader.setupShaderFromSource(GL_FRAGMENT_SHADER,defaultFragmentShader); + ret = defaultShader.setupShaderFromSource(GL_FRAGMENT_SHADER,defaultFragmentShader); } } if(ret){ - ret = currentShader.linkProgram(); + ret = defaultShader.linkProgram(); } + currentShader = defaultShader; return ret; } @@ -133,15 +139,15 @@ void ofGLES2Renderer::draw(ofMesh & vertexData, bool useColors, bool useTextures } if(vertexData.getNumColors() && useColors){ currentShader.setAttribute4fv("color",&vertexData.getColorsPointer()->r,sizeof(ofFloatColor)); - currentShader.setUniform1f("useColors",1); + enableColors(); }else{ - currentShader.setUniform1f("useColors",0); + disableColors(); } if(vertexData.getNumTexCoords() && useTextures){ - currentShader.setUniform1f("useTexture",1); currentShader.setAttribute2fv("texcoord",&vertexData.getTexCoordsPointer()->x,sizeof(ofVec2f)); + enableTexCoords(); }else{ - currentShader.setUniform1f("useTexture",0); + disableTexCoords(); } if(vertexData.getNumIndices()){ @@ -154,13 +160,13 @@ void ofGLES2Renderer::draw(ofMesh & vertexData, bool useColors, bool useTextures glDrawArrays(ofGetGLPrimitiveMode(vertexData.getMode()), 0, vertexData.getNumVertices()); } if(vertexData.getNumColors()){ - glDisableVertexAttribArray(getAttrLocationColor()); + disableColors(); } if(vertexData.getNumNormals()){ - glDisableVertexAttribArray(getAttrLocationNormal()); + disableNormals(); } if(vertexData.getNumTexCoords()){ - glDisableVertexAttribArray(getAttrLocationTexCoord()); + disableTexCoords(); } } @@ -175,15 +181,15 @@ void ofGLES2Renderer::draw(ofMesh & vertexData, ofPolyRenderMode renderType, boo } if(vertexData.getNumColors() && useColors){ currentShader.setAttribute4fv("color",&vertexData.getColorsPointer()->r,sizeof(ofFloatColor)); - currentShader.setUniform1f("useColors",1); + enableColors(); }else{ - currentShader.setUniform1f("useColors",0); + disableColors(); } if(vertexData.getNumTexCoords() && useTextures){ - currentShader.setUniform1f("useTexture",1); currentShader.setAttribute2fv("texcoord",&vertexData.getTexCoordsPointer()->x,sizeof(ofVec2f)); + enableTexCoords(); }else{ - currentShader.setUniform1f("useTexture",0); + disableTexCoords(); } GLenum drawMode; @@ -212,13 +218,13 @@ void ofGLES2Renderer::draw(ofMesh & vertexData, ofPolyRenderMode renderType, boo glDrawArrays(drawMode, 0, vertexData.getNumVertices()); } if(vertexData.getNumColors()){ - glDisableVertexAttribArray(getAttrLocationColor()); + disableColors(); } if(vertexData.getNumNormals()){ - glDisableVertexAttribArray(getAttrLocationNormal()); + disableNormals(); } if(vertexData.getNumTexCoords()){ - glDisableVertexAttribArray(getAttrLocationTexCoord()); + disableTexCoords(); } if (bSmoothHinted) endSmoothing(); } @@ -228,8 +234,8 @@ void ofGLES2Renderer::draw(vector & vertexData, ofPrimitiveMode drawMod if(!vertexData.empty()) { if (bSmoothHinted) startSmoothing(); currentShader.setAttribute3fv("position",&vertexData[0].x,sizeof(ofVec3f)); - currentShader.setUniform1f("useTexture",0); - currentShader.setUniform1f("useColors",0); + disableTexCoords(); + disableColors(); glDrawArrays(ofGetGLPrimitiveMode(drawMode), 0, vertexData.size()); if (bSmoothHinted) endSmoothing(); } @@ -242,8 +248,8 @@ void ofGLES2Renderer::draw(ofPolyline & poly){ if (bSmoothHinted) startSmoothing(); currentShader.setAttribute3fv("position",&poly.getVertices()[0].x,sizeof(ofVec3f)); - currentShader.setUniform1f("useTexture",0); - currentShader.setUniform1f("useColors",0); + disableTexCoords(); + disableColors(); glDrawArrays(poly.isClosed()?GL_LINE_LOOP:GL_LINE_STRIP, 0, poly.size()); // use smoothness, if requested: @@ -283,42 +289,45 @@ void ofGLES2Renderer::draw(ofPath & shape){ //---------------------------------------------------------- void ofGLES2Renderer::draw(ofImage & image, float x, float y, float z, float w, float h, float sx, float sy, float sw, float sh){ if(image.isUsingTexture()){ - currentShader.setUniform1f("useTexture",1); - currentShader.setUniform1f("useColors",0); + enableTexCoords(); + disableColors(); ofTexture& tex = image.getTextureReference(); if(tex.bAllocated()) { tex.drawSubsection(x,y,z,w,h,sx,sy,sw,sh); } else { ofLogWarning() << "ofGLRenderer::draw(): texture is not allocated"; } + disableTexCoords(); } } //---------------------------------------------------------- void ofGLES2Renderer::draw(ofFloatImage & image, float x, float y, float z, float w, float h, float sx, float sy, float sw, float sh){ if(image.isUsingTexture()){ - currentShader.setUniform1f("useTexture",1); - currentShader.setUniform1f("useColors",0); + enableTexCoords(); + disableColors(); ofTexture& tex = image.getTextureReference(); if(tex.bAllocated()) { tex.drawSubsection(x,y,z,w,h,sx,sy,sw,sh); } else { ofLogWarning() << "ofGLRenderer::draw(): texture is not allocated"; } + disableTexCoords(); } } //---------------------------------------------------------- void ofGLES2Renderer::draw(ofShortImage & image, float x, float y, float z, float w, float h, float sx, float sy, float sw, float sh){ if(image.isUsingTexture()){ - currentShader.setUniform1f("useTexture",1); - currentShader.setUniform1f("useColors",0); + enableTexCoords(); + disableColors(); ofTexture& tex = image.getTextureReference(); if(tex.bAllocated()) { tex.drawSubsection(x,y,z,w,h,sx,sy,sw,sh); } else { ofLogWarning() << "ofGLRenderer::draw(): texture is not allocated"; } + disableTexCoords(); } } @@ -966,7 +975,59 @@ void ofGLES2Renderer::endSmoothing(){ //---------------------------------------------------------- void ofGLES2Renderer::setBlendMode(ofBlendMode blendMode){ - // TODO :: needs ES2 code. + switch (blendMode){ + + case OF_BLENDMODE_ALPHA:{ + glEnable(GL_BLEND); + #ifndef TARGET_OPENGLES + glBlendEquation(GL_FUNC_ADD); + #endif + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + break; + } + + case OF_BLENDMODE_ADD:{ + glEnable(GL_BLEND); + #ifndef TARGET_OPENGLES + glBlendEquation(GL_FUNC_ADD); + #endif + glBlendFunc(GL_SRC_ALPHA, GL_ONE); + break; + } + + case OF_BLENDMODE_MULTIPLY:{ + glEnable(GL_BLEND); + #ifndef TARGET_OPENGLES + glBlendEquation(GL_FUNC_ADD); + #endif + glBlendFunc(GL_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA /* GL_ZERO or GL_ONE_MINUS_SRC_ALPHA */); + break; + } + + case OF_BLENDMODE_SCREEN:{ + glEnable(GL_BLEND); + #ifndef TARGET_OPENGLES + glBlendEquation(GL_FUNC_ADD); + #endif + glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ONE); + break; + } + + case OF_BLENDMODE_SUBTRACT:{ + glEnable(GL_BLEND); + #ifndef TARGET_OPENGLES + glBlendEquation(GL_FUNC_REVERSE_SUBTRACT); + #else + ofLog(OF_LOG_WARNING, "OF_BLENDMODE_SUBTRACT not currently supported on OpenGL/ES"); + #endif + glBlendFunc(GL_SRC_ALPHA, GL_ONE); + break; + } + + + default: + break; + } } //---------------------------------------------------------- @@ -1005,10 +1066,59 @@ ofShader & ofGLES2Renderer::getCurrentShader(){ } //---------------------------------------------------------- -void ofGLES2Renderer::setCurrentShader(ofShader & shader){ +void ofGLES2Renderer::setDefaultShader(ofShader & shader){ + defaultShader = shader; +} + +//---------------------------------------------------------- +void ofGLES2Renderer::enableTexCoords(){ + currentShader.setUniform1f("useTexture",1); + texCoordsEnabled = true; +} + +//---------------------------------------------------------- +void ofGLES2Renderer::enableColors(){ + currentShader.setUniform1f("useColors",1); + colorsEnabled = true; +} + +//---------------------------------------------------------- +void ofGLES2Renderer::enableNormals(){ + normalsEnabled = true; +} + +//---------------------------------------------------------- +void ofGLES2Renderer::disableTexCoords(){ + currentShader.setUniform1f("useTexture",0); + texCoordsEnabled = false; +} + +//---------------------------------------------------------- +void ofGLES2Renderer::disableColors(){ + currentShader.setUniform1f("useColors",0); + colorsEnabled = false; +} + +//---------------------------------------------------------- +void ofGLES2Renderer::disableNormals(){ + normalsEnabled = false; +} + +//---------------------------------------------------------- +void ofGLES2Renderer::beginCustomShader(ofShader & shader){ + shader.setUniform1f("useTexture",texCoordsEnabled); + shader.setUniform1f("useColors",colorsEnabled); + shader.setUniform4f("color",currentColor.r,currentColor.g,currentColor.b,currentColor.a); + shader.setUniformMatrix4f("modelViewMatrix",modelViewOrientation); + shader.setUniformMatrix4f("projectionMatrix",projection); + shader.setUniformMatrix4f("textureMatrix",textureMatrix); currentShader = shader; } +void ofGLES2Renderer::endCustomShader(){ + beginCustomShader(defaultShader); +} + //---------------------------------------------------------- void ofGLES2Renderer::drawLine(float x1, float y1, float z1, float x2, float y2, float z2){ linePoints[0].set(x1,y1,z1); @@ -1018,8 +1128,8 @@ void ofGLES2Renderer::drawLine(float x1, float y1, float z1, float x2, float y2, if (bSmoothHinted) startSmoothing(); currentShader.setAttribute3fv("position",&linePoints[0].x,sizeof(ofVec3f)); - currentShader.setUniform1f("useTexture",0); - currentShader.setUniform1f("useColors",0); + disableTexCoords(); + disableColors(); glDrawArrays(GL_LINES, 0, 2); @@ -1045,8 +1155,8 @@ void ofGLES2Renderer::drawRectangle(float x, float y, float z, float w, float h) if (bSmoothHinted && bFilled == OF_OUTLINE) startSmoothing(); currentShader.setAttribute3fv("position",&rectPoints[0].x,sizeof(ofVec3f)); - currentShader.setUniform1f("useTexture",0); - currentShader.setUniform1f("useColors",0); + disableTexCoords(); + disableColors(); glDrawArrays((bFilled == OF_FILLED) ? GL_TRIANGLE_FAN : GL_LINE_LOOP, 0, 4); @@ -1064,8 +1174,8 @@ void ofGLES2Renderer::drawTriangle(float x1, float y1, float z1, float x2, float if (bSmoothHinted && bFilled == OF_OUTLINE) startSmoothing(); currentShader.setAttribute3fv("position",&triPoints[0].x,sizeof(ofVec3f)); - currentShader.setUniform1f("useTexture",0); - currentShader.setUniform1f("useColors",0); + disableTexCoords(); + disableColors(); glDrawArrays((bFilled == OF_FILLED) ? GL_TRIANGLE_FAN : GL_LINE_LOOP, 0, 3); @@ -1084,8 +1194,8 @@ void ofGLES2Renderer::drawCircle(float x, float y, float z, float radius){ if (bSmoothHinted && bFilled == OF_OUTLINE) startSmoothing(); currentShader.setAttribute3fv("position",&circlePoints[0].x,sizeof(ofVec3f)); - currentShader.setUniform1f("useTexture",0); - currentShader.setUniform1f("useColors",0); + disableTexCoords(); + disableColors(); glDrawArrays((bFilled == OF_FILLED) ? GL_TRIANGLE_FAN : GL_LINE_STRIP, 0, circlePoints.size()); @@ -1111,8 +1221,8 @@ void ofGLES2Renderer::drawEllipse(float x, float y, float z, float width, float if (bSmoothHinted && bFilled == OF_OUTLINE) startSmoothing(); currentShader.setAttribute3fv("position",&circlePoints[0].x,sizeof(ofVec3f)); - currentShader.setUniform1f("useTexture",0); - currentShader.setUniform1f("useColors",0); + disableTexCoords(); + disableColors(); glDrawArrays((bFilled == OF_FILLED) ? GL_TRIANGLE_FAN : GL_LINE_STRIP, 0, circlePoints.size()); diff --git a/libs/openFrameworks/gl/ofGLES2Renderer.h b/libs/openFrameworks/gl/ofGLES2Renderer.h index e92c07f7bee..18c1d4921fb 100644 --- a/libs/openFrameworks/gl/ofGLES2Renderer.h +++ b/libs/openFrameworks/gl/ofGLES2Renderer.h @@ -129,8 +129,19 @@ class ofGLES2Renderer: public ofBaseRenderer{ GLint getAttrLocationColor(); GLint getAttrLocationNormal(); GLint getAttrLocationTexCoord(); + ofShader & getCurrentShader(); - void setCurrentShader(ofShader & shader); + void setDefaultShader(ofShader & shader); + + void enableTexCoords(); + void enableColors(); + void enableNormals(); + void disableTexCoords(); + void disableColors(); + void disableNormals(); + + void beginCustomShader(ofShader & shader); + void endCustomShader(); private: void uploadModelViewMatrix(const ofMatrix4x4 & m); @@ -166,7 +177,10 @@ class ofGLES2Renderer: public ofBaseRenderer{ ofRectMode rectMode; ofFbo * currentFbo; + ofShader defaultShader; ofShader currentShader; string vertexFile; string fragmentFile; + + bool verticesEnabled, colorsEnabled, texCoordsEnabled, normalsEnabled; }; diff --git a/libs/openFrameworks/gl/ofGLUtils.cpp b/libs/openFrameworks/gl/ofGLUtils.cpp index 1bff77fd9fe..4278581199a 100644 --- a/libs/openFrameworks/gl/ofGLUtils.cpp +++ b/libs/openFrameworks/gl/ofGLUtils.cpp @@ -3,6 +3,7 @@ #include #include "ofGLES2Renderer.h" #include "ofGraphics.h" +#include "ofShader.h" GLuint ofGetGLPolyMode(ofPolyRenderMode m){ #ifndef TARGET_OPENGLES @@ -176,6 +177,72 @@ GLint ofGetAttrLocationTexCoord(){ return ofGetGLES2Renderer()->getAttrLocationTexCoord(); } +void ofEnableVertices(){ + if(ofGLIsFixedPipeline()){ + glEnableClientState(GL_VERTEX_ARRAY); + }else{ + glEnableVertexAttribArray(ofGetAttrLocationPosition()); + } +} + +void ofEnableTexCoords(){ + if(ofGLIsFixedPipeline()){ + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + }else{ + glEnableVertexAttribArray(ofGetAttrLocationTexCoord()); + ofGetGLES2Renderer()->enableTexCoords(); + } +} + +void ofEnableColorCoords(){ + if(ofGLIsFixedPipeline()){ + glEnableClientState(GL_COLOR_ARRAY); + }else{ + glEnableVertexAttribArray(ofGetAttrLocationColor()); + ofGetGLES2Renderer()->enableColors(); + } +} + +void ofEnableNormals(){ + if(ofGLIsFixedPipeline()){ + glEnableClientState(GL_NORMAL_ARRAY); + }else{ + glEnableVertexAttribArray(ofGetAttrLocationNormal()); + ofGetGLES2Renderer()->enableNormals(); + } +} +void ofDisableVertices(){ + if(ofGLIsFixedPipeline()){ + glDisableClientState(GL_VERTEX_ARRAY); + }else{ + glDisableVertexAttribArray(ofGetAttrLocationPosition()); + } +} + +void ofDisableTexCoords(){ + if(ofGLIsFixedPipeline()){ + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + }else{ + glDisableVertexAttribArray(ofGetAttrLocationTexCoord()); + ofGetGLES2Renderer()->disableTexCoords(); + } +} +void ofDisableColorCoords(){ + if(ofGLIsFixedPipeline()){ + glDisableClientState(GL_COLOR_ARRAY); + }else{ + glDisableVertexAttribArray(ofGetAttrLocationColor()); + ofGetGLES2Renderer()->disableColors(); + } +} +void ofDisableNormals(){ + if(ofGLIsFixedPipeline()){ + glDisableClientState(GL_NORMAL_ARRAY); + }else{ + glDisableVertexAttribArray(ofGetAttrLocationNormal()); + ofGetGLES2Renderer()->disableNormals(); + } +} diff --git a/libs/openFrameworks/gl/ofGLUtils.h b/libs/openFrameworks/gl/ofGLUtils.h index b70b5233acb..b47736dbed5 100644 --- a/libs/openFrameworks/gl/ofGLUtils.h +++ b/libs/openFrameworks/gl/ofGLUtils.h @@ -10,6 +10,8 @@ #include "ofConstants.h" #include "ofTypes.h" + +class ofShader; class ofGLES2Renderer; enum ofPrimitiveMode{ @@ -60,4 +62,14 @@ GLint ofGetAttrLocationPosition(); GLint ofGetAttrLocationColor(); GLint ofGetAttrLocationNormal(); GLint ofGetAttrLocationTexCoord(); + +void ofEnableVertices(); +void ofEnableTexCoords(); +void ofEnableColorCoords(); +void ofEnableNormals(); +void ofDisableVertices(); +void ofDisableTexCoords(); +void ofDisableColorCoords(); +void ofDisableNormals(); + #endif /* OFGLUTILS_H_ */ diff --git a/libs/openFrameworks/gl/ofShader.cpp b/libs/openFrameworks/gl/ofShader.cpp index 1a375ec7aae..a99c52871ec 100644 --- a/libs/openFrameworks/gl/ofShader.cpp +++ b/libs/openFrameworks/gl/ofShader.cpp @@ -2,9 +2,13 @@ #include "ofUtils.h" #include "ofFileUtils.h" #include "ofGraphics.h" +#include "ofGLES2Renderer.h" #include +GLint ofShader::activeProgram=0; +GLint ofShader::prevActiveProgram=0; + static map & getShaderIds(){ static map * ids = new map; return *ids; @@ -76,6 +80,39 @@ ofShader::~ofShader() { unload(); } +//-------------------------------------------------------------- +ofShader::ofShader(const ofShader & mom) : +program(mom.program), +bLoaded(mom.bLoaded), +shaders(mom.shaders), +attribsCache(mom.attribsCache), +uniformsCache(mom.uniformsCache){ + if(mom.bLoaded){ + retainProgram(program); + for(map::const_iterator it = shaders.begin(); it != shaders.end(); ++it){ + GLuint shader = it->second; + retainShader(shader); + } + } +} + +//-------------------------------------------------------------- +ofShader & ofShader::operator=(const ofShader & mom){ + program = mom.program; + bLoaded = mom.bLoaded; + shaders = mom.shaders, + attribsCache = mom.attribsCache, + uniformsCache = mom.uniformsCache; + if(mom.bLoaded){ + retainProgram(program); + for(map::const_iterator it = shaders.begin(); it != shaders.end(); ++it){ + GLuint shader = it->second; + retainShader(shader); + } + } + return *this; +} + //-------------------------------------------------------------- bool ofShader::load(string shaderName) { return load(shaderName + ".vert", shaderName + ".frag"); @@ -287,7 +324,9 @@ void ofShader::unload() { releaseProgram(program); program = 0; } - + + attribsCache.clear(); + uniformsCache.clear(); shaders.clear(); } bLoaded = false; @@ -300,14 +339,27 @@ bool ofShader::isLoaded(){ //-------------------------------------------------------------- void ofShader::begin() { - if (bLoaded == true) + if (bLoaded && activeProgram!=program){ glUseProgram(program); + prevActiveProgram = activeProgram; + activeProgram = program; + if(!ofGLIsFixedPipeline()){ + ofGetGLES2Renderer()->beginCustomShader(*this); + } + } } //-------------------------------------------------------------- void ofShader::end() { - if (bLoaded == true) - glUseProgram(0); + if (bLoaded && activeProgram==program){ + if(!ofGLIsFixedPipeline()){ + ofGetGLES2Renderer()->endCustomShader(); + }else{ + glUseProgram(prevActiveProgram); + activeProgram = prevActiveProgram; + prevActiveProgram = 0; + } + } } //-------------------------------------------------------------- @@ -555,12 +607,22 @@ void ofShader::setAttribute4d(GLint location, double v1, double v2, double v3, d //-------------------------------------------------------------- GLint ofShader::getAttributeLocation(const char* name) { - return glGetAttribLocation(program, name); + if(attribsCache.find(name)!=attribsCache.end()){ + return attribsCache[name]; + } + GLint location = glGetAttribLocation(program, name); + attribsCache[name] = location; + return location; } //-------------------------------------------------------------- GLint ofShader::getUniformLocation(const char* name) { - return glGetUniformLocation(program, name); + if(uniformsCache.find(name)!=uniformsCache.end()){ + return uniformsCache[name]; + } + GLint location = glGetUniformLocation(program, name); + uniformsCache[name] = location; + return location; } //-------------------------------------------------------------- diff --git a/libs/openFrameworks/gl/ofShader.h b/libs/openFrameworks/gl/ofShader.h index 4ae7d2ede5c..fc1c61237b0 100644 --- a/libs/openFrameworks/gl/ofShader.h +++ b/libs/openFrameworks/gl/ofShader.h @@ -18,6 +18,8 @@ class ofShader { public: ofShader(); ~ofShader(); + ofShader(const ofShader & shader); + ofShader & operator=(const ofShader & shader); bool load(string shaderName); bool load(string vertName, string fragName, string geomName=""); @@ -114,8 +116,12 @@ class ofShader { private: GLuint program; + bool bLoaded; map shaders; - + map attribsCache; + map uniformsCache; + static GLint activeProgram; + static GLint prevActiveProgram; GLint getUniformLocation(const char* name); @@ -127,6 +133,5 @@ class ofShader { void checkAndCreateProgram(); - bool bLoaded; }; diff --git a/libs/openFrameworks/gl/ofTexture.cpp b/libs/openFrameworks/gl/ofTexture.cpp index f9fb56cfe3f..8eae9a3f9e4 100644 --- a/libs/openFrameworks/gl/ofTexture.cpp +++ b/libs/openFrameworks/gl/ofTexture.cpp @@ -846,6 +846,7 @@ void ofTexture::bind(){ //---------------------------------------------------------- void ofTexture::unbind(){ + glBindTexture( texData.textureTarget, 0); disableTextureTarget(); if(ofGetUsingNormalizedTexCoords()) { diff --git a/libs/openFrameworks/gl/ofVbo.cpp b/libs/openFrameworks/gl/ofVbo.cpp index 06f658e8907..1696cdd2d35 100644 --- a/libs/openFrameworks/gl/ofVbo.cpp +++ b/libs/openFrameworks/gl/ofVbo.cpp @@ -450,68 +450,50 @@ GLuint ofVbo::getIndexId(){ void ofVbo::bind(){ if(bUsingVerts){ glBindBuffer(GL_ARRAY_BUFFER, vertId); + ofEnableVertices(); if(ofGLIsFixedPipeline()){ - glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(vertSize, GL_FLOAT, vertStride, 0); }else{ glVertexAttribPointer(ofGetAttrLocationPosition(), 3, GL_FLOAT, GL_FALSE, vertStride, 0); - glEnableVertexAttribArray(ofGetAttrLocationPosition()); } } if(bUsingColors) { glBindBuffer(GL_ARRAY_BUFFER, colorId); + ofEnableColorCoords(); if(ofGLIsFixedPipeline()){ - glEnableClientState(GL_COLOR_ARRAY); glColorPointer(4, GL_FLOAT, colorStride, 0); }else{ glVertexAttribPointer(ofGetAttrLocationColor(), 4, GL_FLOAT, GL_FALSE, colorStride, 0); - glEnableVertexAttribArray(ofGetAttrLocationColor()); } } if(bUsingNormals) { glBindBuffer(GL_ARRAY_BUFFER, normalId); + ofEnableNormals(); if(ofGLIsFixedPipeline()){ - glEnableClientState(GL_NORMAL_ARRAY); glNormalPointer(GL_FLOAT, normalStride, 0); }else{ glVertexAttribPointer(ofGetAttrLocationNormal(), 3, GL_FLOAT, GL_FALSE, normalStride, 0); - glEnableVertexAttribArray(ofGetAttrLocationNormal()); } } if(bUsingTexCoords) { glBindBuffer(GL_ARRAY_BUFFER, texCoordId); + ofEnableTexCoords(); if(ofGLIsFixedPipeline()){ - glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer(2, GL_FLOAT, texCoordStride, 0); }else{ glVertexAttribPointer(ofGetAttrLocationTexCoord(), 3, GL_FLOAT, GL_FALSE, texCoordStride, 0); - glEnableVertexAttribArray(ofGetAttrLocationTexCoord()); } } } //-------------------------------------------------------------- void ofVbo::unbind() { -// if(bUsingVerts) glDisableClientState(GL_VERTEX_ARRAY); - if(ofGLIsFixedPipeline()){ - if(bUsingColors) glDisableClientState(GL_COLOR_ARRAY); - if(bUsingNormals) glDisableClientState(GL_NORMAL_ARRAY); - if(bUsingTexCoords) glDisableClientState(GL_TEXTURE_COORD_ARRAY); - }else{ - glDisableVertexAttribArray(ofGetAttrLocationColor()); - glDisableVertexAttribArray(ofGetAttrLocationNormal()); - glDisableVertexAttribArray(ofGetAttrLocationTexCoord()); - } - //glDisableClientState(GL_EDGE_FLAG_ARRAY); - -//#ifndef TARGET_OPENGLES -// glPopClientAttrib(); -// glPopAttrib(); -//#endif - + if(bUsingColors) ofDisableColorCoords(); + if(bUsingNormals) ofDisableNormals(); + if(bUsingTexCoords) ofDisableTexCoords(); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); }