@@ -182,15 +182,6 @@ public class PShapeOpenGL extends PShape {
182182
183183 // ........................................................
184184
185- // Modes inherited from renderer
186-
187- protected int rectMode ;
188- protected int ellipseMode ;
189- protected int shapeMode ;
190- protected int imageMode ;
191-
192- // ........................................................
193-
194185 // Bezier and Catmull-Rom curves
195186
196187 protected int bezierDetail = 20 ;
@@ -307,12 +298,8 @@ public PShapeOpenGL(PApplet parent, int family) {
307298 inGeo = pg .newInGeometry (PGraphicsOpenGL .RETAINED );
308299 }
309300
310- // Modes are retrieved from the current values in the renderer.
301+ // Style parameters are retrieved from the current values in the renderer.
311302 textureMode = pg .textureMode ;
312- rectMode = pg .rectMode ;
313- ellipseMode = pg .ellipseMode ;
314- shapeMode = pg .shapeMode ;
315- imageMode = pg .imageMode ;
316303
317304 colorMode (pg .colorMode ,
318305 pg .colorModeX , pg .colorModeY , pg .colorModeZ , pg .colorModeA );
@@ -757,10 +744,25 @@ public void setTextureMode(int mode) {
757744 if (family == GROUP ) {
758745 for (int i = 0 ; i < childCount ; i ++) {
759746 PShapeOpenGL child = (PShapeOpenGL ) children [i ];
760- child .textureMode (mode );
747+ child .setTextureMode (mode );
761748 }
762749 } else {
763- textureMode = mode ;
750+ setTextureModeImpl (mode );
751+ }
752+ }
753+
754+
755+ protected void setTextureModeImpl (int mode ) {
756+ if (textureMode == mode ) return ;
757+ textureMode = mode ;
758+ if (image != null ) {
759+ float uFactor = image .width ;
760+ float vFactor = image .height ;
761+ if (textureMode == NORMAL ) {
762+ uFactor = 1.0f / uFactor ;
763+ vFactor = 1.0f / vFactor ;
764+ }
765+ scaleTextureUV (uFactor , vFactor );
764766 }
765767 }
766768
@@ -778,17 +780,69 @@ public void setTexture(PImage tex) {
778780 child .texture (tex );
779781 }
780782 } else {
781- PImage tex0 = image ;
782- image = tex ;
783- if (tex0 != tex && parent != null ) {
784- ((PShapeOpenGL )parent ).removeTexture (tex );
783+ setTextureImpl (tex );
784+ }
785+ }
786+
787+
788+ protected void setTextureImpl (PImage tex ) {
789+ PImage image0 = image ;
790+ image = tex ;
791+
792+ if (textureMode == IMAGE && image0 != image ) {
793+ // Need to rescale the texture coordinates
794+ float uFactor = 1 ;
795+ float vFactor = 1 ;
796+ if (image != null ) {
797+ uFactor /= image .width ;
798+ vFactor /= image .height ;
785799 }
786- if (parent != null ) {
787- ((PShapeOpenGL )parent ).addTexture (image );
788- if (is2D () && stroke ) {
789- ((PShapeOpenGL )parent ).strokedTexture (true );
790- }
800+ if (image0 != null ) {
801+ uFactor *= image0 .width ;
802+ vFactor *= image0 .height ;
803+ }
804+ scaleTextureUV (uFactor , vFactor );
805+ }
806+
807+ if (image0 != tex && parent != null ) {
808+ ((PShapeOpenGL )parent ).removeTexture (tex );
809+ }
810+ if (parent != null ) {
811+ ((PShapeOpenGL )parent ).addTexture (image );
812+ if (is2D () && stroke ) {
813+ ((PShapeOpenGL )parent ).strokedTexture (true );
814+ }
815+ }
816+ }
817+
818+
819+ protected void scaleTextureUV (float uFactor , float vFactor ) {
820+ if (PGraphicsOpenGL .same (uFactor , 1 ) &&
821+ PGraphicsOpenGL .same (vFactor , 1 )) return ;
822+
823+ for (int i = 0 ; i < inGeo .vertexCount ; i ++) {
824+ float u = inGeo .texcoords [2 * i + 0 ];
825+ float v = inGeo .texcoords [2 * i + 1 ];
826+ inGeo .texcoords [2 * i + 0 ] = PApplet .min (1 , u * uFactor );
827+ inGeo .texcoords [2 * i + 1 ] = PApplet .min (1 , v * uFactor );
828+ }
829+
830+ if (shapeCreated && tessellated && hasPolys ) {
831+ int last1 = 0 ;
832+ if (is3D ()) {
833+ last1 = lastPolyVertex + 1 ;
834+ } else if (is2D ()) {
835+ last1 = lastPolyVertex + 1 ;
836+ if (-1 < firstLineVertex ) last1 = firstLineVertex ;
837+ if (-1 < firstPointVertex ) last1 = firstPointVertex ;
791838 }
839+ for (int i = firstLineVertex ; i < last1 ; i ++) {
840+ float u = tessGeo .polyTexCoords [2 * i + 0 ];
841+ float v = tessGeo .polyTexCoords [2 * i + 1 ];
842+ tessGeo .polyTexCoords [2 * i + 0 ] = PApplet .min (1 , u * uFactor );
843+ tessGeo .polyTexCoords [2 * i + 1 ] = PApplet .min (1 , v * uFactor );
844+ }
845+ root .setModifiedPolyTexCoords (firstPolyVertex , last1 - 1 );
792846 }
793847 }
794848
@@ -1524,8 +1578,13 @@ public void setTextureUV(int index, float u, float v) {
15241578 return ;
15251579 }
15261580
1581+ if (image != null && textureMode == IMAGE ) {
1582+ u = PApplet .min (1 , u / image .width );
1583+ v = PApplet .min (1 , v / image .height );
1584+ }
15271585 inGeo .texcoords [2 * index + 0 ] = u ;
15281586 inGeo .texcoords [2 * index + 1 ] = v ;
1587+
15291588 markForTessellation ();
15301589 }
15311590
@@ -2629,18 +2688,17 @@ protected void tessellateRect() {
26292688 rounded = true ;
26302689 }
26312690
2632- rectMode = CORNER ;
26332691 inGeo .setMaterial (fillColor , strokeColor , strokeWeight ,
26342692 ambientColor , specularColor , emissiveColor , shininess );
26352693 inGeo .setNormal (normalX , normalY , normalZ );
26362694 if (rounded ) {
26372695 inGeo .addRect (a , b , c , d ,
26382696 tl , tr , br , bl ,
2639- fill , stroke , bezierDetail , rectMode );
2697+ fill , stroke , bezierDetail , CORNER );
26402698 tessellator .tessellatePolygon (false , true , true );
26412699 } else {
26422700 inGeo .addRect (a , b , c , d ,
2643- fill , stroke , rectMode );
2701+ fill , stroke , CORNER );
26442702 tessellator .tessellateQuads ();
26452703 }
26462704 }
@@ -2655,36 +2713,35 @@ protected void tessellateEllipse() {
26552713 d = params [3 ];
26562714 }
26572715
2658- // ellipseMode = CORNER;
26592716 inGeo .setMaterial (fillColor , strokeColor , strokeWeight ,
26602717 ambientColor , specularColor , emissiveColor , shininess );
26612718 inGeo .setNormal (normalX , normalY , normalZ );
2662- inGeo .addEllipse (a , b , c , d , fill , stroke , ellipseMode );
2719+ inGeo .addEllipse (a , b , c , d , fill , stroke , CORNER );
26632720 tessellator .tessellateTriangleFan ();
26642721 }
26652722
26662723
26672724 protected void tessellateArc () {
26682725 float a = 0 , b = 0 , c = 0 , d = 0 ;
26692726 float start = 0 , stop = 0 ;
2670- int mode = 0 ;
2727+ // int mode = 0;
26712728 if (params .length == 6 || params .length == 7 ) {
26722729 a = params [0 ];
26732730 b = params [1 ];
26742731 c = params [2 ];
26752732 d = params [3 ];
26762733 start = params [4 ];
26772734 stop = params [5 ];
2678- if (params .length == 7 ) {
2679- mode = (int )(params [6 ]);
2680- }
2735+ // Not using arc mode since PShape only uses CORNER
2736+ // if (params.length == 7) {
2737+ // mode = (int)(params[6]);
2738+ // }
26812739 }
26822740
2683- // ellipseMode = CORNER;
26842741 inGeo .setMaterial (fillColor , strokeColor , strokeWeight ,
26852742 ambientColor , specularColor , emissiveColor , shininess );
26862743 inGeo .setNormal (normalX , normalY , normalZ );
2687- inGeo .addArc (a , b , c , d , start , stop , fill , stroke , mode );
2744+ inGeo .addArc (a , b , c , d , start , stop , fill , stroke , CORNER );
26882745 tessellator .tessellateTriangleFan ();
26892746 }
26902747
@@ -3960,31 +4017,36 @@ public void disableStyle() {
39604017 @ Override
39614018 protected void styles (PGraphics g ) {
39624019 if (g instanceof PGraphicsOpenGL ) {
3963- if (stroke ) {
4020+ if (g .stroke ) {
4021+ setStroke (true );
39644022 setStroke (g .strokeColor );
39654023 setStrokeWeight (g .strokeWeight );
3966-
3967- // These two don't to nothing probably:
39684024 setStrokeCap (g .strokeCap );
39694025 setStrokeJoin (g .strokeJoin );
39704026 } else {
39714027 setStroke (false );
39724028 }
39734029
3974- if (fill ) {
4030+ if (g .fill ) {
4031+ setFill (true );
39754032 setFill (g .fillColor );
39764033 } else {
39774034 setFill (false );
39784035 }
39794036
4037+ if (g .tint ) {
4038+ setTint (true );
4039+ setTint (g .tintColor );
4040+ }
4041+
39804042 setAmbient (g .ambientColor );
39814043 setSpecular (g .specularColor );
39824044 setEmissive (g .emissiveColor );
39834045 setShininess (g .shininess );
39844046
3985- // What about other style parameters, such as rectMode, etc?
3986- // These should force a tessellation update, same as stroke
3987- // cap and weight... right?
4047+ if ( image != null ) {
4048+ setTextureMode ( g . textureMode );
4049+ }
39884050 } else {
39894051 super .styles (g );
39904052 }
0 commit comments