我有两个类来绘制点和线。但是屏幕上没有绘制点数。 我收到如下错误,
DISTRIB /机器人-emugl /主机/库/翻译/ GLES_V2 / GLESv2Imp.cpp:glGetUniformLocation:1512 错误0x501
点类不能正常工作。在我的渲染类中,我有arraylist来控制点 行对象和代码通过arrrays迭代来绘制这些原语。
我的观点类
private FloatBuffer VertexBuffer;
private final String VertexShaderCode =
// This matrix member variable provides a hook to manipulate
// the coordinates of the objects that use this vertex shader
"uniform mat4 uMVPMatrix;" +
"attribute vec3 vPosition;" +
"void main() {" +
// the matrix must be included as a modifier of gl_Position
" gl_Position = uMVPMatrix * vec4(vPosition,1.0f);" +
"gl_PointSize = 30.0;" +
"}";
private final String FragmentShaderCode =
"precision mediump float;" +
"uniform vec4 vColor;" +
"void main() {" +
" gl_FragColor = vColor;" +
"}";
protected static int GlProgram;
protected int PositionHandle;
protected int ColorHandle;
protected int MVPMatrixHandle;
// number of coordinates per vertex in this array
static final int COORDS_PER_VERTEX = 3;
static float PointCoords[] = {
0.0f, 0.0f, 0.0f,
};
private final int VertexCount = PointCoords.length / COORDS_PER_VERTEX;
private final int VertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex
// Set color with red, green, blue and alpha (opacity) values
float color[] = { 0.0f, 0.0f, 0.0f, 1.0f };
public Point() {
// initialize vertex byte buffer for shape coordinates
ByteBuffer bb = ByteBuffer.allocateDirect(
// (number of coordinate values * 4 bytes per float)
PointCoords.length * 4);
// use the device hardware's native byte order
bb.order(ByteOrder.nativeOrder());
// create a floating point buffer from the ByteBuffer
VertexBuffer = bb.asFloatBuffer();
// add the coordinates to the FloatBuffer
VertexBuffer.put(PointCoords);
// set the buffer to read the first coordinate
VertexBuffer.position(0);
int vertexShader = GLRender.loadShader(GLES20.GL_VERTEX_SHADER, VertexShaderCode);
int fragmentShader = GLRender.loadShader(GLES20.GL_FRAGMENT_SHADER, FragmentShaderCode);
GlProgram = GLES20.glCreateProgram(); // create empty OpenGL ES Program
GLES20.glAttachShader(GlProgram, vertexShader); // add the vertex shader to program
GLES20.glAttachShader(GlProgram, fragmentShader); // add the fragment shader to program
GLES20.glLinkProgram(GlProgram); // creates OpenGL ES program executables
}
public void SetPointVerts(float v0, float v1, float v2) {
PointCoords[0] = v0;
PointCoords[1] = v1;
PointCoords[2] = v2;
VertexBuffer.put(PointCoords);
// set the buffer to read the first coordinate
VertexBuffer.position(0);
}
public void SetPointColor(float red, float green, float blue, float alpha) {
color[0] = red;
color[1] = green;
color[2] = blue;
color[3] = alpha;
}
public void draw(float[] mvpMatrix) {
// Add program to OpenGL ES environment
GLES20.glUseProgram(GlProgram);
// get handle to vertex shader's vPosition member
PositionHandle = GLES20.glGetAttribLocation(GlProgram, "vPosition");
// Enable a handle to the triangle vertices
GLES20.glEnableVertexAttribArray(PositionHandle);
// Prepare the triangle coordinate data
GLES20.glVertexAttribPointer(PositionHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
VertexStride, VertexBuffer);
// get handle to fragment shader's vColor member
ColorHandle = GLES20.glGetUniformLocation(GlProgram, "vColor");
// Set color for drawing the triangle
GLES20.glUniform4fv(ColorHandle, 1, color, 0);
// get handle to shape's transformation matrix
MVPMatrixHandle = GLES20.glGetUniformLocation(GlProgram, "uMVPMatrix");
//GLRender.checkGlError("glGetUniformLocation");
// Apply the projection and view transformation
GLES20.glUniformMatrix4fv(MVPMatrixHandle, 1, false, mvpMatrix, 0);
//GLRender.checkGlError("glUniformMatrix4fv");
// Draw the triangle
GLES20.glDrawArrays(GLES20.GL_POINTS, 0, VertexCount);
// Disable vertex array
GLES20.glDisableVertexAttribArray(PositionHandle);
}
09-08 10:57:28.308 29430-29494 / com.timescale.lenovo.sds I / OpenGLRenderer:初始化的EGL,版本1.4 09-08 10:57:28.330 29430-29494 / com.timescale.lenovo.sds D / OpenGLRenderer:启用调试模式0 09-08 10:57:28.343 29430-29494 / com.timescale.lenovo.sds W / EGL_emulation:eglSurfaceAttrib未实现 09-08 10:57:28.343 29430-29494 / com.timescale.lenovo.sds W / OpenGLRenderer:无法在表面0xae54a080上设置EGL_SWAP_BEHAVIOR,错误= EGL_SUCCESS 09-08 10:57:28.498 29430-29438 / com.timescale.lenovo.sds W / art:暂停所有线程:9.892ms 09-08 10:57:38.010 29430-29494 / com.timescale.lenovo.sds W / EGL_emulation:eglSurfaceAttrib未实现 09-08 10:57:38.011 29430-29494 / com.timescale.lenovo.sds W / OpenGLRenderer:无法在表面0xa30640a0上设置EGL_SWAP_BEHAVIOR,错误= EGL_SUCCESS 09-08 10:57:39.324 29430-29430 / com.timescale.lenovo.sds E / libEGL:调用没有当前上下文的OpenGL ES API(每个线程记录一次) 09-08 10:57:39.392 29430-29493 / com.timescale.lenovo.sds E / emuglGLESv2_enc:glDrawArrays:没有数据绑定到命令 - 忽略 09-08 10:57:39.394 29430-29493 / com.timescale.lenovo.sds E / emuglGLESv2_enc:glDrawArrays:没有数据绑定到命令 - 忽略
答案 0 :(得分:0)
您正在将vPosition属性声明为vec4(齐次坐标),但在Java代码中,您通过glVertexAttribPointer上传vec3顶点(COORDS_PER_VERTEX const设置为3)。更改几何定义或着色器代码以接受3个元素向量:
private final String VertexShaderCode =
// This matrix member variable provides a hook to manipulate
// the coordinates of the objects that use this vertex shader
"uniform mat4 uMVPMatrix;" +
"attribute vec3 vPosition;" +
"void main() {" +
// the matrix must be included as a modifier of gl_Position
" gl_Position = uMVPMatrix * vec4(vPosition, 1.0);" +
" gl_PointSize = 30.0;" +
"}";
旁注,因为您的顶点仅包含位置数据,您只需将步幅设置为0(元素紧密包装)