Your SlideShare is downloading. ×
ゲームグラフィックス特論 第2回 講義ノート
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

ゲームグラフィックス特論 第2回 講義ノート

1,206
views

Published on


0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,206
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
50
Comments
0
Likes
4
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. 62 4 GPU (Graphics Processing Unit) 4.1 4.1.1 63 63
  • 2. 63 ( 64) " ( ) " " " 64
  • 3. 64 4.1.2 ( ) GPU (Graphics Procssing Unit) (Transform) (Lighting ) T & L (Transform and Lighting) " CG 4 4 4 4 ( ) 4 4 4 4 4 4 4 16 " ( ) (Swizzling) 2 1 65 4.1.3
  • 4. 65 GPU ( ) CPU 66 4.1.4 GPU GPU GPU CPU ( CPU ) (peripheral) CPU GPU
  • 5. 66 (discrete) GPU PCI-Express GPU GPU GPU ( ) ( ) GPU 67 68 点 三角形一枚の三角形 四枚に細分割
  • 6. 67 4.1.5 ( ) 69 4.1.6 / (
  • 7. 68 ) ( 70) 70 GPU 4.2 CPU GPU GPU (attribute) GPU (Vertex Buffer Object, VBO) ( ) (Index Buffer Object) (Vertex Array Object) ( ) GPU uniform GPU ラスタライザ 出力画像 バーテックス シェーダ フラグメント シェーダ 補間 画素 頂点属性 選択されたフラグメント バッファ オブジェクト フレーム バッファ 基本図形
  • 8. 69 GPU 71 OpenGL 3.2 OpenGL 4.x 71 GPU アプリケーション ディスプレイ シェーダ 頂点属性 バッファオブジェクト バーテックスシェーダ ラスタライザ フラグメントシェーダ ラスタオペレーション パラメータ テクスチャ 基本図形 uniform レジスタ テクスチャ メモリ 映像出力 クリッピング ジオメトリシェーダ スクリーンマッピング フレームバッファ 頂点番号
  • 9. 70 (primitive)
  • 10. 71 5 5.1 5.1.1 C " HLSL (High Level Shading Language) ! DirectX ! Microsoft NVIDIA " Cg (C for Graphics) ! DirectX OpenGL ! NVIDIA " GLSL (OpenGL Shading Language, GLslang) ! OpenGL ! OpenGL ARB ( Khronos Group) GLSL (OpenGL Shading Language) 5.1.2 OpenGL
  • 11. 72 72 OpenGL OpenGL 2.0 OpenGL 2013 7 4.4 GPU OpenGL 3.2 Core Profile ( ) ( ) Mac OS X 10.7 (Lion) 3 OpenGL GLSL GLSL 3 OpenGL GLSL OpenGL のバージョン 1.5 2.0 2.1 3.0 3.1 3.2 3.3 4.0 4.1 4.2 4.3 4.4 GLSL のバージョン 1.00 (100) 1.10 (110) 1.20 (120) 1.30 (130) 1.40 (140) 1.50 (150) 3.30 (330) 4.00 (400) 4.10 (410) 4.20 (420) 4.30 (430) 4.40 (440)
  • 12. 73 5.2 73 OpenGL 3.2 Core Profile CPU GPU GPU 73 5.3 5.3.1 GLSL 1. glCreateProgram() 2. glCreateShader() 3. glShaderSource() 4. glCompileShader() 5. glAttachShader() 6. glLinkProgram() glUseProgram()
  • 13. 74 5.3.2 " #version*150*core* in*vec4*pv;* void*main()* {* **gl_Position*=*pv;* }* 1 #version GLSL 150 OpenGL 3.2 GLSL 1.50 core Core Profile 2 pv in in CPU attribute vec4 32bit (float ) 4 2 vec2 3 vec3 C “.” { .x, .y, .z, .w } { .s, .t, .p, q } { .r, .g, .b, .a } ( 4) 4 GLSL C C++ main() main() void main() CPU GPU 変数のデータ型 ベクトル型の要素内容 凡例 vec4 pv; vec4 型の変数宣言 .x .y .z .w .r .g .b .a .s .t .p .q float vec2 vec3 vec4 単精度浮動小数点 1 個 単精度浮動小数点 2 個 単精度浮動小数点 3 個 単精度浮動小数点 4 個 pv.xy pv.rgb pv.q pv.yx pv.brg pv の第1,2要素, vec2 型 pv の第1,2,3要素, vec3 型 pv の第4要素, float 型 pv の第1,2要素の順序を入れ替えたもの, vec2 型 pv の第1,2,3要素を3,1,2の順に並べ替え, vec3 型
  • 14. 75 CPU GPU attribute gl_Position GLSL ( ) attribute pv gl_Position " #version*150*core* out*vec4*fc;* void*main()* {* **fc*=*vec4(1.0,*0.0,*0.0,*1.0);* }* 2 fc out vec4() vec4 ( ) fc R ( ) G ( ) B ( ) A ( ) 4 ( ) discard 5.3.3 GPU glCreateProgram() (program) **const*GLuint*program(glCreateProgram());* GLuint glCreateProgram(void) ( ) 0 0 5.3.4 1 #version '#' '¥n' **static*const*GLchar**vsrc[]*=* **{* ****"#version*150*core¥n",*
  • 15. 76 ****"in*vec4*pv;",* ****"void*main()",* ****"{",* ****"**gl_Position*=*pv;",* ****"}",* **};* (vobj) sizeof vsrc / sizeof vsrc[0] vsrc ( ) GL_VERTEX_SHADER GL_FRAGMENT_SHADER **const*GLuint*vobj(glCreateShader(GL_VERTEX_SHADER));* **glShaderSource(vobj,*sizeof*vsrc*/*sizeof*vsrc[0],*vsrc,*NULL);* **glCompileShader(vobj);* 1 '¥n' 2 **static*const*GLchar**vsrc*=* ****"#version*150*core¥n"* ****"in*vec4*pv;¥n"* ****"void*main()¥n"* ****"{¥n"* ****"**gl_Position*=*pv;¥n"* ****"}¥n";* **const*GLuint*vobj(glCreateShader(GL_VERTEX_SHADER));* **glShaderSource(vsrc,*1,*&vsrc,*NULL);** **glCompileShader(vobj);* GLuint glCreateShader(GLenum shaderType) ( ) 0 0 shaderType GL_VERTEX_SHADER GL_FRAGMENT_SHADER void glShaderSource(GLuint shader, GLsizei count, const GLchar **string, const GLint *length) shader glCreateShader() count string string '#' '¥n'
  • 16. 77 length length NULL (0) length string ( ) length string ('¥0') void glCompileShader(GLuint shader) shader **glAttachShader(program,*vobj);* **glDeleteShader(vobj);* void glAttachShader(GLuint program, GLuint shader) program shader void glDetachShader(GLuint program, GLuint shader) program shader void glDeleteShader(GLuint shader) shader void glDeleteProgram(GLuint program) program glDeleteShader() glDetachShader()
  • 17. 78 glDeleteProgram() 5.3.5 ( , attribute) (attribute pv) (index) ( fc ) (colorNumber) 0 CPU GPU GPU **glBindAttribLocation(program,*0,*"pv");* **glBindFragDataLocation(program,*0,*"fc");* **glLinkProgram(program);* void glBindAttribLocation(GLuint program, GLuint index, const GLchar *name) attribute attribute in program attribute index name in ( ) 0 0 name in void glBindFragDataLocation(GLuint program, GLuint colorNumber, const char *name) out program (out ) index name out ( ) 0 0 0 name out
  • 18. 79 void glLinkProgram(GLuint program) program program 5.3.6 NULL //* * //***vsrc:* * //***pv:* *in* * //***fsrc:* * //***fc:* *out* * static*GLuint*createProgram(const*char**vsrc,*const*char**pv,* **const*char**fsrc,*const*char**fc)* {* **//* * **const*GLuint*program(glCreateProgram());* * **if*(vsrc*!=*NULL)* **{* ****//* * ****const*GLuint*vobj(glCreateShader(GL_VERTEX_SHADER));* ****glShaderSource(vobj,*1,*&vsrc,*NULL);* ****glCompileShader(vobj);* * ****//* * ****glAttachShader(program,*vobj);* ****glDeleteShader(vobj);* **}* * **if*(fsrc*!=*NULL)* **{* ****//* * ****const*GLuint*fobj(glCreateShader(GL_FRAGMENT_SHADER));* ****glShaderSource(fobj,*1,*&fsrc,*NULL);* ****glCompileShader(fobj);* * ****//* * ****glAttachShader(program,*fobj);* ****glDeleteShader(fobj);* **}* * **//* * **glBindAttribLocation(program,*0,*pv);* **glBindFragDataLocation(program,*0,*fc);* **glLinkProgram(program);* * **//* * **return*program;* }*
  • 19. 80 5.3.7 glUseProgram() **glUseProgram(program);* void glUseProgram(GLuint program) program 0 " main() createProgram() GLEW glUseProgram() #include*<cstdlib>* #include*<iostream>* #include*<GL/glew.h>* #include*<GLFW/glfw3.h>* * //* * static*void*cleanup()* {* **//*GLFW* * **glfwTerminate();* }* * //* * //***vsrc:* * //***pv:* *in* * //***fsrc:* * //***fc:* *out* * static*GLuint*createProgram(const*char**vsrc,*const*char**pv,* **const*char**fsrc,*const*char**fc)* {* ** * }* * * * int*main()* {* ** * * **//* * **glClearColor(1.0f,*1.0f,*1.0f,*0.0f);* * **//* * **static*const*GLchar*vsrc[]*=* ****"#version*150*core¥n"* ****"in*vec4*pv;¥n"*
  • 20. 81 ****"void*main()¥n"* ****"{¥n"* ****"**gl_Position*=*pv;¥n"* ****"}¥n";* * **//* * **static*const*GLchar*fsrc[]*=* ****"#version*150*core¥n"* ****"out*vec4*fc;¥n"* ****"void*main()¥n"* ****"{¥n"* ****"**fc*=*vec4(1.0,*0.0,*0.0,*1.0);¥n"* ****"}¥n";* * **//* * **const*GLuint*program(createProgram(vsrc,*"pv",*fsrc,*"fc"));* * **//* * **while*(glfwWindowShouldClose(window)*==*GL_FALSE)* **{* ****//* * ****glClear(GL_COLOR_BUFFER_BIT);* * ****//* * ****glUseProgram(program);* * ****//* ****//* * ****//* * ****//* * ****glfwSwapBuffers(window);* * ****//* * ****glfwWaitEvents();* **}* }* " glUseProgram() glUseProgram(program); while ( ) ( ) glUseProgram(0); 5.4 GLSL GL_TRUE GL_FALSE //* * //***shader:* *
  • 21. 82 //***str:* * static*GLboolean*printShaderInfoLog(GLuint*shader,*const*char**str)* {* **//* * **GLint*status;* **glGetShaderiv(shader,*GL_COMPILE_STATUS,*&status);* **if*(status*==*GL_FALSE)*std::cerr*<<*"Compile*Error*in*"*<<*str*<<*std::endl;* * **//* * **GLsizei*bufSize;* **glGetShaderiv(shader,*GL_INFO_LOG_LENGTH*,*&bufSize);* * **if*(bufSize*>*1)* **{* ****//* * ****std::vector<GLchar>*infoLog(bufSize);* ****GLsizei*length;* ****glGetShaderInfoLog(shader,*bufSize,*&length,*&infoLog[0]);* ****std::cerr*<<*&infoLog[0]*<<*std::endl;* **}* * **return*static_cast<GLboolean>(status);* }* void glGetShaderiv(GLuint shader, GLenum pname, GLint *params) shader pname GL_SHADER_TYPE shader (GL_VERTEX_SHADER, GL_FRAGMENT_SHADER) *params GL_DELETE_STATUS shader glDeleteShader() GL_TRUE GL_FALSE *params GL_COMPILE_STATUS shader GL_TRUE GL_FALSE *params GL_INFO_LOG_LENGTH shader *params 0 GL_SHADER_SOURCE_LENGTH shader *params 0
  • 22. 83 params void glGetShaderInfoLog(GLuint shader, GLsizei maxLength, GLsizei *length, GLchar *infoLog) shader maxLength infoLog length infoLog GL_TRUE GL_FALSE //* * //***program:* * static*GLboolean*printProgramInfoLog(GLuint*program)* {* **//* * **GLint*status;* **glGetProgramiv(program,*GL_LINK_STATUS,*&status);* **if*(status*==*GL_FALSE)*std::cerr*<<*"Link*Error."*<<*std::endl;* * **//* * **GLsizei*bufSize;* **glGetProgramiv(program,*GL_INFO_LOG_LENGTH,*&bufSize);* * **if*(bufSize*>*1)* **{* ****//* * ****std::vector<GLchar>*infoLog(bufSize);* ****GLsizei*length;* ****glGetProgramInfoLog(program,*bufSize,*&length,*&infoLog[0]);* ****std::cerr*<<*&infoLog[0]*<<*std::endl;* **}* * **return*static_cast<GLboolean>(status);* }* void glGetProgramiv(GLuint program, GLenum pname, GLint *params) program pname
  • 23. 84 GL_DELETE_STATUS program glDeleteProgram() GL_TRUE GL_FALSE *params GL_LINK_STATUS program GL_TRUE GL_FALSE *params GL_VALIDATE_STATUS program glValidateProgram() *params *params OpenGL GL_TRUE GL_FALSE GL_INFO_LOG_LENGTH program *params 0 GL_ATTACHED_SHADERS program *params params void glValidateProgram(GLuint program) OpenGL glGetProgramiv() pname GL_VALIDATE_STATUS program void glGetProgramInfoLog(GLuint program, GLsizei maxLength, GLsizei *length, GLchar *infoLog) program maxLength infoLog length (infoLog ) infoLog maxLength " createProgram()
  • 24. 85 C++ vector vector #include #include*<cstdlib>* #include*<iostream>* #include*<vector>* #include*<GL/glew.h>* #include*<GLFW/glfw3.h>* * //* * static*void*cleanup()* {* **//*GLFW* * **glfwTerminate();* }* * //* * //***shader:* * //***str:* * static*GLboolean*printShaderInfoLog(GLuint*shader,*const*char**str)* {* ** * }* * //* * //***program:* * static*GLboolean*printProgramInfoLog(GLuint*program)* {* ** * }* * //* * //***vsrc:* * //***pv:* *in* * //***fsrc:* * //***fc:* *out* * static*GLuint*createProgram(const*char**vsrc,*const*char**pv,* **const*char**fsrc,*const*char**fc)* {* **//* * **const*GLuint*program(glCreateProgram());* * **if*(vsrc*!=*NULL)* **{* ****//* * ****const*GLuint*vobj(glCreateShader(GL_VERTEX_SHADER));* ****glShaderSource(vobj,*1,*&vsrc,*NULL);* ****glCompileShader(vobj);* * ****//* * ****if*(printShaderInfoLog(vobj,*"vertex*shader"))* ******glAttachShader(program,*vobj);* ****glDeleteShader(vobj);* **}* * **if*(fsrc*!=*NULL)*
  • 25. 86 **{* ****//* * ****const*GLuint*fobj(glCreateShader(GL_FRAGMENT_SHADER));* ****glShaderSource(fobj,*1,*&fsrc,*NULL);* ****glCompileShader(fobj);* * ****//* * ****if*(printShaderInfoLog(fobj,*"fragment*shader"))* ******glAttachShader(program,*fobj);* ****glDeleteShader(fobj);* **}* * **//* * **glBindAttribLocation(program,*0,*pv);* **glBindFragDataLocation(program,*0,*fc);* **glLinkProgram(program);* * **//* * **if*(printProgramInfoLog(program))* ****return*program;* * **//* *0* * **glDeleteProgram(program);* **return*0;* }* * * 5.5 GLSL C++ C++ C++ C++ readShaderSource() //* * //***name:* * static*GLchar**readShaderSource(const*char**name)* {* **//* *NULL* *NULL* * **if*(name*==*NULL)*return*NULL;* * **//* * **std::ifstream*file(name,*std::ios::binary);* **if*(file.fail())* **{* ****//* * ****std::cerr*<<*"Error:*Can't*open*source*file:*"*<<*name*<<*std::endl;* ****return*NULL;* **}* * **//* * **file.seekg(0L,*std::ios::end);*
  • 26. 87 **const*GLsizei*length(static_cast<GLsizei>(file.tellg()));* * **//* *(‘¥0’)* * **GLchar**buffer(new(std::nothrow)*GLchar[length*+*1]);* **if*(buffer*==*NULL)* **{* ****//* * ****std::cerr*<<*"Error:*Too*large*file:*"*<<*name*<<*std::endl;* ****file.close();* ****return*NULL;* **}* * **//* * **file.seekg(0L,*std::ios::beg);* **file.read(buffer,*length);* **buffer[length]*=*'¥0';* * **if*(file.bad())* **{* ****//* * ****std::cerr*<<*"Error:*Could*not*read*souce*file:*"*<<*name*<<*std::endl;* ****delete*buffer;* ****buffer*=*NULL;* **}* **file.close();* * **// * **return*buffer;* }* createProgram() loadProgram() //* * //***vert:* * //***pv:* *in* * //***frag:* * //***fc:* *out* * static*GLuint*loadProgram(const*char**vert,*const*char**pv,* **const*char**frag,*const*char**fc)* {* **//* * **const*GLchar**vsrc(readShaderSource(vert));* **const*GLchar**fsrc(readShaderSource(frag));* * **//* * **const*GLuint*program(createProgram(vsrc,*pv,*fsrc,*fc));* * **//* * **delete*vsrc;* **delete*fsrc;* * **//* * **return*program;* }*
  • 27. 88 " createProgram() readShaderSource() loadProgram() main() vsrc fsrc createProgram() loadProgram() #include*<cstdlib>* #include*<iostream>* #include*<fstream>* #include*<vector>* #include*<GL/glew.h>* #include*<GLFW/glfw3.h>* * * * //* * //***vsrc:* * //***pv:* *in* * //***fsrc:* * //***fc:* *out* * static*GLuint*createProgram(const*char**vsrc,*const*char**pv,* **const*char**fsrc,*const*char**fc)* {* ** * }* * //* * //***name:* * static*GLchar**readShaderSource(const*char**name)* {* ** * }* * //* * //***vert:* * //***pv:* *in* * //***frag:* * //***fc:* *out* * static*GLuint*loadProgram(const*char**vert,*const*char**pv,* **const*char**frag,*const*char**fc)* {* ** * }* * * * int*main()* {* ** * * **//* * **glClearColor(1.0f,*1.0f,*1.0f,*0.0f);* * **//* *vsrc* * **//* *fsrc* * * **//* *
  • 28. 89 **const*GLuint*program(loadProgram("point.vert",*"pv",*"point.frag",*"fc"));* * **//* * **while*(glfwWindowShouldClose(window)*==*GL_FALSE)* **{* **** * **}* }* C++ point.vert point.frag ( ) C++ ( ) " point.vert #version*150*core* in*vec4*pv;* void*main()* {* **gl_Position*=*pv;* }* point.frag #version*150*core* out*vec4*fc;* void*main()* {* **fc*=*vec4(1.0,*0.0,*0.0,*1.0);* }* " Xcode Mac OS X Xcode C++ Xcode C++ Xcode C++ Xcode Xcode “Set Active Scheme” “Edit Scheme” ( 74)
  • 29. 90 74 “Run ” “Options” “Working Directory” C++ “OK” ( 75) 75 (Working Directory)
  • 30. 91 6 6.1 OpenGL (GPU) (CPU) OpenGL ( 5) GLubyte C C++ unsigned char GLfloat float GLclampf float OpenGL 0 1 1 GLhalf 6.2 OpenGL OpenGL (attribute) GPU GPU (Vertex Buffer Object, VBO) (Vertex Array Object, VAO) OpenGL
  • 31. 92 5 OpenGL 6.3 glGenVertexArrays() glBindVertexArray() **GLuint*vao;* **glGenVertexArrays(1,*&vao);* **glBindVertexArray(vao);* void glGenVertexArrays(GLsizei n, GLuint *arrays) n arrays ( ) n OpenGL の データ型 最小 ビット数 説明 GLboolean 1 論理値 GLbyte 8 符号付き二進整数(二の補数表現) GLubyte 8 符号なし二進整数 GLchar 8 文字列中の文字 GLshort 16 符号付き二進整数(二の補数表現) GLushort 16 符号なし二進整数 GLint 32 符号付き二進整数(二の補数表現) GLuint 32 符号なし二進整数 GLint64 64 符号付き二進整数(二の補数表現) GLuint64 64 符号なし二進整数 GLsizei 32 非負の二進整数で表したサイズ GLenum 32 二進整数で表した列挙子 GLintptr ※ 符号付き二進整数(二の補数表現) GLsizeiptr ※ 非負の二進整数で表したサイズ GLsync ※ 同期オブジェクトのハンドル GLbitfield 32 ビットフィールド GLhalf 16 符号なしの値に符号化された半精度浮動小数点数 GLfloat 32 単線度浮動小数点数 GLclampf 32 [0, 1] にクランプされた単精度浮動小数点数 GLdouble 64 倍線度浮動小数点数 GLclampd 64 [0, 1] にクランプされた倍精度浮動小数点数 ※ ポインタ(アドレス)を保持するのに必要なビット数
  • 32. 93 void glBindVertexArray(GLuint array) array 0 6.4 6.4.1 4 vertices **//* * **static*const*GLfloat*position[][2]*=* **{* ****{*K0.5f,*K0.5f*},* ****{**0.5f,*K0.5f*},* ****{**0.5f,**0.5f*},* ****{*K0.5f,**0.5f*}* **};* * **//* * **static*const*GLuint*vertices(sizeof*position*/*sizeof*position[0]);* GPU GLfloat vertices sizeof (GLfloat) * 2 * vertices **GLuint*vbo;* **glGenBuffers(1,*&vbo);* **glBindBuffer(GL_ARRAY_BUFFER,*vbo);* **glBufferData(GL_ARRAY_BUFFER,* ****sizeof*(GLfloat)***2***vertices,*position,*GL_STATIC_DRAW);* void glGenBuffers(GLsizei n, GLuint *buffers) n arrays ( ) n void glBindBuffer(GLenum target, GLuint buffer) target GL_ARRAY_BUFFER, GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, GL_ELEMENT_ARRAY_BUFFER, GL_PIXEL_PACK_BUFFER,
  • 33. 94 GL_PIXEL_UNPACK_BUFFER, GL_TEXTURE_BUFFE, GL_UNIFORM_BUFFE, GL_TRANSFORM_FEEDBACK_BUFFER GL_ARRAY_BUFFER array 0 void glBufferData(GLenum target, GLsizeiptr size, const GLvoid *data, GLenum usage) ( ) target size GPU data CPU ( ) size 0 GPU usage GL_STREAM_DRAW GL_STREAM_READ GL_STREAM_COPY GL_STATIC_DRAW GL_STATIC_READ GL_STATIC_COPY GL_DYNAMIC_DRAW GL_DYNAMIC_READ GL_DYNAMIC_COPY GPU STREAM STATIC DYNAMIC DRAW READ OpenGL (GPU) COPY OpenGL (GPU)
  • 34. 95 6.4.2 attribute in attribute in glVertexAttribPointer() glEnableVertexAttribArray() in **glVertexAttribPointer(0,*2,*GL_FLOAT,*GL_FALSE,*0,*0);* **glEnableVertexAttribArray(0);* void glVertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *pointer); attribute index glBindAttribLocation() attribute (index) pv index 0 0 size attribute 1 2 3 4 (x, y) 2 (x, y, z) 3 2 type attribute (pointer ) GL_BYTE GL_UNSIGNED_BYTE GL_SHORT GL_UNSIGNED_SHORT GL_INT GL_UNSIGNED_INT GL_FLOAT GL_DOUBLE GLfloat GL_FLOAT normalized GL_TRUE type (GL_BYTE GL_UNSIGNED_BYTE GL_SHORT GL_UNSIGNED_SHORT GL_INT GL_UNSIGNED_INT) GL_FALSE GL_FALSE stride attribute (pointer ) 0 0 pointer attribute 0 int offset static_cast<char *>(0) + offset (char *)0 + offset
  • 35. 96 void glEnableVertexAttribArray(GLuint index) attribute index attribute **glBindBuffer(GL_ARRAY_BUFFER,*0);* **glBindVertexArray(0);* glGenVertexArray() glVertexAttribPointer() glBindBuffer() " //* * //***vertices:* * //***position:* * static*GLuint*createObject(GLuint*vertices,*const*GLfloat*(*position)[2])* {* **//* * **GLuint*vao;* **glGenVertexArrays(1,*&vao);* **glBindVertexArray(vao);* * **//* * **GLuint*vbo;* **glGenBuffers(1,*&vbo);* **glBindBuffer(GL_ARRAY_BUFFER,*vbo);* **glBufferData(GL_ARRAY_BUFFER,* ****sizeof*(GLfloat)***2***vertices,*position,*GL_STATIC_DRAW);* * **//* *in* * **glVertexAttribPointer(0,*2,*GL_FLOAT,*GL_FALSE,*0,*0);* **glEnableVertexAttribArray(0);* * **//* * **glBindBuffer(GL_ARRAY_BUFFER,*0);* **glBindVertexArray(0);* * **return*vao;* }* " createObject() createRectangle() (“Object” )
  • 36. 97 //* * struct*Object* {* **//* * **GLuint*vao;* * **//* * **GLsizei*count;* };* createObject() //* * static*Object*createRectangle()* {* **//* * **static*const*GLfloat*position[][2]*=* **{* ****{*K0.5f,*K0.5f*},* ****{**0.5f,*K0.5f*},* ****{**0.5f,**0.5f*},* ****{*K0.5f,**0.5f*}* **};* * **//* * **static*const*int*vertices(sizeof*position*/*sizeof*position[0]);* * **//* * **Object*object;* **object.vao*=*createObject(vertices,*position);* **object.count*=*vertices;* * **return*object;* }* 6.5 glBindVertexArray() glDrawArrays() ( 76) ****glBindVertexArray(vao);* ****glDrawArrays(GL_LINE_LOOP,*0,*vertices);* void glDrawArrays(GLenum mode, GLint first, GLsizei count) mode 76 OpenGL 4.0 GL_PATCHES first 0 count 4
  • 37. 98 76 OpenGL " * * //* * //***vertices:* * //***position:* * static*GLuint*createObject(GLuint*vertices,*const*GLfloat*(*position)[2])* {* ** * }* * //* * struct*Object* {* ** * };* * //* * static*Object*createRectangle()* {* ** * }* * 0 1 2 3 4 5 GL_TRIANGLE_STRIP 0 1 2 3 4 5 GL_TRIANGLE_FAN 0 1 2 3 4 5 GL_TRIANGLES 0 1 2 3 4 5 GL_LINES 0 1 2 3 4 5 GL_LINE_STRIP 0 1 2 3 4 5 GL_POINTS 0 1 2 3 4 5 GL_LINE_LOOP 0 1 2 3 4 5 GL_TRIANGLE_STRIP_ADJACENCY 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 8 910 11 6 7 8 9 10 11 GL_TRIANGLES_ADJACENCY GL_LINE_STRIP_ADJACENCYGL_LINES_ADJACENCY
  • 38. 99 int*main()* {* ** * * **//* * **glClearColor(1.0f,*1.0f,*1.0f,*0.0f);* * **//* * **const*GLuint*program(loadProgram("point.vert",*"pv",*"point.frag",*"fc"));* * **//* * **const*Object*object(createRectangle());* * **//* * **while*(glfwWindowShouldClose(window)*==*GL_FALSE)* **{* ****//* * ****glClear(GL_COLOR_BUFFER_BIT);* * ****//* * ****glUseProgram(program);* * ****//* * ****glBindVertexArray(object.vao);* ****glDrawArrays(GL_LINE_LOOP,*0,*object.count);* * ****//* * ****glfwSwapBuffers(window);* * ****//* * ****glfwWaitEvents();* **}* }* " 77

×