OpenGL ES for Android(几何着色器)

本文介绍了OpenGL ES 3.2中的几何着色器,展示了如何在Android上使用几何着色器进行图形绘制,包括绘制点、线、房子和实现爆炸效果。通过实例详细解释了几何着色器的工作原理和代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简介

几何着色器(Geometry Shader)是一个可选功能,他介于在顶点和片段着色器之间,接收一组顶点数据,可以对数据进行处理,而且可以根据数据生成不止一个图形,假如你想绘制四个顶点,按照以前的方式,需要for循环四次,每次都顶点数据进行处理,最后传入顶点着色器中。而集合着色器就做了这样一件事。在移动平台上,几何着色器需要OpenGL ES 3.2版本(android 7之上),同样的我们可以先参考文档或其他相关资料学习3.0新特性。

3.0变化

这里简单学习一下3.0和2.0比较重要的变化

  • attribute和varying,取而代之的是 in和out

  • 文件需要添加#version 300 es (如果是3.2则是320,不加则默认2.0)

  • 还有纹理 texture2D和texture3D统统改为 texture

  • 内置函数gl_FragColor和gl_FragData删除,如果片段着色器要输出用out声明字段输出。不过保留了gl_Position

  • 还有的是layout的作用:可以直接指定位置

  // opengl 2.0
  uniform  float intensity;
  // 代码 赋值
  GLES20.glUniform1f(GLES20.glGetAttribLocation(program, 
  "intensity"), 1f)
  //opengl 3.0
  layout (location = 1) uniform  float intensity;
  //直接写上对应的layout的值就可以赋值
  GLES30.glUniform1f(1,1f)

几何着色器

绘制点

先看一个几何着色器的例子:

  #version 320 es
  layout (points) in; // 输入
  layout (points, max_vertices = 4) out; //输出

  in VS_OUT {
      vec3 color;
  } gs_in[];

  out vec3 fColor;
  void build_point(vec4 position);

  void main() {
      build_point(gl_in[0].gl_Position);
  }

  void build_point(vec4 position){
      fColor = gs_in[0].color;
      gl_Position = position + vec4(-0.5, 0.5, 0.0, 0.0);// 1:左上角
      gl_PointSize = 20.0;
      EmitVertex();
      gl_Position = position + vec4(0.5, 0.5, 0.0, 0.0);// 2:右上角
      EmitVertex();
      gl_Position = position + vec4(-0.5, -0.5, 0.0, 0.0);// 3:左下角
      gl_PointSize = 10.0;
      EmitVertex();
      gl_Position = position + vec4(0.5, -0.5, 0.0, 0.0);// 4:右下角
      fColor = vec3(1.0, 1.0, 1.0);
      EmitVertex();
      EndPrimitive();
  }

这个几何着色器的作用是:输入一个顶点的数据,输出四个顶点显示在屏幕上;前两个点大小为20,后两个点大小为10

In Pro OpenGL ES for Android, you'll find out how to harness the full power of OpenGL ES, and design your own 3D applications by building a fully-functional 3D solar system model using Open GL ES! OpenGL has set the standard for 3D computer graphics, and is an essential aspect of Android development. This book offers everything you need to know, from basic mathematical concepts to advanced coding techniques. You'll learn by building a fascinating 3D solar system simulator! After introducing Open GL ES, Pro OpenGL ES for Android explains the basics of 3D math and then orients you to the native Android 3D libraries you'll be using in your own 3D games and the solar system project you'll build using this book. Through the solar system example project, you'll learn how to incorporate a variety of graphic and animation techniques into your applications. You will also discover how the full spectrum of 3D development that awaits, with topics such as lighting, texture-mapping, modeling, shaders, blending modes, and several more advanced concepts. By the time you finish Pro OpenGL ES for Android, you'll have learned all the skills you'll need to build your own incredible 3D applications, based on one of the most powerful 3D libraries available. What you'll learn * The basics of 3D mathematics, and how they are applied in the OpenGL library * How to design and build your 3D worlds * To create 2D interfaces within the 3D world * To develop animation and 3D movement * How to implement 3D shading, coloring, and texturing * The differences between OpenGL and other 3D toolkits * To build a fully-functional 3D solar system simulator using OpenGL ES Who this book is for Experienced Android programmers who want to enter the 3D world of OpenGL ES programming. Table of Contents * Introduction to OpenGL ES and Our 3D Solar System Project * Generating a Basic OpenGL Program * Getting Past the 3D Math * Shading, Lighting and Colors * Materials and Textures * Animation * Creating a User Interface * Blending Modes, Buffer Objects, and Other Cool Stuff * Latest Features of OpenGL ES * Ray Tracing, Hidden Surfaces, and Other Advanced Topics Appendix A: APIs
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值