/// <summary> /// OnRenderFrame is called on every frame for rendering /// </summary> /// <param name="e">event arguments</param> protected override void OnRenderFrame(FrameEventArgs e) { base.OnRenderFrame(e); // Clear the color & depth buffer gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); // Start with identity every frame _viewMatrix.Identity(); // View transform: move the coordinate system away from the camera _viewMatrix.Translate(0, 0, _camZ); // View transform: rotate the coordinate system increasingly _viewMatrix.Rotate(_rotX + _deltaX, 1, 0, 0); _viewMatrix.Rotate(_rotY + _deltaY, 0, 1, 0); // Transform light position & direction into view space SLVec3f lightPosVS = _viewMatrix * _lightPos; // The light dir is not a position. We only take the rotation of the mv matrix. SLMat3f viewRot = _viewMatrix.Mat3(); SLVec3f lightDirVS = viewRot * _lightDir; // Rotate the model so that we see it _modelMatrix.Identity(); _modelMatrix.Rotate(90, -1, 0, 0); // Build the combined modelview-projection matrix SLMat4f mvp = new SLMat4f(_projectionMatrix); SLMat4f mv = new SLMat4f(_viewMatrix); mv.Multiply(_modelMatrix); mvp.Multiply(mv); // Build normal matrix SLMat3f nm = mv.InverseTransposed(); // Pass the matrix uniform variables unsafe { gl.UniformMatrix4(_mvMatrixLoc, 1, false, mv.m); gl.UniformMatrix3(_nMatrixLoc, 1, false, nm.m); gl.UniformMatrix4(_mvpMatrixLoc, 1, false, mvp.m); // Pass lighting uniforms variables gl.Uniform4(_globalAmbiLoc, 1, (float[])_globalAmbi); gl.Uniform3(_lightPosVSLoc, 1, (float[])lightPosVS); gl.Uniform3(_lightSpotDirVSLoc, 1, (float[])lightDirVS); gl.Uniform4(_lightAmbientLoc, 1, (float[])_lightAmbient); gl.Uniform4(_lightDiffuseLoc, 1, (float[])_lightDiffuse); gl.Uniform4(_lightSpecularLoc, 1, (float[])_lightSpecular); gl.Uniform4(_matAmbientLoc, 1, (float[])_matAmbient); gl.Uniform4(_matDiffuseLoc, 1, (float[])_matDiffuse); gl.Uniform4(_matSpecularLoc, 1, (float[])_matSpecular); gl.Uniform4(_matEmissiveLoc, 1, (float[])_matEmissive); } gl.Uniform1(_matShininessLoc, _matShininess); gl.Uniform1(_texture0Loc, 0); ////////////////////// // Draw with 2 VBOs // ////////////////////// // Enable all of the vertex attribute arrays gl.EnableVertexAttribArray(_pLoc); gl.EnableVertexAttribArray(_nLoc); gl.EnableVertexAttribArray(_tLoc); // Activate VBOs gl.BindBuffer(BufferTarget.ArrayBuffer, _vboV); gl.BindBuffer(BufferTarget.ElementArrayBuffer, _vboI); // Activate Texture gl.BindTexture(TextureTarget.Texture2D, _textureID); // For VBO only offset instead of data pointer int stride = 32; int offsetN = 3 * sizeof(float); int offsetT = 6 * sizeof(float); gl.VertexAttribPointer(_pLoc, 3, VertexAttribPointerType.Float, false, stride, 0); gl.VertexAttribPointer(_nLoc, 3, VertexAttribPointerType.Float, false, stride, offsetN); gl.VertexAttribPointer(_tLoc, 2, VertexAttribPointerType.Float, false, stride, offsetT); ///////////////////////////////////////////////////////////////////////////// // Draw cube model triangles by indexes gl.DrawElements(BeginMode.Triangles, _numI, DrawElementsType.UnsignedInt, 0); ///////////////////////////////////////////////////////////////////////////// // Deactivate buffers gl.BindBuffer(BufferTarget.ArrayBuffer, 0); gl.BindBuffer(BufferTarget.ElementArrayBuffer, 0); // Disable the vertex arrays gl.DisableVertexAttribArray(_pLoc); gl.DisableVertexAttribArray(_nLoc); gl.DisableVertexAttribArray(_tLoc); // Fast copy the back buffer to the front buffer. This is OS dependent. SwapBuffers(); // Check for errors glUtils.GetGLError("OnRenderFrame", true); }
/// <summary> /// The forms paint routine where all drawing happens. /// </summary> private void frmHelloCube_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; #region graphicsSetup g.SmoothingMode = SmoothingMode.AntiAlias; e.Graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy; e.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed; e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor; e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed; #endregion addFps(); zBuffer.Reset(); #region cameraMovement // start with identity every frame m_viewMatrix.Identity(); // view transform: move the coordinate system away from the camera m_viewMatrix.Translate(m_cam); // add new Rotations for camera m_viewMatrix.Rotate(m_rotAngleUp + (cursorPosition.y - preCursorPosition.y), new SLVec3f(1, 0, 0)); m_viewMatrix.Rotate(m_rotAngleSide + (cursorPosition.x - preCursorPosition.x), new SLVec3f(0, 1, 0)); #endregion using (BmpG bmpGraphics = new BmpG(ClientRectangle.Width, ClientRectangle.Height, zBuffer, light)) { #region graphicsMode bmpGraphics.phong = phongActive; bmpGraphics.wireframe = xWireframeActive; bmpGraphics.showZ = zShowActive; #endregion foreach (Mesh mesh in meshes) { // all transformed vertecies of the mesh are temporary saved in vertex2 List <SLVertex> vertex2 = new List <SLVertex>(); // Vertex Shader #region transformPipeline SLMat4f mv = new SLMat4f(m_viewMatrix); mv.Multiply(mesh.modelMatrix); SLMat3f nm = new SLMat3f(mv.InverseTransposed()); // build combined matrix out of viewport, projection & modelview matrix SLMat4f mvp = new SLMat4f(); mvp.Multiply(m_viewportMatrix); // screen mvp.Multiply(m_projectionMatrix); // projektion mvp.Multiply(mv); // kamera & view (cube) for (int n = 0; n < mesh.vertices.Length; n++) { vertex2.Add(new SLVertex(mvp.Multiply(mesh.vertices[n].position), nm.Multiply(mesh.vertices[n].normale), mesh.color, mv.Multiply(mesh.vertices[n].position))); } #endregion // Fragment Shader drawVertices(vertex2, mesh.indices, m_cam, bmpGraphics); } // Pixel output g.DrawImageUnscaled(bmpGraphics.Result(), 0, 0); } // Tell the system that the window should be repaint again this.Invalidate(); }