作者 主题: [Processing]2.0的新版OpenGL渲染[连载]  (阅读 8932 次)

vinjn

  • SuperManager
  • Hero Member
  • *****
  • 帖子: 586
[Processing]2.0的新版OpenGL渲染[连载]
« 于: 十二月 22, 2011, 05:11:45 下午 »
OpenGL相关的作者是Andres Colubri
同时他也是GSVideo的作者(赞)

代码在此
processing_svn\java\libraries\opengl\src\processing\opengl\

由如下文件组成

LineShaderFrag.glsl
LineShaderVert.glsl
PFontTexture.java
PFramebuffer.java
PGraphicsOpenGL.java
PointShaderFrag.glsl
PointShaderVert.glsl
PShader.java
PShape3D.java
PTexture.java

下面这段代码极其重要,我整个搬上来
程序代码
public class PGraphicsOpenGL extends PGraphics {
  // JOGL2 objects:
 
  /**
   * How the P3D renderer handles the different OpenGL profiles? Basically,
   * P3D has two pipeline modes: fixed or programmable. In the fixed mode,
   * only the gl and gl2f objects are available. The gl2f object contains the
   * intersection between OpenGL 2.x desktop and OpenGL 1.1 embedded, and in this
   * way it ensures the functionality parity between the P3D render (PC/MAC)
   * and A3D (Android) in the fixed pipeline mode.
   * In the programmable mode, there further options: GL2, GL3 and GL4.
   * GL2 corresponds to the basic programmable profile that results from the common
   * functionality between OpenGL 3.0 desktop and OpenGL 2.0 embedded. As said just
   * before, since P3D and A3D aim at feature parity as much as possible, this is
   * the only programmable-pipeline GL object that the P3D renderer uses.
   * The gl3 and gl4 objects will be available when the pipeline mode is PROG_GL3 or
   * PROG_GL4, respectively. Although P3D doens't make any use of these objects,
   * they are part of the API nonetheless for users (or libraries) requiring advanced
   * functionality introduced with OpenGL 3 or OpenGL 4.
   * By default, P3D tries to auto-select the pipeline mode by with the following
   * priority order: PROG_GL4, PROG_GL3, PROG_GL2, FIXED. In all the programmable modes,
   * the gl2p object is always available. This auto-selection can be optionally
   * overridden when creating the renderer object, so that a specific mode is set.
   * Note that the programmable mode uses the non-backward compatible GL objects
   * (GL3, GL4, and not GL3bc, GL4bc) so no fixed mode calls are possible under this mode.
   */
 
  /** Pipeline mode: FIXED, PROG_GL2, PROG_GL3 or PROG_GL4 */
  public int pipeline;
 
  /** Basic GL functionality, common to all profiles */
  public GL gl;
 
  public GL2 gl2;
 
  /** Advanced GL functionality (usually, things available as extensions in JOGL1).
   * This profile is the intersection between GL 2.0 and GL 3.0 */
  public GL2GL3 gl2x;
 
  /** Fixed GL pipeline, with the functionality common to the GL2 desktop and GLES 1.1 profiles */
  public GL2ES1 gl2f;
 
  /** Basic programmable GL pipeline: intersection of desktop GL3, GL2 and embedded ES2 profile */
  public GL2ES2 gl2p;
 
  /** GL3 programmable pipeline, not backwards compatible with GL2 fixed */
  public GL3 gl3p;
 
  /** GL4 programmable pipeline, not backwards compatible with GL2 fixed */
  public GL4 gl4p;

PGraphicsOpenGL是负责OpenGL渲染事宜的总管。

新版的processing只有两种图形模式,P2D和P3D。P3D就是原先的OpenGL模式,原先的P3D模式已经被删除了。
现代的图形API都有两种渲染流水线(pipeline),固定管线(fixed pipeline)以及可编程管线(programmable pipeline)。
那么,P3D里是怎么同时搞定这两种管线的呢?
答案在上述代码里,稍加解释:

这是P3D的四种渲染管线: (位于core/PConstants.java)
  static final int FIXED    = 0;
  static final int PROG_GL2 = 1;
  static final int PROG_GL3 = 2;
  static final int PROG_GL4 = 3;

OpenGL的版本已经发展到了4
OpenGL ES的版本也到了2
因此,我们的选项很多(并且复杂)
程序代码
import javax.media.opengl.GL;//基础GL,对所有类型通用
import javax.media.opengl.GL2;//基础GL,对所有类型通用

import javax.media.opengl.GL2ES1;//固定管线的GL2,GL2+GLES1.1
import javax.media.opengl.GL2ES2;//可编程管线的GL2,GL3+GL2+GLES2
import javax.media.opengl.GL2GL3;

import javax.media.opengl.GL3;//可编程管线的GL3,与固定管线的GL2不兼容
import javax.media.opengl.GL4;//可编程管线的GL4,与固定管线的GL2不兼容

程序代码
public GL gl;  
public GL2 gl2; 
public GL2GL3 gl2x; 
public GL2ES1 gl2f; 
public GL2ES2 gl2p; 
public GL3 gl3p; 
public GL4 gl4p;
gl对象是一直可以使用的。
固定管线(FIXED)只能使用gl2f对象。gl2f包含了GLES 1.1的API。
可编程管线包含GL2/GL3/GL4。这三种模式中,gl2f对象都是不能使用的。
GL2对应着最基础的可编程功能,由于android平台的A3D渲染模式期望达到与PC平台的P3D模式相似的特性,这是P3D唯一用到的可编程管线类型。
gl3p和gl4p也是可以使用的,但是processing本身并没有用到,如果需要,可以自行扩展。

« 最后编辑时间: 十二月 22, 2011, 05:32:52 下午 作者 vinjn »

vinjn

  • SuperManager
  • Hero Member
  • *****
  • 帖子: 586
Re: [Processing]2.0的新版OpenGL渲染
« 回复 #1 于: 十二月 22, 2011, 05:31:15 下午 »
PShape3D

PShape3D继承自PShape
在其他编程环境中,可能又被称称为Mesh、MeshSceneNode、Model等等

包含顶点、向量、颜色以及纹理坐标,并且这些数据都以Vertex Buffer Object(VBO)的形式保存在GPU的内存中。
因此速度足够快!

程序代码
  public PShape createShape(int type) {
    PShape3D shape = null;
    if (type == PShape.GROUP) {
      shape = new PShape3D(parent, PShape.GROUP);
    } else if (type == POINTS) {
      shape = new PShape3D(parent, PShape.GEOMETRY);
      shape.setKind(POINTS);
    } else if (type == LINES) {
      shape = new PShape3D(parent, PShape.GEOMETRY);
      shape.setKind(LINES);
    } else if (type == TRIANGLES) {
      shape = new PShape3D(parent, PShape.GEOMETRY);
      shape.setKind(TRIANGLES);
    } else if (type == TRIANGLE_FAN) {
      shape = new PShape3D(parent, PShape.GEOMETRY);
      shape.setKind(TRIANGLE_FAN);
    } else if (type == TRIANGLE_STRIP) {     
      shape = new PShape3D(parent, PShape.GEOMETRY);
      shape.setKind(TRIANGLE_STRIP);
    } else if (type == QUADS) {
      shape = new PShape3D(parent, PShape.GEOMETRY);
      shape.setKind(QUADS);
    } else if (type == QUAD_STRIP) {
      shape = new PShape3D(parent, PShape.GEOMETRY);
      shape.setKind(QUAD_STRIP);
    } else if (type == POLYGON) {
      shape = new PShape3D(parent, PShape.GEOMETRY);
      shape.setKind(POLYGON);     
    }
    return shape;
  }

有了Shape3d之后,编程的方式有所改变。举一个绘制红色圆圈的例子来说明:
这是之前的方式,直接调用绘图函数,这被称为立即模式(immediate mode)
程序代码
import processing.opengl.*;

size(800, 600, P3D);

fill(255,0,0);
ellipse(100,100,100,100);

现在的方式,先通过createShape创建obj,调用fill修改obj的颜色,最后通过shape函数将obj显示。
这被称为保留模式(retained mode)
程序代码
import processing.opengl.*;

size(800, 600, P3D);
PShape obj = createShape(ELLIPSE, 100, 100, 100, 100); 
obj.fill(255,0,0);
shape(obj);


再看一个更加复杂的例子,从中能察觉PShape的优雅之之处。
粒子系统
程序代码
PShape system;//一个PShape即一组元素
 
void setup() {
  size(500, 500, P3D);
   
  system = createShape(PShape.GROUP);//注意参数为PShape.GROUP
   
  for (int i = 0; i < 10; i++) {
    PShape pt = createShape(POINT);//创建子元素,为一个点
    pt.strokeWeight(20);//一个很粗壮的点
    pt.stroke(random(255), random(255), random(255));//随机颜色
    pt.vertex(random(width), random(height));//随机位置
    system.addShape(pt);//将这个粗壮的点添加进system中
    pt.end();
  }
}

public void draw () {
  background(255);

  shape(system);//绘制粒子系统
   
  int n_particles = system.getChildCount();//得到子节点的数量
  for (int i = 0; i <n_particles;  i++) {
    PShape pt = system.getChild(i);//遍历每个子节点
     pt.translate(random(-5, 5), random(-5, 5), random(-5, 5));//进行偏移
  }
}


RavenKwok

  • Sr. Member
  • ****
  • 帖子: 277
  • Artist/ Animator/ Coder/ Cynical Asshole
Re: [Processing]2.0的新版OpenGL渲染[连载]
« 回复 #2 于: 十二月 23, 2011, 09:54:19 上午 »
无意发现新开的这个版没有Thank You 功能哎。

Contra

  • SuperManager
  • Sr. Member
  • *****
  • 帖子: 347
  • AC, game dev, new media art&tech.
    • i'm Contra
Re: [Processing]2.0的新版OpenGL渲染[连载]
« 回复 #3 于: 十二月 23, 2011, 02:21:31 下午 »
无意发现新开的这个版没有Thank You 功能哎。

done

herohezi

  • Newbie
  • *
  • 帖子: 6
Re: [Processing]2.0的新版OpenGL渲染[连载]
« 回复 #4 于: 二月 08, 2012, 12:54:05 下午 »
支持 vinjn  大哥继续发帖

Tags: