Пример #1
0
    void ModelMD2::prepareFrame() {
#ifdef DEBUG
        static bool isFirst = true;
        if (isFirst) {
            printf("Call ModelMD2::prepareFrame()...\n");
            isFirst = false;
        }
#endif
        if (m_actionIdx == -1)
            return;

        float *uvs = (float *) malloc(m_header.numTriangles * 6 * sizeof(float));
        float *vertices = (float *) malloc(m_header.numTriangles * 9 * sizeof(float));
        float *normals = (float *) malloc(m_header.numTriangles * 9 * sizeof(float));
        int i, j, uvIdx = 0, vertexIdx = 0, normalIdx = 0;

        md2_frame_t *f = &m_frames[m_frameIdx];

        for (i = 0; i < m_header.numTriangles; i++) {
            md2_triangle_t *t = &m_triangles[i];
            for (j = 0; j < 3; j++) {
                //set uvs
                uvs[uvIdx++] = (float) m_texCoords[t->textureIndices[j]].s / (float) m_header.skinWidth;
                uvs[uvIdx++] = 1.0f - (float) m_texCoords[t->textureIndices[j]].t / (float) m_header.skinHeight;
                //set vertices & normals
                vertices[vertexIdx++] = f->vertices[t->vertexIndices[j]].vertex[0];
                vertices[vertexIdx++] = f->vertices[t->vertexIndices[j]].vertex[1];
                vertices[vertexIdx++] = f->vertices[t->vertexIndices[j]].vertex[2];

                normals[normalIdx++] = f->vertices[t->vertexIndices[j]].normal[0];
                normals[normalIdx++] = f->vertices[t->vertexIndices[j]].normal[2];
                normals[normalIdx++] = f->vertices[t->vertexIndices[j]].normal[1];
            }
        }
        setVertices(vertices, m_header.numTriangles * 9 * sizeof(float));
        setNormals(normals, m_header.numTriangles * 9 * sizeof(float));
        setUvs(uvs, m_header.numTriangles * 6 * sizeof(float));

        FREEANDNULL(vertices);
        FREEANDNULL(normals);
        FREEANDNULL(uvs);

        m_frameIdx++;
        if (m_frameIdx > m_actions[m_actionIdx].max_idx)
            m_frameIdx = m_isLooped ? m_actions[m_actionIdx].min_idx : m_actions[m_actionIdx].max_idx;
    }
Пример #2
0
void Box2DSprite::configureUVs(){
	float mag = std::max(mesh->getTexture(0)->width, mesh->getTexture(0)->height);
	setUvs(sweet::Rectangle(u/mag, v/mag, width/mag, height/mag));
}
Пример #3
0
    void ModelMS3D::prepareFrame() {
#ifdef DEBUG
        static bool isFirst = true;
        if (isFirst) {
            printf("Call ModelMS3D::prepareFrame()...\n");
            isFirst = false;
        }
#endif
        if ((m_frameCount < 0 || m_frameIdx < 0 || (m_joints == NULL)) && m_isPrepared) {
#ifdef DEBUG
            printf("No joint or frame, don't need prepare again...\n");
#endif
            return;
        }

        int i, j, n;

        for (i = 0; i < m_jointsCount; i++) {
            Matrix *transform = new Matrix();

            if (m_frameIdx < m_joints[i].header.numKeyFramesRot)
                transform->setRotationRadians( m_joints[i].keyFramesRot[m_frameIdx].rotation[0],
                                        m_joints[i].keyFramesRot[m_frameIdx].rotation[1],
                                        m_joints[i].keyFramesRot[m_frameIdx].rotation[2] );
            if (m_frameIdx < m_joints[i].header.numKeyFramesTrans)
                transform->setTranslation( m_joints[i].keyFramesTrans[m_frameIdx].position[0],
                                        m_joints[i].keyFramesTrans[m_frameIdx].position[1],
                                        m_joints[i].keyFramesTrans[m_frameIdx].position[2] );

            Matrix *relativeFinal = new Matrix();
            relativeFinal->set(m_joints[i].relMatrix->getMatrix());
            relativeFinal->postMultiply(transform->getMatrix());

            if ( m_joints[i].parentJointIndex == -1 )
                m_joints[i].finMatrix->set(relativeFinal->getMatrix());
            else {
                m_joints[i].finMatrix->set(m_joints[m_joints[i].parentJointIndex].finMatrix->getMatrix());
                m_joints[i].finMatrix->postMultiply(relativeFinal->getMatrix());
            }

                        DELETEANDNULL(transform, false);
            DELETEANDNULL(relativeFinal, false);
        }

        for (i = 0; i < m_groupsCount; i++) {
            int v_idx = 0, n_idx = 0, u_idx = 0;
            float *vertices = new float[m_groups[i].numTriangles * 9];
            float *normals = new float[m_groups[i].numTriangles * 9];
            float *uvs = NULL;

            if (m_materials != NULL && !m_isPrepared)
                uvs = new float[m_groups[i].numTriangles * 6];
            //read all group triangles
            for (j = 0; j < m_groups[i].numTriangles; j++) {
                //get the triangle & vertex indices
                ms3d_triangle_t triangle = m_triangles[m_groups[i].triangleIndices[j]];
                for (int m = 0; m < 3; m++) {
                    //get the vertex, and apply joint data to it
                    ms3d_vertex_t vertex = m_vertices[triangle.vertexIndices[m]];
                    int b_idx = vertex.boneId;
                    if (b_idx != -1) {
                                                Vector *vector_n = new Vector( triangle.vertexNormals[m][0],
                                                        triangle.vertexNormals[m][1],
                                                        triangle.vertexNormals[m][2] );
                                                vector_n->transform3(m_joints[b_idx].finMatrix);
                                                vector_n->normalize();

                                                Vector *vector_v = new Vector( vertex.vertex[0], vertex.vertex[1], vertex.vertex[2] );
                                                vector_v->transform(m_joints[b_idx].finMatrix);
                        for (n = 0; n < 3; n++) {
                            vertices[v_idx++] = vector_v->getVector()[n]; // copy vertices
                            normals[n_idx++] = vector_n->getVector()[n]; // copy normals
                        }

                                                DELETEANDNULL(vector_n, false);
                                                DELETEANDNULL(vector_v, false);
                    } else {
                        //copy vertex data to mesh
                        for (int n = 0; n < 3; n++) {
                            vertices[v_idx++] = m_vertices[triangle.vertexIndices[m]].vertex[n]; // copy vertices
                            normals[n_idx++] = triangle.vertexNormals[m][n]; // copy normals
                        }
                    }
                    if (m_materials != NULL && !m_isPrepared) {
                        uvs[u_idx++] = triangle.s[m];
                        uvs[u_idx++] = 1.0f - triangle.t[m];
                    }
                }
            }
            //set data to mesh
            setVertices(vertices, m_groups[i].numTriangles * 9 * sizeof(float), i);
            setNormals(normals, m_groups[i].numTriangles * 9 * sizeof(float), i);
            if (m_materials != NULL && !m_isPrepared) {
                setUvs(uvs, m_groups[i].numTriangles * 6 * sizeof(float), i);
            }

            DELETEANDNULL(vertices, true);
                        DELETEANDNULL(normals, true);
            DELETEANDNULL(uvs, true);
        }

        m_frameIdx++;
        if (m_frameIdx >= m_frameCount)
            m_frameIdx = m_isLooped ? 0 : m_frameCount - 1;

        //after prepare frame, set m_isPrepared to true
        if (!m_isPrepared)
            m_isPrepared = GL_TRUE;
    }
Пример #4
0
void ModelAM::loadModel (const char* filename)
{
    FILE *file;
    int r, i, j;
    int version = 0;
    int mesh_num = 0;
    int vertex_num = 0;
    int uv_num = 0;
    int triangle_num = 0;
    int temp = 0;
    int material_num = 0;
    float specular_level, glossiness;


    char mesh_name[32];
    float box;
	float *vertices = NULL;
	float *uvs = NULL;
	short *indices = NULL;


    printf ("loadModel %s.\n", filename);

    file = fopen (filename, "r");
    if (file == NULL)
        return;


    r = fscanf (file, "SimpleMeshFormat %d\n", &version);
    printf ("verson = %d.\n", version);

    r = fscanf (file, "%d\n", &mesh_num);
    printf ("mesh_num = %d.\n", mesh_num);
	setMeshCount (mesh_num);


    for (i = 0; i < mesh_num; i++) {

        r = fscanf (file, "%s\n", mesh_name);
        printf ("mesh_name = %s.\n", mesh_name);

        r = fscanf (file, "%d\n", &vertex_num);
        printf ("vertex_num = %d.\n", vertex_num);

        vertices = (float*)malloc (vertex_num * 3 * sizeof (float));
        if (!vertices) {
            //Fixme: free sth if needed.
            return;
        }

        for (j = 0; j < vertex_num; j++)  {
            r = fscanf (file, "%g %g %g\n",
                            &vertices[j*3],
                            &vertices[j*3+1],
                            &vertices[j*3+2]);
        }

		
        setVertices (vertices, vertex_num * 3 * sizeof (float), i);
        FREEANDNULL (vertices);

        r = fscanf (file, "%d\n", &uv_num);
	
        uvs = (float*)malloc (uv_num * 2 * sizeof (float));
        if (!uvs) {
            //Fixme: free sth if needed.
            return;
        }


        for (j = 0; j < uv_num; j++)  {
            r = fscanf (file, "%g %g\n",
                            &uvs[j*2],
                            &uvs[j*2+1]);
        }

        setUvs(uvs, uv_num * 2 * sizeof (float), i);
        FREEANDNULL (uvs);


        r = fscanf (file, "%d\n", &triangle_num);
        printf ("triangle_num = %d.\n", triangle_num);
		
		setTriangleNums(triangle_num, i);

        indices = (short *)malloc (triangle_num * 3 * sizeof (short));
        if (!indices) {
            //Fixme: free sth if needed.
            return;
        }

	
        for (j = 0; j < triangle_num; j++)  {
            r = fscanf (file, "%d %hd %hd %hd %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g\n",
                           &temp, //nSmoothGroup
                           &indices[j*3],
                           &indices[j*3+1],
                           &indices[j*3+2],
                           &box,
                           &box,
                           &box,
                           &box,
                           &box,
                           &box,
                           &box,
                           &box,
                           &box,
                           &box,
                           &box,
                           &box,
                           &box,
                           &box,
                           &box);

        }


        setIndices (indices, triangle_num * 3 * sizeof (short), i);
        FREEANDNULL (indices);
 


//    	setMaterialAmbient (0.9, 0.6, 0.6, 0);
//    	setMaterialDiffuse (0.9, 0.6, 0.6, 0);


		float ambient [3];
    	float diffuse [3];
    	float specular [3];
    	float emission [3];
    	float shininess;



        r = fscanf (file, "%d\n", &material_num);
        //_M3D_PRINTF ("material_num = %d.\n", material_num);


		//FIXME: handle only one material
		//Basicly, diffuse decide mesh color in white light

        r = fscanf (file, "%g %g %g %g %g %g %g %g %g %g %g %g %g %g\n",
                           &specular_level,
                           &glossiness,
                           &ambient[0],
                           &ambient[1],
                           &ambient[2],
                           &diffuse[0],
                           &diffuse[1],
                           &diffuse[2],
                           &specular[0],
                           &specular[1],
                           &specular[2],
                           &emission[0],
                           &emission[1],
                           &emission[2]);

        if (specular_level > 0)
            shininess = glossiness;
        else
            shininess = -1;

			
/*
            _M3D_PRINTF ("loadAmModel: %g %g %g %g %g %g %g %g %g %g\n",
                           shininess,
                           ambient[0],
                           ambient[1],
                           ambient[2],
                           diffuse[0],
                           diffuse[1],
                           diffuse[2],
                           specular[0],
                           specular[1],
                           specular[2]);
        }
*/

        setMaterialAmbient (ambient[0],
                            ambient[1],
                            ambient[2],
                            i);

        setMaterialDiffuse (diffuse[0],
                            diffuse[1],
                            diffuse[2],
                            i);

        setMaterialEmission (emission[0],
                             emission[1],
                             emission[2],
                             i);

        if (shininess > 0) {

             setMaterialShininess (shininess, i);
             setMaterialSpecular (specular[0], specular[1], specular[2], i);

        }


    }



    fclose (file);


    return;
}