Texture doesn't apply

edited October 2013 in Questions about Code

I'm trying to build a memory card game in Processing 2. So far it's going pretty well, the only thing that eludes me is how to apply textures to shapes.

I load and resize the image in the setup(). The image is being resized by cardBack.resize(tileSizeX, tileSizeY) to fit the size of the shape.

Then on draw() I try to draw the shape like this:

    beginShape(QUADS);   
    fill(0, 200, 0);
    vertex(-(tileSizeX / 2), -(tileSizeY / 2), -1, 0, 0);
    vertex(tileSizeX / 2, -(tileSizeY / 2), -1, tileSizeX, 0);
    vertex(tileSizeX / 2,  tileSizeY / 2, -1, tileSizeX, tileSizeY);
    vertex(-(tileSizeX / 2),  tileSizeY / 2, -1, 0, tileSizeY);
    texture(cardBack);
    vertex(-(tileSizeX / 2), -(tileSizeY / 2), 0);
    vertex(tileSizeX / 2, -(tileSizeY / 2), 0);
    vertex(tileSizeX / 2,  tileSizeY / 2, 0);
    vertex(-(tileSizeX / 2),  tileSizeY / 2, 0);
    endShape(CLOSE);

The result is a card with two sides. The down side is green and the upper side is gray-ish. I'm not really seeing what's going wrong here. This is my first project in processing, so I could really use some assistance.

Tagged:

Answers

  • edited November 2013

    I made a comment here that was wrong -- so I have deleted it -- now I have a question. Shouldn't you have two separate beginShape()s -- one for the front and one for the back? Your question text seems to be about texture(), and I think that may not be the problem. It may be just that you need two shapes.

  • edited January 2014

    From the texture() reference:

    Sets a texture to be applied to vertex points. The texture() function must be called between beginShape() and endShape() and before any calls to vertex(). This function only works with the P2D and P3D renderers.

    Clair is right, you have to use two distinct beginShape/endShape calls to create the card and you have to set the texture before any vertex call. The following code should work (untested):

    beginShape(QUADS);   
    fill(0, 200, 0);
    vertex(-(tileSizeX / 2), -(tileSizeY / 2), -1, 0, 0);
    vertex(tileSizeX / 2, -(tileSizeY / 2), -1, tileSizeX, 0);
    vertex(tileSizeX / 2,  tileSizeY / 2, -1, tileSizeX, tileSizeY);
    vertex(-(tileSizeX / 2),  tileSizeY / 2, -1, 0, tileSizeY);
    endShape(); // You don't need close in case you started a non-open shape type (like QUADS)
    beginShape(QUADS);   
    texture(cardBack);
    vertex(-(tileSizeX / 2), -(tileSizeY / 2), 0);
    vertex(tileSizeX / 2, -(tileSizeY / 2), 0);
    vertex(tileSizeX / 2,  tileSizeY / 2, 0);
    vertex(-(tileSizeX / 2),  tileSizeY / 2, 0);
    endShape();
    
Sign In or Register to comment.