pgraphicsopengl.java

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

JAVA
2,167
字号
  ////////////////////////////////////////////////////////////  // HINTS  public void hint(int which) {    // make note of whether these are set, if they are,    // then will prevent the new renderer exception from being thrown.    boolean opengl2X = !hints[DISABLE_OPENGL_2X_SMOOTH];    boolean opengl4X = hints[ENABLE_OPENGL_4X_SMOOTH];    super.hint(which);    if (which == DISABLE_DEPTH_TEST) {      gl.glDisable(GL.GL_DEPTH_TEST);      gl.glClear(GL.GL_DEPTH_BUFFER_BIT);    } else if (which == ENABLE_DEPTH_TEST) {      gl.glEnable(GL.GL_DEPTH_TEST);    } else if (which == DISABLE_OPENGL_2X_SMOOTH) {      if (opengl2X) {        releaseContext();        context.destroy();        context = null;        allocate();        throw new PApplet.RendererChangeException();      }    } else if (which == ENABLE_OPENGL_2X_SMOOTH) {      // do nothing, this is the default in release 0158 and later    } else if (which == ENABLE_OPENGL_4X_SMOOTH) {      if (!opengl4X) {        releaseContext();        context.destroy();        context = null;        allocate();        throw new PApplet.RendererChangeException();      }    }  }  //////////////////////////////////////////////////////////////  // VERTEX SHAPES  // All picked up from either PGraphics or PGraphics3D  //public void beginShape()  //public void beginShape(int kind)  //public void edge(boolean e)  //public void normal(float nx, float ny, float nz)  //public void textureMode(int mode)  //public void texture(PImage image)  //public void vertex(float x, float y)  //public void vertex(float x, float y, float z)  //public void vertex(float x, float y, float u, float v)  //public void vertex(float x, float y, float z, float u, float v)  //protected void vertexTexture(float u, float v);  //public void breakShape()  //public void endShape()  //public void endShape(int mode)  protected void endShapeLighting(boolean lights) {    super.endShapeLighting(lights);    // For now do our own lighting--sum the specular and diffuse light colors    if (lights) {      for (int i = shapeFirst; i < shapeLast; i++) {        float v[] = vertices[i];        v[R] = clamp(v[R] + v[SPR]);        v[G] = clamp(v[G] + v[SPG]);        v[B] = clamp(v[B] + v[SPB]);      }    }  }  //////////////////////////////////////////////////////////////  // BEZIER CURVE VERTICES  // All picked up from either PGraphics or PGraphics3D, however  // a faster version that made use of OpenGL's evaluator methods  // would be a nice improvement.  //protected void bezierVertexCheck();  //public void bezierVertex(float x2, float y2,  //                         float x3, float y3,  //                         float x4, float y4)  //public void bezierVertex(float x2, float y2, float z2,  //                         float x3, float y3, float z3,  //                         float x4, float y4, float z4)  //////////////////////////////////////////////////////////////  // CATMULL-ROM CURVE VERTICES  // Like bezier, these could be implemented using an OpenGL evaluator.  //protected void curveVertexCheck();  //public void curveVertex(float x, float y)  //public void curveVertex(float x, float y, float z)  //protected void curveVertexSegment(float x1, float y1,  //                                  float x2, float y2,  //                                  float x3, float y3,  //                                  float x4, float y4)  //protected void curveVertexSegment(float x1, float y1, float z1,  //                                  float x2, float y2, float z2,  //                                  float x3, float y3, float z3,  //                                  float x4, float y4, float z4)  //////////////////////////////////////////////////////////////  // POINTS (override from P3D)  protected void renderPoints(int start, int stop) {    float sw = vertices[lines[start][VERTEX1]][SW];    if (sw > 0) {      gl.glPointSize(sw);  // can only be set outside glBegin/glEnd      gl.glBegin(GL.GL_POINTS);      for (int i = start; i < stop; i++) {        float[] a = vertices[points[i][VERTEX1]];        gl.glColor4f(a[SR], a[SG], a[SB], a[SA]);        gl.glVertex3f(a[VX], a[VY], a[VZ]);      }      gl.glEnd();    }  }  //protected void rawPoints(int start, int stop)  // PGraphics3D  //////////////////////////////////////////////////////////////  // LINES (override from P3D)  //protected final void addLineBreak()  // PGraphics3D  /**   * Add this line, but disable clipping because GL will handle it.   */  protected void addLine(int a, int b) {    addLineWithoutClip(a, b);  }  //protected final void addLineWithClip(int a, int b)  //protected final void addLineWithoutClip(int a, int b)  /**   * In the current implementation, start and stop are ignored (in OpenGL).   * This will obviously have to be revisited if/when proper depth sorting   * is implemented.   */  protected void renderLines(int start, int stop) {    report("render_lines in");    //int i = 0;    for (int j = 0; j < pathCount; j++) {      int i = pathOffset[j];      float sw = vertices[lines[i][VERTEX1]][SW];      //report("render_lines 1");      // stroke weight zero will cause a gl error      if (sw > 0) {        // glLineWidth has to occur outside glBegin/glEnd        gl.glLineWidth(sw);        gl.glBegin(GL.GL_LINE_STRIP);        // always draw a first point        float a[] = vertices[lines[i][VERTEX1]];        gl.glColor4f(a[SR], a[SG], a[SB], a[SA]);        gl.glVertex3f(a[VX], a[VY], a[VZ]);        // on this and subsequent lines, only draw the second point        //System.out.println(pathLength[j]);        for (int k = 0; k < pathLength[j]; k++) {          float b[] = vertices[lines[i][VERTEX2]];          gl.glColor4f(b[SR], b[SG], b[SB], b[SA]);          //gl.glEdgeFlag(a[EDGE] == 1);          gl.glVertex3f(b[VX], b[VY], b[VZ]);          i++;        }        gl.glEnd();      }    }    report("render_lines out");  }  //protected void rawLines(int start, int stop)  //////////////////////////////////////////////////////////////  // TRIANGLES  /**   * Add the triangle, but disable clipping because GL will handle it.   */  protected void addTriangle(int a, int b, int c) {    addTriangleWithoutClip(a, b, c);  }  protected void renderTriangles(int start, int stop) {    report("render_triangles in");    for (int i = start; i < stop; i++) {      float a[] = vertices[triangles[i][VERTEX1]];      float b[] = vertices[triangles[i][VERTEX2]];      float c[] = vertices[triangles[i][VERTEX3]];      // This is only true when not textured.      // We really should pass specular straight through to triangle rendering.      float ar = clamp(triangleColors[i][0][TRI_DIFFUSE_R] + triangleColors[i][0][TRI_SPECULAR_R]);      float ag = clamp(triangleColors[i][0][TRI_DIFFUSE_G] + triangleColors[i][0][TRI_SPECULAR_G]);      float ab = clamp(triangleColors[i][0][TRI_DIFFUSE_B] + triangleColors[i][0][TRI_SPECULAR_B]);      float br = clamp(triangleColors[i][1][TRI_DIFFUSE_R] + triangleColors[i][1][TRI_SPECULAR_R]);      float bg = clamp(triangleColors[i][1][TRI_DIFFUSE_G] + triangleColors[i][1][TRI_SPECULAR_G]);      float bb = clamp(triangleColors[i][1][TRI_DIFFUSE_B] + triangleColors[i][1][TRI_SPECULAR_B]);      float cr = clamp(triangleColors[i][2][TRI_DIFFUSE_R] + triangleColors[i][2][TRI_SPECULAR_R]);      float cg = clamp(triangleColors[i][2][TRI_DIFFUSE_G] + triangleColors[i][2][TRI_SPECULAR_G]);      float cb = clamp(triangleColors[i][2][TRI_DIFFUSE_B] + triangleColors[i][2][TRI_SPECULAR_B]);      int textureIndex = triangles[i][TEXTURE_INDEX];      if (textureIndex != -1) {        report("before enable");        gl.glEnable(GL.GL_TEXTURE_2D);        report("after enable");        report("before bind");        PImage texture = textures[textureIndex];        bindTexture(texture);        report("after bind");        ImageCache cash = (ImageCache) texture.getCache(this);        float uscale = (float) texture.width / (float) cash.twidth;        float vscale = (float) texture.height / (float) cash.theight;        gl.glBegin(GL.GL_TRIANGLES);        //System.out.println(a[U] + " " + a[V] + " " + uscale + " " + vscale);        //System.out.println(ar + " " + ag + " " + ab + " " + a[A]);        //ar = ag = ab = 1;        gl.glColor4f(ar, ag, ab, a[A]);        gl.glTexCoord2f(a[U] * uscale, a[V] * vscale);        gl.glNormal3f(a[NX], a[NY], a[NZ]);        gl.glEdgeFlag(a[EDGE] == 1);        gl.glVertex3f(a[VX], a[VY], a[VZ]);        gl.glColor4f(br, bg, bb, b[A]);        gl.glTexCoord2f(b[U] * uscale, b[V] * vscale);        gl.glNormal3f(b[NX], b[NY], b[NZ]);        gl.glEdgeFlag(a[EDGE] == 1);        gl.glVertex3f(b[VX], b[VY], b[VZ]);        gl.glColor4f(cr, cg, cb, c[A]);        gl.glTexCoord2f(c[U] * uscale, c[V] * vscale);        gl.glNormal3f(c[NX], c[NY], c[NZ]);        gl.glEdgeFlag(a[EDGE] == 1);        gl.glVertex3f(c[VX], c[VY], c[VZ]);        gl.glEnd();        report("non-binding 6");        gl.glDisable(GL.GL_TEXTURE_2D);      } else {  // no texture        gl.glBegin(GL.GL_TRIANGLES);        gl.glColor4f(ar, ag, ab, a[A]);        gl.glNormal3f(a[NX], a[NY], a[NZ]);        gl.glEdgeFlag(a[EDGE] == 1);        gl.glVertex3f(a[VX], a[VY], a[VZ]);        gl.glColor4f(br, bg, bb, b[A]);        gl.glNormal3f(b[NX], b[NY], b[NZ]);        gl.glEdgeFlag(a[EDGE] == 1);        gl.glVertex3f(b[VX], b[VY], b[VZ]);        gl.glColor4f(cr, cg, cb, c[A]);        gl.glNormal3f(c[NX], c[NY], c[NZ]);        gl.glEdgeFlag(a[EDGE] == 1);        gl.glVertex3f(c[VX], c[VY], c[VZ]);        gl.glEnd();      }    }    report("render_triangles out");  }  //protected void rawTriangles(int start, int stop)  // PGraphics3D  protected void bindTexture(PImage texture) {    ImageCache cash = (ImageCache) texture.getCache(this);  // as in johnny    if (cash == null) {      cash = new ImageCache();      texture.setCache(this, cash);      texture.setModified(true);    }    if (texture.isModified()) {      //System.out.println("texture modified");      // TODO make this more efficient and just update a sub-part      // based on mx1 et al, also use gl function to update      // only a sub-portion of the image.      cash.rebind(texture);      // clear the modified flag      texture.setModified(false);    } else {      gl.glBindTexture(GL.GL_TEXTURE_2D, cash.tindex);    }  }  protected class ImageCache {    int tindex = -1;  // not yet ready    int tpixels[];    IntBuffer tbuffer;    public int twidth, theight;    int[] tp;    /**     * Delete any texture memory that had been allocated.     * Added for 0125 to deal with memory problems reported in Bug #150.     */    protected void finalize() {      if (textureDeleteQueue.length == textureDeleteQueueCount) {        textureDeleteQueue = (int[]) PApplet.expand(textureDeleteQueue);      }      if (tindex != -1) {        textureDeleteQueue[textureDeleteQueueCount++] = tindex;      }    }    /**     * Generate a texture ID and do the necessary bitshifting for the image.     */    public void rebind(PImage source) {      if (textureDeleteQueueCount != 0) {        //gl.glDeleteTextures(1, new int[] { tindex }, 0);        gl.glDeleteTextures(textureDeleteQueueCount, textureDeleteQueue, 0);        textureDeleteQueueCount = 0;      }      //System.out.println("rebinding texture for " + source);      if (tindex != -1) {        // free up the old memory        gl.glDeleteTextures(1, new int[] { tindex }, 0);      }      // generate a new texture number to bind to      int[] tmp = new int[1];      gl.glGenTextures(1, tmp, 0);      tindex = tmp[0];      //System.out.println("got index " + tindex);      // bit shifting this might be more efficient      int width2 = nextPowerOfTwo(source.width);      //(int) Math.pow(2, Math.ceil(Math.log(source.width) / Math.log(2)));      int height2 = nextPowerOfTwo(source.height);      //(int) Math.pow(2, Math.ceil(Math.log(source.height) / Math.log(2)));      // use glGetIntegerv with the argument GL_MAX_TEXTURE_SIZE      // to figure out min/max texture sizes      if (maxTextureSize == 0) {        int maxSize[] = new int[1];        gl.glGetIntegerv(GL.GL_MAX_TEXTURE_SIZE, maxSize, 0);        maxTextureSize = maxSize[0];        //System.out.println("max texture size is " + maxTextureSize);      }      if ((width2 > maxTextureSize) || (height2 > maxTextureSize)) {        throw new RuntimeException("Image width and height cannot be" +                                   " larger than " + maxTextureSize +                                   " with your graphics card.");      }      if ((width2 > twidth) || (height2 > theight)) {        // either twidth/theight are zero, or size has changed        tpixels = null;      }      if (tpixels == null) {        twidth = width2;        theight = height2;        tpixels = new int[twidth * theight];        tbuffer = BufferUtil.newIntBuffer(twidth * theight);      }      // copy image data into the texture      int p = 0;      int t = 0;      if (BIG_ENDIAN) {        switch (source.format) {        case ALPHA:          for (int y = 0; y < source.height; y++) {            for (int x = 0; x < source.width; x++) {              tpixels[t++] = 0xFFFFFF00 | source.pixels[p++];            }            t += twidth - source.width;          }          break;        case RGB:          for (int y = 0; y < source.height; y++) {

⌨️ 快捷键说明

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