Exemplo n.º 1
0
        override public void draw(ref Geometry.Geometry g, ref Matrix4 model, ref Matrix4 vp)
        {
            // bind shader
            GL.Disable(EnableCap.DepthTest);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
            mShader.bind();

            Matrix4 modelIT = model.Inverted();

            modelIT.Transpose();

            GL.Enable(EnableCap.Blend);
            mShader.uploadAttrib <int>("indices", g.mGeometryIndices.Length, 3, 4, VertexAttribPointerType.UnsignedInt, false, ref g.mGeometryIndices, 0);
            mShader.uploadAttrib <float>("position", g.mGeometry.Count(), 3, 4, VertexAttribPointerType.Float, false, ref g.mGeometry, 0);
            if (g.mNormals == null)
            {
                Math.addNormalsToMesh(g); // TODO: I literally hate this, but it was easier than patching Eric's code.
            }
            if (g.mNormals != null)
            {
                mShader.uploadAttrib <float>("normal", g.mNormals.Count(), 3, 4, VertexAttribPointerType.Float, false, ref g.mNormals, 0);
            }
            GL.Uniform1(mShader.uniform("alpha"), mAlpha);
            GL.ProgramUniformMatrix4(mShader.mProgramShader, mShader.uniform("viewProjTransform"), false, ref vp);
            GL.ProgramUniformMatrix4(mShader.mProgramShader, mShader.uniform("modelTransform"), true, ref model);
            GL.ProgramUniformMatrix4(mShader.mProgramShader, mShader.uniform("modelInvTrans"), true, ref modelIT);

            mShader.drawIndexed(g.primitiveType, 0, g.mNumPrimitives);
            GL.Disable(EnableCap.Blend);
            GL.Disable(EnableCap.DepthTest);
        }
Exemplo n.º 2
0
        public void getMatrixHeadtoCamera(uint primaryDeviceIndex)
        {
            Vector3 center = new Vector3(0, 0, 0);

            if (vr_points.Count < 8)
            {
                //only use left controller for the calibration
                center = Math.GetTranslationVector3(UtilOld.getControllerTipPosition(ref mScene, primaryDeviceIndex == mScene.leftControllerIdx));
                Rhino.RhinoApp.WriteLine(center.ToString());

                vr_points.Add(new MCvPoint3D32f(center.X, center.Y, center.Z));
                calib_status = 0;
            }
            else
            {
                marker_points.Add(new MCvPoint3D32f(0, 0, 0));
                marker_points.Add(new MCvPoint3D32f(3, 0, 0));
                marker_points.Add(new MCvPoint3D32f(3, 3, 0));
                marker_points.Add(new MCvPoint3D32f(0, 3, 0));

                marker_points.Add(new MCvPoint3D32f(0, 0, -4.0f));
                marker_points.Add(new MCvPoint3D32f(3.0f, 0, -4.0f));
                marker_points.Add(new MCvPoint3D32f(3.0f, 3.0f, -4.0f));
                marker_points.Add(new MCvPoint3D32f(0, 3.0f, -4.0f));

                CvInvoke.EstimateAffine3D(vr_points.ToArray(), marker_points.ToArray(), out mVRtoMarker, out inliers, 3, 0.99);

                glmVRtoMarker = new Matrix4(
                    (float)mVRtoMarker[0, 0], (float)mVRtoMarker[0, 1], (float)mVRtoMarker[0, 2], (float)mVRtoMarker[0, 3],
                    (float)mVRtoMarker[1, 0], (float)mVRtoMarker[1, 1], (float)mVRtoMarker[1, 2], (float)mVRtoMarker[1, 3],
                    (float)mVRtoMarker[2, 0], (float)mVRtoMarker[2, 1], (float)mVRtoMarker[2, 2], (float)mVRtoMarker[2, 3],
                    0, 0, 0, 1
                    );

                calib_status = 1;
                Rhino.RhinoApp.WriteLine("VRtoMarker matrix");
                Rhino.RhinoApp.WriteLine(glmVRtoMarker.ToString());

                vr_points.Clear();
                marker_points.Clear();

                //Thread start to calculate the view matrix
                ThreadEnd    = false;
                UpdateThread = new Thread(new ThreadStart(Camera_UpdateThread));
                UpdateThread.Start();
            }
        }
Exemplo n.º 3
0
        public override void draw(ref Geometry.Geometry g, ref Matrix4 model, ref Matrix4 vp)
        {
            UtilOld.depthSort(model * vp, g);
            GL.Disable(EnableCap.DepthTest);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
            GL.Enable(EnableCap.Blend);
            Matrix4 modelIT = model.Inverted();

            modelIT.Transpose();
            int dim;

            if (g.primitiveType == BeginMode.Lines)
            {
                dim = 2;
            }
            else
            {
                dim = 3;
            }
            mShader.bind();
            mShader.uploadAttrib <int>("indices", g.mGeometryIndices.Length, dim, 4, VertexAttribPointerType.UnsignedInt, false, ref g.mGeometryIndices, 0);
            mShader.uploadAttrib <float>("position", g.mGeometry.Count(), 3, 4, VertexAttribPointerType.Float, false, ref g.mGeometry, 0);
            if (g.mNormals == null)
            {
                Math.addNormalsToMesh(g);
            }
            if (g.mNormals != null)
            {
                mShader.uploadAttrib <float>("normal", g.mNormals.Count(), 3, 4, VertexAttribPointerType.Float, false, ref g.mNormals, 0);
            }
            GL.Uniform4(mShader.uniform("color"), mColor);
            GL.UniformMatrix4(mShader.uniform("modelTransform"), true, ref model);
            GL.UniformMatrix4(mShader.uniform("viewProjTransform"), false, ref vp); // TODO: Fix this

            GL.ProgramUniformMatrix4(mShader.mProgramShader, mShader.uniform("modelInvTrans"), true, ref modelIT);
            GL.Uniform3(mShader.uniform("lightInt"), 3 * MAX_LIGHT_COUNT, ref lightIntensities[0].X);
            GL.Uniform3(mShader.uniform("lightPos"), 3 * MAX_LIGHT_COUNT, ref lightPositions[0].X);
            mShader.drawIndexed(g.primitiveType, 0, g.mNumPrimitives);
            GL.Disable(EnableCap.Blend);
            GL.Enable(EnableCap.DepthTest);
        }
Exemplo n.º 4
0
        // I'm doing this with too many loops but whatever.
        // Also I'm shattering the ability to put multiple meshes in one file.
        void LoadObjFile(string filename)
        {
            ObjParser.Obj obj = new ObjParser.Obj();
            obj.LoadObj(filename);
            mGeometry        = new float[3 * 3 * obj.FaceList.Count];
            mGeometryIndices = new int[3 * obj.FaceList.Count];
            mNormals         = new float[3 * 3 * obj.FaceList.Count];
            mUvs             = new float[2 * 3 * obj.FaceList.Count];

            // This is the smart indexed way, but I need flat shading right now.
            //int i = 0;
            //foreach (var v in obj.VertexList)
            //{
            //    mGeometry[3 * i + 0] = (float)v.X;
            //    mGeometry[3 * i + 1] = (float)v.Y;
            //    mGeometry[3 * i + 2] = (float)v.Z;
            //    i++;
            //}
            //i = 0;
            //foreach (var f in obj.FaceList)
            //{
            //    mGeometryIndices[3 * i + 0] = f.VertexIndexList[0] - 1;
            //    mGeometryIndices[3 * i + 1] = f.VertexIndexList[1] - 1;
            //    mGeometryIndices[3 * i + 2] = f.VertexIndexList[2] - 1;
            //    i++;
            //}

            mNumPrimitives = obj.FaceList.Count;
            primitiveType  = OpenTK.Graphics.OpenGL4.BeginMode.Triangles;
            int i = 0;

            Vector3[] normalCreators = new Vector3[3];
            Vector3   n;

            foreach (var f in obj.FaceList)
            {
                for (int v = 0; v < 3; v++)
                {
                    normalCreators[v]            = new Vector3((float)obj.VertexList.ElementAt(f.VertexIndexList[v] - 1).X, (float)obj.VertexList.ElementAt(f.VertexIndexList[v] - 1).Y, (float)obj.VertexList.ElementAt(f.VertexIndexList[v] - 1).Z);
                    mGeometry[9 * i + 3 * v + 0] = (float)obj.VertexList.ElementAt(f.VertexIndexList[v] - 1).X;
                    mGeometry[9 * i + 3 * v + 1] = (float)obj.VertexList.ElementAt(f.VertexIndexList[v] - 1).Y;
                    mGeometry[9 * i + 3 * v + 2] = (float)obj.VertexList.ElementAt(f.VertexIndexList[v] - 1).Z;
                }
                n = Math.calculateFaceNormal(normalCreators[0], normalCreators[1], normalCreators[2]);
                for (int j = 0; j < 3; j++)
                {
                    mNormals[9 * i + 3 * j + 0] = n.X;
                    mNormals[9 * i + 3 * j + 1] = n.Y;
                    mNormals[9 * i + 3 * j + 2] = n.Z;
                }

                if (obj.TextureList.Count > 0)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        mUvs[6 * i + 2 * j + 0] = (float)obj.TextureList[f.TextureVertexIndexList.ElementAt(j) - 1].X;
                        mUvs[6 * i + 2 * j + 1] = (float)obj.TextureList[f.TextureVertexIndexList.ElementAt(j) - 1].Y;
                    }
                }
                i++;
            }

            for (i = 0; i < mGeometryIndices.Length; i++)
            {
                mGeometryIndices[i] = i;
            }
        }