Textures on 3D object showing as solid color

edited November 2016 in Questions about Code

I've created a rotating 3D pyramid as a createShape(GROUP) and applied a texture to each side but they display as a solid color instead of the image. The code seems to be correct in comparison to working examples online but I must have something wrong. Any assistance is greatly appreciated.

    PImage img;
    PShape pyramid,side1,side2,side3,side4,base;

    void setup()
    {
      size(940, 540, P3D);
      colorMode(HSB);
      background(0);
      textureMode(NORMAL);
      img = loadImage("galaxy.jpg");


      pyramid = createShape(GROUP);

        side1 = createShape();
        side1.setTexture(img);
         side1.beginShape();
          side1.vertex(-100, -100, -100);
          side1.vertex( 100, -100, -100);
          side1.vertex(   0,    0,  100);
          side1.endShape(CLOSE);
         side2 = createShape();
         side2.setTexture(img);
         side2.beginShape();
          side2.vertex( 100, -100, -100);
          side2.vertex( 100,  100, -100);
          side2.vertex(   0,    0,  100);
         side2.endShape(CLOSE);
         side3 = createShape();
         side3.setTexture(img);
         side3.beginShape();
          side3.vertex( 100, 100, -100);
          side3.vertex(-100, 100, -100);
          side3.vertex(   0,   0,  100);
         side3.endShape(CLOSE);
         side4 = createShape();
         side4.setTexture(img);
         side4.beginShape();
          side4.vertex(-100,  100, -100);
          side4.vertex(-100, -100, -100);
          side4.vertex(   0,    0,  100);
         side4.endShape(CLOSE);
         base = createShape();
         base.setTexture(img);
         base.beginShape();
           base.vertex(-100,-100,-100);
           base.vertex(100,-100,-100);
           base.vertex(100,100,-100);
           base.vertex(-100,100,-100);
         base.endShape(CLOSE);

          pyramid.addChild(side1);
          pyramid.addChild(side2);
          pyramid.addChild(side3);
          pyramid.addChild(side4);
          pyramid.addChild(base);
    }

    void draw()
    { 
      background(0);
      noStroke();

         translate(width/2, height/2, 150);
         rotateX(speedMod*(frameCount*PI/200));
         shape(pyramid);
    }
 }

Answers

  • Brainstorming --

    1. should you call pyramid.beginShape() / pyramid.endShape()?
    2. should side1.setTexture(img) be inside side1.beginShape() / side1.endShape? And the same for all others?
    3. have you tried side1.texture() vs. .setTexture()? I'm not sure, but it might matter.
  • You aren't using any texture coords in your vertex calls, I think you need the 5 argument version.

  • For texture() you need u v coords but for PShapes you're supposed to be able to use setTexture without them. It's the main reason I did them as PShapes. I'll see if I can get it working with texture()

  • @CodeSloth

    Try this

      side1 = createShape();        
      side1.beginShape();
         side1.Texture(img);
         side1.vertex(-100, -100, -100);
         side1.vertex( 100, -100, -100);
         side1.vertex(   0,    0,  100);
       side1.endShape(CLOSE);   // ** NOT sure if you need CLOSE here... I don't use it in my code.
    

    Kf

  • For texture() you need u v coords but for PShapes you're supposed to be able to use setTexture without them

    Citation needed...

Sign In or Register to comment.