From ff16b4dd097930e3eae26f0d9c29613543e4eff0 Mon Sep 17 00:00:00 2001 From: Bergmann89 Date: Sun, 21 Dec 2014 02:55:09 +0100 Subject: [PATCH] * ArrayBuffer: fixed some small bugs for OpenGL ES * Camera: added projection matrix for frustum * Shader: added BindAttribLocation and GetAttribLocation --- uglcArrayBuffer.pas | 20 +++++++---- uglcCamera.pas | 95 ++++++++++++++++++++++++++++++++++++++++++++++------- uglcShader.pas | 45 ++++++++++--------------- 3 files changed, 115 insertions(+), 45 deletions(-) diff --git a/uglcArrayBuffer.pas b/uglcArrayBuffer.pas index 87814e6..5d6f8de 100644 --- a/uglcArrayBuffer.pas +++ b/uglcArrayBuffer.pas @@ -137,9 +137,10 @@ begin {$IFNDEF OPENGL_ES} result := glMapBuffer(GLenum(fTarget), GLenum(aAccess)); {$ELSE} - if not GL_OES_mapbuffer then + if GL_OES_mapbuffer then + result := glMapBufferOES(GLenum(fTarget), GLenum(aAccess)) + else raise EglcArrayBuffer.Create('map buffer is not supported by video card'); - result := glMapBufferOES(GLenum(fTarget), GLenum(aAccess)); {$ENDIF} glcCheckAndRaiseError; end; @@ -152,9 +153,12 @@ begin raise EglcArrayBuffer.Create('map buffer range is not supported by video card'); result := glMapBufferRange(GLenum(fTarget), aOffset, aSize, GLenum(aAccess)); {$ELSE} - if not GL_VERSION_3_0 then + if GL_VERSION_3_0 then + result := glMapBufferRange(GLenum(fTarget), aOffset, aSize, GLenum(aAccess)) + else if GL_EXT_map_buffer_range then + result := glMapBufferRangeEXT(GLenum(fTarget), aOffset, aSize, GLenum(aAccess)) + else raise EglcArrayBuffer.Create('map buffer range is not supported by video card'); - result := glMapBufferRange(GLenum(fTarget), aOffset, aSize, GLenum(aAccess)); {$ENDIF} end; @@ -164,9 +168,12 @@ begin {$IFNDEF OPENGL_ES} glUnmapBuffer(GLenum(fTarget)); {$ELSE} - if not (GL_OES_mapbuffer or GL_VERSION_3_0) then + if GL_VERSION_3_0 then + glUnmapBuffer(GLenum(fTarget)) + else if GL_OES_mapbuffer then + glUnmapBufferOES(GLenum(fTarget)) + else raise EglcArrayBuffer.Create('unmap buffer is not supported by video card'); - glUnmapBuffer(GLenum(fTarget)); {$ENDIF} end; @@ -192,6 +199,7 @@ begin if not GL_VERSION_2_0 then raise EglcArrayBuffer.Create('Create - VertexBuffer: not supported'); {$ENDIF} + glGetError(); inherited Create; glGenBuffers(1, @fID); fDataCount := 0; diff --git a/uglcCamera.pas b/uglcCamera.pas index a61fffe..43110d0 100644 --- a/uglcCamera.pas +++ b/uglcCamera.pas @@ -33,25 +33,30 @@ type //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TglcFrustum = class(TObject) private + fProjMatrix: TgluMatrix4f; + function GetProjMatrixPtr: Pointer; function GetWidth: Single; function GetHeight: Single; function GetFOVAngle: Single; function GetAspectRatio: Single; + procedure UpdateProjMatrix; protected fIsOrthogonal: Boolean; fTop, fBottom, fLeft, fRight, fNear, fFar: Single; public - property Top : Single read fTop; - property Bottom : Single read fBottom; - property Left : Single read fLeft; - property Right : Single read fRight; - property Near : Single read fNear; - property Far : Single read fFar; - property Width : Single read GetWidth; - property Height : Single read GetHeight; - property FOVAngle : Single read GetFOVAngle; - property AspectRatio : Single read GetAspectRatio; - property IsOrthogonal: Boolean read fIsOrthogonal; + property Top: Single read fTop; + property Bottom: Single read fBottom; + property Left: Single read fLeft; + property Right: Single read fRight; + property Near: Single read fNear; + property Far: Single read fFar; + property Width: Single read GetWidth; + property Height: Single read GetHeight; + property FOVAngle: Single read GetFOVAngle; + property AspectRatio: Single read GetAspectRatio; + property IsOrthogonal: Boolean read fIsOrthogonal; + property ProjMatrix: TgluMatrix4f read fProjMatrix; + property ProjMatrixPtr: Pointer read GetProjMatrixPtr; procedure Frustum(const aLeft, aRight, aBottom, aTop, aNear, aFar: Single); procedure Perspective(const aFOVAngle, aAspectRatio, aNear, aFar: Single); @@ -68,8 +73,10 @@ type TglcCamera = class(TglcFrustum) private fPosition: TgluMatrix4f; + function GetPositionPtr: Pointer; public - property Position: TgluMatrix4f read fPosition write fPosition; + property Position: TgluMatrix4f read fPosition write fPosition; + property PositionPtr: Pointer read GetPositionPtr; procedure Move(const aVec: TgluVector3f); procedure Tilt(const aAngle: Single); @@ -86,15 +93,70 @@ implementation uses Math, {$IFNDEF OPENGL_ES}dglOpenGL{$ELSE}dglOpenGLES{$ENDIF}; + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //TglcFrustum/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +procedure TglcFrustum.UpdateProjMatrix; +begin + if fIsOrthogonal then begin + fProjMatrix[maAxisX] := gluVector4f( + 2 / (fRight - fLeft), + 0, + 0, + 0); + fProjMatrix[maAxisY] := gluVector4f( + 0, + 2 / (fTop - fBottom), + 0, + 0); + fProjMatrix[maAxisZ] := gluVector4f( + 0, + 0, + -2 / (fFar - fNear), + 0); + fProjMatrix[maPos] := gluVector4f( + -(fRight + fLeft) / (fRight - fLeft), + -(fTop + fBottom) / (fTop - fBottom), + -(fFar + fNear) / (fFar - fNear), + 1); + end else begin + fProjMatrix[maAxisX] := gluVector4f( + 2 * fNear / (fRight - fLeft), + 0, + 0, + 0); + fProjMatrix[maAxisY] := gluVector4f( + 0, + 2 * fNear / (fTop - fBottom), + 0, + 0); + fProjMatrix[maAxisZ] := gluVector4f( + (fRight + fLeft) / (fRight - fLeft), + (fTop + fBottom) / (fTop - fBottom), + -(fFar + fNear) / (fFar - fNear), + -1); + fProjMatrix[maPos] := gluVector4f( + 0, + 0, + -2 * fFar * fNear / (fFar - fNear), + 0); + end; +end; + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TglcFrustum.GetWidth: Single; begin result := (fRight - fLeft); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +function TglcFrustum.GetProjMatrixPtr: Pointer; +begin + result := @fProjMatrix[0, 0]; +end; + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TglcFrustum.GetHeight: Single; begin result := (fTop - fBottom); @@ -122,6 +184,7 @@ begin fRight := aTop; fNear := aNear; fFar := aFar; + UpdateProjMatrix; end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -134,6 +197,7 @@ begin fBottom := -fTop; fRight := aAspectRatio * fTop; fLeft := -fRight; + UpdateProjMatrix; end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -146,6 +210,7 @@ begin fBottom := aBottom; fNear := aNear; fFar := aFar; + UpdateProjMatrix; end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -209,6 +274,12 @@ end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //TglcCamera//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +function TglcCamera.GetPositionPtr: Pointer; +begin + result := @fPosition[0, 0]; +end; + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TglcCamera.Move(const aVec: TgluVector3f); begin fPosition := gluMatrixMult(gluMatrixTranslate(aVec), fPosition); diff --git a/uglcShader.pas b/uglcShader.pas index 0708643..bcc2360 100644 --- a/uglcShader.pas +++ b/uglcShader.pas @@ -145,6 +145,8 @@ type function GetUniformfv(const aName: String; aP: PGLfloat): Boolean; function GetUniformfi(const aName: String; aP: PGLint): Boolean; + procedure BindAttribLocation(const aName: String; const aAttribIndex: GLint); + function GetAttribLocation(const aName: String): Integer; function HasUniform(const aName: String): Boolean; procedure LoadFromFile(const aFilename: String); @@ -193,10 +195,10 @@ end; //@result: TRUE wenn ohne Fehler kompiliert, sonst FALSE; function TglcShaderObject.GetCompiled: Boolean; var - value: glInt; + value: GLint; begin glGetShaderiv(fShaderObj, GL_COMPILE_STATUS, @value); - result := (value = GL_TRUE); + result := (value = GLint(GL_TRUE)); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -329,7 +331,7 @@ var value: glInt; begin glGetProgramiv(fProgramObj, GL_LINK_STATUS, @value); - result := (value = GL_TRUE); + result := (value = GLint(GL_TRUE)); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -771,6 +773,19 @@ begin end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +procedure TglcShaderProgram.BindAttribLocation(const aName: String; const aAttribIndex: GLint); +begin + CreateProgramObj; + glBindAttribLocation(fProgramObj, aAttribIndex, PGLchar(aName)); +end; + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +function TglcShaderProgram.GetAttribLocation(const aName: String): Integer; +begin + result := glGetAttribLocation(fProgramObj, PGLchar(aName)); +end; + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TglcShaderProgram.HasUniform(const aName: String): Boolean; var pos: GLint; @@ -862,30 +877,6 @@ begin rx.Free; sl.Free; end; - - - { - if Assigned(aStream) then begin - Clear; - fFilename := ''; - reader := TutlStreamReader.Create(aStream); - try - if reader.ReadAnsiString <> GLSL_FILE_HEADER then - raise EglcShader.Create('TglShaderProgram.SaveToStream - incompatible file'); - v := reader.ReadInteger; - - if v >= 100 then begin //version 1.00 - c := reader.ReadInteger; - for i := 0 to c-1 do begin - Add(TglcShaderObject.Create(Cardinal(reader.ReadInteger), fOnLog)); - Last.fCode := reader.ReadAnsiString; - end; - end; - finally - reader.Free; - end; - end else - } end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -- 2.1.4