texture(PImage) doesn't show up on a 3D quad
in
Programming Questions
•
2 years ago
Hey all, I'm trying to make a 3D terrain generator. My approach was to use perlin noise to make the heights and just have a simple X/Z grid with a Y associated with each X/Z combination. Draw some quads that connect all the points, and voila! Then I tried to put textures onto the quads, just a simple 10x10 grid of greenish colors for each quad to make them sort of flow together, but I cannot for the life of me make the textures show up. Code:
You can clearly see each quad, which for some reason are still the green colors from the PImages, but each quad should be covered in smaller squares and should be nigh indistinguishable from the others, except for the topography. Maybe the texture() method is grabbing the first pixel or something from the images and using that for the whole quad? roar.
I hope my question is clear enough.
- import processing.opengl.*;
import damkjer.ocd.*;
Camera cam;
float x, y;
boolean up=false, down=false, left=false, right=false; // for camera controls
float amt = .01;
float[][] terrain;
PImage tile0, tile1, tile2, tile3, tile4;
PImage[][] tiles;
void setup() {
size(400, 400, OPENGL);
smooth();
// tile0 = createImage(16, 16, RGB); // generate new tiles
// tile1 = createImage(16, 16, RGB);
// tile2 = createImage(16, 16, RGB);
// tile3 = createImage(16, 16, RGB);
// tile4 = createImage(16, 16, RGB);
tile0 = loadImage("tile0.png"); //use premade tiles
tile1 = loadImage("tile1.png");
tile2 = loadImage("tile2.png");
tile3 = loadImage("tile3.png");
tile4 = loadImage("tile4.png");
//makeTileSet(); // generate new tiles
float noiser = 0.0;
cam = new Camera(this, 0, -1*width/2, width/5, width/2, 0, height/2);
terrain = new float[width / 10][height / 10];
tiles = new PImage[terrain.length][terrain[0].length];
for (x=0;x<terrain.length;x++)
{
for (y=0;y<terrain[int(x)].length;y++)
{
noiser += random(0, 1);
terrain[int(x)][int(y)] = 5 * noise(noiser, noiser);
tiles[int(x)][int(y)] = getRandomTile();
}
}
noStroke();
lights();
}
void draw()
{
background(100, 200, 200);
if (up) cam.arc(-amt);
if (down) cam.arc(amt);
if (left) cam.circle(-amt);
if (right) cam.circle(amt);
fill(255);
for (int i = 0; i < terrain.length; i++)
{
for (int j = 0; j < terrain[i].length; j++)
{
//fill(255 / 5 * terrain[i][j]);
if (i<terrain.length-1 && j < terrain[i].length-1)
{
textureMode(NORMALIZED);
beginShape(QUADS);
texture(tiles[i][j]); // the problem!
vertex(i*10, terrain[i][j], j*10);
vertex((i+1)*10, terrain[i+1][j], j*10);
vertex((i+1)*10, terrain[i+1][j+1], (j+1)*10);
vertex(i*10, terrain[i][j+1], (j+1)*10);
endShape();
}
}
}
cam.feed();
}
void keyPressed()
{
if (key == CODED) {
if (keyCode == LEFT)
{
left = true;
}
if (keyCode == RIGHT)
{
right = true;
}
if (keyCode == UP)
{
up = true;
}
if (keyCode == DOWN)
{
down = true;
}
}
}
void keyReleased()
{
if (key == CODED) {
if (keyCode == LEFT)
{
left = false;
}
if (keyCode == RIGHT)
{
right = false;
}
if (keyCode == UP)
{
up = false;
}
if (keyCode == DOWN)
{
down = false;
}
}
}
PImage getRandomTile()
{
switch(int(random(0, 5))) {
case 0:
return tile0;
case 1:
return tile1;
case 2:
return tile2;
case 3:
return tile3;
default :
return tile4;
}
}
void makeTileSet()
{
tile0.loadPixels();
tile1.loadPixels();
tile2.loadPixels();
tile3.loadPixels();
tile4.loadPixels();
for (int i = 0; i < tile0.pixels.length; i++)
{
tile0.pixels[i] = color(0, int(random(50, 200)), 0);
tile1.pixels[i] = color(0, int(random(50, 200)), 0);
tile2.pixels[i] = color(0, int(random(50, 200)), 0);
tile3.pixels[i] = color(0, int(random(50, 200)), 0);
tile4.pixels[i] = color(0, int(random(50, 200)), 0);
}
tile0.updatePixels();
tile1.updatePixels();
tile2.updatePixels();
tile3.updatePixels();
tile4.updatePixels();
tile0.save("tile0.png");
tile1.save("tile1.png");
tile2.save("tile2.png");
tile3.save("tile3.png");
tile4.save("tile4.png");
}
You can clearly see each quad, which for some reason are still the green colors from the PImages, but each quad should be covered in smaller squares and should be nigh indistinguishable from the others, except for the topography. Maybe the texture() method is grabbing the first pixel or something from the images and using that for the whole quad? roar.
I hope my question is clear enough.
1