Skip to content

Unwanted shadow in 3D flat shape #5727

@dviertel

Description

@dviertel

Description

In a simple flat 3D shape with some vertices (constant x coordinate) there is an area with a dark triangle shaped shadow whereas the whole shape should have a uniform colouring.

Expected Behavior

The display without this shadow would be nice.

Current Behavior

Steps to Reproduce

Sorry for the lengthy code, it is not minimal. All rotation related statements help you to experiment with the display of the two shapes and they have no direct relationship to the problem.
The relevant functions to produce the two shapes is in the functions drawOberteil1() (this red shape is ok) and drawUnterteil1() (this green shape shows up with the error). If you modify drawUnterteil1() in the marked line the error does not occur.
So here is the code (sorry, doesn't display as expected [improved with hints by MLanghof, thanks]):

/*
  Tasten
  ' '  Teile zusammen/auseinander schieben
  
  Maus
  ziehen links
       rotieren um X/Y-Achse
  ziehen rechts
       rotieren um Y-Achse
*/
//========================================================
float  // Rotation beim Start
       RotateX0 = -0.58643055+TWO_PI,
       RotateY0 = -0.40840715+TWO_PI,
       RotateZ0 = -0.25656506+TWO_PI,
       // aktuelle Rotation
       RotateX = RotateX0,
       RotateY = RotateY0,
       RotateZ = RotateZ0,
       // Schrittweite beim Zurückdrehen zum Start
       dRotateX = 0,
       dRotateY = 0,
       dRotateZ = 0;
       // Zähler für Schritte beim Zurückdrehen
int    RotateTo0Count = 0;

boolean  zeigeAchsen = true;

float  L = 100,
       H = 0.2*L,
       LH = L/3+H/2;
//========================================================
void setup()
{
  size(300,300,P3D);
  smooth(2);
}
//========================================================
void draw()
{
  lights();
  background(220);
  translate(width/2, height/2,0);

  if (RotateTo0Count > 0)  // Würfel in Ausgangslage drehen ?
  {
    RotateX += dRotateX;
    RotateY += dRotateY;
    RotateZ += dRotateZ;
    RotateTo0Count--;
  }
  
  // aktuelle Drehung
  rotateY(RotateY);
  rotateX(RotateX);
  rotateY(RotateZ);
  rotateX(PI);

  // Koordinatenachsen
  if (zeigeAchsen)
  {
    stroke(255,0,0); line(0,0,0 , 300,0,0);
    stroke(0,255,0); line(0,0,0 , 0,300,0);
    stroke(0,0,255); line(0,0,0 , 0,0,300);
  }
  
  // hier zeichnen
  drawUnterteil1();
  drawOberteil1();
}
//========================================================
void drawOberteil1()
{
  stroke(0);
  fill(255,0,0);

  beginShape();
  vertex(0, 0, L);
  vertex(0, L, L);
  vertex(0, L,-L);
  vertex(0, 0,-L);
  vertex(0, 0,-LH+H);
  vertex(0, H,-LH);
  vertex(0, H,LH);
  vertex(0, 0,LH-H);
  endShape(CLOSE);
} // drawOberteil1
//========================================================
void drawUnterteil1()
{
  stroke(0);
  fill(0,255,0);
  
  beginShape();
  vertex(0,-L, L);
  vertex(0, 0, L);
  vertex(0, 0, LH-H);
  vertex(0, H, LH);
  vertex(0, H,-LH);  // <<<<<<<<<< try this: vertex(0,-H,-LH);
  vertex(0, 0,-LH+H);
  vertex(0, 0,-L);
  vertex(0,-L,-L);
  endShape(CLOSE);
} // drawUnterteil1
//========================================================
void mouseDragged()
{
  if (mouseButton == LEFT)
  {
    RotateX += map(pmouseY-mouseY , 0,height , 0,PI);
    RotateY -= map(pmouseX-mouseX , 0,width  , 0,PI);
  }
  else if (mouseButton == RIGHT)
  {
    RotateZ += map(pmouseY-mouseY , 0,height , 0,PI);
  }
}
//========================================================
void keyPressed()
{
  switch (key)
  {
    case '0' :
      RotateBereinigen();
      RotateTo0Count = int(frameRate);
      dRotateX = OptimizeRotate(RotateX0-RotateX) / RotateTo0Count;
      dRotateY = OptimizeRotate(RotateY0-RotateY) / RotateTo0Count;
      dRotateZ = OptimizeRotate(RotateZ0-RotateZ) / RotateTo0Count;
      break;
  }
}
//========================================================
void RotateBereinigen()
{
  // alle 3 Winkel in den Bereich 0...2π bringen
  while (RotateX < 0     ) RotateX += TWO_PI;
  while (RotateX > TWO_PI) RotateX -= TWO_PI;
  while (RotateY < 0     ) RotateY += TWO_PI;
  while (RotateY > TWO_PI) RotateY -= TWO_PI;
  while (RotateZ < 0     ) RotateZ += TWO_PI;
  while (RotateZ > TWO_PI) RotateZ -= TWO_PI;
}
//========================================================
float OptimizeRotate(float a)
{
  if (a > PI) a -= TWO_PI;
  if (a < -PI) a += TWO_PI;
  return a;
}
//========================================================

Your Environment

  • Processing version:
    3.4
  • Operating System and OS version:
    Win 7 Pro, same result on Win 10 Home
  • Other information:

Possible Causes / Solutions

I have no idea.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions