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



沒有留言:

張貼留言

同軸度 Coaxiality

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