louise
YaBB Newbies
Offline
Posts: 1
Re: Camera positioning, orbit
Reply #2 - Nov 22nd , 2009, 1:45pm
some help for OPENGL....Code : //----------------------------------------------------- import processing.opengl.*; import javax.media.opengl.*; float cameraX = 0,cameraY=0,cameraZ=0,targetX=0,targetY=0,targetZ=0,N_cameraX=0,N_cameraY=1,N_ca meraZ=0; float cameraAlpha = 150/*30*/, cameraBeta= 150/*30*/; float cameraRadius = 200; Debug debug; void setup(){ size(800,800,OPENGL); background(0); PGraphicsOpenGL pgl = (PGraphicsOpenGL) g; debug = new Debug(pgl); } void draw(){ background(0); PGraphicsOpenGL pgl = (PGraphicsOpenGL) g; GL gl = pgl.gl; debug.show(pgl); camera(); cameraMove(); camera( cameraX, cameraY, cameraZ, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0); lights(); sphere(40); } void cameraMove(){ cameraX = cameraRadius * cos(radians(cameraAlpha))*cos(radians(cameraBeta)); cameraY = cameraRadius * sin(radians(cameraAlpha)); cameraZ = cameraRadius *cos(radians(cameraAlpha)) *sin(radians(cameraBeta)); if (cameraY > 0 && cameraY< cameraRadius) { N_cameraX= 0;//-cameraX; N_cameraY = -cameraY ;//-cameraY + cameraRadius/cameraY; N_cameraZ = 0;//-cameraZ; } println("cameraX = " + cameraX); println("cameraY = " + cameraY); println("cameraZ = " + cameraZ); println("NcameraX = " + N_cameraX); println("NcameraY = " + N_cameraY); println("NcameraZ = " + N_cameraZ); } void keyPressed(){ if (keyCode == DOWN) cameraAlpha -= 1; if (keyCode == UP) cameraAlpha += 1; if (keyCode == LEFT) cameraBeta -= 1; if (keyCode== RIGHT) cameraBeta += 1; if (key == 'z') cameraRadius -=1; if (key == 'Z') cameraRadius +=1; if(key == 'r' || key == 'R'){ cameraX = 0; cameraY=0; cameraZ=0; targetX=0; targetY=0; targetZ=0; N_cameraX=0; N_cameraY=1; N_cameraZ=0; cameraAlpha = 30; cameraBeta=30; cameraRadius = 20; } } void mouseDragged(){ if(mouseButton == RIGHT){ cameraX -= (pmouseX-mouseX)/2; cameraY -= (pmouseY-mouseY)/2; targetX -= (pmouseX-mouseX)/2; targetY -= (pmouseY-mouseY)/2; } if(mouseButton == LEFT){ cameraAlpha -= pmouseY - mouseY; cameraBeta += pmouseX - mouseX; } } public class Debug { public void show(PGraphicsOpenGL pgl) { draw_axes(pgl); } public Debug(PGraphicsOpenGL pgl) { } public void draw_axes (PGraphicsOpenGL pgl){ GL gl; gl = pgl.beginGL(); gl.glPushMatrix(); gl.glColor3f(0.,255.,0.); gl.glBegin(GL.GL_LINES); gl.glVertex3f(0, 0, 0); gl.glVertex3f(250, 0, 0); gl.glEnd(); gl.glColor3f(255.,0.,0.); gl.glBegin(GL.GL_LINES); gl.glVertex3f(0, 250, 0); gl.glVertex3f(0, 0, 0); gl.glEnd(); gl.glColor3f(0.,0.,255.); gl.glBegin(GL.GL_LINES); gl.glVertex3f(0, 0, 250); gl.glVertex3f(0, 0, 0); gl.glEnd(); gl.glPopMatrix(); pgl.endGL(); } }