pierre59
YaBB Newbies
Offline
Posts: 41
Re: Sphere mapping
Reply #2 - Feb 20th , 2009, 6:29pm
Hello, thank's. Here is the complet code of my program. If I write 'opengl=false', it does not works correctly ! No texture is visible ! But if I write 'opengl=true', it works very well ! I don't understand where is the error. Thank's for your help. import processing.core.*; import toxi.geom.Vec3D; import processing.opengl.*; import javax.media.opengl.*; import javax.media.opengl.glu.*; import java.applet.*; import java.awt.*; import javax.swing.*; public class Sphere extends JFrame { PGraphicsOpenGL pgl; GL gl; boolean opengl=false; int x1, y1, z1; float x9,y9,z9; sp fenetre; float rayon_sphere; float rotX, rotY, rotZ; PImage texImg; int NUM_ROWS = 100, NUM_LINES = 100; Vec3D p[][]; public static void main(String args[]) { new Sphere().setVisible(true); } public Sphere() { setSize(1400,900); add(fenetre = new sp()); fenetre.init(); } class sp extends PApplet { float u, theta, v, phi, x, y, z; public void Sphere(){ p = new Vec3D[NUM_ROWS][NUM_LINES]; for(int i=0; i<NUM_ROWS; i++){ u = (float)i/(NUM_ROWS-1); theta = u*TWO_PI; for(int j=0; j<NUM_LINES; j++){ v = (float)j/(NUM_LINES-1); phi = PI*v; x = cos(theta) * sin(phi); y = sin(theta) * sin(phi); z = cos(phi); p[i][j] = new Vec3D(x, y, z); //normalized vector of one point in the sphere } } rotX = 90; rotY = 0; rotZ = 0; texImg = loadImage("textures_planetes/marsmap2.jpg"); } public void move(){ rotZ -= 0.1; } public void render(){ gl.glViewport(0,0,1440,900); gl.glPushMatrix(); gl.glTranslatef(x1, y1, z1); gl.glScalef(rayon_sphere, rayon_sphere, rayon_sphere); gl.glRotatef(rotX, 1.0f, 0.0f, 0.0f); gl.glRotatef(rotY, 0.0f, 1.0f, 0.0f); gl.glRotatef(rotZ, 0.0f, 0.0f, 1.0f); renderSphere(); gl.glPopMatrix(); } public void render_bis(){ float u, uNext, v; rayon_sphere=300; translate(720,450,0); rotateX(radians(90.0f)); rotateY((float) (radians(270.0f))); textureMode(IMAGE); for(int i=0; i<NUM_ROWS; i++){ beginShape(TRIANGLE_STRIP); texture(texImg); int next = (i+1)%NUM_ROWS; u = (float)i/(NUM_ROWS-1); uNext = (float)next/(NUM_ROWS-1); for(int j=0; j<NUM_LINES; j++){ v = (float)j/(NUM_LINES-1); float vNext = (float)(j+1)/(NUM_LINES-1); // '-' en x pour afficher correctement la sphère. // Car sinon les formations et labels sont inversés de gauche à droite. vertex(-p[i][j].x*rayon_sphere, p[i][j].y*rayon_sphere, p[i][j].z*rayon_sphere,u,v); vertex(-p[next][j].x*rayon_sphere, p[next][j].y*rayon_sphere, p[next][j].z*rayon_sphere,u,vNext); vertex(-p[next][j].x*rayon_sphere, p[next][j].y*rayon_sphere, p[next][j].z*rayon_sphere,uNext,v); } endShape(); } } public void renderSphere(){ pgl.bindTexture(texImg); float u, uNext; float v; for(int i=0; i<NUM_ROWS; i++){ gl.glBegin(gl.GL_TRIANGLE_STRIP); int next = (i+1)%NUM_ROWS; u = (float)i/(NUM_ROWS-1); uNext = (float)next/(NUM_ROWS-1); for(int j=0; j<NUM_LINES; j++){ v = (float)j/(NUM_LINES-1); gl.glTexCoord2f(u, v); // '-' en x pour afficher correctement la sphère. // Car sinon les formations et labels sont inversés de gauche à droite. gl.glVertex3f(-p[i][j].x, p[i][j].y, p[i][j].z); gl.glTexCoord2f(uNext, v); gl.glVertex3f(-p[next][j].x, p[next][j].y, p[next][j].z); } gl.glEnd(); } } public void setup(){ size(1400, 900, opengl ? OPENGL: P3D); background(0); fill(255); rayon_sphere = 50; x1=720; // A partir du bas de l'écran. y1=450; z1=500; Sphere(); } public void draw(){ background(0); move(); if (!opengl) { render_bis(); } else { pgl = (PGraphicsOpenGL) g; gl = pgl.beginGL(); gl.setSwapInterval(1); float debut=millis(); gl.glEnable(gl.GL_TEXTURE_2D); render(); gl.glDisable(gl.GL_TEXTURE_2D); gl.glEnd(); pgl.endGL(); } } } }