ksam
YaBB Newbies
Offline
Posts: 19
Textured grid with OpenGL
Jun 9th , 2008, 12:31pm
A new turn to an older post. I wish to achieve the same thing as here: http://processing.org/learning/3d/texturedsphere.html Only instead of an sphere, I wish to texture a plane (a grid of 800 times 800). It shouldn't be too difficult, but I've spend some hours staring at the code... I intend to use an image as a heightmap to move vertices along the z-axis later. But first the grid and the texturing should be right. Please take a look at the code and tell me what to do... import processing.opengl.*; PImage texmap; float rotationX = 0; float rotationY = 0; float velocityX = 0; float velocityY = 0; float sizeSquare = 800; float sizeQuad = 16; float pushBack = 0; float verX; float verY; float[] positionX,positionY,positionZ; float vertexX[]; float vertexY[]; int QUAD_LENGTH = int((sizeSquare/sizeQuad)+1); void setup() { size(1280, 800, OPENGL); texmap = loadImage("VincentsRoom.jpg"); initializeGrid(QUAD_LENGTH); } void draw() { background(100); renderGrid(); } void renderGrid() { pushMatrix(); translate((width/2.0 - 100), height/2.0, pushBack); pushMatrix(); noFill(); stroke(255,200); strokeWeight(2); smooth(); popMatrix(); lights(); pushMatrix(); rotateX( radians(-rotationX) ); rotateY( radians(270 - rotationY) ); fill(200); noStroke(); textureMode(IMAGE); texturedGrid(texmap); popMatrix(); popMatrix(); rotationX += velocityX; rotationY += velocityY; velocityX *= 0.95; velocityY *= 0.95; // Implements mouse control (interaction will be inverse when sphere is upside down) if(mousePressed){ velocityX += (mouseY-pmouseY) * 0.01; velocityY -= (mouseX-pmouseX) * 0.01; } } void initializeGrid(int QUAD_LENGTH) { // Computing vertexlist; vertexlist starts at south pole int vertCount = QUAD_LENGTH * (QUAD_LENGTH +1); //int currVert = 0; // Re-init arrays to store vertices positionX = new float[vertCount]; positionY = new float[vertCount]; positionZ = new float[vertCount]; for (int i = 1; i < QUAD_LENGTH; i++) { for (int j = 0; j < QUAD_LENGTH; j++) { positionX[currVert] = i * sizeQuad; verX = i * sizeQuad; positionY[currVert] = j * sizeQuad; verY = j * sizeQuad; ellipse(verX, verY, 5, 5); vertex(positionX[currVert], positionY[currVert], 0); positionZ[currVert++] = 0; } } } void texturedGrid(PImage t) { int v1,v11,v2; float iu=(float)(t.width-1)/(QUAD_LENGTH); float iv=(float)(t.height-1)/(QUAD_LENGTH); float u=0,v=iv; int voff = 0; for(int i = 2; i < QUAD_LENGTH; i++) { v1=v11=voff; voff += QUAD_LENGTH; v2=voff; u=0; beginShape(TRIANGLE_STRIP); texture(t); for (int j = 0; j < QUAD_LENGTH; j++) { vertex(positionX[v1], positionY[v1], positionZ[v1], u, v); vertex(positionX[v2], positionY[v2], positionZ[v2], u, v+iv); u+=iu; } // Close v1=v11; v2=voff; vertex(positionX[v1], positionY[v1], positionZ[v1], u, v); vertex(positionX[v2], positionY[v2], positionZ[v2], u, v+iv); endShape(); v+=iv; } u=0; } Where do I go wrong??? Please help!