OpenGL ES2学习笔记(6)-- Line Strip和Line Loop

Line Strip

上一篇文章画了两条线段,但是用了4个点。如果几条线段首尾相接的话,可以让OpenGL把他们当成Line Strip来画,这样就可以用n+1个点来画n条线段。

代码

 

import java.nio.ByteBuffer
import java.nio.ByteOrder
import javax.media.opengl.GL

def vertexShaderCode = """
  attribute vec4 a_Position;

  void main() {                              
    gl_Position = a_Position;
    gl_PointSize = 10.0;
  }
"""

def fragmentShaderCode = """
  precision mediump float; 

  void main() {
    gl_FragColor = vec4(1.0, 0.5, 0.0, 1.0);
  }
"""

def shaderProgram = glob.compileAndLink(vertexShaderCode, fragmentShaderCode)
def aPositionLocation = gl.glGetAttribLocation(shaderProgram.objectId, "a_Position")
shaderProgram.use()


def BYTES_PER_FLOAT = 4
def POSITION_ELEMENT_COUNT = 2
def POINT_COUNT = 4

def vertices = [
 -0.2f,-0.8f,
 -0.2f,-0.2f,
  0.0f, 0.0f,
  0.2f,-0.6f,
] as float[]

def vertexData = ByteBuffer
        .allocateDirect(vertices.length * BYTES_PER_FLOAT)
        .order(ByteOrder.nativeOrder())
        .asFloatBuffer()
vertexData.put(vertices)
vertexData.position(0)
gl.glVertexAttribPointer(aPositionLocation,
       POSITION_ELEMENT_COUNT, gl.GL_FLOAT, false, 0, vertexData)
gl.glEnableVertexAttribArray(aPositionLocation)


gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f)
gl.glClear(gl.GL_COLOR_BUFFER_BIT)
gl.glLineWidth(4)
gl.glDrawArrays(gl.GL_LINE_STRIP, 0, POINT_COUNT)

唯一有必要说明的就是这段代码:

 

 

gl.glDrawArrays(gl.GL_LINE_STRIP, 0, POINT_COUNT)

传入的参数是 GL_LINE_STRIP而不是 GL_LINES

 

效果

Line Loop

如果Line Strip闭合的话,那么就变成了Line Loop。修改上面的脚本,把传递给glDrawArrays()方法的GL_LINE_STRIP参数改成GL_LINE_LOOP

 

gl.glDrawArrays(gl.GL_LINE_LOOP, 0, POINT_COUNT)


 


 

posted on 2013-10-10 11:39  我的小人生  阅读(2232)  评论(1编辑  收藏  举报