草庐IT

java - 如何将正确的数据传递给 Android OpenGL-ES 2.0 着色器程序

coder 2023-12-23 原文

我有一个渲染器,它试图将点绘制为带纹理的正方形。没有任何崩溃,我还可以很好地绘制其他项目,但是没有渲染这些方 block ,我相信这与在我的 drawTexturedPoint() 函数中传递给着色器程序的数据有关。

我有一个保存顶点位置的 FloatBuffer geometryBuffer。具有完全相同顶点坐标的 6 个顶点,一个用于两个三角形的每个角。此缓冲区内有多个点。

着色器程序获取这些顶点并根据传递给着色器的点(或正方形)大小将它们操纵到正确的位置。

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, 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;该值应该是大小(以像素为单位)除以视口(viewport)大小(以像素为单位)。

下面是将数据传递给着色器并进行绘制的函数。

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, 0, mPointSize);

    GLES20.glEnableVertexAttribArray(mPointSizeHandle);

    // Pass in the position information
    geometryBuffer.position(0);
    GLES20.glVertexAttribPointer(mPointPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false, mPositionFloatStrideBytes, geometryBuffer);

    GLES20.glEnableVertexAttribArray(mPointPositionHandle);

    // Pass in the texture coordinate information
    mSquareTextureCoordinates.position(0);
    GLES20.glVertexAttribPointer(mTextureCoordinateHandle, mVec2DataSize, GLES20.GL_FLOAT, false, 0, mSquareTextureCoordinates);

    GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle);


    GLES20.glUniformMatrix4fv(mPointMVPMatrixHandle, 1, false, mMVPMatrix, 0);

    // Draw the cube.
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, geometryBuffer.capacity()/mPositionDataSize);                               
}

这里是绘图函数中使用的其他一些相关变量

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;

//纹理坐标数据。

final float[] squareTextureCoordinateData =
{
    // Front face
    0.0f, 0.0f,                 
    0.0f, 1.0f,
    1.0f, 0.0f,
    0.0f, 1.0f,
    1.0f, 1.0f,
    1.0f, 0.0f
};

这是我设置正方形大小的方式(现在是硬编码的)。

float psize = 25f/480f;
mPointSize.position(0);
mPointSize.put(psize);
mPointSize.put(psize);
mPointSize.flip();

不胜感激!

[编辑] @user2359247 好的,我明白你的意思了,我会保持统一,所以我改变了: mPointSizeHandle = GLES20.glGetUniformLocation(mPointsProgramHandle, "u_pointSize");

不太确定如何传递缓冲区,但是我以前没有遇到过这个。

另一个友好的问题是,你明白我想要完成的事情吗,我只是在想知道我的 mPointSize 缓冲区中是否有正确的数据时才问? 这个将点渲染为纹理正方形的解决方案来自其他人,所以我正在尝试将其拼凑起来。

所以我真的不明白如何设置点大小变量值或者应该使用什么类型的函数将它传递给着色器:

u_pointSize is a vec2 in normalised device coordinates; the value should be the size in pixels divided by the viewport size in pixels.

尝试交换:

mPointSize.position(0);
GLES20.glVertexAttribPointer(mPointSizeHandle, mVec2DataSize, GLES20.GL_FLOAT, false, 0, mPointSize); //Error code gets sent back after this line.
GLES20.glEnableVertexAttribArray(mPointSizeHandle);

GLES20.glUniform2fv(mPointSizeHandle, 1, mPointSize);

这是目前的表面:
应该更像这个模型:

最佳答案

您试图在点大小统一的情况下获取属性位置:

final String pointVertexShader =
"uniform vec2 u_pointSize;  

...

mPointSizeHandle = GLES20.glGetAttribLocation(mPointsProgramHandle, "u_pointSize");

将点大小更改为属性或使用 glGetUniformLocation。

关于java - 如何将正确的数据传递给 Android OpenGL-ES 2.0 着色器程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18476572/

有关java - 如何将正确的数据传递给 Android OpenGL-ES 2.0 着色器程序的更多相关文章

  1. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  2. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  3. ruby - Ruby 有 `Pair` 数据类型吗? - 2

    有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳

  4. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  5. java - 我的模型类或其他类中应该有逻辑吗 - 2

    我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我

  6. ruby-on-rails - 如何在 Ruby on Rails 中实现由 JSF 2.0 (Primefaces) 驱动的 UI 魔法 - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭10年前。问题1)我想知道ruby​​onrails是否有功能类似于primefaces的gem。我问的原因是如果您使用primefaces(http://www.primefaces.org/showcase-labs/ui/home.jsf),开发人员无需担心javascript或jquery的东西。据我所知,JSF是一个规范,基于规范的各种可用实现,prim

  7. java - 什么相当于 ruby​​ 的 rack 或 python 的 Java wsgi? - 2

    什么是ruby​​的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht

  8. ruby - 我如何添加二进制数据来遏制 POST - 2

    我正在尝试使用Curbgem执行以下POST以解析云curl-XPOST\-H"X-Parse-Application-Id:PARSE_APP_ID"\-H"X-Parse-REST-API-Key:PARSE_API_KEY"\-H"Content-Type:image/jpeg"\--data-binary'@myPicture.jpg'\https://api.parse.com/1/files/pic.jpg用这个:curl=Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")curl.multipart_form_

  9. 世界前沿3D开发引擎HOOPS全面讲解——集3D数据读取、3D图形渲染、3D数据发布于一体的全新3D应用开发工具 - 2

    无论您是想搭建桌面端、WEB端或者移动端APP应用,HOOPSPlatform组件都可以为您提供弹性的3D集成架构,同时,由工业领域3D技术专家组成的HOOPS技术团队也能为您提供技术支持服务。如果您的客户期望有一种在多个平台(桌面/WEB/APP,而且某些客户端是“瘦”客户端)快速、方便地将数据接入到3D应用系统的解决方案,并且当访问数据时,在各个平台上的性能和用户体验保持一致,HOOPSPlatform将帮助您完成。利用HOOPSPlatform,您可以开发在任何环境下的3D基础应用架构。HOOPSPlatform可以帮您打造3D创新型产品,HOOPSSDK包含的技术有:快速且准确的CAD

  10. FOHEART H1数据手套驱动Optitrack光学动捕双手运动(Unity3D) - 2

    本教程将在Unity3D中混合Optitrack与数据手套的数据流,在人体运动的基础上,添加双手手指部分的运动。双手手背的角度仍由Optitrack提供,数据手套提供双手手指的角度。 01  客户端软件分别安装MotiveBody与MotionVenus并校准人体与数据手套。MotiveBodyMotionVenus数据手套使用、校准流程参照:https://gitee.com/foheart_1/foheart-h1-data-summary.git02  数据转发打开MotiveBody软件的Streaming,开始向Unity3D广播数据;MotionVenus中设置->选项选择Unit

随机推荐