3D Computer Grapihcs Using OpenGL - 08 Text File Shaders

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

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

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

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

VertexShaderCode.glsl

 1 #version 430
 2
 3 in layout(location=0) vec2 position;
 4 in layout(location=1) 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 430
 2
 3 in vec3 passingColor;
 4 out vec4 finalColor;
 5
 6
 7 void main()
 8 {
 9   finalColor = vec4(passingColor,1.0);
10 }         

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

MyGlWindow.h中添加include:

#include <string>

添加成员函数声明:

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

MyGlWindow.cpp中添加include:

#include <iostream>
#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(1);
 8     }
 9     return std::string(
10         std::istreambuf_iterator<char>(myInput),
11         std::istreambuf_iterator<char>());
12 }

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

//extern const char* vertexShaderCode;
//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, 1, &vertexShaderCode, 0);
 9
10     tmp = ReadShaderCode("FragmentShaderCode.glsl");
11     const char* fragmentShaderCode = tmp.c_str();
12     glShaderSource(fragmentShaderID, 1, &fragmentShaderCode, 0);
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*。

原文地址:https://www.cnblogs.com/AnKen/p/8338023.html

时间: 2024-10-14 20:11:23

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

3D Computer Grapihcs Using OpenGL - 05 EBO

本节将采用两种方法绘制两个三角形. 先看第一种方法的代码 MyGlWindow.cpp 1 #include <gl\glew.h> 2 #include "MyGlWindow.h" 3 4 void MyGlWindow::initializeGL() 5 { 6 glewInit(); 7 8 GLfloat verts[] = 9 { 10 +0.0f, +0.0f, 11 +1.0f, +1.0f, 12 -1.0f, +1.0f, 13 14 +0.0f, +0

3D Computer Grapihcs Using OpenGL - 10 Color Buffer

本节我们将尝试利用三角形制作一个"走马灯"效果. 一个三角形如图示方式,从左向右依次移动. 先看一下代码: MyGlWindow.cpp 1 #include <gl\glew.h> 2 #include "MyGlWindow.h" 3 #include <iostream> 4 #include <fstream> 5 6 float triangleWidth = 0.1f; 7 float bytesPerTriangle

3D Computer Grapihcs Using OpenGL - 07 Passing Data from Vertex to Fragment Shader

上节的最后我们实现了两个绿色的三角形,而绿色是直接在Fragment Shader中指定的. 这节我们将为这两个三角形进行更加自由的着色--五个顶点各自使用不同的颜色. 要实现这个目的,我们分两步进行,首先 在顶点数组里增加数据用来表示颜色 修改sendDataToOpenGL()函数中的verts数组: 1 GLfloat verts[] = 2 { 3 +0.0f, +0.0f, //Vertex 0 4 +1.0, +0.0, +0.0f, //Color 0 5 +1.0f, +1.0f

3D Computer Grapihcs Using OpenGL - 20 结合Buffer

在上一节的案例中,我们使用了四个Buffer Object,立方体的VertexBuffer,立方体的索引Buffer,四面体的VertexBuffer,四面体的索引Buffer. 我们这节尝试把两个图形的Vertex Buffer结合,两个图形的索引Buffer结合,形成两个Buffer,让程序更加简化. 先看最终代码: MyGlWindow.cpp: 1 #include <gl\glew.h> 2 #include "MyGlWindow.h" 3 #include

What is a text file and what is a binary file :)

If you are not coming from a programming background it might not yet be clear what is really a file? What is a binary file and what makes something a text file? 其实总归一句话:[Files that consist exclusively of ASCII characters are known as text ?les. All o

create feature from text file

'''---------------------------------------------------------------------------------- Tool Name: CreateFeaturesFromTextFile Source Name: CreateFeaturesFromTextFile.py Version: ArcGIS 9.1 Author: Environmental Systems Research Institute Inc. Required

shell脚本执行时报&quot;bad interpreter: Text file busy&quot;的解决方法

在执行一个shell脚本时,遇到了"-bash: ./killSession.sh: /bin/bash: bad interpreter: Text file busy"错误提示,如下所示: [[email protected] bin]$ ./killSession.sh      -bash: ./killSession.sh: /bin/bash: bad interpreter: Text file busy 此时只需要在#!/bin/bash,加一空格#! /bin/bas

Writing Text File From A Tabular Block In Oracle Forms

The example given below for writing text file or CSV using Text_IO package from a tabular block in Oracle Forms. Suppose there is a tabular grid data block "Job_History" in your forms and you want to write a CSV on click of a button by reading w

SharePoint 2010: Export User Profile Properties to a Text File or Excel using PowerShell

导出到txt [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server") [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.UserProfiles") [void][System.Reflection.Assembly]::LoadWithParti