使用OpenGLEs for Android绘制Pixel

时间:2010-05-13 14:00:07

标签: android opengl-es drawing

如何使用OpenGlEs为Android绘制像素(2D视图)?

在用于绘画的时候很简单

draw(Canvas canvas)
{
   ...
} 

所以使用它我们绘制

canvas.drawPoint(i, j, paint);

但在OpenGlEs中,我仍然没有任何类似的功能。

请回复 提前感谢

2 个答案:

答案 0 :(得分:7)

    GLES20Renderer.programLight = GLES20.glCreateProgram();
    int vertexShaderLight       = GLES20Renderer.loadShader(GLES20.GL_VERTEX_SHADER, GLES20Renderer.vertexShaderCodeLight);
    int fragmentShaderLight     = GLES20Renderer.loadShader(GLES20.GL_FRAGMENT_SHADER, GLES20Renderer.fragmentShaderCodeLight);
    GLES20.glAttachShader(GLES20Renderer.programLight, vertexShaderLight);
    GLES20.glAttachShader(GLES20Renderer.programLight, fragmentShaderLight);
    GLES20.glLinkProgram(GLES20Renderer.programLight);
    uPLocationLight             = GLES20.glGetUniformLocation(GLES20Renderer.programLight, "uP");
    uVPositionLocationLight     = GLES20.glGetUniformLocation(GLES20Renderer.programLight, "uVPosition");

    GLES20.glUseProgram(GLES20Renderer.programLight);
    GLES20.glUniform4f(uVPositionLocationLight, LightPosInEyeSpace[0], LightPosInEyeSpace[1], LightPosInEyeSpace[2], LightPosInEyeSpace[3]);
    GLES20.glUniformMatrix4fv(uPLocationLight, 1, false, ProjectionMatrix, 0);
    GLES20.glDrawArrays(GLES20.GL_POINTS, 0, 1);


private static final String vertexShaderCodeLight = 
        "uniform vec4 uVPosition;                   \n"
    +   "uniform mat4 uP;                           \n"
    +   "void main(){                               \n"
    +   " gl_PointSize = 15.0;                      \n"
    +   " gl_Position = uP * uVPosition;            \n"
    +   "}                                          \n";
private static final String fragmentShaderCodeLight = 
        "#ifdef GL_FRAGMENT_PRECISION_HIGH          \n"
    +   "precision highp float;                     \n"
    +   "#else                                      \n"
    +   "precision mediump float;                   \n"
    +   "#endif                                     \n"
    +   "void main(){                               \n"
    +   " gl_FragColor = vec4(1.0,1.0,1.0,1.0);     \n"
    +   "}                                          \n";

答案 1 :(得分:3)

glDrawElements(GL_POINTS,0,num_points,point_array); 如果先使用glVertexPointer,也可以使用glDrawArrays。

使用glPointParameter,glPointSize和glColor修改点的外观。使用glPointSizePointer可以指定一个点大小数组,这对于粒子效果非常方便。