Example #1
0
void renProjectTriangle(S_Renderer *pRenderer, S_Model *pModel, int i)
{
    S_Coords    aa, bb, cc;             /* souradnice vrcholu po transformaci */
    S_Coords    naa, nbb, ncc;          /* normaly ve vrcholech po transformaci */
    S_Coords    nn;                     /* normala trojuhelniku po transformaci */
    int         u1, v1, u2, v2, u3, v3; /* souradnice vrcholu po projekci do roviny obrazovky */
    S_Triangle  * triangle;

    IZG_ASSERT(pRenderer && pModel && i >= 0 && i < trivecSize(pModel->triangles));

    /* z modelu si vytahneme trojuhelnik */
    triangle = trivecGetPtr(pModel->triangles, i);

    /* transformace vrcholu matici model */
    trTransformVertex(&aa, cvecGetPtr(pModel->vertices, triangle->v[0]));
    trTransformVertex(&bb, cvecGetPtr(pModel->vertices, triangle->v[1]));
    trTransformVertex(&cc, cvecGetPtr(pModel->vertices, triangle->v[2]));

    /* promitneme vrcholy trojuhelniku na obrazovku */
    trProjectVertex(&u1, &v1, &aa);
    trProjectVertex(&u2, &v2, &bb);
    trProjectVertex(&u3, &v3, &cc);

    /* pro osvetlovaci model transformujeme take normaly ve vrcholech */
    trTransformVector(&naa, cvecGetPtr(pModel->normals, triangle->v[0]));
    trTransformVector(&nbb, cvecGetPtr(pModel->normals, triangle->v[1]));
    trTransformVector(&ncc, cvecGetPtr(pModel->normals, triangle->v[2]));

    /* normalizace normal */
    coordsNormalize(&naa);
    coordsNormalize(&nbb);
    coordsNormalize(&ncc);

    /* transformace normaly trojuhelniku matici model */
    trTransformVector(&nn, cvecGetPtr(pModel->trinormals, triangle->n));
    
    /* normalizace normaly */
    coordsNormalize(&nn);

    /* je troj. privraceny ke kamere, tudiz viditelny? */
    if( !renCalcVisibility(pRenderer, &aa, &nn) )
    {
        /* odvracene troj. vubec nekreslime */
        return;
    }

    /* rasterizace trojuhelniku */
    renDrawTriangle(pRenderer,
                    &aa, &bb, &cc,
                    &naa, &nbb, &ncc,
                    u1, v1, u2, v2, u3, v3
                    );
}
Example #2
0
File: main.c Project: pavelfryz/fit
void renderModel()
{
    int         i;
    
    IZG_ASSERT(renderer && polymodel);
    
    /* pomocĂ­ rendereru postupne vykreslime vsechny trojuhelniky modelu */
    for( i = 0; i < trivecSize(polymodel->triangles); ++i )
    {
        /* zavolame rutinu pro vykresleni */
        renderer->projectTriangleFunc(renderer, polymodel, i);
    }
}
Example #3
0
void studrenProjectTriangle(S_Renderer *pRenderer, S_Model *pModel, int i, float n)
{
    S_Coords    aa, bb, cc;             /* souradnice vrcholu po transformaci */
    S_Coords    naa, nbb, ncc;          /* normaly ve vrcholech po transformaci */
    S_Coords    nn;                     /* normala trojuhelniku po transformaci */
    int         u1, v1, u2, v2, u3, v3; /* souradnice vrcholu po projekci do roviny obrazovky */
    S_Triangle  * triangle;
    int         vertexOffset, normalOffset; /* offset pro vrcholy a normalove vektory trojuhelniku */
    int         i0, i1, i2, in;             /* indexy vrcholu a normaly pro i-ty trojuhelnik n-teho snimku */
    S_Coords    aa1, bb1, cc1;             /* souradnice vrcholu po transformaci */
    S_Coords    naa1, nbb1, ncc1;          /* normaly ve vrcholech po transformaci */
    S_Coords    nn1;                     /* normala trojuhelniku po transformaci */
    int vertexOffset1, normalOffset1;
    int i01, i11, i21, in1;				/* indexy vrcholu a normaly pro i-ty trojuhelnik (n+1)-teho snimku */
    IZG_ASSERT(pRenderer && pModel && i >= 0 && i < trivecSize(pModel->triangles) && n >= 0 );

    int N = (int) floor(n);
    float xN = n - floor(n);

    /* z modelu si vytahneme i-ty trojuhelnik */
    triangle = trivecGetPtr(pModel->triangles, i);

    /* ziskame offset pro vrcholy n-teho snimku */
    vertexOffset = (N % pModel->frames) * pModel->verticesPerFrame;
    vertexOffset1 = ((N + 1) % pModel->frames) * pModel->verticesPerFrame;
    /* ziskame offset pro normaly trojuhelniku n-teho snimku */
    normalOffset = (N % pModel->frames) * pModel->triangles->size;
    normalOffset1 = ((N + 1) % pModel->frames) * pModel->triangles->size;

    /* indexy vrcholu pro i-ty trojuhelnik n-teho snimku - pricteni offsetu */
    i0 = triangle->v[ 0 ] + vertexOffset;
    i1 = triangle->v[ 1 ] + vertexOffset;
    i2 = triangle->v[ 2 ] + vertexOffset;

    i01 = triangle->v[ 0 ] + vertexOffset1;
	i11 = triangle->v[ 1 ] + vertexOffset1;
	i21 = triangle->v[ 2 ] + vertexOffset1;

    /* index normaloveho vektoru pro i-ty trojuhelnik n-teho snimku - pricteni offsetu */
    in = triangle->n + normalOffset;
    in1 = triangle->n + normalOffset1;

    /* transformace vrcholu matici model */
    trTransformVertex(&aa, cvecGetPtr(pModel->vertices, i0));
    trTransformVertex(&bb, cvecGetPtr(pModel->vertices, i1));
    trTransformVertex(&cc, cvecGetPtr(pModel->vertices, i2));
    trTransformVertex(&aa1, cvecGetPtr(pModel->vertices, i01));
	trTransformVertex(&bb1, cvecGetPtr(pModel->vertices, i11));
	trTransformVertex(&cc1, cvecGetPtr(pModel->vertices, i21));

	/* Interpolace vrcholu*/
	aa.x = (1 - xN) * aa.x + xN * aa1.x;
	aa.y = (1 - xN) * aa.y + xN * aa1.y;
	aa.z = (1 - xN) * aa.z + xN * aa1.z;

	bb.x = (1 - xN) * bb.x + xN * bb1.x;
	bb.y = (1 - xN) * bb.y + xN * bb1.y;
	bb.z = (1 - xN) * bb.z + xN * bb1.z;

	cc.x = (1 - xN) * cc.x + xN * cc1.x;
	cc.y = (1 - xN) * cc.y + xN * cc1.y;
	cc.z = (1 - xN) * cc.z + xN * cc1.z;

    /* promitneme vrcholy trojuhelniku na obrazovku */
    double h0 = trProjectVertex(&u1, &v1, &aa);
    double h1 = trProjectVertex(&u2, &v2, &bb);
    double h2 = trProjectVertex(&u3, &v3, &cc);

    /* pro osvetlovaci model transformujeme take normaly ve vrcholech */
    trTransformVector(&naa, cvecGetPtr(pModel->normals, i0));
    trTransformVector(&nbb, cvecGetPtr(pModel->normals, i1));
    trTransformVector(&ncc, cvecGetPtr(pModel->normals, i2));
    trTransformVector(&naa1, cvecGetPtr(pModel->normals, i01));
	trTransformVector(&nbb1, cvecGetPtr(pModel->normals, i11));
	trTransformVector(&ncc1, cvecGetPtr(pModel->normals, i21));

	/* Interpolace normal */
	naa.x = (1 - xN) * naa.x + xN * naa1.x;
	naa.y = (1 - xN) * naa.y + xN * naa1.y;
	naa.z = (1 - xN) * naa.z + xN * naa1.z;

	nbb.x = (1 - xN) * nbb.x + xN * nbb1.x;
	nbb.y = (1 - xN) * nbb.y + xN * nbb1.y;
	nbb.z = (1 - xN) * nbb.z + xN * nbb1.z;

	ncc.x = (1 - xN) * ncc.x + xN * ncc1.x;
	ncc.y = (1 - xN) * ncc.y + xN * ncc1.y;
	ncc.z = (1 - xN) * ncc.z + xN * ncc1.z;

    /* normalizace normal */
    coordsNormalize(&naa);
    coordsNormalize(&nbb);
    coordsNormalize(&ncc);

    /* transformace normaly trojuhelniku matici model */
    trTransformVector(&nn, cvecGetPtr(pModel->trinormals, in));
    trTransformVector(&nn1, cvecGetPtr(pModel->trinormals, in1));

    /* Interpolace normaly */
    nn.x = (1 - xN) * nn.x + xN * nn1.x;
    nn.y = (1 - xN) * nn.y + xN * nn1.y;
    nn.z = (1 - xN) * nn.z + xN * nn1.z;


    /* normalizace normaly */
    coordsNormalize(&nn);

    /* je troj. privraceny ke kamere, tudiz viditelny? */
    if( !renCalcVisibility(pRenderer, &aa, &nn) )
    {
        /* odvracene troj. vubec nekreslime */
        return;
    }
    S_Triangle *texture = trivecGetPtr(pModel->triangles, i);

    /* rasterizace trojuhelniku */
    studrenDrawTriangle(pRenderer,
                    &aa, &bb, &cc,
                    &naa, &nbb, &ncc,
					&triangle->t[0], &triangle->t[1], &triangle->t[2],
                    u1, v1, u2, v2, u3, v3,
					h0, h1, h2
                    );
}