Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

Re : 2.0b7 Skybox - not working

$
0
0
Figured it out... wish this was somehow automatically included in Processing.  Have to load the matrix (not sure if it is in each draw or just once in the setup).  As described by Andres:
You have to do the glLoadMatrixf() call manually because the OpenGL renderer only uses functions available in the GLES2 API, and all the matrix-related functions are gone in GLES2 (and GL 3.2+).
  1. import processing.opengl.PGraphicsOpenGL;
  2. import javax.media.opengl.GL2;
  3. import com.jogamp.opengl.util.texture.Texture;
  4. import com.jogamp.opengl.util.texture.TextureIO;
  5. import com.jogamp.opengl.util.texture.awt.AWTTextureIO;
  6. import javax.media.opengl.GLProfile;
  7. import java.io.File;
  8. import javax.imageio.ImageIO;
  9. float[] projMatrix;
  10. float[] mvMatrix;
  11. // set skybox filename without orientation part here...
  12. String skyboxName = "besiege";   
  13. PGraphicsOpenGL pg;
  14. GL2 gl;
  15. int skybox;
  16. float a = 0.0;
  17. void setup()
  18. {
  19.   size(600, 500, OPENGL);
  20.   pg = (PGraphicsOpenGL) g;
  21.   gl = g.beginPGL().gl.getGL2();
  22.   noStroke();  // comment it to see cube edges
  23.   loadSkybox(skyboxName, ".png");
  24.   pg.beginPGL();
  25.   skybox = gl.glGenLists(1);
  26.   gl.glNewList(skybox, GL2.GL_COMPILE);
  27.  
  28.   gl.glFrontFace(GL2.GL_CCW);
  29.   gl.glEnable(GL2.GL_CULL_FACE);
  30.   TexturedCube();
  31.   gl.glDisable(GL2.GL_CULL_FACE);
  32.   gl.glEndList();
  33.   pg.endPGL();
  34.   float fov = PI/3.0;
  35.   float cameraZ = (height/2.0) / tan(fov/2.0);
  36.   perspective(fov, float(width)/float(height), cameraZ/10.0, cameraZ*200.0);
  37.   projMatrix = new float[16];
  38.   mvMatrix = new float[16];
  39.  
  40.   // gl.glEnable(GL.GL_CULL_FACE);
  41. }
  42. void draw()
  43. {
  44.   background(0);
  45.   loadMatrix();
  46.   pg.beginPGL();
  47.   gl.glCallList( skybox );
  48.   pg.endPGL();
  49.   fill(255);
  50.   pushMatrix();
  51.   translate(width/2,height/2,0);
  52.   sphere(20);
  53.   popMatrix();
  54.   //println("FPS: "+ frameRate);
  55. }
  56. float p = 40000;   // half skybox size
  57. float m = -p;
  58. // create cube edges
  59. PVector P000 = new PVector (m,m,m);
  60. PVector P010 = new PVector (m,p,m);
  61. PVector P110 = new PVector (p,p,m);
  62. PVector P100 = new PVector (p,m,m);
  63. PVector P001 = new PVector (m,m,p);
  64. PVector P011 = new PVector (m,p,p);
  65. PVector P111 = new PVector (p,p,p);
  66. PVector P101 = new PVector (p,m,p);
  67. Texture tex1,tex2,tex3,tex4,tex5,tex6;   // texture images
  68. // load six skybox images as cube texture
  69. void loadSkybox(String skyboxName, String fExt)
  70. {
  71.   try {
  72.     tex1 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_front" + fExt))), true);
  73.     tex2 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_back" + fExt))), true);
  74.     tex3 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_left" + fExt))), true);
  75.     tex4 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_right" + fExt))), true);
  76.     tex5 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_bottom" + fExt))), true);
  77.     tex6 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_top" + fExt))), true);
  78.   }
  79.   catch (IOException e) {    
  80.     println( e);
  81.   }
  82.   //textureMode(NORMALIZED);
  83. }
  84. // Assign six texture to the six cube faces
  85. void TexturedCube()
  86. {
  87.   TexturedCubeSide (P100, P000, P010, P110, tex1);   // -Z "front" face
  88.   TexturedCubeSide (P001, P101, P111, P011, tex2);   // +Z "back" face
  89.   TexturedCubeSide (P000, P001, P011, P010, tex3);   // -X "left" face
  90.   TexturedCubeSide (P101, P100, P110, P111, tex4);   // +X "right" face
  91.   TexturedCubeSide (P110, P010, P011, P111, tex5);   // +Y "base" face
  92.   TexturedCubeSide (P101, P001, P000, P100, tex6);   // -Y "top" face
  93. }
  94. // create a cube side given by 4 edge vertices and a texture
  95. void TexturedCubeSide(PVector P1, PVector P2, PVector P3, PVector P4, Texture tex)
  96. {
  97.   tex.enable(gl);
  98.   tex.bind(gl);
  99.   gl.glBegin(GL2.GL_QUADS);
  100.   gl.glTexCoord2f(1.0f, 0.0f);
  101.   gl.glVertex3f(P1.x, P1.y, P1.z);
  102.   gl.glTexCoord2f(0.0f, 0.0f);
  103.   gl.glVertex3f(P2.x, P2.y, P2.z);
  104.   gl.glTexCoord2f(0.0f, 1.0f);
  105.   gl.glVertex3f(P3.x, P3.y, P3.z);
  106.   gl.glTexCoord2f(1.0f, 1.0f);
  107.   gl.glVertex3f(P4.x, P4.y, P4.z);
  108.   gl.glEnd();
  109.   tex.disable(gl);
  110. }
  111. void loadMatrix() {
  112.   gl.glMatrixMode(GL2.GL_PROJECTION);
  113.   projMatrix[0] = pg.projection.m00;
  114.   projMatrix[1] = pg.projection.m10;
  115.   projMatrix[2] = pg.projection.m20;
  116.   projMatrix[3] = pg.projection.m30;
  117.  
  118.   projMatrix[4] = pg.projection.m01;
  119.   projMatrix[5] = pg.projection.m11;
  120.   projMatrix[6] = pg.projection.m21;
  121.   projMatrix[7] = pg.projection.m31;
  122.  
  123.   projMatrix[8] = pg.projection.m02;
  124.   projMatrix[9] = pg.projection.m12;
  125.   projMatrix[10] = pg.projection.m22;
  126.   projMatrix[11] = pg.projection.m32;
  127.  
  128.   projMatrix[12] = pg.projection.m03;
  129.   projMatrix[13] = pg.projection.m13;
  130.   projMatrix[14] = pg.projection.m23;
  131.   projMatrix[15] = pg.projection.m33;
  132.  
  133.   gl.glLoadMatrixf(projMatrix, 0);
  134.  
  135.   gl.glMatrixMode(GL2.GL_MODELVIEW);
  136.   mvMatrix[0] = pg.modelview.m00;
  137.   mvMatrix[1] = pg.modelview.m10;
  138.   mvMatrix[2] = pg.modelview.m20;
  139.   mvMatrix[3] = pg.modelview.m30;
  140.  
  141.   mvMatrix[4] = pg.modelview.m01;
  142.   mvMatrix[5] = pg.modelview.m11;
  143.   mvMatrix[6] = pg.modelview.m21;
  144.   mvMatrix[7] = pg.modelview.m31;
  145.  
  146.   mvMatrix[8] = pg.modelview.m02;
  147.   mvMatrix[9] = pg.modelview.m12;
  148.   mvMatrix[10] = pg.modelview.m22;
  149.   mvMatrix[11] = pg.modelview.m32;
  150.  
  151.   mvMatrix[12] = pg.modelview.m03;
  152.   mvMatrix[13] = pg.modelview.m13;
  153.   mvMatrix[14] = pg.modelview.m23;
  154.   mvMatrix[15] = pg.modelview.m33;
  155.   gl.glLoadMatrixf(mvMatrix, 0);
  156. }

Viewing all articles
Browse latest Browse all 1768

Trending Articles