public static int decodeColor(int value, MKDS_Course_Editor.nclr_e.CColorFormat format) { int[] res = format.getResolution(); int rgb = Color.FromArgb(255, 0, 0, 0).ToArgb(); int shift = 0; int length = res.Length / 2; for (int i = 0; i < length; i++) { int mode = res[length - i - 1]; int nshift = res[length * 2 - i - 1]; int mult = shiftList[nshift][1]; int and = shiftList[nshift][0]; int n = (value >> shift & and) * mult; switch (mode) { case 0: // '\0' rgb |= n << 24; break; case 1: // '\001' rgb |= n << 16; break; case 2: // '\002' rgb |= n << 8; break; case 3: // '\003' rgb |= n; break; case 4: // '\004' rgb = n << 16 | n << 8 | n; break; } shift += nshift; } return rgb; }
//MTX44[] mt; /// <summary> /// Render model to OpenGL surface. /// </summary> public void RenderModel(string file2, MKDS_Course_Editor.NSBTA.NSBTA.NSBTA_File ani, int[] aniframeS, int[] aniframeT, int[] aniframeScaleS, int[] aniframeScaleT, int[] aniframeR, MKDS_Course_Editor.NSBCA.NSBCA.NSBCA_File ca, RenderMode r, bool anim, bool anim2, int selectedanim, float X, float Y, float dist, float elev, float ang, bool licht, MKDS_Course_Editor.NSBTP.NSBTP.NSBTP_File p, Nsbmd nsb) { MTX44 tmp = new MTX44(); file = file2; for (var j = 0; j < Model.Polygons.Count - 1; j++) { var poly = Model.Polygons[j]; int matid = poly.MatId; var mat = Model.Materials[matid]; } int light = Gl.glIsEnabled(Gl.GL_LIGHTING); Gl.glDisable(Gl.GL_LIGHTING); Gl.glLineWidth(2.0F); /*float xmin = Model.boundXmin; float ymin = Model.boundYmin; float zmin = Model.boundZmin; float xmax = Model.boundXmax + Model.boundXmin; float ymax = Model.boundYmax +Model.boundYmin; float zmax = -Model.boundZmax +Model.boundZmin; float[][] box = {new float[] { xmin, ymin, zmin }, new float[]{ xmax, ymin, zmin }, new float[]{ xmax, ymin, zmin }, new float[]{ xmax, ymax, zmin }, new float[]{ xmax, ymax, zmin }, new float[]{ xmin, ymax, zmin }, new float[]{ xmin, ymax, zmin }, new float[]{ xmin, ymax, zmax }, new float[]{ xmin, ymax, zmax }, new float[]{ xmin, ymin, zmax }, new float[]{ xmin, ymin, zmax }, new float[]{ xmax, ymin, zmax }, new float[]{ xmax, ymin, zmax }, new float[]{ xmax, ymax, zmax }, new float[]{ xmin, ymin, zmin }, new float[]{ xmin, ymax, zmin }, new float[]{ xmin, ymin, zmin }, new float[]{ xmin, ymin, zmax }, new float[]{ xmax, ymin, zmin }, new float[]{ xmax, ymin, zmax }, new float[]{ xmax, ymax, zmin }, new float[]{ xmax, ymax, zmax }, new float[]{ xmin, ymax, zmax }, new float[]{ xmax, ymax, zmax } }; Gl.glColor3f(1.0F, 1.0F, 1.0F); Gl.glBegin(1); for(int i = 0; i < box.Length; i++) Gl.glVertex3f(box[i][0], box[i][1], box[i][2]); Gl.glEnd();*/ if (light == 1) { Gl.glEnable(Gl.GL_LIGHTING); } Gl.glLineWidth(1.0F); //float[] fmatrix = new float[64 * 16]; //float[] jmatrix = new float[Model.Objects.Count * 16]; //////////////////////////////////////////////////////////// // prepare the matrix stack for (var i = 0; i < Model.Objects.Count; i++) { var obj = Model.Objects[i]; var m_trans = obj.TransVect; if (obj.RestoreID != -1) Gl.glLoadMatrixf(MatrixStack[obj.RestoreID].Floats); if (obj.StackID != -1) { if (obj.Trans) Gl.glTranslatef(m_trans[0], m_trans[1], m_trans[2]); /*if ( obj.rot ) { mtx_Rotate( &tmp, obj->pivot, obj->neg, obj->a, obj->b ); glMultMatrixf( (GLfloat *)&tmp ); } // TODO */ Gl.glGetFloatv(Gl.GL_MODELVIEW_MATRIX, MatrixStack[obj.StackID].Floats); stackID = obj.StackID; // save the last stackID } } Gl.glLoadIdentity(); //////////////////////////////////////////////////////////// // display one polygon of the current model at a time //Todo //////////////////////////////////////////////////////////// // display all polygons of the current model for (var i = 0; i < Model.Polygons.Count - 1; i++) { var poly = Model.Polygons[i]; if (gOptTexture && !gOptWireFrame && g_mat) { int matid = poly.MatId; if (matid == -1) { Gl.glBindTexture(Gl.GL_TEXTURE_2D, 0); if (writevertex) { mattt.Add(new System.Windows.Media.ImageBrush()); } } else { if (writevertex) { mattt.Add(matt[matid]); } NsbmdMaterial mat = Model.Materials[matid]; if ((mat.format == 1 || mat.format == 6) && r != RenderMode.Translucent) continue; if ((mat.format == 0 || mat.format == 2 || mat.format == 3 || mat.format == 4 || mat.format == 5 || mat.format == 7) && r != RenderMode.Opaque) continue; Gl.glBindTexture(Gl.GL_TEXTURE_2D, matid + 1 + matstart); // Convert pixel coords to normalised STs Gl.glMatrixMode(Gl.GL_TEXTURE); Gl.glLoadIdentity(); if (p.Header.file_size != 0 && new List<string>(p.MPT.names).Contains(mat.MaterialName)) { NsbmdMaterial mmm = mat; int texid = 0; for (int l = 0; l < nsb.Textures.Count; l++) { if (nsb.Textures[l].texname == p.AnimData[new List<string>(p.MPT.names).IndexOf(mat.MaterialName)].KeyFrames[frame_[new List<string>(p.MPT.names).IndexOf(mat.MaterialName)]].texName) { texid = l; break; } } mmm.spdata = nsb.Textures[texid].spdata; mmm.texdata = nsb.Textures[texid].texdata; mmm.texname = nsb.Textures[texid].texname; mmm.texoffset = nsb.Textures[texid].texoffset; mmm.texsize = nsb.Textures[texid].texsize; mmm.width = nsb.Textures[texid].width; mmm.height = nsb.Textures[texid].height; mmm.format = nsb.Textures[texid].format; mmm.color0 = nsb.Textures[texid].color0; int palid = 0; for (int l = 0; l < nsb.Textures.Count; l++) { if (nsb.Palettes[l].palname == p.AnimData[new List<string>(p.MPT.names).IndexOf(mat.MaterialName)].KeyFrames[frame_[new List<string>(p.MPT.names).IndexOf(mat.MaterialName)]].palName) { palid = l; break; } } mmm.paldata = nsb.Palettes[palid].paldata; mmm.palname = nsb.Palettes[palid].palname; mmm.paloffset = nsb.Palettes[palid].paloffset; mmm.palsize = nsb.Palettes[palid].palsize; MakeTexture(matid, mmm); if (anim2) { if (nr[new List<string>(p.MPT.names).IndexOf(mat.MaterialName)] == Math.Round((float)(p.MPT.infoBlock.Data[new List<string>(p.MPT.names).IndexOf(mat.MaterialName)].Unknown1) / 512f)) { nr[new List<string>(p.MPT.names).IndexOf(mat.MaterialName)] = 0; if (frame[new List<string>(p.MPT.names).IndexOf(mat.MaterialName)] == p.MPT.NoFrames - 1) { frame[new List<string>(p.MPT.names).IndexOf(mat.MaterialName)] = 0; frame_[new List<string>(p.MPT.names).IndexOf(mat.MaterialName)] = 0; } else { frame[new List<string>(p.MPT.names).IndexOf(mat.MaterialName)]++; if (p.AnimData[new List<string>(p.MPT.names).IndexOf(mat.MaterialName)].KeyFrames.Length != frame_[new List<string>(p.MPT.names).IndexOf(mat.MaterialName)] + 1) { if (frame[new List<string>(p.MPT.names).IndexOf(mat.MaterialName)] == p.AnimData[new List<string>(p.MPT.names).IndexOf(mat.MaterialName)].KeyFrames[frame_[new List<string>(p.MPT.names).IndexOf(mat.MaterialName)] + 1].Start) { frame_[new List<string>(p.MPT.names).IndexOf(mat.MaterialName)]++; } } } } else { nr[new List<string>(p.MPT.names).IndexOf(mat.MaterialName)]++;//= (float)p.MPT.infoBlock.Data[new List<string>(p.MPT.names).IndexOf(mat.MaterialName)].Unknown1 / 4096f; } } } try { if (ani.Header.file_size != 0 && new List<string>(ani.MAT.names).Contains(mat.MaterialName)) { int index = new List<string>(ani.MAT.names).IndexOf(mat.MaterialName); Gl.glScaled((double)ani.SRTData[index].scaleS[aniframeScaleS[index]], (double)ani.SRTData[index].scaleT[aniframeScaleT[index]], 1); Gl.glRotated((double)ani.SRTData[index].rotate[aniframeR[index]], 1, 0, 0); //Gl.glRotated((double)ani.SRTData[index].rotate[aniframeR[index] + 1], 0, 1, 0); Gl.glTranslated((double)ani.SRTData[index].translateS[aniframeS[index]], (double)ani.SRTData[index].translateT[aniframeT[index]], 0); if (anim2) { if (aniframeS[index] == ani.SRTData[index].translateS.Length - 1) { aniframeS[index] = 0; } else { aniframeS[index]++; } if (aniframeT[index] == ani.SRTData[index].translateT.Length - 1) { aniframeT[index] = 0; } else { aniframeT[index]++; } if (aniframeR[index] == (ani.SRTData[index].rotate.Length - 2) / 2) { aniframeR[index] = 0; } else { aniframeR[index]++; } if (aniframeScaleS[index] == ani.SRTData[index].scaleS.Length - 1) { aniframeScaleS[index] = 0; } else { aniframeScaleS[index]++; } if (aniframeScaleT[index] == ani.SRTData[index].scaleT.Length - 1) { aniframeScaleT[index] = 0; } else { aniframeScaleT[index]++; } } goto noscale; } else { goto scale; } } catch { } noscale: if (!mat.isEnvironmentMap) { Gl.glScalef(1.0f / ((float)mat.width), 1.0f / ((float)mat.height), 1.0f); } goto end; scale: if (!mat.isEnvironmentMap) { if (mat.mtx == null) { Gl.glScalef((float)mat.scaleS / ((float)mat.width), (float)mat.scaleT / ((float)mat.height), 1.0f); Gl.glRotatef(mat.rot, 0, 1, 0); Gl.glTranslatef(mat.transS, mat.transT, 0); } else { Gl.glScalef(1.0f / ((float)mat.width), 1.0f / ((float)mat.height), 1.0f); Gl.glMultMatrixf(mat.mtx); } } end: //Gl.glColor4f(1, 1, 0, 0); Gl.glEnable(Gl.GL_ALPHA_TEST); Gl.glAlphaFunc(Gl.GL_GREATER, 0f); Gl.glColor4f(0xff, 0xff, 0xff, 0xff);//(float)mat.Alpha / 31.0f); if (licht && (((mat.PolyAttrib >> 0) & 0x1) == 0 && ((mat.PolyAttrib >> 1) & 0x1) == 0 && ((mat.PolyAttrib >> 2) & 0x1) == 0 && ((mat.PolyAttrib >> 3) & 0x1) == 0) == false) { //Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_POSITION, new float[] { 1, 1, 1, 0 }); Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_DIFFUSE, new float[] { (float)mat.DiffuseColor.R / 255f, (float)mat.DiffuseColor.G / 255f, (float)mat.DiffuseColor.B / 255f, (float)mat.DiffuseColor.A / 255f }); Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_AMBIENT, new float[] { (float)mat.AmbientColor.R / 255f, (float)mat.AmbientColor.G / 255f, (float)mat.AmbientColor.B / 255f, (float)mat.AmbientColor.A / 255f }); Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_SPECULAR, new float[] { (float)mat.SpecularColor.R / 255f, (float)mat.SpecularColor.G / 255f, (float)mat.SpecularColor.B / 255f, (float)mat.SpecularColor.A / 255f }); Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_EMISSION, new float[] { (float)mat.EmissionColor.R / 255f, (float)mat.EmissionColor.G / 255f, (float)mat.EmissionColor.B / 255f, (float)mat.EmissionColor.A / 255f }); //Gl.glLightfv(Gl.GL_LIGHT1, Gl.GL_POSITION, new float[] { 1, 1, 1, 0 }); Gl.glLightfv(Gl.GL_LIGHT1, Gl.GL_DIFFUSE, new float[] { (float)mat.DiffuseColor.R / 255f, (float)mat.DiffuseColor.G / 255f, (float)mat.DiffuseColor.B / 255f, (float)mat.DiffuseColor.A / 255f }); Gl.glLightfv(Gl.GL_LIGHT1, Gl.GL_AMBIENT, new float[] { (float)mat.AmbientColor.R / 255f, (float)mat.AmbientColor.G / 255f, (float)mat.AmbientColor.B / 255f, (float)mat.AmbientColor.A / 255f }); Gl.glLightfv(Gl.GL_LIGHT1, Gl.GL_SPECULAR, new float[] { (float)mat.SpecularColor.R / 255f, (float)mat.SpecularColor.G / 255f, (float)mat.SpecularColor.B / 255f, (float)mat.SpecularColor.A / 255f }); Gl.glLightfv(Gl.GL_LIGHT1, Gl.GL_EMISSION, new float[] { (float)mat.EmissionColor.R / 255f, (float)mat.EmissionColor.G / 255f, (float)mat.EmissionColor.B / 255f, (float)mat.EmissionColor.A / 255f }); //Gl.glLightfv(Gl.GL_LIGHT2, Gl.GL_POSITION, new float[] { 1.0f, 1.0f, 1.0f, 0 }); Gl.glLightfv(Gl.GL_LIGHT2, Gl.GL_DIFFUSE, new float[] { (float)mat.DiffuseColor.R / 255f, (float)mat.DiffuseColor.G / 255f, (float)mat.DiffuseColor.B / 255f, (float)mat.DiffuseColor.A / 255f }); Gl.glLightfv(Gl.GL_LIGHT2, Gl.GL_AMBIENT, new float[] { (float)mat.AmbientColor.R / 255f, (float)mat.AmbientColor.G / 255f, (float)mat.AmbientColor.B / 255f, (float)mat.AmbientColor.A / 255f }); Gl.glLightfv(Gl.GL_LIGHT2, Gl.GL_SPECULAR, new float[] { (float)mat.SpecularColor.R / 255f, (float)mat.SpecularColor.G / 255f, (float)mat.SpecularColor.B / 255f, (float)mat.SpecularColor.A / 255f }); Gl.glLightfv(Gl.GL_LIGHT2, Gl.GL_EMISSION, new float[] { (float)mat.EmissionColor.R / 255f, (float)mat.EmissionColor.G / 255f, (float)mat.EmissionColor.B / 255f, (float)mat.EmissionColor.A / 255f }); //Gl.glLightfv(Gl.GL_LIGHT3, Gl.GL_POSITION, new float[] { 1.0f, 1.0f, 1.0f, 0 }); Gl.glLightfv(Gl.GL_LIGHT3, Gl.GL_DIFFUSE, new float[] { (float)mat.DiffuseColor.R / 255f, (float)mat.DiffuseColor.G / 255f, (float)mat.DiffuseColor.B / 255f, (float)mat.DiffuseColor.A / 255f }); Gl.glLightfv(Gl.GL_LIGHT3, Gl.GL_AMBIENT, new float[] { (float)mat.AmbientColor.R / 255f, (float)mat.AmbientColor.G / 255f, (float)mat.AmbientColor.B / 255f, (float)mat.AmbientColor.A / 255f }); Gl.glLightfv(Gl.GL_LIGHT3, Gl.GL_SPECULAR, new float[] { (float)mat.SpecularColor.R / 255f, (float)mat.SpecularColor.G / 255f, (float)mat.SpecularColor.B / 255f, (float)mat.SpecularColor.A / 255f }); Gl.glLightfv(Gl.GL_LIGHT3, Gl.GL_EMISSION, new float[] { (float)mat.EmissionColor.R / 255f, (float)mat.EmissionColor.G / 255f, (float)mat.EmissionColor.B / 255f, (float)mat.EmissionColor.A / 255f }); Gl.glEnable(Gl.GL_LIGHTING); if (((mat.PolyAttrib >> 0) & 0x1) == 1) Gl.glEnable(Gl.GL_LIGHT0); else Gl.glDisable(Gl.GL_LIGHT0); if (((mat.PolyAttrib >> 1) & 0x1) == 1) Gl.glEnable(Gl.GL_LIGHT1); else Gl.glDisable(Gl.GL_LIGHT1); if (((mat.PolyAttrib >> 2) & 0x1) == 1) Gl.glEnable(Gl.GL_LIGHT2); else Gl.glDisable(Gl.GL_LIGHT2); if (((mat.PolyAttrib >> 3) & 0x1) == 1) Gl.glEnable(Gl.GL_LIGHT3); else Gl.glDisable(Gl.GL_LIGHT3); if (mat.diffuseColor) { //Gl.glColorMaterial(Gl.GL_FRONT_AND_BACK, Gl.GL_DIFFUSE); Gl.glColor4f((float)mat.DiffuseColor.R / 255f, (float)mat.DiffuseColor.G / 255f, (float)mat.DiffuseColor.B / 255f, (float)mat.DiffuseColor.A / 255f); //Gl.glColorMaterial(Gl.GL_FRONT_AND_BACK, Gl.GL_AMBIENT); //Gl.glColor4f((float)mat.AmbientColor.R / 255f, (float)mat.AmbientColor.G / 255f, (float)mat.AmbientColor.B / 255f, (float)mat.AmbientColor.A / 255f); //Gl.glColorMaterial(Gl.GL_FRONT_AND_BACK, Gl.GL_SPECULAR); //Gl.glColor4f((float)mat.SpecularColor.R / 255f, (float)mat.SpecularColor.G / 255f, (float)mat.SpecularColor.B / 255f, (float)mat.SpecularColor.A / 255f); //Gl.glColorMaterial(Gl.GL_FRONT_AND_BACK, Gl.GL_EMISSION); //Gl.glColor4f((float)mat.EmissionColor.R / 255f, (float)mat.EmissionColor.G / 255f, (float)mat.EmissionColor.B / 255f, (float)mat.EmissionColor.A / 255f); //Gl.glEnable(Gl.GL_COLOR_MATERIAL); } // Gl.glMaterialfv(Gl.GL_FRONT_AND_BACK, Gl.GL_DIFFUSE, new float[] { (float)mat.DiffuseColor.R / 255f, (float)mat.DiffuseColor.G / 255f, (float)mat.DiffuseColor.B / 255f, (float)mat.Alpha / 31.0f }); //Gl.glMaterialfv(Gl.GL_FRONT_AND_BACK, Gl.GL_AMBIENT, new float[] { (float)mat.AmbientColor.R / 255f, (float)mat.AmbientColor.G / 255f, (float)mat.AmbientColor.B / 255f, (float)mat.AmbientColor.A / 255f }); //Gl.glMaterialfv(Gl.GL_FRONT_AND_BACK, Gl.GL_SPECULAR, new float[] { (float)mat.SpecularColor.R / 255f, (float)mat.SpecularColor.G / 255f, (float)mat.SpecularColor.B / 255f, (float)mat.SpecularColor.A / 255f }); //Gl.glMaterialfv(Gl.GL_FRONT_AND_BACK, Gl.GL_EMISSION, new float[] { (float)mat.EmissionColor.R / 255f, (float)mat.EmissionColor.G / 255f, (float)mat.EmissionColor.B / 255f, (float)mat.EmissionColor.A / 255f }); //Gl.glEnable(Gl.GL_COLOR_MATERIAL); } else { Gl.glDisable(Gl.GL_LIGHTING); Gl.glDisable(Gl.GL_LIGHT0); Gl.glDisable(Gl.GL_LIGHT1); Gl.glDisable(Gl.GL_LIGHT2); Gl.glDisable(Gl.GL_LIGHT3); if (mat.diffuseColor) { //Gl.glColorMaterial(Gl.GL_FRONT_AND_BACK, Gl.GL_DIFFUSE); Gl.glColor4f((float)mat.DiffuseColor.R / 255f, (float)mat.DiffuseColor.G / 255f, (float)mat.DiffuseColor.B / 255f, (float)mat.DiffuseColor.A / 255f); //Gl.glColorMaterial(Gl.GL_FRONT_AND_BACK, Gl.GL_AMBIENT); //Gl.glColor4f((float)mat.AmbientColor.R / 255f, (float)mat.AmbientColor.G / 255f, (float)mat.AmbientColor.B / 255f, (float)mat.AmbientColor.A / 255f); //Gl.glColorMaterial(Gl.GL_FRONT_AND_BACK, Gl.GL_SPECULAR); //Gl.glColor4f((float)mat.SpecularColor.R / 255f, (float)mat.SpecularColor.G / 255f, (float)mat.SpecularColor.B / 255f, (float)mat.SpecularColor.A / 255f); //Gl.glColorMaterial(Gl.GL_FRONT_AND_BACK, Gl.GL_EMISSION); //Gl.glColor4f((float)mat.EmissionColor.R / 255f, (float)mat.EmissionColor.G / 255f, (float)mat.EmissionColor.B / 255f, (float)mat.EmissionColor.A / 255f); //Gl.glEnable(Gl.GL_COLOR_MATERIAL); } //else //{ // Gl.glColor4f(1, 1, 1, 1); //Gl.glDisable(Gl.GL_COLOR_MATERIAL); //} } Gl.glEnable(Gl.GL_BLEND); if (mat.isEnvironmentMap) { Gl.glTexGeni(Gl.GL_S, Gl.GL_TEXTURE_GEN_MODE, Gl.GL_SPHERE_MAP); Gl.glTexGeni(Gl.GL_T, Gl.GL_TEXTURE_GEN_MODE, Gl.GL_SPHERE_MAP); Gl.glEnable(Gl.GL_TEXTURE_GEN_S); Gl.glEnable(Gl.GL_TEXTURE_GEN_T); //Gl.glBlendFunc(Gl.GL_ONE, Gl.GL_ONE); } else { Gl.glDisable(Gl.GL_TEXTURE_GEN_S); Gl.glDisable(Gl.GL_TEXTURE_GEN_T); Gl.glBlendFunc(Gl.GL_SRC_ALPHA, Gl.GL_ONE_MINUS_SRC_ALPHA); } int mode = -1; switch ((mat.PolyAttrib >> 4) & 0x3) { case 0: mode = Gl.GL_MODULATE; break; case 1: mode = Gl.GL_DECAL; break; case 2: mode = Gl.GL_MODULATE; break; case 3: mode = Gl.GL_MODULATE; break; } Gl.glTexEnvi(Gl.GL_TEXTURE_ENV, Gl.GL_TEXTURE_ENV_MODE, mode); int cullmode = -1; //Gl.glEnable(Gl.GL_CULL_FACE); switch (mat.PolyAttrib >> 6 & 0x03) { case 0x03: cullmode = Gl.GL_NONE; break; case 0x02: cullmode = Gl.GL_BACK; break; case 0x01: cullmode = Gl.GL_FRONT; break; case 0x00: cullmode = Gl.GL_FRONT_AND_BACK; break; } Gl.glCullFace(cullmode); } } else { Gl.glBindTexture(Gl.GL_TEXTURE_2D, 0); } // Gl.glMatrixMode(Gl.GL_MODELVIEW_MATRIX); //Gl.glLoadIdentity(); if (!gOptColoring) { Gl.glColor3f(1, 1, 1); } stackID = poly.StackID; // the first matrix used by this polygon Process3DCommand(poly.PolyData, Model.Materials[poly.MatId], poly.JointID, true); /*if (MatrixStack[poly.StackID][0] == 0 && MatrixStack[poly.StackID][5] == 0 && MatrixStack[poly.StackID][10] == 0 && MatrixStack[poly.StackID][15] == 0) { } else { }*/ } writevertex = false; }
public void RenderModel(string file2, MKDS_Course_Editor.NSBTA.NSBTA.NSBTA_File ani, int[] aniframeS, int[] aniframeT, int[] aniframeScaleS, int[] aniframeScaleT, int[] aniframeR, MKDS_Course_Editor.NSBCA.NSBCA.NSBCA_File ca, bool anim, int selectedani, float X, float Y, float dist, float elev, float ang, MKDS_Course_Editor.NSBTP.NSBTP.NSBTP_File p, Nsbmd nsb) { RenderModel(file2, ani, aniframeS, aniframeT, aniframeScaleS, aniframeScaleT, aniframeR, ca, anim, selectedani, X, Y, dist, elev, ang, true, p, nsb); }
public void RenderModel(string file2, MKDS_Course_Editor.NSBTA.NSBTA.NSBTA_File ani, int[] aniframeS, int[] aniframeT, int[] aniframeScaleS, int[] aniframeScaleT, int[] aniframeR, MKDS_Course_Editor.NSBCA.NSBCA.NSBCA_File ca, bool anim, int selectedani, float X, float Y, float dist, float elev, float ang, bool licht, MKDS_Course_Editor.NSBTP.NSBTP.NSBTP_File p, Nsbmd nsb) { if (lastselectedanim != selectedani && selectedani != -1) { Tx = new int[ca.JAC[selectedani].ObjInfo.Length]; Ty = new int[ca.JAC[selectedani].ObjInfo.Length]; Tz = new int[ca.JAC[selectedani].ObjInfo.Length]; R = new int[ca.JAC[selectedani].ObjInfo.Length]; Sx = new int[ca.JAC[selectedani].ObjInfo.Length]; Sy = new int[ca.JAC[selectedani].ObjInfo.Length]; Sz = new int[ca.JAC[selectedani].ObjInfo.Length]; S2x = new int[ca.JAC[selectedani].ObjInfo.Length]; S2y = new int[ca.JAC[selectedani].ObjInfo.Length]; S2z = new int[ca.JAC[selectedani].ObjInfo.Length]; lastselectedanim = selectedani; } if (frame == null && p.Header.file_size != 0) { frame = new int[p.MPT.names.Length]; frame_ = new int[p.MPT.names.Length]; nr = new int[p.MPT.names.Length]; } RenderModel(file2, ani, aniframeS, aniframeT, aniframeScaleS, aniframeScaleT, aniframeR, ca, RenderMode.Opaque, anim, anim, selectedani, X, Y, dist, elev, ang, licht, p, nsb); //anim, anim); RenderModel(file2, ani, aniframeS, aniframeT, aniframeScaleS, aniframeScaleT, aniframeR, ca, RenderMode.Translucent, false, anim, selectedani, X, Y, dist, elev, ang, licht, p, nsb); }
public bool doJointAnimation(MKDS_Course_Editor.NSBCA.NSBCA.NSBCA_File ca, int selectedanim, bool anim, int i) { try { if (ca.Header.file_size != 0 && selectedanim != -1) { float[] s = loadIdentity(); float[] r = loadIdentity(); float[] t = loadIdentity(); //float[] mt = loadIdentity(); //scale float scalex = float.NaN; float scaley = float.NaN; float scalez = float.NaN; if (ca.JAC[selectedanim].ObjInfo[i].scale[0][0].Count != 0) { scalex = ca.JAC[selectedanim].ObjInfo[i].scale[0][0][0];// + ca.JAC[selectedanim].ObjInfo[i].scale[0][1][0]) / 2;// - ca.JAC[selectedanim].ObjInfo[i].scale[0][1][S2x[i]];//ca.JAC[selectedanim].ObjInfo[i].scale[0][0][S2x[i]]; } else if (ca.JAC[selectedanim].ObjInfo[i].scale_keyframes[0][0].Count != 0) { scalex = ca.JAC[selectedanim].ObjInfo[i].scale_keyframes[0][0][Sx[i]];// -ca.JAC[selectedanim].ObjInfo[i].scale_keyframes[0][1][Sx[i]]; } if (ca.JAC[selectedanim].ObjInfo[i].scale[1][0].Count != 0) { scaley = ca.JAC[selectedanim].ObjInfo[i].scale[1][0][0];// + ca.JAC[selectedanim].ObjInfo[i].scale[1][1][0]) / 2;// -ca.JAC[selectedanim].ObjInfo[i].scale[1][1][S2y[i]];//ca.JAC[selectedanim].ObjInfo[i].scale[1][0][S2y[i]]; } else if (ca.JAC[selectedanim].ObjInfo[i].scale_keyframes[1][0].Count != 0) { scaley = ca.JAC[selectedanim].ObjInfo[i].scale_keyframes[1][0][Sy[i]];// -ca.JAC[selectedanim].ObjInfo[i].scale_keyframes[1][1][Sy[i]]; } if (ca.JAC[selectedanim].ObjInfo[i].scale[2][0].Count != 0) { scalez = ca.JAC[selectedanim].ObjInfo[i].scale[2][0][0];// + ca.JAC[selectedanim].ObjInfo[i].scale[2][1][0]) / 2;// -ca.JAC[selectedanim].ObjInfo[i].scale[2][1][S2z[i]];//ca.JAC[selectedanim].ObjInfo[i].scale[2][0][S2z[i]]; } else if (ca.JAC[selectedanim].ObjInfo[i].scale_keyframes[2][0].Count != 0) { scalez = ca.JAC[selectedanim].ObjInfo[i].scale_keyframes[2][0][Sz[i]];// -ca.JAC[selectedanim].ObjInfo[i].scale_keyframes[2][1][Sz[i]]; } if (!float.IsNaN(scalex) && !float.IsNaN(scaley) && !float.IsNaN(scalez)) { s = scale(s, scalex, scaley, scalez); } else { //s = scale(s, Model.Objects[i].scale[0], Model.Objects[i].scale[1], Model.Objects[i].scale[2]); } /*if(ca.JAC[selectedanim].ObjInfo[i].scale[0][0].Count != 0) { //mt = scale(mt, ca.JAC[0].ObjInfo[i].scale[0][0][S2[i]], ca.JAC[0].ObjInfo[i].scale[0][1][S2[i]], ca.JAC[0].ObjInfo[i].scale[0][2][S2[i]]); s = scale(s, ca.JAC[selectedanim].ObjInfo[i].scale[0][0][S2[i]], ca.JAC[selectedanim].ObjInfo[i].scale[1][0][S2[i]], ca.JAC[selectedanim].ObjInfo[i].scale[2][0][S2[i]]); //Gl.glScalef(ca.JAC[selectedanim].ObjInfo[i].scale[0][0][S2[i]], ca.JAC[selectedanim].ObjInfo[i].scale[1][0][S2[i]], ca.JAC[selectedanim].ObjInfo[i].scale[2][0][S2[i]]); }*/ try { if (ca.JAC[selectedanim].ObjInfo[i].rotate.Count != 0) { if ((((int)ca.JAC[selectedanim].ObjInfo[i].rotate[0]) >> 15 & 0x1) == 1) { int pvneg = ca.JAC[selectedanim].JointData[(((int)ca.JAC[selectedanim].ObjInfo[i].rotate[0]) & 0x7fff) * 6];//Utils.Read2BytesAsInt16(ca.JAC[selectedanim].JointData, (int)ca.JAC[selectedanim].ObjInfo[i].rotate_keyframes[0][R[i]] * 6 + 0); int a = Utils.Read2BytesAsInt16(ca.JAC[selectedanim].JointData, (((int)ca.JAC[selectedanim].ObjInfo[i].rotate[0]) & 0x7fff) * 6 + 2); int b = Utils.Read2BytesAsInt16(ca.JAC[selectedanim].JointData, (((int)ca.JAC[selectedanim].ObjInfo[i].rotate[0]) & 0x7fff) * 6 + 4); a = sign(a, 16); b = sign(b, 16); //mt = multMatrix(mt, Nsbmd.mtxPivot(new float[] { (float)a / 4096f, (float)b / 4096f }, (pvneg >> 0 & 0x0f), (pvneg >> 4 & 0x0f))); //Gl.glMultMatrixf(Nsbmd.mtxPivot(new float[] { (float)a / 4096f, (float)b / 4096f }, (pvneg >> 0 & 0x0f), (pvneg >> 4 & 0x0f))); r = Nsbmd.mtxPivot(new float[] { (float)a / 4096f, (float)b / 4096f }, (pvneg >> 0 & 0x0f), (pvneg >> 4 & 0x0f));//multMatrix(r, Nsbmd.mtxPivot(new float[] { (float)a / 4096f, (float)b / 4096f }, (pvneg >> 0 & 0x0f), (pvneg >> 4 & 0x0f))); } else { int x = Utils.Read2BytesAsInt16(ca.JAC[selectedanim].RotationData, (((int)ca.JAC[selectedanim].ObjInfo[i].rotate[0]) & 0x7fff) * 10 + 2); x = sign(x, 16); int y = Utils.Read2BytesAsInt16(ca.JAC[selectedanim].RotationData, (((int)ca.JAC[selectedanim].ObjInfo[i].rotate[0]) & 0x7fff) * 10 + 4); y = sign(y, 16); int z = Utils.Read2BytesAsInt16(ca.JAC[selectedanim].RotationData, (((int)ca.JAC[selectedanim].ObjInfo[i].rotate[0]) & 0x7fff) * 10 + 6); z = sign(z, 16); OpenTK.Matrix4 X = OpenTK.Matrix4.CreateRotationX((float)x / 131072F);//((float)x * (float)Math.PI) / 32768F); OpenTK.Matrix4 Y = OpenTK.Matrix4.CreateRotationY((float)y / 131072F);//((float)y * (float)Math.PI) / 32768F); OpenTK.Matrix4 Z = OpenTK.Matrix4.CreateRotationZ((float)z / 131072F);//((float)z * (float)Math.PI) / 32768F); OpenTK.Matrix4 full = OpenTK.Matrix4.Identity; full = OpenTK.Matrix4.Mult(full, X); full = OpenTK.Matrix4.Mult(full, Y); full = OpenTK.Matrix4.Mult(full, Z); } } else if (ca.JAC[selectedanim].ObjInfo[i].rotate_keyframes[0].Count != 0) { //Gl.glMultMatrixf(Nsbmd.mtxPivot(new float[]{ca.JAC[0].ObjInfo[i].rotate_keyframes[1][R[i]],ca.JAC[0].ObjInfo[i].rotate_keyframes[0][R[i]]}, obj.Pivot, obj.Neg)); //Gl.glRotatef(ca.JAC[0].ObjInfo[i].rotate_keyframes[0][R[i]], 0,(ca.JAC[0].ObjInfo[i].rotate_keyframes[1][R[i]] == 1 ? 1 : 0),(ca.JAC[0].ObjInfo[i].rotate_keyframes[1][R[i]] == 0 ? 1 : 0)); if (ca.JAC[selectedanim].ObjInfo[i].rotate_keyframes[1][R[i]] == 1) { int pvneg = ca.JAC[selectedanim].JointData[(int)ca.JAC[selectedanim].ObjInfo[i].rotate_keyframes[0][R[i]] * 6];//Utils.Read2BytesAsInt16(ca.JAC[selectedanim].JointData, (int)ca.JAC[selectedanim].ObjInfo[i].rotate_keyframes[0][R[i]] * 6 + 0); int a = Utils.Read2BytesAsInt16(ca.JAC[selectedanim].JointData, (int)ca.JAC[selectedanim].ObjInfo[i].rotate_keyframes[0][R[i]] * 6 + 2); int b = Utils.Read2BytesAsInt16(ca.JAC[selectedanim].JointData, (int)ca.JAC[selectedanim].ObjInfo[i].rotate_keyframes[0][R[i]] * 6 + 4); a = sign(a, 16); b = sign(b, 16); //mt = multMatrix(mt, Nsbmd.mtxPivot(new float[] { (float)a / 4096f, (float)b / 4096f }, (pvneg >> 0 & 0x0f), (pvneg >> 4 & 0x0f))); //Gl.glMultMatrixf(Nsbmd.mtxPivot(new float[] { (float)a / 4096f, (float)b / 4096f }, (pvneg >> 0 & 0x0f), (pvneg >> 4 & 0x0f))); r = Nsbmd.mtxPivot(new float[] { (float)a / 4096f, (float)b / 4096f }, (pvneg >> 0 & 0x0f), (pvneg >> 4 & 0x0f));//multMatrix(r, Nsbmd.mtxPivot(new float[] { (float)a / 4096f, (float)b / 4096f }, (pvneg >> 0 & 0x0f), (pvneg >> 4 & 0x0f))); } else { float param = ca.JAC[selectedanim].RotationData[(int)ca.JAC[selectedanim].ObjInfo[i].rotate_keyframes[0][R[i]] * 10]; //float u = (float)Math.Pow(2.0f, param) * (float)180 / 131072F; int x = Utils.Read2BytesAsInt16(ca.JAC[selectedanim].RotationData, (int)ca.JAC[selectedanim].ObjInfo[i].rotate_keyframes[0][R[i]] * 10 + 2); x = sign(x, 16); int y = Utils.Read2BytesAsInt16(ca.JAC[selectedanim].RotationData, (int)ca.JAC[selectedanim].ObjInfo[i].rotate_keyframes[0][R[i]] * 10 + 4); y = sign(y, 16); int z = Utils.Read2BytesAsInt16(ca.JAC[selectedanim].RotationData, (int)ca.JAC[selectedanim].ObjInfo[i].rotate_keyframes[0][R[i]] * 10 + 6); z = sign(z, 16); OpenTK.Matrix4 X = OpenTK.Matrix4.CreateRotationX((float)x / 131072F);//((float)x * (float)Math.PI) / 32768F); OpenTK.Matrix4 Y = OpenTK.Matrix4.CreateRotationY((float)y / 131072F);//((float)y * (float)Math.PI) / 32768F); OpenTK.Matrix4 Z = OpenTK.Matrix4.CreateRotationZ((float)z / 131072F);//((float)z * (float)Math.PI) / 32768F); OpenTK.Matrix4 full = OpenTK.Matrix4.Identity; full = OpenTK.Matrix4.Mult(full, X); full = OpenTK.Matrix4.Mult(full, Y); full = OpenTK.Matrix4.Mult(full, Z); /*int x = (param) & 0xFF; if ((x & 0x200) != 0) x |= -256; int y = (param >> 8) & 0xFF; if ((y & 0x200) != 0) y |= -256; int z = (param >> 16) & 0xFF; if ((z & 0x200) != 0) z |= -256;*/ //Gl.glRotatef((float)x / 32768F * 180F, 1, 0, 0); //Gl.glRotatef((float)y / 32768F * 180F, 0, 1, 0); //Gl.glRotatef((float)z / 32768F * 180F, 0, 0, 1); //param /= 4096f; //float un = 2048f;//(float)Utils.Read4BytesAsInt32(ca.JAC[selectedanim].RotationData, (int)ca.JAC[selectedanim].ObjInfo[i].rotate_keyframes[0][R[i]] * 10 + 6) / 4096f; //Gl.glRotatef(x / un, 1, 0, 0);//((float)x * (float)Math.PI) / 180f, 1, 0, 0); //Gl.glRotatef(y / un, 0, 1, 0);//((float)y * (float)Math.PI) / 180f, 0, 1, 0); //Gl.glRotatef(z / un, 0, 0, 1);//((float)z * (float)Math.PI) / 180f, 0, 0, 1); //mt = rotate(mt, x, y, z); //Gl.glMultMatrixf(full.ToFloat()); } } else { //r = multMatrix(r, Model.Objects[i].rotate_mtx); } } catch (Exception e) { } float transx = float.NaN; float transy = float.NaN; float transz = float.NaN; if (ca.JAC[selectedanim].ObjInfo[i].translate[0].Count != 0) { transx = ca.JAC[selectedanim].ObjInfo[i].translate[0][0];// Model.modelScale; } else if (ca.JAC[selectedanim].ObjInfo[i].translate_keyframes[0].Count != 0) { transx = ca.JAC[selectedanim].ObjInfo[i].translate_keyframes[0][Tx[i]];// Model.modelScale; } if (ca.JAC[selectedanim].ObjInfo[i].translate[1].Count != 0) { transy = ca.JAC[selectedanim].ObjInfo[i].translate[1][0];// Model.modelScale; } else if (ca.JAC[selectedanim].ObjInfo[i].translate_keyframes[1].Count != 0) { transy = ca.JAC[selectedanim].ObjInfo[i].translate_keyframes[1][Ty[i]];// Model.modelScale; } if (ca.JAC[selectedanim].ObjInfo[i].translate[2].Count != 0) { transz = ca.JAC[selectedanim].ObjInfo[i].translate[2][0];// Model.modelScale; } else if (ca.JAC[selectedanim].ObjInfo[i].translate_keyframes[2].Count != 0) { transz = ca.JAC[selectedanim].ObjInfo[i].translate_keyframes[2][Tz[i]];// Model.modelScale; } if (!float.IsNaN(transx) && !float.IsNaN(transy) && !float.IsNaN(transz)) { t = translate(t, transx / Model.modelScale/* - (Model.Objects[i].X)*/, transy / Model.modelScale/* - (Model.Objects[i].Y)*/, transz / Model.modelScale/* - (Model.Objects[i].Z)*/); } else { //t = translate(t, Model.Objects[i].X, Model.Objects[i].Y, Model.Objects[i].Z); } //if(ca.JAC[selectedanim].ObjInfo[i].translate[0].Count != 0) //{ //mt = translate(mt, ca.JAC[0].ObjInfo[i].translate[0][T2[i]], ca.JAC[0].ObjInfo[i].translate_keyframes[1][T2[i]], ca.JAC[0].ObjInfo[i].translate_keyframes[2][T2[i]]); //Gl.glTranslatef(ca.JAC[selectedanim].ObjInfo[i].translate[0][T2[i]] / Model.modelScale, ca.JAC[selectedanim].ObjInfo[i].translate_keyframes[1][T2[i]] / Model.modelScale, ca.JAC[selectedanim].ObjInfo[i].translate_keyframes[2][T2[i]] / Model.modelScale); // t = translate(t,ca.JAC[selectedanim].ObjInfo[i].translate[0][T2[i]] / Model.modelScale, ca.JAC[selectedanim].ObjInfo[i].translate_keyframes[1][T2[i]] / Model.modelScale, ca.JAC[selectedanim].ObjInfo[i].translate_keyframes[2][T2[i]] / Model.modelScale); //} float[] m = loadIdentity(); //Gl.glGetFloatv(Gl.GL_MODELVIEW_MATRIX, m); m = multMatrix(m, t); //m = translate(m, Model.Objects[i].X, Model.Objects[i].Y, Model.Objects[i].Z); //if (Model.Objects[i].IsRotated) //{ //float[] rt = loadIdentity(); //rt = multMatrix(rt, r); //rt = multMatrix(rt, Model.Objects[i].rotate_mtx); m = multMatrix(m, r); // m = multMatrix(m, Model.Objects[i].rotate_mtx); //} //else //{ // m = multMatrix(m, r); //} m = multMatrix(m, s); //m = scale(m, Model.Objects[i].scale[0], Model.Objects[i].scale[1], Model.Objects[i].scale[2]); Gl.glMultMatrixf(m); //s = loadIdentity(); //r = loadIdentity(); //t = loadIdentity(); //Gl.glMultMatrixf(mt); //mt = loadIdentity(); //if(ca.JAC[selectedanim].ObjInfo[i].scale_keyframes[0][0].Count != 0) // { //mt = scale(mt, ca.JAC[0].ObjInfo[i].scale_keyframes[0][0][S[i]], ca.JAC[0].ObjInfo[i].scale_keyframes[0][1][S[i]], ca.JAC[0].ObjInfo[i].scale_keyframes[0][2][S[i]]); //Gl.glScalef((ca.JAC[selectedanim].ObjInfo[i].scale_keyframes[0][0][S[i]] - ca.JAC[selectedanim].ObjInfo[i].scale_keyframes[0][1][S[i]]), (ca.JAC[selectedanim].ObjInfo[i].scale_keyframes[1][0][S[i]] - ca.JAC[selectedanim].ObjInfo[i].scale_keyframes[1][1][S[i]]), (ca.JAC[selectedanim].ObjInfo[i].scale_keyframes[2][0][S[i]] - ca.JAC[selectedanim].ObjInfo[i].scale_keyframes[2][1][S[i]])); //s = scale(s, (ca.JAC[selectedanim].ObjInfo[i].scale_keyframes[0][0][S[i]] - ca.JAC[selectedanim].ObjInfo[i].scale_keyframes[0][1][S[i]]), (ca.JAC[selectedanim].ObjInfo[i].scale_keyframes[1][0][S[i]] - ca.JAC[selectedanim].ObjInfo[i].scale_keyframes[1][1][S[i]]), (ca.JAC[selectedanim].ObjInfo[i].scale_keyframes[2][0][S[i]] - ca.JAC[selectedanim].ObjInfo[i].scale_keyframes[2][1][S[i]])); //} //if(ca.JAC[selectedanim].ObjInfo[i].translate_keyframes[0].Count != 0) //{ //mt = translate(mt, ca.JAC[0].ObjInfo[i].translate_keyframes[0][T[i]], ca.JAC[0].ObjInfo[i].translate_keyframes[1][T[i]], ca.JAC[0].ObjInfo[i].translate_keyframes[2][T[i]]); //Gl.glTranslatef(ca.JAC[selectedanim].ObjInfo[i].translate_keyframes[0][T[i]] / Model.modelScale, ca.JAC[selectedanim].ObjInfo[i].translate_keyframes[1][T[i]] / Model.modelScale, ca.JAC[selectedanim].ObjInfo[i].translate_keyframes[2][T[i]] / Model.modelScale); // t = translate(t, ca.JAC[selectedanim].ObjInfo[i].translate_keyframes[0][T[i]] / Model.modelScale, ca.JAC[selectedanim].ObjInfo[i].translate_keyframes[1][T[i]] / Model.modelScale, ca.JAC[selectedanim].ObjInfo[i].translate_keyframes[2][T[i]] / Model.modelScale); //} // m = loadIdentity(); //m = multMatrix(m, t); //m = multMatrix(m, r); //m = multMatrix(m, s); //Gl.glMultMatrixf(m); if (anim) { if (Tx[i] == ca.JAC[selectedanim].ObjInfo[i].translate_keyframes[0].Count - 1) { Tx[i] = 0; } else { Tx[i]++; } if (Ty[i] == ca.JAC[selectedanim].ObjInfo[i].translate_keyframes[1].Count - 1) { Ty[i] = 0; } else { Ty[i]++; } if (Tz[i] == ca.JAC[selectedanim].ObjInfo[i].translate_keyframes[2].Count - 1) { Tz[i] = 0; } else { Tz[i]++; } if (R[i] == ca.JAC[selectedanim].ObjInfo[i].rotate_keyframes[0].Count - 1) { R[i] = 0; } else { R[i]++; } if (Sx[i] == ca.JAC[selectedanim].ObjInfo[i].scale_keyframes[0][0].Count - 1) { Sx[i] = 0; } else { Sx[i]++; } if (Sy[i] == ca.JAC[selectedanim].ObjInfo[i].scale_keyframes[1][0].Count - 1) { Sy[i] = 0; } else { Sy[i]++; } if (Sz[i] == ca.JAC[selectedanim].ObjInfo[i].scale_keyframes[2][0].Count - 1) { Sz[i] = 0; } else { Sz[i]++; } } return true; } else { return false; } } catch { return true; } }