@H_301_8@
我有一个floatBuffer geometryBuffer,它保存顶点位置. 6个顶点具有完全相同的顶点坐标,一个用于两个三角形的每个角.此缓冲区中有多个点.@H_301_8@
着色器程序获取这些顶点并根据传递给着色器的点(或方形)大小将它们 *** 作到正确的位置.@H_301_8@
@H_301_8@
protected String getPointVertexShader(){ // define a simple shader program for our points. final String pointVertexShader = "uniform vec2 u_pointSize; + "uniform mat4 u_MVPMatrix; \n" + "attribute vec4 a_position; \n" + "attribute vec2 a_TexCoordinate; \n" + "varying vec2 v_TexCoordinate; \n" // Passed into the fragment shader. + "voID main() \n" + "{ \n" + " v_TexCoordinate = a_TexCoordinate; \n" // Pass through the texture coordinate. + " gl_position = u_MVPMatrix * a_position; \n" // gl_position is a special variable used to store the final position. + " gl_position += vec4(gl_position.w * u_pointSize * (a_TexCoordinate - vec2(0.5,0.5)),0);\n" + "} \n"; return pointVertexShader;}protected String getPointFragmentShader(){ final String pointFragmentShader = "precision mediump float; \n" + "uniform sampler2D u_Texture; \n" // The input texture. + "varying vec2 v_TexCoordinate;\n" // Interpolated texture coordinate per fragment. + "voID main() \n" // The entry point for our fragment shader. + "{ \n" + " gl_Fragcolor = (texture2D(u_Texture,v_TexCoordinate));\n" // Pass the color directly through the pipeline. + "} \n"; return pointFragmentShader;}
请注意,u_pointSize是规范化设备坐标中的vec2;该值应为以像素为单位的大小除以视口大小(以像素为单位).@H_301_8@
下面是将数据传递到着色器并进行绘制的函数.@H_301_8@
@H_301_8@
private voID drawTexturedPoint(final floatBuffer geometryBuffer){ //GeometryBuffer holds all the points in one buffer. GLES20.gluseProgram(mPointsProgramHandle); mPointSizeHandle = GLES20.glGetAttribLocation(mPointsProgramHandle,"u_pointSize"); mPointMVPMatrixHandle = GLES20.glGetUniformlocation(mPointsProgramHandle,"u_MVPMatrix"); mTextureUniformHandle = GLES20.glGetUniformlocation(mPointsProgramHandle,"u_Texture"); mPointpositionHandle = GLES20.glGetAttribLocation(mPointsProgramHandle,"a_position"); mTextureCoordinateHandle = GLES20.glGetAttribLocation(mPointsProgramHandle,"a_TexCoordinate"); // Pass in the texture coordinate information mPointSize.position(0); GLES20.glVertexAttribPointer(mPointSizeHandle,mVec2DataSize,GLES20.GL_float,false,mPointSize); GLES20.glEnabLevertexAttribarray(mPointSizeHandle); // Pass in the position information geometryBuffer.position(0); GLES20.glVertexAttribPointer(mPointpositionHandle,mpositionDataSize,mpositionfloatStrIDeBytes,geometryBuffer); GLES20.glEnabLevertexAttribarray(mPointpositionHandle); // Pass in the texture coordinate information mSquareTextureCoordinates.position(0); GLES20.glVertexAttribPointer(mTextureCoordinateHandle,mSquareTextureCoordinates); GLES20.glEnabLevertexAttribarray(mTextureCoordinateHandle); GLES20.gluniformMatrix4fv(mPointMVPMatrixHandle,1,mMVPMatrix,0); // Draw the cube. GLES20.glDrawArrays(GLES20.GL_TRIANGLES,geometryBuffer.capacity()/mpositionDataSize); }
以下是绘制函数中使用的一些其他相关变量@H_301_8@
@H_301_8@
private final int mBytesPerfloat = 4; private final int mpositionOffset = 0;private final int mpositionDataSize = 3;private final int mpositionfloatStrIDeBytes = mpositionDataSize * mBytesPerfloat;private floatBuffer mPointSize;private final int mVec2DataSize = 2;
//纹理坐标数据.@H_301_8@
@H_301_8@
final float[] squareTextureCoordinateData ={ // Front face 0.0f,0.0f,1.0f,0.0f};
这是我设置方形大小的方式(现在硬编码).@H_301_8@
@H_301_8@
float psize = 25f/480f;mPointSize.position(0);mPointSize.put(psize);mPointSize.put(psize);mPointSize.flip();
最有帮助的一些!@H_301_8@
[编辑]
@ user2359247好的我明白了你的意思,我会保持它的统一,所以我改变了:
mPointSizeHandle = GLES20.glGetUniformlocation(mPointsProgramHandle,“u_pointSize”);@H_301_8@
但是不太确定如何通过缓冲区,我之前没有遇到过这个问题.@H_301_8@
另一个友好的问题是,你明白我想要完成什么,我只是问,因为我想知道我的mPointSize缓冲区中是否有正确的数据?
将点作为纹理方块渲染的解决方案来自其他人,因此我试图将其拼凑在一起.
所以我真的不明白如何设置磅值变量值或应该使用什么类型的函数将它传递到着色器:@H_301_8@
@H_301_8@
u_pointSize is a vec2 in normalised device coordinates; the value
should be the size in pixels divIDed by the vIEwport size in pixels.@H_301_8@
尝试交换:@H_301_8@
@H_301_8@
mPointSize.position(0);GLES20.glVertexAttribPointer(mPointSizeHandle,mPointSize); //Error code gets sent back after this line.GLES20.glEnabLevertexAttribarray(mPointSizeHandle);
同@H_301_8@
@H_301_8@
GLES20.gluniform2fv(mPointSizeHandle,mPointSize);
这是目前的表面:@H_301_8@
应该更像这个模型:@H_301_8@解决方法 在点大小统一时,您正尝试获取属性位置:
@H_301_8@
@H_301_8@
final String pointVertexShader ="uniform vec2 u_pointSize;
…@H_301_8@
@H_301_8@
mPointSizeHandle = GLES20.glGetAttribLocation(mPointsProgramHandle,"u_pointSize");
将点大小更改为属性或使用glGetUniformlocation.@H_301_8@ 总结
以上是内存溢出为你收集整理的如何将正确的数据传递给OpenGL-ES 2.0着色器程序Android全部内容,希望文章能够帮你解决如何将正确的数据传递给OpenGL-ES 2.0着色器程序Android所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)