From d5957a5e72bb2df9117b3a1fb58ef911586fa588 Mon Sep 17 00:00:00 2001 From: Bergmann89 Date: Fri, 26 Sep 2014 18:52:37 +0200 Subject: [PATCH] * added small example code snippets --- uglcArrayBuffer.pas | 75 ++++++++++++++++++++++++++++++++++++++++++++++- uglcCamera.pas | 19 +++++++++++- uglcFrameBufferObject.pas | 32 +++++++++++++++++++- uglcShader.pas | 50 ++++++++++++++++++++++++++++++- 4 files changed, 172 insertions(+), 4 deletions(-) diff --git a/uglcArrayBuffer.pas b/uglcArrayBuffer.pas index e7acaa2..8a65b62 100644 --- a/uglcArrayBuffer.pas +++ b/uglcArrayBuffer.pas @@ -2,7 +2,80 @@ unit uglcArrayBuffer; { Package: OpenGLCore Prefix: glc - OpenGL Core - Beschreibung: diese Unit enthält eine Klassen-Kapselung für OpenGL Array Buffer } + Beschreibung: diese Unit enthält eine Klassen-Kapselung für OpenGL Array Buffer + Beispiel: + type + TVertex = packed record + pos: TgluVector3f; // vertex position + tex: TgluVector2f; // texture coordinates + nor: TgluVector3f; // normal + end; + PVertex = ^TVertex; + + var + vBuffer: TglcArrayBuffer; + iBuffer: TglcArrayBuffer; + p: Pointer; + + vBuffer := TglcArrayBuffer.Create(TglcBufferTarget.btArrayBuffer); + iBuffer := TglcArrayBuffer.Create(TglcBufferTarget.btElementArrayBuffer); + try + // write vertex data to vertex buffer + vBuffer.Bind; + vBuffer.BufferData(4, SizeOf(TresMeshVertex), TglcBufferUsage.buStaticDraw, nil); + p := vBuffer.MapBuffer(TglcBufferAccess.baWriteOnly); + try + PVertex(p).pos := gluVertex3f(0.0, 0.0, 0.0); + PVertex(p).tex := gluVertex2f(0.0, 0.5); + PVertex(p).nor := gluVertex3f(0.0, 1.0, 0.0); + inc(p, SizeOf(TVertex)); + // ... + finally + vBuffer.UnmapBuffer; + vBuffer.Unbind; + end; + + // write indices to index buffer + iBuffer.Bind; + iBuffer.BufferData(4, SizeOf(GLuint), TglcBufferUsage.buStaticDraw, nil); + p := iBuffer.MapBuffer(TglcBufferAccess.baWriteOnly); + try + PGLuint(p) := 0; + // ... + finally + iBuffer.UnmapBuffer; + iBuffer.Unbind; + end; + + // use array buffers to draw primitive + vBuffer.Bind; + iBuffer.Bind; + + glEnableClientState(GL_VERTEX_ARRAY); + glVertexPointer(3, GL_FLOAT, 8, Pointer(0)); + + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glTexCoordPointer(2, GL_FLOAT, 8, Pointer(3)); + + glEnableClientState(GL_NORMAL_ARRAY); + glNormalPointer(GL_FLOAT, 8, Pointer(5)); + + glEnableClientState(GL_INDEX_ARRAY); + glIndexPointer(GL_INT, 0, nil); + + glDrawElements(GL_QUADS, iBuffer.DataCount, GL_UNSIGNED_INT, nil); + + glDisableClientState(GL_INDEX_ARRAY); + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glDisableClientState(GL_NORMAL_ARRAY); + + fIndexBuffer.Unbind; + fVertexBuffer.Unbind; + finally + FreeAndNil(vBuffer); + FreeAndNil(iBuffer); + end; } {$mode objfpc}{$H+} diff --git a/uglcCamera.pas b/uglcCamera.pas index 93a3bf7..197f676 100644 --- a/uglcCamera.pas +++ b/uglcCamera.pas @@ -2,7 +2,24 @@ unit uglcCamera; { Package: OpenGLCore Prefix: glc - OpenGL Core - Beschreibung: diese Unit enthält eine Klassen-Kapselung für OpenGL Frustum und Kamera } + Beschreibung: diese Unit enthält eine Klassen-Kapselung für OpenGL Frustum und Kamera + Beispiel: + var + camera: TglcCamera; + + camera := TglcCamera.Create; + try + camera.Perspective(45, 0.01, 100, 800/600); // define perspective view + camera.Move(gluVector(2, 3, -5)); // move 2 right, 3 up and 5 back + camera.Tilt(-25); // turn 25 degrees down + camera.Turn(-10); // turn 10 degrees left + camera.Activate; // activate camera + + // do normal rendering + + finally + FreeAndNil(camera); + end; } {$mode objfpc}{$H+} diff --git a/uglcFrameBufferObject.pas b/uglcFrameBufferObject.pas index 7fcc673..d468b9d 100644 --- a/uglcFrameBufferObject.pas +++ b/uglcFrameBufferObject.pas @@ -2,7 +2,37 @@ unit uglcFrameBufferObject; { Package: OpenGLCore Prefix: glc - OpenGL Core - Beschreibung: diese Unit enthält eine Klassen-Kapselung der OpenGL FrameBufferObjekte } + Beschreibung: diese Unit enthält eine Klassen-Kapselung der OpenGL FrameBufferObjekte + Beispiel: + var + fbo: TglcFrameBufferObject; + tex: TglcTextureBuffer; + buf: TglcRenderBuffer; + + fbo := TglcFrameBufferObject; + try + ffbo.SetSize(800, 600); + + // creating texture buffer as color buffer + tex := TglcTextureBuffer.Create(TglcFormat.fmRGBA, TglcInternalFormat.ifRGBA16F); + fbo.AddBuffer(tex, TglcAttachment.atColor0, true); + + // creating render buffer as depth buffer + buf := TglcRenderBuffer.Create(TglcInternalFormat.ifDepthComponent); + fbo.AddBuffer(buf, TglcAttachment.atDepth, true); + + // render to frame buffer object + fbo.Bind; + // do normal rendering + fbo.Unbind; + + // use texture buffer + tex.Bind; + // do normal rendering + tex.Unbind; + finally + FreeAndNil(fbo); + end; } {$mode objfpc}{$H+} diff --git a/uglcShader.pas b/uglcShader.pas index 37f84c9..4235108 100644 --- a/uglcShader.pas +++ b/uglcShader.pas @@ -2,7 +2,55 @@ unit uglcShader; { Package: OpenGLCore Prefix: glc - OpenGL Core - Beschreibung: diese Unit enthält eine Klassen-Kapselung der OpenGL Shader Objekte } + Beschreibung: diese Unit enthält eine Klassen-Kapselung der OpenGL Shader Objekte + Beispiel: + var + shader: TglcShaderProgram; + + //write log message to console + // @param aSender: object that send the message + // @param aMsg: message to write to console + procedure LogMessage(aSender: TObject; const aMsg: String); + begin + writeln(Format('[%p]: %s', [aSender, aMsg]); + end; + + //load shader object from file and add it to 'shader' + // @param aFilename: name of file to load shader code from + // @param aType: type of shader object to create + procedure LoadShaderObject(const aFilename: String; const aType: TglcShaderType); + var + sl: TStringList; + so: TglcShaderObject; + begin + sl := TStringList.Create; + try + sl.LoadFromFile(aFileName); + so := TglcShaderObject.Create(aType); + shader.add(so); + finally + FreeAndNil(sl, @LogMessage); + end; + end; + + shader := TglcShaderProgram.Create(@LogMessage); + try + // load shader objects + LoadShaderObject('./test_shader.vert', TglcShaderType.stVertex); + LoadShaderObject('./test_shader.frag', TglcShaderType.stFragment); + + // compile shader + shader.Compile; + + // use shader + shader.Enable; + shader.Uniform1f('uTest', 0.1234); + // do normal rendering + shader.Disable; + + finally + FreeAndNil(shader); + end; } {$mode objfpc}{$H+} -- 2.1.4