3D Computer Grapihcs Using OpenGL - 08 Text File Shaders

來源:互聯網
上載者:User

標籤:glsl   const   刪除   create   extern c   win   對象   風格   pil   

使用之前的方法寫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行,先用一個stringObject Storage Service讀取到的字串,然後使用.c_str()返回C語言風格字串 const char*。

3D Computer Grapihcs Using OpenGL - 08 Text File Shaders

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.