We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Textured grid with OpenGL
Page Index Toggle Pages: 1
Textured grid with OpenGL (Read 1188 times)
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!
Re: Smarter distortion with a grid?
Reply #1 - Jun 9th, 2008, 3:37pm
 
IMHO, it is not a good idea to multiply the topics on your program, it kinds of dilute the information (in the future, the posts you refer to will be hard to find!).
I might be wrong, but displacement map and grid might be related or even the same thing.
Think of a grid, with more or less squares, dividing your image. A displacement map, AFAIK, would distort this grid, and the pixels in each square will be moved according to the distortion of the square.
I thought of this effect as a cool extension of your caricature code, but I lack time to try and implement it now.
I hope you will find somebody else to help you. Since it is interesting, I might try it someday, but I cannot give a deadline! Tongue
Re: Textured grid with OpenGL
Reply #2 - Jun 13th, 2008, 3:55pm
 
I'm sorry I have two post with relating subjects. I thought it was a new subject, but appearently they are more related then I thought. I appreciate your help.

The program is part of my graduation project for which I will make digital paintings for psychological research. So if you could help me with this crucial part of my program before Monday June 23, it would be an honor to mention you in my research paper. And of course I will send you a copy of the program including my drawings for the research and  my paper in English (digitally or hard copy).

This is not only for PhiLho. Anyone who really contributes to finishing my program code gets this 'reward'; it's my pleasure!
Re: Textured grid with OpenGL
Reply #3 - Jun 15th, 2008, 11:06pm
 
Hey!

I think I found a way to get the effect I wanted to achieve with image distortion. I work with quads of textures and vertices. Okay, it's inefficient but al least affective!

beginShape();  
texture(a);
vertex(0, 0, v1, 0, 0);  
vertex(200, 0, v2, 400, 0);      
vertex(200, 200, v3, 400, 400);  
vertex(0, 200, v4, 0, 400);      
endShape();

with v the values of heightmap with different levels of gray.

I'm still working on the height map PhiLho wrote about, but there's some progress finally...
Page Index Toggle Pages: 1