Exemple #1
0
        //For use with solid colors

        public cTexture(cTextureInfo ptextureinfo)
        {
            _transparent = false;
            _usemipmap   = true;
            uint bytesperpixel = 4;

            _pixeldataformat = 4;
            int imagecx = _cx = ptextureinfo._width;
            int imagecy = _cy = ptextureinfo._height;

            _imageaspect = ( double )_cx / ( double )_cy;               /* By default we set the aspect to match the aspect
                                                                         * of the  image we loaded, before doing the makePowersOfTwo call. Even though we are going
                                                                         * to clamp the texture to powers of two, if we apply it to a rectangle with proportions
                                                                         * matching _imageaspect, the image will appear unstretched; the two stretches cancel out. */

            makePowersOfTwo(ref _cx, ref _cy);
            if (_cx != imagecx || _cy != imagecy)               //Need to rescale
            {
                uint arraysize =
                    bytesperpixel * ( uint )_cx * ( uint )_cy;
                byte [] pdatascaled = new byte[arraysize];
                GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1);
                GL.PixelStore(PixelStoreParameter.PackAlignment, 1);
                Glu.ScaleImage(
                    (_pixeldataformat == 3) ? PixelFormat.Rgb : PixelFormat.Rgba,
                    imagecx, imagecy, PixelType.UnsignedByte,
                    ptextureinfo.Data, _cx, _cy, PixelType.UnsignedByte,
                    pdatascaled);
                string errorstring = Glu.ErrorString(GL.GetError());
                ptextureinfo.resetData(_cx, _cy, pdatascaled);
            }

            _maketexture(ptextureinfo.Data);
        }
Exemple #2
0
        public bool Lookup(string skinfilename, out cTextureInfo ptextureinfo)
        {
            bool success;
            int  skinfileKey = _skinFilenamePool.lookupKey(skinfilename);

            ptextureinfo = (cTextureInfo)this[skinfileKey];
            if (ptextureinfo != null) //We've already read in and saved the _pdata before.
            {
                return(true);
            }
            //Otherwise create and register a new cTextureInfo* object that's loaded with the skinfile.
            ptextureinfo = new cTextureInfo();
            success      = ptextureinfo.LoadPCXTexture(skinfilename);
            if (success) //Save the ptextureinfo
            {
                this[skinfileKey] = ptextureinfo;
            }

            return(success);
        }
Exemple #3
0
        public     vector_t[] vertexList;       // vertex list


        public bool Load(string modelfilename, string skinfilename)
        {
            //First load the modelfilename==================
            byte[] buffer;             // file buffer

            modelHeader_t modelHeader; // model header

            frame_t frame;             // frame data
            int     vertexListPos;     // index variable
            int     i, j;              // index variables

            // open the model file and read entire file into buffer
            buffer = File.ReadAllBytes(modelfilename);


            // extract model file header from buffer
            int c = 0;

            modelHeader.ident = BitConverter.ToInt32(buffer, c);
            c += 4;
            modelHeader.version = BitConverter.ToInt32(buffer, c);
            c += 4;
            modelHeader.skinwidth = BitConverter.ToInt32(buffer, c);
            c += 4;
            modelHeader.skinheight = BitConverter.ToInt32(buffer, c);
            c += 4;
            modelHeader.framesize = BitConverter.ToInt32(buffer, c);
            c += 4;
            modelHeader.numSkins = BitConverter.ToInt32(buffer, c);
            c += 4;
            modelHeader.numXYZ = BitConverter.ToInt32(buffer, c);
            c += 4;
            modelHeader.numST = BitConverter.ToInt32(buffer, c);
            c += 4;
            modelHeader.numTris = BitConverter.ToInt32(buffer, c);
            c += 4;
            modelHeader.numGLcmds = BitConverter.ToInt32(buffer, c);
            c += 4;
            modelHeader.numFrames = BitConverter.ToInt32(buffer, c);
            c += 4;
            modelHeader.offsetSkins = BitConverter.ToInt32(buffer, c);
            c += 4;
            modelHeader.offsetST = BitConverter.ToInt32(buffer, c);
            c += 4;
            modelHeader.offsetTris = BitConverter.ToInt32(buffer, c);
            c += 4;
            modelHeader.offsetFrames = BitConverter.ToInt32(buffer, c);
            c += 4;
            modelHeader.offsetGLcmds = BitConverter.ToInt32(buffer, c);
            c += 4;
            modelHeader.offsetEnd = BitConverter.ToInt32(buffer, c);
            c += 4;

            vertexList = new vector_t[modelHeader.numXYZ * modelHeader.numFrames];
            for (i = 0; i < modelHeader.numXYZ * modelHeader.numFrames; i++)
            {
                vertexList[i] = new vector_t(0);
            }

            numVertices = modelHeader.numXYZ;    //Vertices per frame
            numFrames   = modelHeader.numFrames;
            frameSize   = modelHeader.framesize; //Size of a frame in bytes

            for (j = 0; j < numFrames; j++)
            {
                c        = modelHeader.offsetFrames + frameSize * j;
                frame    = new frame_t(0);
                frame.fp = new framePoint_t[numVertices];
                for (int vernum = 0; vernum < numVertices; vernum++)
                {
                    frame.fp[vernum] = new framePoint_t(0);
                }
                for (int scalenum = 0; scalenum < 3; scalenum++)
                {
                    frame.scale[scalenum] = BitConverter.ToSingle(buffer, c);
                    c += 4;
                }
                for (int translatenum = 0; translatenum < 3; translatenum++)
                {
                    frame.translate[translatenum] = BitConverter.ToSingle(buffer, c);
                    c += 4;
                }
                for (int namenum = 0; namenum < 16; namenum++)
                {
                    frame.name[namenum] = (char)buffer[c++];
                }
                for (int vernum = 0; vernum < numVertices; vernum++)
                {
                    for (int vnum = 0; vnum < 3; vnum++)
                    {
                        frame.fp[vernum].v[vnum] = buffer[c++];
                    }
                    c++;    // I had to put this in to compensate for boundary alignment -- JC
                }
                vertexListPos = numVertices * j;
                for (i = 0; i < numVertices; i++)
                {
                    vertexList[vertexListPos + i].point[0] = frame.scale[0] * frame.fp[i].v[0] + frame.translate[0];
                    vertexList[vertexListPos + i].point[1] = frame.scale[1] * frame.fp[i].v[1] + frame.translate[1];
                    vertexList[vertexListPos + i].point[2] = frame.scale[2] * frame.fp[i].v[2] + frame.translate[2];
                }
            }

            _numTriangles = modelHeader.numTris;
            _triIndex     = new mesh_t[_numTriangles];
            for (int n = 0; n < _numTriangles; n++)
            {
                _triIndex[n] = new mesh_t(0);
            }

            // set c to triangle indexes in buffer
            c = modelHeader.offsetTris;

            for (i = 0; i < _numTriangles; i++)
            {
                _triIndex[i].meshIndex[0] = BitConverter.ToUInt16(buffer, c);
                c += 2;
                _triIndex[i].meshIndex[1] = BitConverter.ToUInt16(buffer, c);
                c += 2;
                _triIndex[i].meshIndex[2] = BitConverter.ToUInt16(buffer, c);
                c += 2;
                _triIndex[i].stIndex[0] = BitConverter.ToUInt16(buffer, c);
                c += 2;
                _triIndex[i].stIndex[1] = BitConverter.ToUInt16(buffer, c);
                c += 2;
                _triIndex[i].stIndex[2] = BitConverter.ToUInt16(buffer, c);
                c += 2;
            }

            /* Now get the cTextureInfo* object, which will have the effect of registering
             * the filename and creating and retgistering a cTextureInfo*, if this is the first time
             * the skin file name is used. */
            cTextureInfo ptextureinfo = null;
            bool         success      = cTextureInfo._mapFilenameToTextureInfo.Lookup(skinfilename, out ptextureinfo);

            if (!success || ptextureinfo == null)
            {
                return(false);
            }

            //Use the model info and skin info together to initialize texture coords st
            int imagewidth = ptextureinfo._width;

            int imageheight = ptextureinfo._height;

            /* The stPtr[i].s and stPtr[i].s are actually pixel count
             * numbers, as in "m-th pixel over and n-th pixel down in my skin file".
             * We convert them into proportions between 0.0 and 1.0 relative to the
             * size of the original skin image that was saved with the MD2 model
             * and save these values in the st[i].s and st[i].t so the OpenGL
             * texture can use them.
             *  Even if we plan to rescale the image that won't in fact affect the
             * proportional locations of the texture points since we will still think
             * of the map as a swatch in the plane from corner (0.0,0.0) to corner
             * (1.0, 1.0).  So there seems to be no need to compute here the rescaled
             * image size with cTexture::makePowersOfTwo(imagewidth, imageheight);
             * and then use it in any fashion. */
            numST = modelHeader.numST;
            st    = new texCoord_t[numST];
            c     = modelHeader.offsetST;

            for (i = 0; i < numST; i++)
            {
                float s = BitConverter.ToInt16(buffer, c);
                c += 2;
                float t = BitConverter.ToInt16(buffer, c);
                c      += 2;
                st[i].s = s / (float)imagewidth;
                st[i].t = t / (float)imageheight;
            }

            return(true);
        }