OpenSourceのpasGltfライブラリの使い方の解説です
かなり大きく難解なライブラリなのでページを作りました
ほぼ自分用です
pasGltfViewer.dpr
draw関数内の
629行から
行列を使ったカメラの設定
アニメーションの設定
スカイボックスやテクスチャやその他OpenGLの設定
シェーダーとglDrawArrayでの描画
描画部分
かなり大きく難解なライブラリなのでページを作りました
ほぼ自分用です
pasGltfViewer.dpr
draw関数内の
629行から
行列を使ったカメラの設定
ViewMatrix:=MatrixLookAt(Vector3Add(Center, Vector3ScalarMul(Vector3Normalize(Vector3(sin(CameraRotationX*PI*2.0)*cos(-CameraRotationY*PI*2.0), sin(-CameraRotationY*PI*2.0), cos(CameraRotationX*PI*2.0)*cos(-CameraRotationY*PI*2.0))), Max(Max(Bounds[0],Bounds[1]),Bounds[2])*3.0*Zoom)), Center, Vector3(0.0,1.0,0.0)); ProjectionMatrix:=Matrix4x4ProjectionReversedZ(45.0,ViewPortWidth/ViewPortHeight,1e-3); end;646行
アニメーションの設定
if assigned(GLTFInstance) then begin GLTFInstance.Scene:=SceneIndex; GLTFInstance.Animation:=AnimationIndex; if (LastAnimationIndex<>AnimationIndex) and Shadows then begin LastAnimationIndex:=AnimationIndex; // GLTFInstance.UpdateWorstCaseStaticBoundingBox; end; if AnimationTime<AnimationBeginTime then begin AnimationTime:=AnimationBeginTime; end; if AnimationTime>AnimationEndTime then begin AnimationTime:=Modulo(AnimationTime-AnimationBeginTime,AnimationEndTime-AnimationBeginTime)+AnimationBeginTime; if AnimationTime>=AnimationEndTime then begin AnimationTime:=AnimationEndTime; end; end; GLTFInstance.AnimationTime:=AnimationTime; GLTFInstance.Update; GLTFInstance.Upload; end;680行
スカイボックスやテクスチャやその他OpenGLの設定
begin glBindFrameBuffer(GL_FRAMEBUFFER,HDRSceneFBO.FBOs[0]); glDrawBuffer(GL_COLOR_ATTACHMENT0); glViewport(0,0,HDRSceneFBO.Width,HDRSceneFBO.Height); glClearColor(0.0,0.0,0.0,0.0); glClearDepth(0.0); glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT); glClipControl(GL_LOWER_LEFT,GL_ZERO_TO_ONE); glDepthFunc(GL_GEQUAL); SkyBoxViewProjectionMatrix:=ViewMatrix; SkyBoxViewProjectionMatrix[3]:=0.0; SkyBoxViewProjectionMatrix[7]:=0.0; SkyBoxViewProjectionMatrix[11]:=0.0; SkyBoxViewProjectionMatrix[12]:=0.0; SkyBoxViewProjectionMatrix[13]:=0.0; SkyBoxViewProjectionMatrix[14]:=0.0; SkyBoxViewProjectionMatrix:=MatrixMul(SkyBoxViewProjectionMatrix,ProjectionMatrix); begin glDisable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_CUBE_MAP,EnvMapTextureHandle); EnvMapDrawShader.Bind; glUniform1i(EnvMapDrawShader.uTexture,0); glUniformMatrix4fv(EnvMapDrawShader.uViewProjectionMatrix,1,false,@SkyBoxViewProjectionMatrix); glBindVertexArray(EmptyVertexArrayObjectHandle); glDrawArrays(GL_TRIANGLES,0,36); glBindVertexArray(0); EnvMapDrawShader.Unbind; end; begin glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D,BRDFLUTFBO.TextureHandles[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_CUBE_MAP,EnvMapFBO.TextureHandles[0]); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); if Shadows then begin glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D,ShadowMapFBOs[2].TextureHandles[0]); end; glActiveTexture(GL_TEXTURE0); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); for ShadingShader in ShadingShaders do begin ShadingShader.Bind; glUniform1i(ShadingShader.uEnvMapMaxLevel,Min(EnvMapFBO.WorkMaxLevel,16)); glUniform1i(ShadingShader.uShadows,ord(Shadows) and 1); ShadingShader.Unbind; end; if assigned(GLTFInstance) then begin GLTFInstance.DrawFinal(TPasGLTF.TMatrix4x4(Pointer(@ModelMatrix)^), TPasGLTF.TMatrix4x4(Pointer(@ViewMatrix)^), TPasGLTF.TMatrix4x4(Pointer(@ProjectionMatrix)^), ShadingShaders[false,false], ShadingShaders[false,true], ShadingShaders[true,false], ShadingShaders[true,true]); if ShowJoints then begin GLTFInstance.DrawJoints(TPasGLTF.TMatrix4x4(Pointer(@ModelMatrix)^), TPasGLTF.TMatrix4x4(Pointer(@ViewMatrix)^), TPasGLTF.TMatrix4x4(Pointer(@ProjectionMatrix)^), SolidColorShader); end; end; end; glClipControl(GL_LOWER_LEFT,GL_NEGATIVE_ONE_TO_ONE); end;749行
シェーダーとglDrawArrayでの描画
begin glBindFrameBuffer(GL_FRAMEBUFFER,LDRSceneFBO.FBOs[0]); glDrawBuffer(GL_COLOR_ATTACHMENT0); glViewport(0,0,LDRSceneFBO.Width,LDRSceneFBO.Height); glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT); glDisable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D,HDRSceneFBO.TextureHandles[0]); HDRToLDRShader.Bind; glUniform1i(HDRToLDRShader.uTexture,0); glBindVertexArray(EmptyVertexArrayObjectHandle); glDrawArrays(GL_TRIANGLES,0,3); glBindVertexArray(0); HDRToLDRShader.Unbind; end;760行
描画部分
begin glBindFrameBuffer(GL_FRAMEBUFFER,0); glDrawBuffer(GL_BACK); glViewport(0,0,ViewPortWidth,ViewPortHeight); glClearColor(0.0,0.0,0.0,0.0); glClearDepth(1.0); glViewport(ViewPortX,ViewPortY,ViewPortWidth,ViewPortHeight); glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT); glDisable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D,LDRSceneFBO.TextureHandles[0]); AntialiasingShader.Bind; glUniform1i(AntialiasingShader.uTexture,0); glBindVertexArray(EmptyVertexArrayObjectHandle); glDrawArrays(GL_TRIANGLES,0,3); glBindVertexArray(0); AntialiasingShader.Unbind; glDisable(GL_BLEND); ConsoleInstance.Draw(DeltaTime,ViewPortX,ViewPortY,ViewPortWidth,ViewPortHeight); glEnable(GL_DEPTH_TEST); end;
コメントをかく