pgraphicsopengl.java

来自「This is processing for java examples.」· Java 代码 · 共 2,167 行 · 第 1/5 页

JAVA
2,167
字号
            for (int x = 0; x < source.width; x++) {              int pixel = source.pixels[p++];              tpixels[t++] = (pixel << 8) | 0xff;            }            t += twidth - source.width;          }          break;        case ARGB:          for (int y = 0; y < source.height; y++) {            for (int x = 0; x < source.width; x++) {              int pixel = source.pixels[p++];              tpixels[t++] = (pixel << 8) | ((pixel >> 24) & 0xff);            }            t += twidth - source.width;          }          break;        }      } else {  // LITTLE_ENDIAN        // ARGB native, and RGBA opengl means ABGR on windows        // for the most part just need to swap two components here        // the sun.cpu.endian here might be "false", oddly enough..        // (that's why just using an "else", rather than check for "little")        switch (source.format) {        case ALPHA:          for (int y = 0; y < source.height; y++) {            for (int x = 0; x < source.width; x++) {              tpixels[t++] = (source.pixels[p++] << 24) | 0x00FFFFFF;            }            t += twidth - source.width;          }          break;        case RGB:          for (int y = 0; y < source.height; y++) {            for (int x = 0; x < source.width; x++) {              int pixel = source.pixels[p++];              // needs to be ABGR, stored in memory xRGB              // so R and B must be swapped, and the x just made FF              tpixels[t++] =                0xff000000 |  // force opacity for good measure                ((pixel & 0xFF) << 16) |                ((pixel & 0xFF0000) >> 16) |                (pixel & 0x0000FF00);            }            t += twidth - source.width;          }          break;        case ARGB:          for (int y = 0; y < source.height; y++) {            for (int x = 0; x < source.width; x++) {              int pixel = source.pixels[p++];              // needs to be ABGR stored in memory ARGB              // so R and B must be swapped, A and G just brought back in              tpixels[t++] =                ((pixel & 0xFF) << 16) |                ((pixel & 0xFF0000) >> 16) |                (pixel & 0xFF00FF00);            }            t += twidth - source.width;          }          break;        }      }      tbuffer.put(tpixels);      tbuffer.rewind();      //      gl.glBindTexture(GL.GL_TEXTURE_2D, tindex);      gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);      //gl.glPixelStorei(GL.GL_UNPACK_SWAP_BYTES, 0);      gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, 4, twidth, theight,                      //0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, cash.tpixels);                      0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, tbuffer);      gl.glTexParameterf(GL.GL_TEXTURE_2D,                         //GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);                         GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);      gl.glTexParameterf(GL.GL_TEXTURE_2D,                         //GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);                         GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);      //      /*int err =*/ glu.gluBuild2DMipmaps(GL.GL_TEXTURE_2D, 4,                                          twidth, theight,                                          GL.GL_RGBA,                                          GL.GL_UNSIGNED_BYTE, tbuffer);      //System.out.println("mipmap: " + err);      // The MAG_FILTER should only be GL_LINEAR or GL_NEAREST.      // Some cards are OK with LINEAR_MIPMAP_LINEAR, but not the      // Radeon 9700, which is in all the PB G4s.. Not sure if this      // is an OpenGL version thing, tho it makes sense MIN_FILTER      // is the only one that uses mipmapping.      gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER,                         GL.GL_LINEAR);      gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER,                         GL.GL_LINEAR_MIPMAP_LINEAR);//      gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP);//      gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP);      gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE);      gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE);      //      gl.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_MODULATE);    }    private int nextPowerOfTwo(int val) {      int ret = 1;      while (ret < val) {        ret <<= 1;      }      return ret;    }  }  //////////////////////////////////////////////////////////////  // RENDERING  //public void flush()  //protected void render()  //protected void sort()  //////////////////////////////////////////////////////////////  // POINT, LINE, TRIANGLE, QUAD  // Because vertex(x, y) is mapped to vertex(x, y, 0), none of these commands  // need to be overridden from their default implementation in PGraphics.  //public void point(float x, float y)  //public void point(float x, float y, float z)  //public void line(float x1, float y1, float x2, float y2)  //public void line(float x1, float y1, float z1,  //                 float x2, float y2, float z2)  //public void triangle(float x1, float y1, float x2, float y2,  //                     float x3, float y3)  //public void quad(float x1, float y1, float x2, float y2,  //                 float x3, float y3, float x4, float y4)  //////////////////////////////////////////////////////////////  // RECT  //public void rectMode(int mode)  //public void rect(float a, float b, float c, float d)  //protected void rectImpl(float x1, float y1, float x2, float y2)  //////////////////////////////////////////////////////////////  // ELLIPSE  //public void ellipseMode(int mode)  //public void ellipse(float a, float b, float c, float d)  /*  boolean ellipseInited;  int ellipseFillList;  int ellipseStrokeList;  protected void ellipseImpl(float x1, float y1, float w, float h) {    float hradius = w / 2f;    float vradius = h / 2f;    float centerX = x1 + hradius;    float centerY = y1 + vradius;    // adapt accuracy to radii used w/ a minimum of 4 segments [toxi]    // now uses current scale factors to determine "real" transformed radius    //int cAccuracy = (int)(4+Math.sqrt(hradius*abs(m00)+vradius*abs(m11))*2);    //int cAccuracy = (int)(4+Math.sqrt(hradius+vradius)*2);    // notched this up to *3 instead of *2 because things were    // looking a little rough, i.e. the calculate->arctangent example [fry]    // also removed the m00 and m11 because those were causing weirdness    // need an actual measure of magnitude in there [fry]    int accuracy = (int)(4+Math.sqrt(hradius+vradius)*3);    //System.out.println("accuracy is " + accuracy);    //accuracy = 5;    // [toxi031031] adapted to use new lookup tables    float inc = (float)SINCOS_LENGTH / accuracy;    float val = 0;    if (fill) {      boolean savedStroke = stroke;      stroke = false;      beginShape(TRIANGLE_FAN);      normal(0, 0, 1);      vertex(centerX, centerY);      for (int i = 0; i < accuracy; i++) {        vertex(centerX + cosLUT[(int) val] * hradius,               centerY + sinLUT[(int) val] * vradius);        val += inc;      }      // back to the beginning      vertex(centerX + cosLUT[0] * hradius,             centerY + sinLUT[0] * vradius);      endShape();      stroke = savedStroke;    }    if (stroke) {      boolean savedFill = fill;      fill = false;      val = 0;      beginShape(); //LINE_LOOP);      for (int i = 0; i < accuracy; i++) {        vertex(centerX + cosLUT[(int) val] * hradius,               centerY + sinLUT[(int) val] * vradius);        val += inc;      }      endShape(CLOSE);      fill = savedFill;    }  }  */  /*    pgl.beginGL();    //PGraphics gr = PApplet.this.g;    //GL gl = ((PGraphicsOpenGL).gr).beginGL();    if (!ellipseInited) {      ellipseList = gl.glGenLists(1);      gl.glNewList(ellipseList, GL.GL_COMPILE);      gl.glBegin(GL.GL_LINE_LOOP);      int seg = 15;      float segf = 15;      for (int i = 0; i < seg; i++) {        float theta = TWO_PI * (float)i / segf;        gl.glVertex2f(cos(theta), sin(theta));      }      gl.glEnd();      gl.glEndList();      ellipseInited = true;    }    for (int i=1; i<numSegments-1; i++) {    gl.glPushMatrix();    gl.glTranslatef(x[i], y[i], z);    float r = w[i]/2f;    gl.glScalef(r, r, r);    gl.glColor4f(1, 1, 1, 150.0/255.0);    gl.glCallList(ellipseList);    gl.glScalef(0.5, 0.5, 0.5);    gl.glColor4f(1, 1, 1, 50.0/255.0);    gl.glCallList(ellipseList);    gl.glPopMatrix();    }    pgl.endGL();  */  //public void arc(float a, float b, float c, float d,  //                float start, float stop)  //protected void arcImpl(float x, float y, float w, float h,  //                       float start, float stop)  //////////////////////////////////////////////////////////////  // BOX  // TODO P3D overrides box to turn on triangle culling, but that's a waste  // for OpenGL. Also could just use the cube method from GL or GLUT.  //public void box(float size)  //public void box(float w, float h, float d)  // P3D  //////////////////////////////////////////////////////////////  // SPHERE  // TODO P3D overrides sphere to turn on triangle culling, but that's a waste  // for OpenGL. Also could just use the cube method from GL or GLUT.  //public void sphereDetail(int res)  //public void sphereDetail(int ures, int vres)  //public void sphere(float r)  //////////////////////////////////////////////////////////////  // BEZIER  //public float bezierPoint(float a, float b, float c, float d, float t)  //public float bezierTangent(float a, float b, float c, float d, float t)  //public void bezierDetail(int detail)  //public void bezier(float x1, float y1,  //                   float x2, float y2,  //                   float x3, float y3,  //                   float x4, float y4)  //public void bezier(float x1, float y1, float z1,  //                   float x2, float y2, float z2,  //                   float x3, float y3, float z3,  //                   float x4, float y4, float z4)  //////////////////////////////////////////////////////////////  // CATMULL-ROM CURVES  //public float curvePoint(float a, float b, float c, float d, float t)  //public float curveTangent(float a, float b, float c, float d, float t)  //public void curveDetail(int detail)  //public void curveTightness(float tightness)  //public void curve(float x1, float y1,  //                  float x2, float y2,  //                  float x3, float y3,  //                  float x4, float y4)  //public void curve(float x1, float y1, float z1,  //                  float x2, float y2, float z2,  //                  float x3, float y3, float z3,  //                  float x4, float y4, float z4)  //////////////////////////////////////////////////////////////  // SMOOTH  public void smooth() {    smooth = true;    if (hints[DISABLE_OPENGL_2X_SMOOTH]) {      //gl.glEnable(GL.GL_MULTISAMPLE);      gl.glEnable(GL.GL_POINT_SMOOTH);      gl.glEnable(GL.GL_LINE_SMOOTH);      gl.glEnable(GL.GL_POLYGON_SMOOTH);    }  }  public void noSmooth() {    smooth = false;    if (hints[DISABLE_OPENGL_2X_SMOOTH]) {      //gl.glDisable(GL.GL_MULTISAMPLE);      gl.glDisable(GL.GL_POINT_SMOOTH);      gl.glDisable(GL.GL_LINE_SMOOTH);      gl.glDisable(GL.GL_POLYGON_SMOOTH);    }  }  //////////////////////////////////////////////////////////////  // IMAGES

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?