顯示具有 OpenGL Learning 標籤的文章。 顯示所有文章
顯示具有 OpenGL Learning 標籤的文章。 顯示所有文章

2012年11月29日 星期四

OpenGL - programmable Pipeline, GLSL 學習(3) Shading Methods

(11/26)

看了兩個tutorial在實作GLSL shading 的方法
1.glslcookbook
2.opengl-tutorial

結果兩個在vs fs的地方有出入:

opengl官方直接在 vs 裡面算完 shading,再把lightcolor 丟給 fs,

而glslcookbook 則是在vs裡面做一次導向後,實際在fs裡面實作。

這兩個方法的差異,在cookbook p61有寫:

這是Per-vertex & Per-fragment的問題,Per-vertex 缺點是只有在vertex上lighting,
有可能會在faces rendering時,少掉整個faces 上面每個點 specular light 的細節
變成所謂的Gourand shading

所以在最後fs再算才會是Phong Shading!!

-------------------------------------------------------------------------------------------------------------
GLSL Shading Methods

1.Flat Shading

in main:
    //set color of the polygon which uses flat shading to be the color of the first vertex.
    glProvokingVertex(GL_FIRST_VERTEX_CONVENTION);

in Shader.vs :

    //adding the 'flat' qualifier indicates that no interpolation of values is to be done before reaching fragment shader!
    //也就是vs裡面甚麼也不做!
    flat out vec3 LightingIntensity;


in Shader.fs :

    flat in vec3 LightingIntensity;


 tbc....

OpenGL - programmable Pipeline, GLSL 學習(2) Switching Shaders

如何在一個程式內使用不同的shader的方法 : 
 http://pouet.net/topic.php?which=6164


簡而言之就是在主程式內創造多個program object,
每個各自有著不同的shader連接於其上,之後要轉換使用各種不同的shader的話就用

glUseProgram(Gluint programID)

依據keyboard input來切換不同的shader



(至於如何連接shader至program obj,我使用tutorial的shader.cpp 的code來講解

/*************************************************************************************************/
//Read the notation first!! It's just a fragment of loading shader, not a full example.
//first load the shader text in, and regard it as c string
    char const * VertexSourcePointer = VertexShaderCode.c_str();

//create shader obj and compile it
    glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
    glCompileShader(VertexShaderID);

// Check Vertex Shader Compile error
    glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
    glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
    std::vector<char> VertexShaderErrorMessage(InfoLogLength);
    glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
    fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]);
//end checking compile error

// Link shader to the program
    fprintf(stdout, "Linking program\n");
    GLuint ProgramID = glCreateProgram();
    glAttachShader(ProgramID, VertexShaderID);
    glAttachShader(ProgramID, FragmentShaderID);
    glLinkProgram(ProgramID);

// Check the program
    glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
    glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
    std::vector<char> ProgramErrorMessage( max(InfoLogLength, int(1)) );
    glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
    fprintf(stdout, "%s\n", &ProgramErrorMessage[0]);
//....後續還有

/*************************************************************************************************


2012年11月25日 星期日

OpenGL - Environment setting 環境設定

11/24 Day2

安裝環境

找了很多網路上面的教學 還是官方比較好 雖然他講的很籠統= =
http://glew.sourceforge.net/install.html

GL RENDERER = GeForce GT 240/PCIe/SSE2
GL VENDOR = NVIDIA Corporation
GL VERSION = 3.3.0
GLSL's Version = 3.30 NVIDIA via Cg compiler

以上為我電腦的環境資料;Coding IDE使用M$ Visual Studio 2010

*在安裝環境之前有先從Nvidia官網下載 update GPU driver。

方法:

 [首先放lib檔]

1. 下載 freeglut , glew32 binary 檔 (都是要32 bit)

2. 將兩個的bin files放在:

        a. C:\Windows\SysWOW64

        b. %PROGRAMFILES(X86)\VX.X(版本)\lib 兩個中

  /*IMPORTANT : 32bit 的 freeglut.dll 放在SysWOW64中,64版的不行!!!!!!!! */

3. 將兩個的.h 標頭檔放在:

        %PROGRAMFILES(X86)\VX.X\Include\gl 中


這樣所需檔案就放好了,接下來改設定VS2010的環境


----------------------------------------------------------------------------------------------------

[VS2010 Project Properties] 
 以下都在改project的屬性                //不確定的步驟我打上*號

4. VC++ Directories -> Library Directories 增加上

        C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Lib\glew32.lib

        *和C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Lib\
          (這個Macro應該預設就有

*5. /* IMPORTANT!!! */ 11/25補 ((後來編譯發現這步驟不用做= =
   在C/C++ -> Preprocessor -> Preprocessor Definition裡加上

        "GLEW_BUILD" !!!! ((這步驟官網講得頗籠統 還有若是include glew.c file則要加"GLEW_STATIC"

6. linker -> input -> additional dependencies 加上 "glew32.lib freeglut.lib"

 (linker -> general -> show progress 選 display all configurations 可以在compile時於output看到有沒有連到library)

[Include GLM and GLFW
/*GLM (Mathematics) is a header on C++, as an extension of GLSL, */

GLMGLFW相對於上面兩套lib安裝方式我採用直接include 檔案這個較簡單的方法。

直接下載完後將裡面所需的.hpp include 至標頭就大功告成,詳細說明有官網的spec,可以參考看看
(在 project proporties -> VC++ Dir -> Include Dir 增加 .h檔案位置
      project proporties -> C/C++ General -> Additional Include Dir 增加 .hpp檔案位置

以上就是我記錄的步驟


((但目前裝完後幫艾斯後還是有問題 再找時間找我到底還有改哪裡= =||

-------------------------------------------
另外紀錄步驟一下 在嘗試過程有使用glew source pack裡面的 build\VS
四個檔案build完會產生 glew32d.dll 和 glew32d.lib 但我後來把這個刪掉了

還有使用glew binary pack的時候
有執行過bin\glewinfo visualinfo
雖然也不知道會有甚麼影響啦XD

-------------------------------------------

This won't work:
http://stackoverflow.com/questions/4711113/glew-in-vs-2010-unresolved-external-symbol-imp-glewinit

Mainly based on this one:
http://openglbook.com/setting-up-opengl-glew-and-freeglut-in-visual-c/
但有些步驟有問題(像是改成C編譯那部,我只有取其中一些做)



----------------------------------------------------------------------------
[run on linux]

相對簡單很多
先用apt-cache search 需要的lib : libglew, libglew-dev, libglm, .....

glfw要先安裝
http://stackoverflow.com/questions/7139086/static-compile-glfw

主要是在Makefile裡面設定 glm glfw的位置
static library:
http://stackoverflow.com/questions/7132340/static-build-glew-glfw-on-linux 
-L[Path]
-I[Lib]
-lGLEW -lm
  

OpenGL - programmable Pipeline ,GLSL 學習(1)

官方toturial:
http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/

看完畫三角形的教學和老師給的code稍微搞懂

如果要用programmable opengl的話要重寫HW2

附檔裡面使用蠻多deprecated functions


然後他提供的textfile.cpp的用意是

因為之後要自己寫flat ground phong 三種shading

一個shading 法額外存一個GLSL external file (副檔名可為.txt or .glsl, 這不重要

而老師提供的那個就只是幫助我們把他read進來

純粹讀文字的檔案

-----(紀錄一下)

心得: 直接搬運toturial code比較快 =3=

--
待補~

---------------------------------

11/25

Homogeneous Coordinates
    [x,y,z,w]

  • If w == 1, then the vector (x,y,z,1) is a position in space. 
  • If w == 0, then the vector (x,y,z,0) is a direction.

移動 w=0 的direction 沒有意義。(EX: Translate Matrix : dx, dy, dz * w = 0, 等於沒乘)


1.Translation
//in c++ using GLM
glm::mat4 myMatrix = glm::translate(10,0,0);
//GLSL
vec4 transformedVector = myMatrix * myVector;

這個有基本GLSL介紹: 寫得還蠻易懂的
http://www.packtpub.com/article/tips-tricks-getting-started-with-opengl-glsl-4 


12/04 
Compile時候發現 fragment shader附檔名不能叫做.fs ,會跟F#檔案相衝突
(F#不吃Hard Tab)