opengl es着色器原理和过程

本文详细介绍了OpenGL ES中着色器的工作原理,包括顶点着色器和片段着色器的角色。通过加载和编译着色器代码,创建着色器对象,并将它们链接成程序对象进行使用。在Android环境下,通过ShaderHelper类实现编译、链接以及验证着色器程序,并在AirHockeyRenderer中展示了如何应用着色器进行图形绘制。
摘要由CSDN通过智能技术生成

上一篇文章具体参考上文:
Android上Java程序和Opengl通信方式和opengl es着色器

着色器原理:

我们之前多次介绍过OpenGL里面图形都是通过顶点着色器和片段着色器共同完成的,顶点着色器计算每个顶点在屏幕上的最终位置,OpenGL把这些顶点组装成点,直线,三角形并且分解成片段,会询问片段着色器每个片段的最终颜色,如果没有顶点着色器OpenGL就不知道在哪绘制图形,如果没有片段着色器就不知道要怎么绘制组成图形的点,直线,三角形的片段,所以他们总是一起工作的,最终一起合成屏幕上的一幅图像。

上文中我们已经编写了两个着色器,simple_vertex_shader.glsl和simple_fragment_shader.glsl,本文将具体讲解如何编译和使用。

加载着色器:

打开TextResourceReader类代码如下:

/***
 * Excerpted from "OpenGL ES for Android",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/kbogla for more book information.
***/
package opengl.timothy.net.openglesproject_lesson2.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.content.Context;
import android.content.res.Resources;

public class TextResourceReader {
   
    /**
     * Reads in text from a resource file and returns a String containing the
     * text.
     */
    public static String readTextFileFromResource(Context context,
        int resourceId) {
        StringBuilder body = new StringBuilder();

        try {
            InputStream inputStream = 
                context.getResources().openRawResource(resourceId);
            InputStreamReader inputStreamReader = 
                new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            String nextLine;

            while ((nextLine = bufferedReader.readLine()) != null) {
                body.append(nextLine);
                body.append('\n');
            }
        } catch (IOException e) {
            throw 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值