- procedure TForm1.FormCreate(Sender: TObject);
- const
- vertexSource: PAnsiChar =
- '#version 330'#10 +
- 'uniform mat4 MVPMatrix;'#10 +
- 'in vec3 VertexPosition;'#10 +
- 'in vec4 VertexColor;'#10 +
- 'out vec4 Color;'#10 +
- 'void main()'#10 +
- '{'#10 +
- ' gl_Position = MVPMatrix * vec4(VertexPosition, 1.0);'#10 +
- ' Color = VertexColor;'#10 +
- '}';
- fragmentSource: PAnsiChar =
- '#version 330'#10 +
- 'in vec4 Color;'#10 +
- 'out vec4 FragColor;'#10 +
- 'void main()'#10 +
- '{'#10 +
- ' FragColor = Color;'#10+
- '}';
- vertices: array[0..8] of GLfloat = (
- 0,1,0,
- -1,-1,0,
- 1,-1,0
- );
- colors: array[0..11] of GLfloat = (
- 1,1,0,1,
- 0,1,1,1,
- 1,0,1,1
- );
- begin
- FDC := GetDC(Handle);
- FRC := CreateWGLRenderingContext(FDC, TGLOption.Create(8,8,8,8,24,8,True), 4, 0).RenderingContext;
- wglMakeCurrent(FDC, FRC);
- FVertexShader := glCreateShader(GL_VERTEX_SHADER);
- glShaderSource(FVertexShader, 1, @vertexSource, nil);
- glCompileShader(FVertexShader); {Status abgefragt:OK}
- FFragmentShader := glCreateShader(GL_FRAGMENT_SHADER);
- glShaderSource(FFragmentShader, 1, @fragmentSource, nil);
- glCompileShader(FFragmentShader); {Status abgefragt:OK}
- FProgram := glCreateProgram;
- glAttachShader(FProgram, FVertexShader);
- glAttachShader(FProgram, FFragmentShader);
- glLinkProgram(FProgram); {Status abgefragt:OK}
- glUseProgram(FProgram);
- FMVPMatrixLocation := glGetUniformLocation(FProgram, 'MVPMatrix');
- FPositionLocation := glGetAttribLocation(FProgram, 'VertexPosition');
- FColorLocation := glGetAttribLocation(FProgram, 'VertexColor');
- glGenVertexArrays(1, @FVertexArray);
- glBindVertexArray(FVertexArray); {GL_NO_ERROR}
- glGenBuffers(1, @FPositionBuffer);
- glBindBuffer(GL_ARRAY_BUFFER, FPositionBuffer); {GL_NO_ERROR}
- glBufferData(GL_ARRAY_BUFFER, SizeOf(vertices), @vertices, GL_STATIC_DRAW); {GL_NO_ERROR}
- glVertexAttribPointer(FPositionLocation, 3, GL_FLOAT, GL_FALSE, 0, nil); {GL_NO_ERROR}
- glEnableVertexAttribArray(FPositionLocation); {GL_NO_ERROR}
- glGenBuffers(1, @FColorBuffer);
- glBindBuffer(GL_ARRAY_BUFFER, FColorBuffer); {GL_NO_ERROR}
- glBufferData(GL_ARRAY_BUFFER, SizeOf(colors), @colors, GL_STATIC_DRAW); {GL_NO_ERROR}
- glVertexAttribPointer(FColorLocation, 4, GL_FLOAT, GL_FALSE, 0, nil); {GL_NO_ERROR}
- glEnableVertexAttribArray(FColorLocation); {GL_NO_ERROR}
- glBindVertexArray(0);
- glClearColor(0, 0, 1, 1);
- glClearDepth(1.0);
- glDepthFunc(GL_LEQUAL);
- glEnable(GL_DEPTH_TEST); {GL_NO_ERROR}
- Application.OnIdle := AppIdle;
- end;
- procedure TForm1.AppIdle(Sender: TObject; var Done: Boolean);
- var AFov: ovrFovPort;
- finalUp, finalForward, shiftedEyePos: TVector3D;
- model, view, proj, mvp: TMatrix3D;
- begin
- Done := False;
- glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT); {GL_NO_ERROR}
- with afov do
- begin
- uptan:=1.329; {von der Oculus-Demo abgeschrieben}
- downtan:=1.329;
- lefttan:=1.0587;
- righttan:=1.0924;
- end;
- finalUp := Vector3D(0,1,0,0); {von der Oculus-Demo abgeschrieben}
- finalForward := Vector3D(0,0,-1,0);
- shiftedEyePos:= Vector3D(0, 0, 1);
- model:=IdentityMatrix3D;
- view := MatrixLookAtRH(shiftedEyePos, shiftedEyePos + finalForward, finalUp);
- proj := TMatrix3D(ovrMatrix4f_Projection(AFov, 0.01, 10000.0, True));
- TransposeMatrix3D(proj);
- mvp := model * view * proj;
- glViewPort(0,0,800,600); {GL_NO_ERROR}
- glUniformMatrix4fv(FMVPMatrixLocation, 1, GL_FALSE, @mvp); {GL_NO_ERROR}
- glBindVertexArray(FVertexArray); {GL_NO_ERROR}
- glEnableVertexAttribArray(FColorLocation); {GL_NO_ERROR}
- glDrawArrays(GL_TRIANGLES, 0, 3); {GL_NO_ERROR}
- glBindVertexArray(0);
- end;