Beispiel #1
0
        private void timerUpdateFrame_Tick(object sender, EventArgs e)
        {
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, ibo_elements);
            GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indicedata.Length * sizeof(int)), indicedata, BufferUsageHint.StaticDraw);

            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo_position);
            GL.BufferData <Vector3>(BufferTarget.ArrayBuffer, (IntPtr)(vertdata.Length * Vector3.SizeInBytes), vertdata, BufferUsageHint.StaticDraw);
            GL.VertexAttribPointer(attribute_vpos, 3, VertexAttribPointerType.Float, false, 0, 0);

            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo_color);
            GL.BufferData <Vector3>(BufferTarget.ArrayBuffer, (IntPtr)(coldata.Length * Vector3.SizeInBytes), coldata, BufferUsageHint.StaticDraw);
            GL.VertexAttribPointer(attribute_vcol, 3, VertexAttribPointerType.Float, true, 0, 0);

            GL.UseProgram(pgmID);
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);

            var perspectiveFov = Matrix4.CreatePerspectiveFieldOfView(ExtensionGL.Radians(FOV), aspect, ZNEAR, ZFAR);

            foreach (Volume v in objs)
            {
                v.ViewProjectionMatrix      = camera.GetViewMatrix() * perspectiveFov;
                v.ModelViewProjectionMatrix = transform * v.ModelMatrix * v.ViewProjectionMatrix;
            }
            panelOpenGL.Invalidate();
        }
Beispiel #2
0
        private void getRotateAroundCamera(Point endPoint)
        {
            float adjustPos = (float)Math.Tan(ExtensionGL.Radians(FOV / 2.0f)) * 0.1f / (panelOpenGL.Height / 2.0f);
            float dx        = (endPoint.X - startPoint.X);
            float dy        = (endPoint.Y - startPoint.Y);
            float dxmin     = panelOpenGL.Width / 100f;
            float dymin     = panelOpenGL.Height / 100f;

            float x = (Math.Abs(dx) >= dxmin) ? dx * adjustPos : 0;
            float y = (Math.Abs(dy) >= dymin) ? dy * adjustPos : 0;

            Vector3 perp  = new Vector3(-y, -x, 0f);
            float   theta = (float)Math.Atan(perp.Length / 0.1f);

            //Compute the length of the perpendicular vector
            if (perp.Length > float.Epsilon)             //if its non-zero
            {
                // Return the perpendicular vector as the transform after all
                rotation.X = perp.X;
                rotation.Y = perp.Y;
                rotation.Z = perp.Z;
                //In the quaternion values, w is cosine (theta / 2), where theta is the rotation angle
                rotation.W = (float)Math.Cos(theta / 2);

                var trans = new Transform3DGroup();
                trans.TranslateBy(-camera.Position);
                trans.RotateBy(rotation);
                trans.TranslateBy(camera.Position);
                currentTrans = trans;

                updateModelMatricies();
            }
        }
Beispiel #3
0
        private Vector3 getPointerPositionOnObjectPlane(Point endPoint)
        {
            // 3D line equation by 2 points camera position "Vc" and near plane position "Vn" (from mouse position)
            // V = Vc + t*(a,b,c)
            // with (a,b,c) = Vn - Vc;
            float   adjustPos = (float)Math.Tan(ExtensionGL.Radians(FOV / 2.0f)) * 0.1f / (panelOpenGL.Height / 2.0f);
            float   dx        = (endPoint.X - panelOpenGL.Width / 2);
            float   dy        = (endPoint.Y - panelOpenGL.Height / 2);
            float   xn        = dx * adjustPos;
            float   yn        = -dy * adjustPos;
            Vector3 vn        = new Vector3(xn, yn, camera.Position.Z - ZNEAR);
            Vector3 vc        = camera.Position;
            Vector3 abc       = vn - vc;

            Matrix4 tmpTrans     = getPreviousTransform();
            Vector3 objectOrigin = tmpTrans.ExtractTranslation();
            Vector3 v            = new Vector3();

            v.Z = objectOrigin.Z;

            float t = (v.Z - vc.Z) / abc.Z;

            v.X = t * abc.X;
            v.Y = t * abc.Y;
            return(v);
        }
Beispiel #4
0
        private void panelOpenGL_Resize(object sender, EventArgs e)
        {
            GL.Viewport(panelOpenGL.ClientRectangle);
            aspect = panelOpenGL.ClientSize.Width / (float)panelOpenGL.ClientSize.Height;
            var projection = Matrix4.CreatePerspectiveFieldOfView(ExtensionGL.Radians(FOV), aspect, ZNEAR, ZFAR);

            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadMatrix(ref projection);
        }
Beispiel #5
0
        private void initProgram()
        {
            camera.Position = new Vector3(0f, 0f, 1f);
            pgmID           = GL.CreateProgram();
            ExtensionGL.LoadShader("Shaders/vs.glsl", ShaderType.VertexShader, pgmID, out vsID);
            ExtensionGL.LoadShader("Shaders/fs.glsl", ShaderType.FragmentShader, pgmID, out fsID);
            GL.LinkProgram(pgmID);
            attribute_vpos = GL.GetAttribLocation(pgmID, "vPos");
            attribute_vcol = GL.GetAttribLocation(pgmID, "vColor");
            uniform_mview  = GL.GetUniformLocation(pgmID, "modelView");
            if (attribute_vpos == -1 || attribute_vcol == -1 || uniform_mview == -1)
            {
                MessageBox.Show("Error binding attributes [vPos vColor modelView]" +
                                $"= [{attribute_vpos} {attribute_vcol} {uniform_mview}]", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            GL.GenBuffers(1, out vbo_position);
            GL.GenBuffers(1, out vbo_color);
            GL.GenBuffers(1, out vbo_mview);
            GL.GenBuffers(1, out ibo_elements);
        }