2015年7月12日 星期日

OpenGL red book(ver4.3) ch1_sample1 in visaul studio c++2013(Traditional Chinese and English)

這篇也是記錄我在搞第一篇設定所遇到的問題的延伸,首先是得確保我所引用的header沒有問題,因此我只先試了
This article continues what I have met in learning OpenGL. Before we start,we must make sure the header files have no bug. So I just tried :

#include<vgl.h>
#include<LoadShaders.h>
using namespace std;

int main(){
return 0;
}

我在vgl.h這邊遇到了不少問題,首先在執行時先跳出了一個錯誤:
I met plenty of problems from vgl.h. At first there was an error when complied.
--------------------------------------------------------------------------------------------------------------------------
error C1189: #error :  Static linking is not supported with this build. Please remove the FREEGLUT_STATIC preprocessor directive, or download the source code from http://freeglut.sf.net/ and build against that. c:\program files (x86)\microsoft visual studio 12.0\vc\include\gl\freeglut_std.h
--------------------------------------------------------------------------------------------------------------------------
這個版本並沒有支援靜態連結(?),不過看問題源頭跟freeglut_std.h有關,所以我檢查了這個header,
Static linking is not supported (?),there seemed had something from freeglut_std.h,so I checked this header file.

裏頭有一行:
I found out that:

/* Windows static library */
#   ifdef FREEGLUT_STATIC

#error Static linking is not supported with this build. Please remove the FREEGLUT_STATIC preprocessor directive, or download the source code from http://freeglut.sf.net/ and build against that.

/* Windows shared library (DLL) */

同時,因為FREEGLUT_STATIC連結不成功,因此之後的FGAPI都會被當成重複定義的語法錯誤。
meanwhile,cos of   FREEGLUT_STATIC unlinked,cause FGAPI as syntax error of redefined.

我當下覺得不對,我明明沒有引用freeglut_std.h,為何程式會call這個header,所以我查了vgl.h,這裡頭重要的資訊在於如何連結lib:
But I did not #include the freeglut_std_h,but how did it call the header file. then I checked vgl.h, this head file responsible for calling lib.
part of vgl.h, that will link to glew lib,written by redbook group

part of vgl.h, that will link to freeglut lib,written by redbook group


我根據vgl.h所#include的header找到問題了,因為#include<GL/freeglut.h>所連結到的是我之前下載來玩的freeglut套件,而非redbook所寫可以跟freeglut_static_vs2010_d.lib互相連結的freeglut_std.h(freeglut是開放式套件,因此有非常多的版本和開發者~~~)
According to the #include in vgl.h,I figured out the solution.Because #include<GL/freeglut.h> linked to the freeglut tool kit that I had download from other site,instead of the header that can link freeglut_static_vs2010_d.lib. (freeglut is an open source tool kit ,so there are many editions and developers)
it is a part of  freeglut_std.h that I download from other site.
freeglut_std.h that the redbook edtion
所以我把舊的引用做更改,讓所有的header都是redbook的版本,之後就compile success了。
After I alternated the #include path, make sure all the heaader files are redbook's.Then compile success.
========================================================================
redbook第一章範例一程式碼
code of redbook chapter 1 sample1

#include<iostream>
#include<gl_rb_include\vgl .h>
#include<gl_rb_include\LoadShaders.h>


using namespace std;

enum  VAO_IDs{Triangles,NumVAOs};
enum Buffer_IDs{ArrayBuffers,NumBuffers};
enum Attrib_IDs{vPositions=0};

GLuint VAOs[NumVAOs];
GLuint Buffers[NumBuffers];
const GLuint NumVertices = 6;

///=======================init=================================
//
//

void init(void)
{
glGenVertexArrays(NumVAOs, VAOs);
glBindVertexArray(VAOs[Triangles]);

GLfloat vertices[NumVertices][2] = {
{-0.9,-0.9},//triangle 1
{0.85,-0.9},
{-0.9,0.85},
{0.9,-0.85},//triangle 2
{ 0.9, 0.9},
{-0.85,0.9}
          };
glGenBuffers(NumBuffers, Buffers);
glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffers]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
ShaderInfo shaders[] = {
{GL_VERTEX_SHADER,"triangles.vert"},
{ GL_FRAGMENT_SHADER, "triangles.frag" },
{GL_NONE,NULL}
};
GLuint program = LoadShaders(shaders);
glUseProgram(program);
glVertexAttribPointer(vPositions, 2, GL_FLOAT, GL_FALSE, 0,BUFFER_OFFSET(0) );
glEnableVertexAttribArray(vPositions);

}

//-------display-------------
//
//

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);

glBindVertexArray(VAOs[Triangles]);
glDrawArrays(GL_TRIANGLES, 0, NumVertices);

glFlush();

}



int main(int argc,char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(512, 512);
glutInitContextVersion(2,3);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutCreateWindow(argv[0]);
if (glewInit()){
cerr << "unable to initialize glew....exiting" << endl;

exit(EXIT_FAILURE);
}

init();
glutDisplayFunc(display);

glutMainLoop();


}
//=======================================================================
原始碼長這樣,不過在complie時又出現錯誤了
there were some error while compiled.
--------------------------------------------------------------------------------------------------------------------------
error LNK2019: 無法解析的外部符號 _LoadShaders 在函式 "void __cdecl init(void)" (?init@@YAXXZ) 中被參考 C:\Users\WZL\Documents\Visual Studio 2013\Projects\OPENGL V4.3 FOR STL VIEWER\OPENGL V4.3 FOR STL VIEWER\MAIN.obj OPENGL V4.3 FOR STL VIEWER
--------------------------------------------------------------------------------------------------------------------------
因此我去查了LoadShaders.h,發現LoadShaders根本沒定義功能,只有名稱和呼叫方法而已...
because the function LoadShader can not be ref, So I checked LoadShaders.h, and found there is no function algorithm ,just only calling method and type. 
LoadShaders.h
後來我在redbook的lib資料夾裏頭發現LoadShaders.cpp這個檔案,把它掛載於資源檔,程式就可以執行了~~
I found LoadShaders.cpp in redbook's lib folder. Took it into project,the program can run without error.

不過執行時還是發生了一點小問題,不過那是fopen和fopen_s的問題,那就不詳述了,因為還有更大的問題,就是"unable to initialize glew....exiting".....
But there were still some glitches,that was fopen and fopen_s.You can find solution in MSVC official community. Another glitch was "unable to initialize glew....exiting"..... While you run the program, the console will turn off like this.

這個問題困擾我也好一段時間,但後來發現跟glutInitContextVersion(,)這個函式有關係,我發現只要你指定的數字多於你現在的glut版本,程式就無法執行,由於我的glut是3.0的,因此在只要數字在3.1以上就爆炸了!!!!但是只要是以下就安全~~~
The glitch disturb me for a while,somehow I found it had something to do with glutInitContextVersion(,).I found when your glut version is newer than the function you assigned.the program will turn off. Because my glut version is 3.0, so if I assign glutInitContextVersion(3,1),the program will turn off, but when you assign smaller number,it works~~~

最後執行的結果長這樣:
Result is:(without shaders)
sample1 of redbook



OpenGL Red book(ver.4.3) set up in visual studio c++2013(Tratdional Chines and English)


就我們所知,紅皮書算是學習openGL的經典了,但是從官網下載的套件使用在mvsc++上就是會出現不少問題,因此這篇文章記錄我目前所遇到的問題還有解決方案。
Red book(OpenGL programming guide) is an epic in learning opneGL, but the tool kit that downloaded from redbook official site using in mvsc++ would trigger some problems or glitches. This blog recorded the problems I have met and solved.


1.可從此網站下載範例和套件 http://www.opengl-redbook.com/
1.We can download sample code and tool kit from this site: http://www.opengl-redbook.com/
2.解壓縮後有這些內容
2.the content we unzip:
其中lib和include是我們需要用到的,但是這份檔案並不包含dll,要就得自己包一個出來,不然就是得從freeglut下載vc適合版的套件,當初沒發現這件事情焦頭爛額了好一陣子。
the "lib" and "include" folders are what we need, but dll file is not included. We can download MSVC package from freeglut site or made dll file  by yourself.

環境設定
IDE set up

3.開啟vc,點選專案->屬性 ,會開啟如下的畫面
3.run vc, click project->attribute,the figure below will be shown(sorry it is not English version)
4.點選VC++目錄,會出現下圖畫面
4.click VC++ Directories, like the picture shows below.


5.點選Include目錄,加入redbook的include,如下圖所示
5.choose Include Directories, add the path of redbook's include folder.
由於每個人檔案夾放置位置不同,因此include擷取位置因人而異,按下確定後include目錄設定就完成了。
Cos of the include folder path would be different from one to another,depending on user's setting.Click OK after include folder path set.

6.設定lib路徑,也是從VC++目錄中,點選程式庫目錄,把redbook中l的ib加入參考即可,如下圖所示
6.set lib folder path,just like how you set include folder path,choose LIBRARY DIRECTORIES,take lib folder of redbook into reference. Click OK.
7.接著點選連結器->輸入->其他相依性的選項,選擇需要呼叫的lib
7.click  Linker->input->Additional Dependencies,choose the lib you need.

在redbook中,雖然它也是從freeglut而來,但它和一般freeglut的lib不大一樣,就是它的lib內容多了很多東西,以下是比較圖
although redbook tool kit comes from freeglut,but there is much different in lib.the file number of lib is much more than freeglut's. There is comparsion.
Inhalt des rotbuch's lib
redbook的lib資料夾內容
content of freeglut's lib
freeglut的lib資料夾內容
引用的lib如下圖,由於這個例子只需要freeglut和glew在vs2010的debug_mode版的lib,因此我只引用這兩個,如下圖所示
the lib I use as the picture shown below,because I need are freeglut and glew  lib in vs2010(compatible to newer version ) of debug_mode.
按下確定後,開發環境即設定完成,可以開始寫程式了。
Click OK,then the IDE set up is done.




同軸度 Coaxiality

同軸度和同心度是兩個非常容易搞混的幾何公差. 同軸度的符號如下: 跟正位度的符號是一樣的! 有這種設定,主要是它的定義和正位度的使用方法很像,反而跟同心度沒這麼相似. 首先來個範例: 由於這個不是繪圖軟體做的,只是示意用,不合工程圖規範的部分還請包涵. 這個是...