Opaque polygon not rendering correctly
in
Programming Questions
•
10 months ago
WILL NEED PEASYCAM TO VIEW
The reason I need individual surfaces is so that I can build a cylinder with any amount of sides. (Even if there is a better way to do this, I'd be interested in knowing the reason why I get the functionality reported here).
When I create a 'segment' shape, I draw each side with a alpha value of 50, and the colour specified in the constructor...
Just to stress this:
The undesired functionality here is that the sides of the segment show what's behind them at certain times and not at others.
Here is the code:
V1.pde
- //import controlP5.*;
- import peasy.*;
- //ControlP5 cp5;
- //CheckBox checkbox;
- Segment seg1;
- PeasyCam cam;
- void setup() {
- size(400, 400, P3D);
- stroke(0);
- color c = color(255.0, 0, 0);
- int numOfSides = 4;
- float widthOfSides = 50.0;
- float heightOfSides = 100;
- float x_bottom_left = 200.0f;
- float y_bottom_left = 200.0f;
- float z_bottom = 0.0f;
- seg1 = new Segment(c, x_bottom_left, y_bottom_left, z_bottom, 1.0f, numOfSides, widthOfSides, heightOfSides);
- cam = new PeasyCam(this, x_bottom_left + (widthOfSides/2), y_bottom_left + (heightOfSides/2), z_bottom, 200);
- cam.setMinimumDistance(50);
- cam.setMaximumDistance(500);
- cam.setYawRotationMode();
- // cp5 = new ControlP5(this);
- // checkbox = cp5.addCheckBox("checkBox")
- // .setPosition(300, 000)
- // .setColorForeground(color(255, 0, 0))
- // .setColorActive(color(0, 255, 0))
- // .setColorLabel(color(255))
- // .setSize(10, 10)
- // .setItemsPerRow(2)
- // .setSpacingColumn(30)
- // .setSpacingRow(20)
- // .addItem("burgalry", 0)
- // .addItem("50", 50)
- // .addItem("100", 100)
- // .addItem("150", 150)
- // .addItem("200", 200)
- // .addItem("255", 255);
- }
- void draw() {
- background(0, 0, 0);
- float dirY = (mouseY / float(height) - 0.5) * 2;
- float dirX = (mouseX / float(width) - 0.5) * 2;
- //directionalLight(204, 204, 204, -dirX, -dirY, -1);
- //directionalLight(255, 255, 255, -1, -1, 0);
- //lights();
- //ambientLight(255, 255, 255);
- //translate(0, 100, 0);
- seg1.display();
- }
- //void controlEvent(ControlEvent theEvent) {
- // if (theEvent.isFrom(checkbox)) {
- // }
- //}
Segment.pde:
- class Segment {
- color c_Color = color(255);
- float f_bottomLeftX = 0;
- float f_bottomLeftY = 0;
- float f_bottomLeftZ = 0;
- float f_widthOfSides;
- float f_heightOfSides;
- float f_scale = 1;
- int i_numOfSides = 3;
- float f_radiansBetSides = 0;
- boolean printed = false;
- Segment() {
- }
- Segment(color c, float x, float y, float z, float scale, int sides, float widthOfSides, float heightOfSides) {
- c_Color = c;
- f_bottomLeftX = x;
- f_bottomLeftY = y;
- f_bottomLeftZ = z;
- f_scale = scale;
- i_numOfSides = sides;
- f_widthOfSides = widthOfSides;
- f_heightOfSides = heightOfSides;
- f_radiansBetSides = radians(360f/((float)i_numOfSides));
- println("angleBetSides: " + degrees(f_radiansBetSides));
- }
- float getX() {
- return f_bottomLeftX;
- }
- void setX(float x) {
- f_bottomLeftX = x;
- }
- float getY() {
- return f_bottomLeftY;
- }
- void setY(float y) {
- f_bottomLeftY = y;
- }
- float getZ() {
- return f_bottomLeftZ;
- }
- void setZ(float z) {
- f_bottomLeftZ = z;
- }
- float getScale() {
- return f_scale;
- }
- void setScale(float scale) {
- f_scale = scale;
- }
- void display() {
- fill(c_Color, 50);
- pushMatrix();
- //spinSegment();
- for (int i=0; i < i_numOfSides; i++) {
- translate(getXDispplacement(), getYDispplacement(), getZDisplacement());
- rotateY(f_radiansBetSides);
- translate(-getXDispplacement(), -getYDispplacement(), -getZDisplacement());
- side(f_bottomLeftX, f_bottomLeftX, scaleSize(f_bottomLeftX, true), scaleSize(f_bottomLeftX, true), f_bottomLeftY, scaleSize(f_bottomLeftY, false), scaleSize(f_bottomLeftY, false), f_bottomLeftY, f_bottomLeftZ, f_bottomLeftZ, f_bottomLeftZ, f_bottomLeftZ);
- }
- popMatrix();
- }
- float getXDispplacement(){
- return (f_bottomLeftX + (f_widthOfSides/2));
- }
- float getYDispplacement(){
- return (f_bottomLeftY + (f_heightOfSides/2));
- }
- float getZDisplacement() {
- float halfBottom = f_widthOfSides/2;
- //float side = halfBottom / sin(f_radiansBetSides/2);
- //float zDisp = sqrt((side*side) + (halfBottom * halfBottom));
- float zDisp = halfBottom / tan(f_radiansBetSides/2);
- // if (!printed) {
- //println("side: " + side);
- // println("zDisp: " + zDisp);
- // printed = true;
- // }
- return zDisp;
- }
- float scaleSize(float defaultPos, boolean isX) {
- float retPos = 0f;
- if (isX) {
- retPos = (defaultPos + f_widthOfSides) * f_scale;
- }
- else {
- retPos = (defaultPos + f_heightOfSides) * f_scale;
- }
- return retPos;
- }
- void side(float x1, float x2, float x3, float x4, float y1, float y2, float y3, float y4, float z1, float z2, float z3, float z4) {
- beginShape();
- vertex(x1, y1, z1);
- vertex(x2, y2, z2);
- vertex(x3, y3, z3);
- vertex(x4, y4, z4);
- endShape();
- }
- void spinSegment() {
- //pushMatrix();
- translate(getXDispplacement(), getYDispplacement(), getZDisplacement());
- //translate(200, 200);
- rotateX(-PI/6);
- rotateY(radians(frameCount));
- translate(-getXDispplacement(), -getYDispplacement(), -getZDisplacement());
- //translate(-200, -200);
- //popMatrix();
- }
- }
I Hope that somebody can help!
planty182
2