幟äœåŠã·ã§ãŒããŒ
é ç¹ã·ã§ãŒããŒãšãã©ã°ã¡ã³ãã·ã§ãŒããŒã®ã¹ããŒãžã®éã«ã¯ããžãªã¡ããªã·ã§ãŒããŒãå®è¡ããããã«èšèšããããªãã·ã§ã³ã®ã¹ããŒãžããããŸãã ãžãªã¡ããªã·ã§ãŒããŒã®å ¥åã«ã¯ãOpenGLã§èš±å®¹ãããããªããã£ãïŒãã€ã³ããäžè§åœ¢ãªã©ïŒã®1ã€ã圢æããäžé£ã®é ç¹ããããŸãã ãã®äœæ¥ã®çµæããžãªã¡ããªã·ã§ãŒããŒã¯ã次ã®ã·ã§ãŒããŒã¹ããŒãžã«è»¢éããåã«ããã®äžé£ã®é ç¹ãèªç±è£éã§å€æã§ããŸãã åæã«ããžãªã¡ããªã·ã§ãŒããŒã®æãèå³æ·±ãæ©èœã«æ³šç®ãã䟡å€ããããŸãïŒãã®äœæ¥ã®éçšã§ãå ¥åé ç¹ã®ã»ãããå€æããŠå®å šã«ç°ãªãããªããã£ããè¡šãããšãã§ããå ¥åããŒã¿ã«åºã¥ããŠå®å šã«æ°ããé ç¹ãçæããé ç¹ã®ç·æ°ãå¢ããããšãã§ããŸãã
å
容
ããŒã1.ã¯ããã«
ããŒã2.åºæ¬çãªç §æ
ããŒã3. 3Dã¢ãã«ãããŠã³ããŒããã
ããŒã4.é«åºŠãªOpenGLæ©èœ
ããŒã5.é«åºŠãªç §æ
ããŒã6. PBR
- Opengl
- ãŠã£ã³ããŠäœæ
- ããã«ã¡ã¯ãŠã£ã³ããŠ
- ããã«ã¡ã¯ãã©ã€ã¢ã³ã°ã«
- ã·ã§ãŒããŒ
- ãã¯ã¹ãã£ãŒ
- å€æ
- 座æšç³»
- ã«ã¡ã©
ããŒã2.åºæ¬çãªç §æ
ããŒã3. 3Dã¢ãã«ãããŠã³ããŒããã
ããŒã4.é«åºŠãªOpenGLæ©èœ
- 深床ãã¹ã
- ã¹ãã³ã·ã«ãã¹ã
- è²æ··å
- é¡ã®ã¯ãªããã³ã°
- ãã¬ãŒã ãããã¡
- ãã¥ãŒããã¯ã«ãŒã
- é«åºŠãªããŒã¿åŠç
- é«åºŠãªGLSL
- 幟äœåŠã·ã§ãŒããŒ
- ã€ã³ã¹ã¿ã³ã¹å
- ã¹ã ãŒãžã³ã°
ããŒã5.é«åºŠãªç §æ
- é«åºŠãªç §æã Blinn-Fongã¢ãã«ã
- ã¬ã³ãè£æ£
- ã·ã£ããŠã«ãŒã
- å šæ¹åã·ã£ããŠããã
- æ³ç·ãããã³ã°
- èŠå·®ãããã³ã°
- HDR
- ãã«ãŒã
- é 延ã¬ã³ããªã³ã°
- SSAO
ããŒã6. PBR
é·ãéå©çšãããããã«ãžãªã¡ããªã·ã§ãŒããŒã®äŸã«æ»ããŸãã
#version 330 core layout (points) in; layout (line_strip, max_vertices = 2) out; void main() { gl_Position = gl_in[0].gl_Position + vec4(-0.1, 0.0, 0.0, 0.0); EmitVertex(); gl_Position = gl_in[0].gl_Position + vec4( 0.1, 0.0, 0.0, 0.0); EmitVertex(); EndPrimitive(); }
ã·ã§ãŒããŒã³ãŒãã®å é ã§ãé ç¹ã·ã§ãŒããŒã¹ããŒãžããããŒã¿ãååŸããããªããã£ãã®ã¿ã€ããæå®ããå¿ èŠããããŸãã ããã¯ãinããŒã¯ãŒãã®åã«ããã¬ã€ã¢ãŠãæå®åã䜿çšããŠè¡ãããŸãã æå®åã§ç€ºãããããªããã£ãã®ã¿ã€ãã¯ãé ç¹ã·ã§ãŒããŒã«ãã£ãŠåŠçãããããªããã£ãã®ã¿ã€ãã«å¯Ÿå¿ãã次ã®å€ã®ãããããåãããšãã§ããŸãã
- points ïŒGL_POINTSïŒ1ïŒãåºåããå Žåã
- lines ïŒGL_LINESãŸãã¯GL_LINE_STRIPïŒ2ïŒãåºåããå Žåã
- lines_adjacency ïŒGL_LINES_ADJACENCYãŸãã¯GL_LINE_STRIP_ADJACENCYïŒ4ïŒãåºåããå Žåã
- triangles ïŒGL_TRIANGLESãGL_TRIANGLE_STRIPããŸãã¯GL_TRIANGLE_FANïŒ3ïŒãåºåããå Žåã
- triangles_adjacency ïŒGL_TRIANGLES_ADJACENCYãŸãã¯GL_TRIANGLE_STRIP_ADJACENCYïŒ6ïŒãåºåããå Žåã
çµæãšããŠã glDrawArraysïŒïŒã®ãããªã¬ã³ããªã³ã°é¢æ°ãžã®åãæž¡ãã«æå¹ãªãäºå®äžãã¹ãŠã®ã¿ã€ãã®ããªããã£ããããã«ãªã¹ããããŸãã GL_TRIANGLESã䜿çšããŠã¬ã³ããªã³ã°ããå Žåã ãã©ã€ã¢ã³ã°ã«ãã©ã¡ãŒã¿ãŒãæå®åã§æå®ããå¿ èŠããããŸãã ããã®æ¬åŒ§å ã®æ°åã¯ã1ã€ã®ããªããã£ãã«å«ãŸããé ç¹ã®æå°æ°ãæå³ããŸãã
次ã«ããã®ã·ã§ãŒããŒã®åºåããªããã£ãã®ã¿ã€ããæå®ããå¿ èŠããããŸãã ãããã£ãŠãããã¯outããŒã¯ãŒãã®åã«ã¬ã€ã¢ãŠãæå®åãä»ããŠè¡ãããŸãã ãã®äŸã§ã¯ãæ倧2ã€ã®é ç¹ãæã€line_stripãåºåã§çæãããŸãã
å¿ããå ŽåïŒ ã©ã€ã³ã¹ããªããããªããã£ãã¯ã»ããå ã®ãã€ã³ããæ¥ç¶ããã»ããå ã®2ã€ã®ãã€ã³ãããå§ãŸãé£ç¶ããã©ã€ã³ã圢æããŸãã 2ã€ãè¶ ãããã€ã³ããè¿œå ããããã³ã«ãæ°ãããã€ã³ãããåã®ãã€ã³ããŸã§äŒžã³ãå¥ã®ç·ã»ã°ã¡ã³ããäœæãããŸãã 以äžã¯ã5ãã€ã³ãã»ã°ã¡ã³ãã®ç»åã§ãã
瀺ãããŠããã·ã§ãŒããŒã®äŸã§ã¯ãããªããã£ãã®é ç¹ã®æ倧æ°ãæ瀺çã«2ã«èšå®ããŠãããããåã ã®çŽç·ã»ã°ã¡ã³ãã®ã¿ãçæã§ããŸãã
ã·ã§ãŒããŒãäœãæçšãªããšãè¡ããããã«ããã«ã¯ãåã®ã·ã§ãŒããŒã¹ããŒãžã®åºåããããŒã¿ãååŸããå¿ èŠããããŸãã GLSLã«ã¯çµã¿èŸŒã¿å€æ°gl_inãçšæãããŠããã次ã®æ§é ã§è¡šãããšãã§ããŸãã
in gl_Vertex { vec4 gl_Position; float gl_PointSize; float gl_ClipDistance[]; } gl_in[];
ãããã£ãŠããã®å€æ°ã¯æåŸã®ã¬ãã¹ã³ã§èª¬æããã€ã³ã¿ãŒãã§ã€ã¹ãããã¯ã«äŒŒãŠãããé ç¹ã·ã§ãŒããŒã®çµæãšããŠèšå®ãããé ç¹äœçœ®ãã¯ãã«ãå«ãgl_Positionãããã€ãã®ãã£ãŒã«ããå«ãã§ããŸãã
ã»ãšãã©ã®ããªããã£ãã«ã¯è€æ°ã®é ç¹ãå«ãŸããŠããããžãªã¡ããªã·ã§ãŒããŒã®ã¹ããŒãžã¯åŠçãããããªããã£ãã®ãã¹ãŠã®é ç¹ãå ¥åãšããŠåãåãããããã®å€æ°ã¯é åã§ããããšã«æ³šæããŠãã ããã
é ç¹ã·ã§ãŒããŒã®åºåããé ç¹ããŒã¿ãåãåã£ãããæ°ããããŒã¿ã®çæãéå§ã§ããŸããããã¯ããžãªã¡ããªã·ã§ãŒããŒã®2ã€ã®ç¹å¥ãªé¢æ°EmitVertexïŒïŒããã³EndPrimitiveïŒïŒã䜿çšããŠå®è¡ãããŸãã ã³ãŒãã§ã¯ãåºåãšããŠå®£èšãããå°ãªããšã1ã€ã®ããªããã£ããçæããããšãæåŸ ãããŠããŸãã ãã®äŸã§ã¯ãå°ãªããšã1ã€ã®line_stripã¿ã€ãã®ããªããã£ããè¿ãå¿ èŠããããŸãã
void main() { gl_Position = gl_in[0].gl_Position + vec4(-0.1, 0.0, 0.0, 0.0); EmitVertex(); gl_Position = gl_in[0].gl_Position + vec4( 0.1, 0.0, 0.0, 0.0); EmitVertex(); EndPrimitive(); }
EmitVertexïŒïŒãåŒã³åºããã³ã«ã gl_Positionå€æ°ã®çŸåšã®å€ãçŸåšã®ããªããã£ãã€ã³ã¹ã¿ã³ã¹ã«è¿œå ãããŸãã EndPrimitiveïŒïŒãåŒã³åºããšãçæããããã¹ãŠã®é ç¹ãæçµçââã«ããªããã£ãã®æå®ãããåºåã¿ã€ãã«ãã€ã³ããããŸãã EmitVertexïŒïŒã® 1ã€ä»¥äžã®åŒã³åºãã®åŸã«EndPrimitiveïŒïŒã®åŒã³åºããç¹°ãè¿ãããšã«ãããããªããã£ãã®æ°ããã€ã³ã¹ã¿ã³ã¹ãäœæãç¶ããããšãã§ããŸãã å ·äœçã«ã¯ããã®äŸã§ã¯ã2ã€ã®é ç¹ãçæãããå ¥åé ç¹ã®äœçœ®ããå°ãè·é¢ã移åããŠããã EndPrimitiveïŒïŒåŒã³åºããè¡ãã ãããã2ã€ã®çæãããé ç¹ãã2ã€ã®é ç¹ãå«ã1ã€ã®ã©ã€ã³ã¹ããªããã圢æããŸãã
ãããã£ãŠãïŒçè«çã«ã¯ïŒãžãªã¡ããªã·ã§ãŒããŒã®ä»çµã¿ãç¥ã£ãŠããã°ããããããã®äŸã®å¹æãäœã§ãããããã§ã«æšæž¬ããŠããã§ãããã å ¥åæã«ãã·ã§ãŒããŒã¯ãã€ã³ãããªããã£ããåãåããããã«åºã¥ããŠå ¥åã©ã€ã³ãã¡ããã©çãäžã«ããæ°Žå¹³ç·ãäœæããŸãã ãã®ãããªã·ã§ãŒããŒã䜿çšããããã°ã©ã ã®åºåã以äžã«ç€ºããŸãã
ããŸãå°è±¡çã§ã¯ãããŸãããã1åã®æç»åŒã³åºããå®è¡ããã ãã§ãã®ãããªçµæãåŸãããããšãèãããšããã§ã«èå³æ·±ããã®ã§ãã
glDrawArrays(GL_POINTS, 0, 4);
ãã®äŸã¯éåžžã«åçŽã§ãããéèŠãªååã瀺ããŠããŸãã幟äœåŠçã·ã§ãŒããŒã䜿çšããŠæ°ãã圢ç¶ãåçã«äœæããæ©èœã§ãã åŸã»ã©ããžãªã¡ããªã·ã§ãŒããŒã«åºã¥ããŠå®è£ ãããããã€ãã®èå³æ·±ããšãã§ã¯ããèŠãŠãããŸãããä»ã®ãšããã¯ãåçŽãªã·ã§ãŒããŒã䜿çšããŠåºæ¬çãªäœæ¥ãè¡ããŸãã
ãžãªã¡ããªã·ã§ãŒããŒã䜿çšãã
ãžãªã¡ããªã·ã§ãŒããŒã®äœ¿çšæ¹æ³ã瀺ãããã«ãããã€ã¹ã®æ£èŠåããã座æšïŒNDCïŒã§XoYå¹³é¢ã«ãã4ã€ã®ãã€ã³ããã¬ã³ããªã³ã°ããç°¡åãªããã°ã©ã ã䜿çšããŸãã ãã€ã³ã座æšïŒ
float points[] = { -0.5f, 0.5f, // - 0.5f, 0.5f, // - 0.5f, -0.5f, // - -0.5f, -0.5f // - };
é ç¹ã·ã§ãŒããŒã¯ã·ã³ãã«ã§ã-ãã€ã³ããç®çã®ãã¬ãŒã³ã«ãããããã ãã§ãïŒ
#version 330 core layout (location = 0) in vec2 aPos; void main() { gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0); }
ãã©ã°ã¡ã³ãã·ã§ãŒããŒãç°¡åã§ããã©ã°ã¡ã³ãã«ããŒãã³ãŒããããè²ã䜿çšããŸãã
#version 330 core out vec4 FragColor; void main() { FragColor = vec4(0.0, 1.0, 0.0, 1.0); }
ããã°ã©ã ã³ãŒãã§ã¯ãéåžžã©ãããé ç¹ããŒã¿ã®VAOãšVBOãäœæãã glDrawArraysïŒïŒãåŒã³åºããŠã¬ã³ããªã³ã°ããŸãã
shader.use(); glBindVertexArray(VAO); glDrawArrays(GL_POINTS, 0, 4);
ãã®çµæãç»é¢ã«ã¯å®å šãªæéãš4ã€ã®ã»ãšãã©èŠããªãç·è²ã®ãããããããŸãã
ãã®ãããªæ鬱ãªåçãæ®ãããã ãã«å€ãã®ããšãåŠãã ã®ã¯ãã¡ãã£ãšæ²ããããšã§ãã ãã®ãããããã«ã·ãŒã³ã«ä»å ¥ãããžãªã¡ããªãã¯ã·ã§ãŒããŒã®æ©èœã䜿çšããŠãã®æ²èŠ³çãªäººãèããŸãã
ããããæåã«ããã¬ãŒãã³ã°ã®ç®çã§ã ãšã³ãããŒãšã³ãã®ãžãªã¡ããªã·ã§ãŒããŒãã©ã®ããã«æ©èœããããäœæããŠææ¡ããå¿ èŠããããŸããããã¯ãå ¥åããªããã£ãã®ããŒã¿ãååŸããå€æŽããã«éä¿¡ããŸãã
#version 330 core layout (points) in; layout (points, max_vertices = 1) out; void main() { gl_Position = gl_in[0].gl_Position; EmitVertex(); EndPrimitive(); }
çŸæç¹ã§ã¯ããã³ããªãã§ã·ã§ãŒããŒã³ãŒããæ¢ã«ç解ã§ããŸãã ããã§ã¯ãé ç¹ã·ã§ãŒããŒããååŸããäœçœ®ã«é ç¹ãçæããåããã€ã³ãããªããã£ããçæããŸãã
ãžãªã¡ããªã·ã§ãŒããŒã«ã¯ãé ç¹ã·ã§ãŒããŒãšãã©ã°ã¡ã³ãã·ã§ãŒããŒãšåæ§ã«ãã³ã³ãã€ã«ãšããã°ã©ã ãªããžã§ã¯ããžã®ãªã³ã¯ãå¿ èŠã§ãã ãã ããä»åã¯ã·ã§ãŒããŒã®ã¿ã€ããšããŠGL_GEOMETRY_SHADERãæå®ãããã·ã§ãŒããŒãªããžã§ã¯ããäœæãããŸã ã
geometryShader = glCreateShader(GL_GEOMETRY_SHADER); glShaderSource(geometryShader, 1, &gShaderCode, NULL); glCompileShader(geometryShader); ... glAttachShader(program, geometryShader); glLinkProgram(program);
å®éãã³ã³ãã€ã«ã³ãŒãã¯ä»ã®çš®é¡ã®ã·ã§ãŒããŒãšãŸã£ããåãã§ãã ã³ã³ãã€ã«ããã³ãªã³ã¯ãšã©ãŒã®ãã§ãã¯ãå¿ããªãã§ãã ããïŒ
å®è¡ãããšãããªãã¿ã®ç»åãåŸãããŸãïŒ
幟äœåŠçã·ã§ãŒããŒãªãã®å Žåãšåããã®ãåŸãããŸãã...éå±ã§ãïŒ ãã ãããããã¯ãŸã 衚瀺ãããŠããã®ã§ãå°ãªããšãã·ã§ãŒããŒãæ©èœããŠããããšãšãããèå³æ·±ããã®ã«é²ãããšãã§ããããšã確èªããŠããŸãã
家ã建ãŠã
åçŽãªç·ãšç¹ãæãããšã¯ãç§ãã¡ãæåŸ ãããã®ãšã¯ãŸã£ããç°ãªããŸãããããã£ãŠãå°ãåµé æ§ãè¿œå ããå ¥åé ç¹ã«ãã£ãŠäžããããç¹ã«å®¶ãæããŸãã ãããè¡ãã«ã¯ãåºåããªããã£ãã®ã¿ã€ããtriangle_stripã«å€æŽãã3ã€ã®äžè§åœ¢ãæç»ããå¿ èŠããããŸãã2ã€ã¯æ£æ¹åœ¢ã®ããŒã¹ãäœæãã1ã€ã¯å±æ ¹ã«äœ¿çšããŸãã
OpenGLããªããã£ãTriangle Stripã¯ãå ¥ãå£ã§å¿ èŠãªé ç¹ãå°ãªãäžè§åœ¢ãæãããã®ããå¹ççãªæ¹æ³ã§ãã æåã®äžè§åœ¢ãã¬ã³ããªã³ã°ããåŸãåŸç¶ã®åé ç¹ã¯ãåã®äžè§åœ¢ã«é£æ¥ããå¥ã®äžè§åœ¢ãäœæããŸãã 6ã€ã®é ç¹ãäžè§åœ¢ã¹ããªããã®äžéšãšããŠäžããããå Žåãçµæã¯æ¬¡ã®äžè§åœ¢ã®ã·ãŒã±ã³ã¹ã«ãªããŸãïŒïŒ1,2,3ïŒãïŒ2,3,4ïŒãïŒ3,4,5ïŒããã³ïŒ4,5,6ïŒ 4ã€ã®æãããäžè§åœ¢ã«ããµããŸãã ãã®ããªããã£ãã§ã¯ãã¬ã³ããªã³ã°ãæåãããããã«å°ãªããšã3ã€ã®é ç¹ãèšå®ããå¿ èŠããããŸãã äžè¬çãªå ŽåãN-2åã®äžè§åœ¢ãå°åºãããŸãã 以äžã«ç€ºãããã«ã6ã€ã®é ç¹ããã6-2 = 4ã®äžè§åœ¢ãåŸãããŸããã
äžè§åœ¢ã®ã¹ããªããã䜿çšãããšã3ã€ã®é£æ¥ããäžè§åœ¢ããç®çã®åœ¢ç¶ãç°¡åã«åœ¢æããæ£ããé åºã§èšå®ã§ããŸãã 次ã®ç»åã¯ãç®çã®äžè§åœ¢ãååŸããããã«é ç¹ãæç»ããé åºã瀺ããŠããŸãã éãç¹ã¯ãå ¥åé ç¹ã®äœçœ®ã瀺ããŠããŸãã
çµæã®å¹ŸäœåŠçã·ã§ãŒããŒïŒ
#version 330 core layout (points) in; layout (triangle_strip, max_vertices = 5) out; void build_house(vec4 position) { gl_Position = position + vec4(-0.2, -0.2, 0.0, 0.0); // 1:bottom-left EmitVertex(); gl_Position = position + vec4( 0.2, -0.2, 0.0, 0.0); // 2:bottom-right EmitVertex(); gl_Position = position + vec4(-0.2, 0.2, 0.0, 0.0); // 3:top-left EmitVertex(); gl_Position = position + vec4( 0.2, 0.2, 0.0, 0.0); // 4:top-right EmitVertex(); gl_Position = position + vec4( 0.0, 0.4, 0.0, 0.0); // 5:top EmitVertex(); EndPrimitive(); } void main() { build_house(gl_in[0].gl_Position); }
ã·ã§ãŒããŒã¯ãå ¥åé ç¹ã®äœçœ®ãããªãã»ããããäœçœ®ã«5ã€ã®é ç¹ãäœæãããããããã¹ãŠäžè§åœ¢ã¹ããªããã¿ã€ãã®1ã€ã®ããªããã£ãã«é 眮ããŸãã 次ã«ããã®ããªããã£ããã©ã¹ã¿ã©ã€ãºã®ããã«éä¿¡ããããã©ã°ã¡ã³ãã·ã§ãŒãããã®è¡šé¢ãç·è²ã«ãã€ã³ãããŸãã åãšã³ããªãã€ã³ãã«1ã€ã®ã°ãªãŒã³ããŠã¹ãååŸããŸãã
ããã§ã¯ãå家ãå®éã«3ã€ã®äžè§åœ¢ã§æ§æãããŠããããšãããããŸãããããã¯ãã¹ãŠãå ¥åããŒã¿ã®åäžãã€ã³ãã«åºã¥ããŠæ§ç¯ãããŠããŸãã
ãããããŸã å°ãéå±ã«èŠããŸãïŒ ããããã®å®¶ãç¬èªã®è²ã§è²ä»ãããŠã¿ãŸãããã ãããè¡ãããã«ãé ç¹ã«é¢ããè²æ å ±ãä¿åããå¥ã®é ç¹å±æ§ãæŽçããŸãã é ç¹ã·ã§ãŒããŒã¯ãé ç¹ã®å±æ§å€ãèªã¿åãããžãªã¡ããªã·ã§ãŒããŒã«æž¡ããŸãããžãªã¡ããªã·ã§ãŒããŒã¯ãè²ã®å€ããã©ã°ã¡ã³ãã·ã§ãŒããŒã«åããŸãã
æŽæ°ãããé ç¹ããŒã¿ã¯æ¬¡ã®ãšããã§ãã
float points[] = { -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, // - 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, // - 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, // - -0.5f, -0.5f, 1.0f, 1.0f, 0.0f // - };
次ã«ãã€ã³ã¿ãŒãã§ã€ã¹ãããã¯ã䜿çšããŠããžãªã¡ããªã·ã§ãŒããŒã«è²å±æ§ãæž¡ãããã®é ç¹ã·ã§ãŒããŒã³ãŒããæå®ããŸãã
#version 330 core layout (location = 0) in vec2 aPos; layout (location = 1) in vec3 aColor; out VS_OUT { vec3 color; } vs_out; void main() { gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0); vs_out.color = aColor; }
æããã«ããžãªã¡ããªã·ã§ãŒããŒã§åãã¿ã€ãã®ïŒãã ãååã¯ç°ãªãïŒã€ã³ã¿ãŒãã§ã€ã¹ãŠããããå®çŸ©ããå¿ èŠããããŸãã
in VS_OUT { vec3 color; } gs_in[];
ãžãªã¡ããªã·ã§ãŒããŒã¯å ¥åé ç¹ã®ã»ããå šäœã§å®è¡ããããããåäžã®é ç¹ãå ¥åãããå Žåã§ãããã®å ¥åãã©ã¡ãŒã¿ãŒã¯åžžã«é åã§ãã
å®éããžãªã¡ããªãããã¯ã«ããŒã¿ã転éããããã«ã€ã³ã¿ãŒãã§ã€ã¹ãããã¯ã䜿çšããå¿ èŠã¯ãããŸããã é ç¹ã·ã§ãŒããŒãout vec3 vColorãšããŠè²ãæã€ãã¯ãã«ãæž¡ããå Žåã次ã®ããã«èšè¿°ã§ããŸãã
in vec3 vColor[];
ãã ããäžè¬ã«ãç¹ã«å¹ŸäœåŠçãªã·ã§ãŒããŒã§ã¯ãã€ã³ã¿ãŒãã§ã€ã¹ãŠãããã®æäœã¯ã¯ããã«ç°¡åã§ãã å®éã«ã¯ããžãªã¡ããªã·ã§ãŒããŒã®å ¥åãã©ã¡ãŒã¿ãŒã¯ãããªã倧ããªããŒã¿ã»ããã§è¡šãããããšãå€ããããããé åã§è¡šãããåäžã®ã€ã³ã¿ãŒãã§ã€ã¹ãŠãããã«çµåããããšã¯ãå®å šã«äºæ³ãããã¹ãããã§ãã
ãŸããè²ããŒã¿ããã©ã°ã¡ã³ãã·ã§ãŒããŒã«éãåºåå€æ°ã宣èšããå¿ èŠããããŸãã
out vec3 fColor;
ãã©ã°ã¡ã³ãã·ã§ãŒããŒã¯åäžã®ïŒè£éãããïŒã«ã©ãŒå€ãæ³å®ããŠãããããã«ã©ãŒãã¯ãã«ã®é åãéä¿¡ããŠãæå³ããããŸããã ããããããã§fColorãé åã§ã¯ãªããåäžã®ãã¯ãã«ã§ããçç±ã§ã ã é ç¹ãã¹ããŒã³ãããšããããã¯ããããããã©ã°ã¡ã³ãã·ã§ãŒããŒã®åŒã³åºãã®ããã«fColorå€æ°ã«ãã£ãæåŸã®å€ãèšæ¶ããŸãã ãããã£ãŠã家ã®å Žåãé ç¹ã·ã§ãŒããŒã¹ããŒãžããååŸããè²ã§fColorãäžåºŠã ãå¡ãã€ã¶ããŠãå®¶å šäœã®è²ãèšå®ã§ããŸãã
fColor = gs_in[0].color; // gs_in[0] gl_Position = position + vec4(-0.2, -0.2, 0.0, 0.0); // 1:- EmitVertex(); gl_Position = position + vec4( 0.2, -0.2, 0.0, 0.0); // 2:- EmitVertex(); gl_Position = position + vec4(-0.2, 0.2, 0.0, 0.0); // 3:- EmitVertex(); gl_Position = position + vec4( 0.2, 0.2, 0.0, 0.0); // 4:- EmitVertex(); gl_Position = position + vec4( 0.0, 0.4, 0.0, 0.0); // 5: EmitVertex(); EndPrimitive();
ãã®çµæãçæããããã¹ãŠã®é ç¹ã«ã¯ãé ç¹å±æ§ã®è²ã«å¯Ÿå¿ããfColorå€æ°ã®è²ã®å€ãæ ŒçŽãããŸãã çŸåšãå家ã¯ç¬èªã®è²ã§å¡è£ ãããŠããŸãã
ããå°ãåµé æ§ãè¿œå ããä»®æ³ã®å¬ãã¢ã¬ã³ãžããŠã家ã®å±æ ¹ã«éªãæ¯ããããŸãã ãã®æåŸã®é ç¹ã«å¯ŸããŠãçœè²ãåå¥ã«å²ãåœãŠãŸãã
fColor = gs_in[0].color; gl_Position = position + vec4(-0.2, -0.2, 0.0, 0.0); // 1:- EmitVertex(); gl_Position = position + vec4( 0.2, -0.2, 0.0, 0.0); // 2:- EmitVertex(); gl_Position = position + vec4(-0.2, 0.2, 0.0, 0.0); // 3:- EmitVertex(); gl_Position = position + vec4( 0.2, 0.2, 0.0, 0.0); // 4:- EmitVertex(); gl_Position = position + vec4( 0.0, 0.4, 0.0, 0.0); // 5: fColor = vec3(1.0, 1.0, 1.0); EmitVertex(); EndPrimitive();
ãã®çµæã次ã®ããšãã§ããŸãã
ã¢ããªã±ãŒã·ã§ã³ã³ãŒããäŸãšæ¯èŒã§ããŸãã
ãã®æç¹ã§ã幟äœåŠçãªã·ã§ãŒããŒã¯ãåçŽãªããªããã£ãã䜿çšããŠããååãªåµé çå¯èœæ§ãæäŸããããšã¯æ¢ã«æããã ãšæããŸãã ãžãªã¡ããªã¯GPUã®è¶ é«éã³ã¢å ã§åçã«äœæããããããé ç¹ãããã¡ãŒã䜿çšããŠåããžãªã¡ããªãæå®ãããããã¯ããã«å¹ççã§ãã ãžãªã¡ããªã·ã§ãŒããŒã¯ããã¯ã»ã«ã¬ã³ããªã³ã°çšã®ãã¥ãŒãããªãŒãã³ã¹ããŒã¹ã·ãŒã³ã®èã®èãªã©ãåçŽã§é »ç¹ã«ç¹°ãè¿ããã圢ç¶ã®ã¬ã³ããªã³ã°ãæé©åããããã®ååãªæ©äŒãæäŸããŸãã
ãªããžã§ã¯ããççºãããŸã
ãã¡ããã家ãæãããšã¯çŽ æŽãããããšã§ãããç§ãã¡ããã°ãã°ååããªããã°ãªããªããã®ã§ã¯ãããŸããã ãããã£ãŠãç±ãå ããŠã3次å ã¢ãã«ã®ççºã«çŽè¡ããŠãã ããïŒ ããŒãããããããããããŸãé »ç¹ã«è¡ãå¿ èŠã¯ãªãã§ããããã幟äœåŠçã·ã§ãŒããŒã®æ©èœã®åªãããã¢ã³ã¹ãã¬ãŒã·ã§ã³ãšããŠåœ¹ç«ã¡ãŸãã
ãªããžã§ã¯ããçç Žãããšããããšã¯ã貎éãªããŒã¯ã®æåéãã®ç Žå£ãæå³ããã®ã§ã¯ãªããæéã®çµéã«äŒŽãéåžžã®æ¹åã«æ²¿ã£ãåäžè§åœ¢ã®åããæå³ããŸãã ãã®çµæããã®å¹æã¯ãªããžã§ã¯ãã®ççºã®ããã«èŠããæ³ç·ãã¯ãã«ã®æ¹åã«ç§»åããå¥ã ã®äžè§åœ¢ã«åå²ããŸãã 以äžã¯ãããã¹ãŒãã¢ãã«ã«é©çšãããå¹æã§ãã
泚ç®ãã¹ãã¯ããžãªã¡ããªã·ã§ãŒããŒã䜿çšãããšããã®è€éãã«é¢ä¿ãªããä»»æã®ãªããžã§ã¯ãã§ãšãã§ã¯ãã䜿çšã§ããããšã§ãã
æ³ç·ãã¯ãã«ã«æ²¿ã£ãŠäžè§åœ¢ãã·ããããå¿ èŠããããããæåã«èšç®ããå¿ èŠããããŸãã å ·äœçã«ã¯ãäžè§åœ¢ã®è¡šé¢ã«åçŽã§ãé ç¹ã3ã€ãããªããã¯ãã«ãèŠã€ããå¿ èŠããããŸãã å€æã®ã¬ãã¹ã³ãããä»ã®2ã€ã«åçŽãªãã¯ãã«ã¯å€ç©æŒç®ã䜿çšããŠååŸã§ããããšãããããèŠããŠããã§ãããã äžè§åœ¢ã®è¡šé¢ã«å¹³è¡ãª2ã€ã®ãã¯ãã«aãšbãèŠã€ããããšãã§ããå Žåãè¡šé¢ã«åçŽãªãã¯ãã«ã¯åçŽã«ãããã®ãã¯ãã«ç©ã®çµæã«ãªããŸãã å®éã以äžã®ãžãªã¡ããªã·ã§ãŒããŒã³ãŒãã¯ãããå®è¡ããŸããå ¥åäžè§åœ¢ã®3ã€ã®é ç¹ã䜿çšããŠæ³ç·ãã¯ãã«ãèšç®ããŸãã
vec3 GetNormal() { vec3 a = vec3(gl_in[0].gl_Position) - vec3(gl_in[1].gl_Position); vec3 b = vec3(gl_in[2].gl_Position) - vec3(gl_in[1].gl_Position); return normalize(cross(a, b)); }
ããã§ã¯ãæžç®ã䜿çšããŠãäžè§åœ¢ã®è¡šé¢ã«å¹³è¡ãª2ã€ã®ãã¯ãã«aãšbãååŸããŸãã ãã¯ãã«ãæžç®ãããšãå ã®2ã€ã®ãã¯ãã«ã®å·®ã§ããå¥ã®ãã¯ãã«ãåŸãããŸãã 3ã€ã®é ç¹ã¯ãã¹ãŠäžè§åœ¢ã®å¹³é¢ã«ãããããäžè§åœ¢ã®é ç¹ãè¡šããã¯ãã«ã®å·®ã«ãããäžè§åœ¢ã®è¡šé¢ã«å¹³è¡ãªãã¯ãã«ãçæãããŸãã crossïŒïŒé¢æ°ã®ãšã¯ã¹ããŒãã§ã®ãã©ã¡ãŒã¿ãŒã®é åºã«æ³šæããŠãã ãããaãšbã亀æãããšãæ³ç·ãã¯ãã«ã®æ¹åã¯éã«ãªããŸãã
ææã¡ã®æ³ç·ãèŠã€ããæ¹æ³ãã§ãããã explodeïŒïŒé¢æ°ã®å®è£ ã«é²ãããšãã§ããŸãããã®é¢æ°ã¯ãæ³ç·ãã¯ãã«ãšé ç¹ã®äœçœ®ãã¯ãã«ãåãå ¥ããæ³ç·ã«æ²¿ã£ãŠã·ããããæ°ããé ç¹äœçœ®ãè¿ããŸãã
vec4 explode(vec4 position, vec3 normal) { float magnitude = 2.0; vec3 direction = normal * ((sin(time) + 1.0) / 2.0) * magnitude; return position + vec4(direction, 0.0); }
ã³ãŒãã¯ããªãæçœã§ãã sinïŒïŒé¢æ°ã¯ãçŸåšã®æéã«é¢é£ä»ããããæéå€æ°ã«äŸåããéé[-1ãã1.]ã®å€ãå®æçã«è¿ããŸãã å åãã®ççºïŒå ç ŽïŒã®å¹æã¯ç§ãã¡ã«ãšã£ãŠèå³æ·±ããã®ã§ã¯ãªãããã sinïŒïŒã®å€ãåºé[0.ã1.]ã«å¶éããŸãã ããã«ãåŸãããå€ãšå€§ããå¶åŸ¡å®æ°ã¯ãæçµæ¹åãã¯ãã«ã®èšç®ã§æ³ç·ãã¯ãã«ãã¹ã±ãŒãªã³ã°ããããã«äœ¿çšãããŸãã ãã®ãã¯ãã«ã¯ãæ°ãããªãã»ããäœçœ®ãååŸããããã«ãé ç¹äœçœ®ã®å ¥åãã©ã¡ãŒã¿ãŒã«è¿œå ãããŸãã
察å¿ããã¬ãã¹ã³ã®3Dã¢ãã«ãã¡ã€ã«ã®ã¬ã³ããªã³ã°ã³ãŒãã䜿çšããå Žåã®ççºå¹æã®ãžãªã¡ããªã·ã§ãŒããŒã®å®å šãªã³ãŒãã以äžã«ç€ºããŸãã
#version 330 core layout (triangles) in; layout (triangle_strip, max_vertices = 3) out; in VS_OUT { vec2 texCoords; } gs_in[]; out vec2 TexCoords; uniform float time; vec4 explode(vec4 position, vec3 normal) { ... } vec3 GetNormal() { ... } void main() { vec3 normal = GetNormal(); gl_Position = explode(gl_in[0].gl_Position, normal); TexCoords = gs_in[0].texCoords; EmitVertex(); gl_Position = explode(gl_in[1].gl_Position, normal); TexCoords = gs_in[1].texCoords; EmitVertex(); gl_Position = explode(gl_in[2].gl_Position, normal); TexCoords = gs_in[2].texCoords; EmitVertex(); EndPrimitive(); }
åé ç¹ãçæããåã«ã察å¿ãããã¯ã¹ãã£åº§æšãæž¡ãããšã«æ³šæããŠãã ããã
ãŸããã¯ã©ã€ã¢ã³ãã³ãŒãã§åäžãªæéã®å€ãèšå®ããããšãå¿ããªãã§ãã ããã
shader.setFloat("time", glfwGetTime());
çµæã¯ãå®æçã«ççºããŠå ã®ç¶æ ã«æ»ãã¢ãã«ã®ããã·ãŒã³ã§ãã äŸã¯ããããªããšã§ããã幟äœåŠçãªã·ã§ãŒããŒã培åºçã«äœ¿çšããããšã«ã€ãªãããŸãã
çµæã®ã³ãŒãã¯ãäŸãšæ¯èŒã§ããŸãã
æ³ç·ãã¯ãã«ã衚瀺ããŸã
ä»åã¯ããžãªã¡ããªã·ã§ãŒããŒã䜿çšããŠãå®éã«éåžžã«äŸ¿å©ãªãã®ãå®è£ ããããšããŸããã¬ã³ããªã³ã°ããããžãªã¡ããªã®æ³ç·ãã¯ãã«ã衚瀺ããŸãã ã©ã€ãã£ã³ã°ã¢ã«ãŽãªãºã ãå®è£ ãããšãå¿ ç¶çã«å¥åŠãªçµæãšèŠèŠçãªäžå ·åãçºçããŸããããã®åå ãç¹å®ããã®ã¯å®¹æã§ã¯ãããŸããã ã©ã€ãã£ã³ã°ãæäœããéã®æãäžè¬çãªãšã©ãŒã®1ã€ã¯ãé ç¹ããŒã¿ã®èªã¿èŸŒã¿ãšã©ãŒãé ç¹å±æ§ã®åœ¢åŒã®èšå®ãšã©ãŒããŸãã¯ã·ã§ãŒããŒã§ã®åçŽãªå€æãšã©ãŒã«èµ·å ãããäžæ£ãªæ³ç·ã®ã¿ã¹ã¯ã§ãã æäŸãããæ³ç·ã®æ£ç¢ºããå€æããããŒã«ããããšãã°ãããã§ããããæ³ç·ã®èŠèŠåã¯ãã®ãããªããŒã«ã®1ã€ã§ããã幟äœåŠçãªã·ã§ãŒããŒã¯ãã®å®è£ ã®ããã«åçŽã«äœæãããŸãã
èãæ¹ã¯åçŽã§ãããŸããå«ãŸãããžãªã¡ããªã·ã§ãŒããŒãªãã§éåžžã®æ¹æ³ã§ã·ãŒã³ãã¬ã³ããªã³ã°ãã次ã«2åç®ã®ãã¹ãè¡ããŸããããžãªã¡ããªã·ã§ãŒããŒã«ãã£ãŠçæãããæ³ç·ã®ã¿ã衚瀺ããŸããã·ã§ãŒããŒã¯ãå ¥åã§äžè§åœ¢ã¿ã€ãã®ããªããã£ããååŸããåé ç¹ã®äœçœ®ã§æ³ç·ãã¯ãã«ã®æ¹åã«3ã€ã®ã»ã°ã¡ã³ããäœæããŸããæ¬äŒŒã³ãŒãã®åœ¢åŒã§ã¯ã次ã®ããã«ãªããŸãã
shader.use(); DrawScene(); normalDisplayShader.use(); DrawScene();
ä»åããžãªã¡ããªã·ã§ãŒããŒã¯ããªã³ã¶ãã©ã€ã§èšç®ããã®ã§ã¯ãªããé ç¹å±æ§ãšããŠæå®ãããæ³ç·ã䜿çšããŸãããžãªã¡ããªã·ã§ãŒãã¯ãå ¥åãšããŠã¯ãªããïŒã®ç©ºéã«ãããäœçœ®ãã¯ãã«ãããã¯ãªãã空éïŒã®ã§ãæã ã¯ãåã空éã«æ³ç·ãã¯ãã«ãå€æããªããã°ãªããŸããããããããããè¡ãåã«ãæ³ç·è¡åã®å©ããåããŠæ³ç·ãã¯ãã«ãå€æããå¿ èŠããããŸã-ããã¯ã¹ã±ãŒãªã³ã°ãšå転ãèæ ®ããæ¹æ³ã§ãïŒãã¥ãŒãšã¢ãã«è¡åã«ãã£ãŠäžããããŸãïŒãããã¯ãã¹ãŠé ç¹ã·ã§ãŒããŒã§è¡ãããŸãã
#version 330 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec3 aNormal; out VS_OUT { vec3 normal; } vs_out; uniform mat4 projection; uniform mat4 view; uniform mat4 model; void main() { gl_Position = projection * view * model * vec4(aPos, 1.0); mat3 normalMatrix = mat3(transpose(inverse(view * model))); vs_out.normal = normalize(vec3(projection * vec4(normalMatrix * aNormal, 0.0))); }
ã¯ãªããã¹ããŒã¹ã«å€æãããæ³ç·ãã¯ãã«ã¯ãã€ã³ã¿ãŒãã§ã€ã¹ãŠããããä»ããŠæ¬¡ã®ã·ã§ãŒããŒã¹ããŒãžã«æž¡ãããŸãããžãªã¡ããªã·ã§ãŒããŒã¯ãé ç¹ã®å±æ§ïŒäœçœ®ãšæ³ç·ãã¯ãã«ïŒãèªã¿åããåé ç¹ã®äœçœ®ã«æ³ç·æ¹åã«ã»ã°ã¡ã³ãã衚瀺ããŸãã
#version 330 core layout (triangles) in; layout (line_strip, max_vertices = 6) out; in VS_OUT { vec3 normal; } gs_in[]; const float MAGNITUDE = 0.4; void GenerateLine(int index) { gl_Position = gl_in[index].gl_Position; EmitVertex(); gl_Position = gl_in[index].gl_Position + vec4(gs_in[index].normal, 0.0) * MAGNITUDE; EmitVertex(); EndPrimitive(); } void main() { GenerateLine(0); // GenerateLine(1); // ... GenerateLine(2); // ... }
çŸæç¹ã§ã¯ãã³ãŒãã«è¿œå ã®èª¬æã¯å¿ èŠãªããšæããŸããæ³ç·ãã¯ãã«ã¯MAGNITUDEå®æ°ã䜿çšããŠã¹ã±ãŒãªã³ã°ãããããšã«æ³šæããŠãã ãããããã«ããã衚瀺ãããã»ã°ã¡ã³ãã®é·ããå¶éã§ããŸãïŒããã§ãªãå Žåããããã«å€§ããããŸãïŒã
æ³ç·ã®åºåã¯äž»ã«ãããã°ã®ç®çã«äœ¿çšãããããããã©ã°ã¡ã³ãã·ã§ãŒããŒã䜿çšããŠåãè²ã®ç·ã§ç°¡åã«è¡šç€ºã§ããŸãã
#version 330 core out vec4 FragColor; void main() { FragColor = vec4(1.0, 1.0, 0.0, 1.0); }
ãã®çµæãéåžžã®ã·ã§ãŒããŒã䜿çšããã¢ãã«ã¬ã³ãã©ãŒãšãæ°ããéåžžã®å¯èŠåã·ã§ãŒããŒã䜿çšããåã¬ã³ããªã³ã°ã®çµã¿åããã«ããã次ã®å³ãåŸãããŸãã
ããã¹ãŒãããããã³ã°ããŒãã®æ¯ãããããã®ç·ã®ããã«èŠãããšããäºå®ãç¡èŠãããšãã·ãŒã³ã§äœ¿çšãããŠããã¢ãã«ã®æ³ç·ãã¯ãã«ã®æ£ç¢ºããå€æããããã®éåžžã«äŸ¿å©ãªæ¹æ³ãåŸãããšãã§ããŸããããã§ããã¡ãŒãšãã§ã¯ããå®è£ ããã·ã§ãŒããŒã§ã䌌ããããªãã®ã䜿çšãããŠããããšãæããã«ãªããŸããã
ãã®äŸã®ãœãŒã¹ã³ãŒãã¯ãã¡ãã§ãã
PSïŒè»¢éã調æŽããããã®é»å ±confããããŸãã翻蚳ãæäŒããããšããçå£ãªé¡æãããã°ã倧æè¿ã§ãïŒ