使用之前的方法写Shader是一件很痛苦的事情,把Shader代码直接卸载C++文件中,需要使用很多引号来包裹,既不美观也不方便。

我们这节的目的是使用纯文本文件保存Shader。

首先在工程中创建两个文件,分别命名为VertexShaderCode.glsl 和 FragmentShaderCode.glsl,后缀名可以自己随意指定,不一定非要是glsl。

然后把上节的Shader代码拷贝到两个文件中去。

VertexShaderCode.glsl

  1. #version
  2.  
  3. in layout(location=) vec2 position;
  4. in layout(location=) vec3 vertexColor;
  5.  
  6. out vec3 passingColor;
  7.  
  8. void main()
  9. {
  10. gl_Position= vec4(position,0.0,1.0);
  11. passingColor= vertexColor;
  12. }

FragmentShaderCode.glsl

  1. #version
  2.  
  3. in vec3 passingColor;
  4. out vec4 finalColor;
  5.  
  6. void main()
  7. {
  8. finalColor = vec4(passingColor,1.0);
  9. }

另外在MyGlWindow中添加一个函数用来读取文件

MyGlWindow.h中添加include:

  1. #include <string>

添加成员函数声明:

  1. std::string ReadShaderCode(const char* fileName);

MyGlWindow.cpp中添加include:

  1. #include <iostream>
  2. #include <fstream>

添加成员函数定义:

  1. std::string MyGlWindow::ReadShaderCode(const char* fileName)
  2. {
  3. std::ifstream myInput(fileName);
  4. if (!myInput.good())
  5. {
  6. std::cout << "File failed to load..." << fileName;
  7. exit();
  8. }
  9. return std::string(
  10. std::istreambuf_iterator<char>(myInput),
  11. std::istreambuf_iterator<char>());
  12. }

删除掉开头的两个extern声明:

  1. //extern const char* vertexShaderCode;
  2. //extern const char* fragmentShaderCode;

修改installShaders()函数:

  1. void MyGlWindow::installShaders()
  2. {
  3. GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
  4. GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
  5.  
  6. std::string tmp = ReadShaderCode("VertexShaderCode.glsl");
  7. const char* vertexShaderCode = tmp.c_str();
  8. glShaderSource(vertexShaderID, , &vertexShaderCode, );
  9.  
  10. tmp = ReadShaderCode("FragmentShaderCode.glsl");
  11. const char* fragmentShaderCode = tmp.c_str();
  12. glShaderSource(fragmentShaderID, , &fragmentShaderCode, );
  13.  
  14. glCompileShader(vertexShaderID);
  15. glCompileShader(fragmentShaderID);
  16.  
  17. GLuint programID = glCreateProgram();
  18. glAttachShader(programID, vertexShaderID);
  19. glAttachShader(programID, fragmentShaderID);
  20.  
  21. glLinkProgram(programID);
  22.  
  23. glUseProgram(programID);
  24. }

注意第6-7行,先用一个string对象存储读取到的字符串,然后使用.c_str()返回C语言风格字符串 const char*。

3D Computer Grapihcs Using OpenGL - 08 Text File Shaders的更多相关文章

  1. 3D Computer Grapihcs Using OpenGL - 19 Vertex Array Object(顶点数组对象)

    大部分OpenGL教程都会在一开始就讲解VAO,但是该教程的作者认为这是很不合理的,因为要理解它的作用需要建立在我们此前学过的知识基础上.因此直到教程已经进行了一大半,作者才引入VAO这个概念.在我看 ...

  2. 3D Computer Grapihcs Using OpenGL - 11 Model View Projection Matrices

    本节我们将绘制一个3维物体,立方体. 如果要渲染3D物体,我们需要了解MVP(Model View Projection),它表示三个转换矩阵.实际上这个名字不够明确,更加确切的释义如下: Model ...

  3. 3D Computer Grapihcs Using OpenGL - 09 Enable Depth Test

    启用Depth Test OpenGL是个3D绘图API,也就是说不只有xy坐标轴,还有第三个坐标轴z,z轴的方向是垂直于屏幕,指向屏幕内. 靠近人眼的方向是负方向,标准化设备坐标的最小值是-1, 最 ...

  4. 3D Computer Grapihcs Using OpenGL - 17 添加相机(旋转)

    在11节我们说过,MVP矩阵中目前只应用了两个矩阵,World to View 矩阵被省略了,这就导致我们的画面没有办法转换视角. 本节我们将添加这一环节,让相机可以旋转. 为了实现这一目的,我们添加 ...

  5. 3D Computer Grapihcs Using OpenGL - 16 使用DrawElementsInstanced绘制立方体

    我们使用15节学到的知识来绘制14节的立方体. 在第14节我们使用了两次glDrawElements实现了OpenGL实例化,发现这样仍然不太方便,如果需要绘制成千上万的立方体,就需要手写成千上万次的 ...

  6. 3D Computer Grapihcs Using OpenGL - 15 Draw Element Instanced

    友情提示:继续本节之前,需要保存此前的代码,本节为了试验,会对代码做一些修改,但后续的修改需要我们把代码返回之前的进度. OpenGL内置支持Instancing,有专门的函数来处理这件事情. 为了方 ...

  7. 3D Computer Grapihcs Using OpenGL - 14 OpenGL Instancing

    如果我们需要绘制两个(或者多个)一样的立方体(或者物体),只是位置.缩放.旋转不一样,那么我们可以不需要多次将这个物体的顶点信息.颜色信息等发送到显卡,而是发送一次,绘制多次,仅仅是每次绘制之前应用不 ...

  8. 3D Computer Grapihcs Using OpenGL - 10 Color Buffer

    本节我们将尝试利用三角形制作一个“走马灯”效果. 一个三角形如图示方式,从左向右依次移动. 先看一下代码: MyGlWindow.cpp #include <gl\glew.h> #inc ...

  9. 3D Computer Grapihcs Using OpenGL - 06 Vertex and Fragment Shaders

    从这里就接触到了可编程图形渲染管线. 下面介绍使用Vertex Shader (顶点着色器)和 Fragment Shader(像素着色器)的方法. 我们的目标是使用这两个着色器给三角形填充绿色. 添 ...

随机推荐

  1. spring @Value 获取配置文件为 null 常见的几种方式

    第一种方式: xx.properties 属性名称错误,未与@Value("${xxx}") 进行对应 第二种方式: 该类未注入到spring bean容器中 @Component ...

  2. CM使用MySQL数据库预处理scm_prepare_database.sh执行报错:java.sql.SQLException: Access denied for user 'scm'@'hadoop101.com' (using password: YES)

    1.报错提示: [root@hadoop101 ~]# /opt/module/cm/cm-/share/cmf/schema/scm_prepare_database.sh mysql cm -hh ...

  3. 洛谷 P2196 挖地雷 & [NOIP1996提高组](搜索,记录路径)

    传送门 解题思路 就是暴力!!! 没什么好说的,总之,就是枚举每一个起点,然后暴力算一遍以这个点为起点的所有路径,在算的过程中,只要比目前找到的答案更优,就有可能是最后的答案,于是就把路径更新一遍,保 ...

  4. pureftp安装

    1.下载 #cd /usr/local/src #wget http://download.pureftpd.org/pub/pure-ftpd/releases/pure-ftpd-1.0.36.t ...

  5. Consul服务发现在windows下简单使用

    目录 基本介绍: 服务连接: 客户端: 系列章节: 回到顶部 基本介绍: 安装: 下载地址:https://www.consul.io/downloads.html 运行: consul agent ...

  6. 2017第二届广东省强网杯线上赛--Nonstandard

    测试文件:http://static2.ichunqiu.com/icq/resources/fileupload/CTF/echunqiu/qwb/Nonstandard_26195e1832795 ...

  7. 关于原型链,原来这么简单?—————终结__proto__和prototype的那些事

    今天,一个技术群里小朋友提出一个问题: Object.prototype.a = function () { console.log('a') } Function.prototype.b = fun ...

  8. 纯手写实现ajax分页功能

    前言 最近用到了这个功能,百度大半天,网上的不是有各种问题就是需要引入其他的插件,无奈,只能自己写,大致功能已经完成.前端页面用bootstrap做样式,分页功能用ajax实现,没用其他插件哦,只引入 ...

  9. python 快速排序实现

    # -*- coding: utf-8 -*- def quicksort(array): # 基线条件:为空或只包含一个元素的数组是"有序"的 if len(array) < ...

  10. MySQL之表查询

    语法执行顺序 from >>>从那张表 where >>> 全局的筛选条件 group by>>> 必须是用在where 之后一组就是为了接下来我 ...