简体   繁体   English

适用于Android的OpenGL ES 2.0中的着色器

[英]Shaders in OpenGL ES 2.0 for Android

I want to make some AR stuff. 我想做一些AR东西。 I walked through several steps of some tutorials like 我完成了一些教程的一些步骤,例如

I am Using OpenGL ES 2.0. 我正在使用OpenGL ES 2.0。 Especially in the first tutorial they implement some GLSL shader code for the fragment and the vertex shader. 特别是在第一个教程中,他们为片段和顶点着色器实现了GLSL着色器代码。 And then they compile it. 然后他们编译它。

Do I need to implement such code for every primitive object I want to draw with OpenGL? 我需要为要使用OpenGL绘制的每个原始对象实现此类代码吗? Or can I reuse shader code for drawing different types of shapes and different instances of the same type of shape? 还是可以重用着色器代码来绘制不同类型的形状以及相同类型的不同实例?

Furthermore: Can I only reuse shader code or can I also reuse a compiled shader program? 此外:我只能重用着色器代码还是可以重用已编译的着色器程序?

Reusing the same shader for several geometries is a common way of improving performance as they will be treated as a single draw-call. 对几种几何形状重复使用相同的着色器是提高性能的一种常用方法,因为它们将被视为一次绘制调用。 If you set the shader (glUseProgram) it will be available to any number of subsequent calls. 如果设置了着色器(glUseProgram),它将可用于任意数量的后续调用。

The tutorials are very basic and you should abstract the shader code into a more object oriented approach. 这些教程非常基础,您应该将着色器代码抽象为一种更加面向对象的方法。 For example: 例如:

public class Material{
   String mVertexShaderCode;
   String mFragmentShaderCode;
   int mProgram;

   void initialize(){
      // do loadShader and attachShader here
   }

   void draw(){
     GLES20.glUseProgram(mProgram);
     // do more draw stuff
   }
}

Maybe this makes it easier to wrap you head around how you can use and reuse the shader code. 也许这使您更容易了解如何使用和重用着色器代码。

To answer the follow-up question; 回答后续问题; Yes you can reuse both. 是的,您可以重用两者。 The limitation is that you can't use different shader specific attributes if you also reuse the compiled shader. 限制是,如果您还重复使用已编译的着色器,则不能使用不同的着色器特定属性。 The limitations will become obvious as you start using them. 当您开始使用它们时,这些限制将变得显而易见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM