Index \ Search \ Members \ Profile \ Logout \ RSS
Welcome, Guest. Please Login or Register
Processing 1.0Programming Questions & HelpOpenGL and 3D Libraries › Modifying Textured Globe code
Page Index Toggle Pages: 1
Send Topic Print
Modifying Textured Globe code (Read 613 times)
Modifying Textured Globe code
Nov 30th, 2008, 10:37pm
 
I'm trying to modify the Textured Globe example from the processing website so that the it produces a globe that spins on one axis and doesn't take in a texture to create the sphere. I just want to be able to set a color or draw a pattern to be iterated throughout the surface of the globe. I wanted to use createGraphics(), but I can't use that with OpenGL. Do I have to import an image when using this method to draw a sphere? I'm kind of new to processing. Does textures mean imported images? Should I try to just use the sphere() method? I've considered this, but I couldn't figure out how to make it rotate. Thanks for any help at all.

Code:

import processing.opengl.*;

PImage bg;
PImage texmap;

int sphereSetting = 35;
float Xrot = 0;
float Yrot = 0;
float Xvel = .5;
float Yvel = .5;
float pushBack = 0;

float[] cx, cz, sphereX, sphereY, sphereZ;
float sinLUT[];
float cosLUT[];
float SINCOS_PRECISION = 0.5;
int SINCOS_LENGTH = int(360.0 / SINCOS_PRECISION);


void setup() {
 size(1024, 768, OPENGL);  
 texmap = loadImage("world32k.jpg");    
 initializeSphere(sphereSetting);
}

void draw() {    
 background(0);            
 globe();
}

void globe() {
 pushMatrix();
 translate(width/2.0, height/2.0, pushBack);
 pushMatrix();
 noFill();
 stroke(255,200);
 strokeWeight(2);
 smooth();
 popMatrix();
 lights();    
 pushMatrix();
 rotateX( radians(-Xrot) );  
 rotateY( radians(270 - Yrot) );
 fill(200);
 noStroke();
 textureMode(IMAGE);  
 texturedSphere(450, texmap);
 popMatrix();  
 popMatrix();
 Xrot += Xvel;
 Yrot += Yvel;
 
}

void initializeSphere(int res)
{
 sinLUT = new float[SINCOS_LENGTH];
 cosLUT = new float[SINCOS_LENGTH];

 for (int i = 0; i < SINCOS_LENGTH; i++) {
   sinLUT[i] = (float) Math.sin(i * DEG_TO_RAD * SINCOS_PRECISION);
   cosLUT[i] = (float) Math.cos(i * DEG_TO_RAD * SINCOS_PRECISION);
 }

 float delta = (float)SINCOS_LENGTH/res;
 float[] cx = new float[res];
 float[] cz = new float[res];
 
 // Calc unit circle in XZ plane
 for (int i = 0; i < res; i++) {
   cx[i] = -cosLUT[(int) (i*delta) % SINCOS_LENGTH];
   cz[i] = sinLUT[(int) (i*delta) % SINCOS_LENGTH];
 }
 
 // Computing vertexlist vertexlist starts at south pole
 int vertCount = res * (res-1) + 2;
 int currVert = 0;
 
 // Re-init arrays to store vertices
 sphereX = new float[vertCount];
 sphereY = new float[vertCount];
 sphereZ = new float[vertCount];
 float angle_step = (SINCOS_LENGTH*0.5f)/res;
 float angle = angle_step;
 
 // Step along Y axis
 for (int i = 1; i < res; i++) {
   float curradius = sinLUT[(int) angle % SINCOS_LENGTH];
   float currY = -cosLUT[(int) angle % SINCOS_LENGTH];
   for (int j = 0; j < res; j++) {
     sphereX[currVert] = cx[j] * curradius;
     sphereY[currVert] = currY;
     sphereZ[currVert++] = cz[j] * curradius;
   }
   angle += angle_step;
 }
 sphereSetting = res;
}

// Generic routine to draw textured sphere
void texturedSphere(float r, PImage t)
{
 int v1,v11,v2;
 r = (r + 240 ) * 0.33;
 beginShape(TRIANGLE_STRIP);
 texture(t);
 float iu=(float)(t.width-1)/(sphereSetting);
 float iv=(float)(t.height-1)/(sphereSetting);
 float u=0,v=iv;
 for (int i = 0; i < sphereSetting; i++) {
   vertex(0, -r, 0,u,0);
   vertex(sphereX[i]*r, sphereY[i]*r, sphereZ[i]*r, u, v);
   u+=iu;
 }
 vertex(0, -r, 0,u,0);
 vertex(sphereX[0]*r, sphereY[0]*r, sphereZ[0]*r, u, v);
 endShape();  
 
 // Middle rings
 int voff = 0;
 for(int i = 2; i < sphereSetting; i++) {
   v1=v11=voff;
   voff += sphereSetting;
   v2=voff;
   u=0;
   beginShape(TRIANGLE_STRIP);
   texture(t);
   for (int j = 0; j < sphereSetting; j++) {
     vertex(sphereX[v1]*r, sphereY[v1]*r, sphereZ[v1++]*r, u, v);
     vertex(sphereX[v2]*r, sphereY[v2]*r, sphereZ[v2++]*r, u, v+iv);
     u+=iu;
   }
 
   // Close each ring
   v1=v11;
   v2=voff;
   vertex(sphereX[v1]*r, sphereY[v1]*r, sphereZ[v1]*r, u, v);
   vertex(sphereX[v2]*r, sphereY[v2]*r, sphereZ[v2]*r, u, v+iv);
   endShape();
   v+=iv;
 }
 u=0;
 
 // Add the northern cap
 beginShape(TRIANGLE_STRIP);
 texture(t);
 for (int i = 0; i < sphereSetting; i++) {
   v2 = voff + i;
   vertex(sphereX[v2]*r, sphereY[v2]*r, sphereZ[v2]*r, u, v);
   vertex(0, r, 0,u,v+iv);    
   u+=iu;
 }
 vertex(sphereX[voff]*r, sphereY[voff]*r, sphereZ[voff]*r, u, v);
 endShape();
 
}
Page Index Toggle Pages: 1
Send Topic Print
« Home ‹ Board Top of this page