Beispiel #1
0
void ogl::mesh::cache_verts(const ogl::mesh *that, const mat4& M,
                                                   const mat4& I, int id)
{
    const size_t n = that->vv.size();

    // Cache that mesh's transformed vertices here.  Update bounding volume.

    vv.resize(n);
    nv.resize(n);
    tv.resize(n);
    uv.resize(n);

    bound = aabb();

    for (size_t i = 0; i < n; ++i)
    {
        transform_vertex(vv[i].v,           M,  that->vv[i].v);
        transform_normal(nv[i].v, transpose(I), that->nv[i].v);
        transform_normal(tv[i].v, transpose(I), that->tv[i].v);

        bound.merge(vec3(double(vv[i].v[0]),
                         double(vv[i].v[1]),
                         double(vv[i].v[2])));

        // Handy trick: Store the unit ID in the texture coordinate.

        uv[i].v[0] = that->uv[i].v[0];
        uv[i].v[1] = that->uv[i].v[1];
        uv[i].v[2] = GLfloat(id);
    }

    dirty_verts = true;
}
Beispiel #2
0
void	transform_model(float *vtx, matrix mtx, int triangle_count)
{
	int i;

	i = 0;
	while (i < triangle_count)
	{
		transform_vertex(vtx, mtx, i);
		i += 3;
	}
}
	shared_str	parse_vertex	(luabind::object const &table, LPCSTR identifier, bool const &in)
	{
		return					(
			transform_vertex(
				parse_string(
					table,
					identifier
				),
				in
			)
		);
	}
Beispiel #4
0
HRESULT d3d_execute_buffer_execute(struct d3d_execute_buffer *buffer,
        struct d3d_device *device, struct d3d_viewport *viewport)
{
    DWORD vs = buffer->data.dwVertexOffset;
    DWORD is = buffer->data.dwInstructionOffset;
    char *instr = (char *)buffer->desc.lpData + is;
    unsigned int i;

    if (viewport->active_device != device)
    {
        WARN("Viewport %p active device is %p.\n",
                viewport, viewport->active_device);
        return DDERR_INVALIDPARAMS;
    }

    /* Activate the viewport */
    viewport_activate(viewport, FALSE);

    TRACE("ExecuteData :\n");
    if (TRACE_ON(ddraw))
        _dump_executedata(&(buffer->data));

    for (;;)
    {
        D3DINSTRUCTION *current = (D3DINSTRUCTION *)instr;
	BYTE size;
	WORD count;
	
	count = current->wCount;
	size = current->bSize;
	instr += sizeof(D3DINSTRUCTION);
	
	switch (current->bOpcode) {
	    case D3DOP_POINT: {
	        WARN("POINT-s          (%d)\n", count);
		instr += count * size;
	    } break;

	    case D3DOP_LINE: {
	        WARN("LINE-s           (%d)\n", count);
		instr += count * size;
	    } break;

            case D3DOP_TRIANGLE:
            {
                D3DTLVERTEX *tl_vx = buffer->vertex_data;
		TRACE("TRIANGLE         (%d)\n", count);

                if (buffer->nb_indices < count * 3)
                {
                    buffer->nb_indices = count * 3;
                    HeapFree(GetProcessHeap(), 0, buffer->indices);
                    buffer->indices = HeapAlloc(GetProcessHeap(), 0, sizeof(*buffer->indices) * buffer->nb_indices);
                }

                for (i = 0; i < count; ++i)
                {
                    D3DTRIANGLE *ci = (D3DTRIANGLE *)instr;
		    TRACE("  v1: %d  v2: %d  v3: %d\n",ci->u1.v1, ci->u2.v2, ci->u3.v3);
		    TRACE("  Flags : ");
                    if (TRACE_ON(ddraw))
                    {
                        /* Wireframe */
                        if (ci->wFlags & D3DTRIFLAG_EDGEENABLE1)
                            TRACE("EDGEENABLE1 ");
                        if (ci->wFlags & D3DTRIFLAG_EDGEENABLE2)
                            TRACE("EDGEENABLE2 ");
                        if (ci->wFlags & D3DTRIFLAG_EDGEENABLE1)
                            TRACE("EDGEENABLE3 ");
                        /* Strips / Fans */
                        if (ci->wFlags == D3DTRIFLAG_EVEN)
                            TRACE("EVEN ");
                        if (ci->wFlags == D3DTRIFLAG_ODD)
                            TRACE("ODD ");
                        if (ci->wFlags == D3DTRIFLAG_START)
                            TRACE("START ");
                        if ((ci->wFlags > 0) && (ci->wFlags < 30))
                            TRACE("STARTFLAT(%u) ", ci->wFlags);
                        TRACE("\n");
                    }
                    buffer->indices[(i * 3)    ] = ci->u1.v1;
                    buffer->indices[(i * 3) + 1] = ci->u2.v2;
                    buffer->indices[(i * 3) + 2] = ci->u3.v3;
                    instr += size;
                }
                if (count)
                    IDirect3DDevice7_DrawIndexedPrimitive(&device->IDirect3DDevice7_iface,
                            D3DPT_TRIANGLELIST, D3DFVF_TLVERTEX, tl_vx, buffer->nb_vertices,
                            buffer->indices, count * 3, 0);
	    } break;

	    case D3DOP_MATRIXLOAD:
	        WARN("MATRIXLOAD-s     (%d)\n", count);
	        instr += count * size;
	        break;

            case D3DOP_MATRIXMULTIPLY:
                TRACE("MATRIXMULTIPLY   (%d)\n", count);
                for (i = 0; i < count; ++i)
                {
                    D3DMATRIXMULTIPLY *ci = (D3DMATRIXMULTIPLY *)instr;
                    D3DMATRIX *a, *b, *c;

                    a = ddraw_get_object(&device->handle_table, ci->hDestMatrix - 1, DDRAW_HANDLE_MATRIX);
                    b = ddraw_get_object(&device->handle_table, ci->hSrcMatrix1 - 1, DDRAW_HANDLE_MATRIX);
                    c = ddraw_get_object(&device->handle_table, ci->hSrcMatrix2 - 1, DDRAW_HANDLE_MATRIX);

                    if (!a || !b || !c)
                    {
                        ERR("Invalid matrix handle (a %#x -> %p, b %#x -> %p, c %#x -> %p).\n",
                                ci->hDestMatrix, a, ci->hSrcMatrix1, b, ci->hSrcMatrix2, c);
                    }
                    else
                    {
                        TRACE("dst %p, src1 %p, src2 %p.\n", a, b, c);
                        multiply_matrix(a, c, b);
                    }

                    instr += size;
                }
                break;

            case D3DOP_STATETRANSFORM:
                TRACE("STATETRANSFORM   (%d)\n", count);
                for (i = 0; i < count; ++i)
                {
                    D3DSTATE *ci = (D3DSTATE *)instr;
                    D3DMATRIX *m;

                    m = ddraw_get_object(&device->handle_table, ci->u2.dwArg[0] - 1, DDRAW_HANDLE_MATRIX);
                    if (!m)
                    {
                        ERR("Invalid matrix handle %#x.\n", ci->u2.dwArg[0]);
                    }
                    else
                    {
                        if (ci->u1.dtstTransformStateType == D3DTRANSFORMSTATE_WORLD)
                            device->world = ci->u2.dwArg[0];
                        if (ci->u1.dtstTransformStateType == D3DTRANSFORMSTATE_VIEW)
                            device->view = ci->u2.dwArg[0];
                        if (ci->u1.dtstTransformStateType == D3DTRANSFORMSTATE_PROJECTION)
                            device->proj = ci->u2.dwArg[0];
                        IDirect3DDevice7_SetTransform(&device->IDirect3DDevice7_iface,
                                ci->u1.dtstTransformStateType, m);
                    }

                    instr += size;
                }
                break;

            case D3DOP_STATELIGHT:
                TRACE("STATELIGHT       (%d)\n", count);
                for (i = 0; i < count; ++i)
                {
                    D3DSTATE *ci = (D3DSTATE *)instr;

                    if (FAILED(IDirect3DDevice3_SetLightState(&device->IDirect3DDevice3_iface,
                            ci->u1.dlstLightStateType, ci->u2.dwArg[0])))
                        WARN("Failed to set light state.\n");

                    instr += size;
                }
                break;

            case D3DOP_STATERENDER:
                TRACE("STATERENDER      (%d)\n", count);
                for (i = 0; i < count; ++i)
                {
                    D3DSTATE *ci = (D3DSTATE *)instr;

                    if (FAILED(IDirect3DDevice3_SetRenderState(&device->IDirect3DDevice3_iface,
                            ci->u1.drstRenderStateType, ci->u2.dwArg[0])))
                        WARN("Failed to set render state.\n");

                    instr += size;
                }
                break;

            case D3DOP_PROCESSVERTICES:
            {
                /* TODO: Share code with d3d_vertex_buffer7_ProcessVertices()
                 * and / or wined3d_device_process_vertices(). */
                D3DMATRIX view_mat, world_mat, proj_mat, mat;

                TRACE("PROCESSVERTICES  (%d)\n", count);

                /* Get the transform and world matrix */
                /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
                wined3d_device_get_transform(device->wined3d_device,
                        D3DTRANSFORMSTATE_VIEW, (struct wined3d_matrix *)&view_mat);
                wined3d_device_get_transform(device->wined3d_device,
                        D3DTRANSFORMSTATE_PROJECTION, (struct wined3d_matrix *)&proj_mat);
                wined3d_device_get_transform(device->wined3d_device,
                        WINED3D_TS_WORLD_MATRIX(0), (struct wined3d_matrix *)&world_mat);

                if (TRACE_ON(ddraw))
                {
                    TRACE("  Projection Matrix:\n");
                    dump_D3DMATRIX(&proj_mat);
                    TRACE("  View Matrix:\n");
                    dump_D3DMATRIX(&view_mat);
                    TRACE("  World Matrix:\n");
                    dump_D3DMATRIX(&world_mat);
                }

                multiply_matrix(&mat, &view_mat, &world_mat);
                multiply_matrix(&mat, &proj_mat, &mat);

                for (i = 0; i < count; ++i)
                {
                    D3DPROCESSVERTICES *ci = (D3DPROCESSVERTICES *)instr;
                    D3DTLVERTEX *dst = (D3DTLVERTEX *)buffer->vertex_data + ci->wDest;
                    DWORD op = ci->dwFlags & D3DPROCESSVERTICES_OPMASK;

                    TRACE("  start %u, dest %u, count %u, flags %#x.\n",
                            ci->wStart, ci->wDest, ci->dwCount, ci->dwFlags);

                    if (ci->dwFlags & D3DPROCESSVERTICES_UPDATEEXTENTS)
                        FIXME("D3DPROCESSVERTICES_UPDATEEXTENTS not implemented.\n");
                    if (ci->dwFlags & D3DPROCESSVERTICES_NOCOLOR)
                        FIXME("D3DPROCESSVERTICES_NOCOLOR not implemented.\n");

                    switch (op)
                    {
                        case D3DPROCESSVERTICES_TRANSFORMLIGHT:
                        {
                            const D3DVERTEX *src = (D3DVERTEX *)((char *)buffer->desc.lpData + vs) + ci->wStart;
                            unsigned int vtx_idx;
                            static unsigned int once;

                            if (!once++)
                                FIXME("Lighting not implemented.\n");

                            for (vtx_idx = 0; vtx_idx < ci->dwCount; ++vtx_idx)
                            {
                                transform_vertex(&dst[vtx_idx], &mat, &viewport->viewports.vp1,
                                        src[vtx_idx].u1.x, src[vtx_idx].u2.y, src[vtx_idx].u3.z);
                                /* No lighting yet */
                                dst[vtx_idx].u5.color = 0xffffffff; /* Opaque white */
                                dst[vtx_idx].u6.specular = 0xff000000; /* No specular and no fog factor */
                                dst[vtx_idx].u7.tu = src[vtx_idx].u7.tu;
                                dst[vtx_idx].u8.tv = src[vtx_idx].u8.tv;
                            }
                            break;
                        }

                        case D3DPROCESSVERTICES_TRANSFORM:
                        {
                            const D3DLVERTEX *src = (D3DLVERTEX *)((char *)buffer->desc.lpData + vs) + ci->wStart;
                            unsigned int vtx_idx;

                            for (vtx_idx = 0; vtx_idx < ci->dwCount; ++vtx_idx)
                            {
                                transform_vertex(&dst[vtx_idx], &mat, &viewport->viewports.vp1,
                                        src[vtx_idx].u1.x, src[vtx_idx].u2.y, src[vtx_idx].u3.z);
                                dst[vtx_idx].u5.color = src[vtx_idx].u4.color;
                                dst[vtx_idx].u6.specular = src[vtx_idx].u5.specular;
                                dst[vtx_idx].u7.tu = src[vtx_idx].u6.tu;
                                dst[vtx_idx].u8.tv = src[vtx_idx].u7.tv;
                            }
                            break;
                        }

                        case D3DPROCESSVERTICES_COPY:
                        {
                            const D3DTLVERTEX *src = (D3DTLVERTEX *)((char *)buffer->desc.lpData + vs) + ci->wStart;

                            memcpy(dst, src, ci->dwCount * sizeof(*dst));
                            break;
                        }

                        default:
                            FIXME("Unhandled vertex processing op %#x.\n", op);
                            break;
                    }

                    instr += size;
                }
                break;
            }

	    case D3DOP_TEXTURELOAD: {
	        WARN("TEXTURELOAD-s    (%d)\n", count);

		instr += count * size;
	    } break;

	    case D3DOP_EXIT: {
	        TRACE("EXIT             (%d)\n", count);
		/* We did this instruction */
		instr += size;
		/* Exit this loop */
		goto end_of_buffer;
	    } break;

            case D3DOP_BRANCHFORWARD:
                TRACE("BRANCHFORWARD    (%d)\n", count);
                for (i = 0; i < count; ++i)
                {
                    D3DBRANCH *ci = (D3DBRANCH *)instr;

                    if ((buffer->data.dsStatus.dwStatus & ci->dwMask) == ci->dwValue)
                    {
                        if (!ci->bNegate)
                        {
                            TRACE(" Branch to %d\n", ci->dwOffset);
                            if (ci->dwOffset) {
                                instr = (char*)current + ci->dwOffset;
                                break;
                            }
			}
		    } else {
		        if (ci->bNegate) {
                            TRACE(" Branch to %d\n", ci->dwOffset);
                            if (ci->dwOffset) {
                                instr = (char*)current + ci->dwOffset;
                                break;
                            }
			}
		    }

		    instr += size;
                }
                break;

	    case D3DOP_SPAN: {
	        WARN("SPAN-s           (%d)\n", count);

		instr += count * size;
	    } break;

            case D3DOP_SETSTATUS:
                TRACE("SETSTATUS        (%d)\n", count);
                for (i = 0; i < count; ++i)
                {
                    buffer->data.dsStatus = *(D3DSTATUS *)instr;
                    instr += size;
                }
                break;

	    default:
	        ERR("Unhandled OpCode %d !!!\n",current->bOpcode);
	        /* Try to save ... */
	        instr += count * size;
	        break;
	}
    }

end_of_buffer:
    return D3D_OK;
}
Beispiel #5
0
/*
 * Draw a textured quad
 */
static void d3d_draw_textured_quad(
   ALLEGRO_DISPLAY_D3D *disp, ALLEGRO_BITMAP_D3D *bmp, ALLEGRO_COLOR tint,
   float sx, float sy, float sw, float sh, int flags)
{
   float right;
   float bottom;
   float tu_start;
   float tv_start;
   float tu_end;
   float tv_end;
   int texture_w;
   int texture_h;
   DWORD d3d_color;

   const float z = 0.0f;
   
   ALLEGRO_DISPLAY* aldisp = (ALLEGRO_DISPLAY*)disp;

   if (aldisp->num_cache_vertices != 0 && (uintptr_t)bmp != aldisp->cache_texture) {
      aldisp->vt->flush_vertex_cache(aldisp);
   }
   aldisp->cache_texture = (uintptr_t)bmp;

   D3D_TL_VERTEX* vertices = (D3D_TL_VERTEX*)aldisp->vt->prepare_vertex_cache(aldisp, 6);

   right  = sw;
   bottom = sh;

   texture_w = bmp->texture_w;
   texture_h = bmp->texture_h;
   tu_start = sx / texture_w;
   tv_start = sy / texture_h;
   tu_end = sw / texture_w + tu_start;
   tv_end = sh / texture_h + tv_start;

   if (flags & ALLEGRO_FLIP_HORIZONTAL) {
      float temp = tu_start;
      tu_start = tu_end;
      tu_end = temp;
   }
   if (flags & ALLEGRO_FLIP_VERTICAL) {
      float temp = tv_start;
      tv_start = tv_end;
      tv_end = temp;
   }

   d3d_color = D3DCOLOR_COLORVALUE(tint.r, tint.g, tint.b, tint.a);

   vertices[0].x = 0;
   vertices[0].y = 0;
   vertices[0].z = z;
   vertices[0].diffuse = d3d_color;
   vertices[0].tu = tu_start;
   vertices[0].tv = tv_start;

   vertices[1].x = right;
   vertices[1].y = 0;
   vertices[1].z = z;
   vertices[1].diffuse = d3d_color;
   vertices[1].tu = tu_end;
   vertices[1].tv = tv_start;

   vertices[2].x = right;
   vertices[2].y = bottom;
   vertices[2].z = z;
   vertices[2].diffuse = d3d_color;
   vertices[2].tu = tu_end;
   vertices[2].tv = tv_end;

   vertices[5].x = 0;
   vertices[5].y = bottom;
   vertices[5].z = z;
   vertices[5].diffuse = d3d_color;
   vertices[5].tu = tu_start;
   vertices[5].tv = tv_end;

   if (aldisp->cache_enabled) {
      transform_vertex(&vertices[0].x, &vertices[0].y);
      transform_vertex(&vertices[1].x, &vertices[1].y);
      transform_vertex(&vertices[2].x, &vertices[2].y);
      transform_vertex(&vertices[5].x, &vertices[5].y);
   }
   
   vertices[3] = vertices[0];
   vertices[4] = vertices[2];

   if(!aldisp->cache_enabled)
      aldisp->vt->flush_vertex_cache(aldisp);
}
Beispiel #6
0
void ram(void) {
	int i;
    frame = 0;

    struct vertex2d projected[NUM_VERTICES];

    float matrix_projection[16];
    init_matrix_projection_fov(matrix_projection, DEG2RAD(2), 0.1, 50, 1);

    while (true) {
        lcdFill(COLOR_BACKGROUND);

        float mat_a[16];
        float mat_b[16];
        float mat_c[16];
        float matrix_worldviewprojection[16];

        init_vertices(vertices);

        // Calculate worldviewprojection matrix by concatenating the
        // following operations:
        //  - rotate y axis
        //  - rotate x axis
        //  - translate
        //  - project
        float rad = (float)frame * 0.015f;
        init_matrix_rotation_y(mat_a, rad);
        init_matrix_rotation_x(mat_b, -rad*0.8);
        matrix_multiplication(mat_a, mat_b, mat_c);
        init_matrix_translation(mat_a, 0, -2, 11.5f);
        matrix_multiplication(mat_c, mat_a, mat_b);
        matrix_multiplication(mat_b, matrix_projection, matrix_worldviewprojection);

        // Transform vertices
        for (i=0; i<NUM_VERTICES; i++) {
            struct vertex transformed;

            transform_vertex(&vertices[i], &transformed, matrix_worldviewprojection);

            if (transformed.w > -0.0001)
                projected[i].x = VERTEX_BEHIND_CAMERA;
            else {
                projected[i].x = (int)(transformed.x / transformed.w) + SCREEN_WIDTH / 2;
                projected[i].y = (int)(transformed.y / transformed.w) + SCREEN_HEIGHT / 2;
            }
        }

        // Draw line grid
        struct vertex2d from, to;
        for (int x=0; x<TESSELATION; x++)
        for (int y=0; y<TESSELATION; y++)
        {
            if (x < (TESSELATION - 1)) {
                from = projected[x + y * TESSELATION];
                to = projected[x + y * TESSELATION + 1];
                line(from.x, from.y, to.x, to.y, COLOR_LINE);
            }

            if (y < (TESSELATION - 1)) {
                from = projected[x + y * TESSELATION];
                to = projected[x + y * TESSELATION + TESSELATION];
                line(from.x, from.y, to.x, to.y, COLOR_LINE);
            }
        }

        lcdDisplay();
        rad += 0.015;
        frame++;

        int key = getInputRaw();
        if (key == BTN_ENTER)
            break;
    }
}
Beispiel #7
0
void GLWidget::zoomTo(int toothNum)
{
    if (toothNum > 0 && toothNum <= N_TEETH)
    {
        context->makeCurrent();

        glLoadIdentity();

        glTranslatef(tx, ty - Rb*zoom, 0);
        glScalef(zoom, zoom, zoom);

        glRotatef(-ry, 1.0f, 0.0f, 0.0f);
        glRotatef(rx, 0.0f, 1.0f, 0.0f);
        glMultMatrixf(rotAccum);

        glRotatef((toothNum-1) * 360.0f / N_TEETH, 0.0f, 0.0f, 1.0f);

        // ---

        glPushMatrix();
        glRotatef(Radians2Degrees(PSI), 0.0f, 0.0f, 1.0f);

        GLdouble mm1[16];
        glGetDoublev(GL_MODELVIEW_MATRIX, mm1);

        glPopMatrix();

        // ----

        glPushMatrix();
        glRotatef(Radians2Degrees(-PSI), 0.0f, 0.0f, 1.0f);

        GLdouble mm2[16];
        glGetDoublev(GL_MODELVIEW_MATRIX, mm2);

        glPopMatrix();

        // ----
        Vertex3D v1 = R_EVOL_IJ(0, 0);
        Vertex3D v2 = R_EVOL_IJ(0, PHI1_STEPS-1);

        Vertex3D v3 = L_EVOL_IJ(0, 0);
        Vertex3D v4 = L_EVOL_IJ(0, PHI1_STEPS-1);

        Vertex3D v1t, v2t, v3t, v4t;
        transform_vertex(v1, mm1, v1t);
        transform_vertex(v2, mm1, v2t);
        transform_vertex(v3, mm2, v3t);
        transform_vertex(v4, mm2, v4t);

        float r1 = sqrtf((v1t.x - v4t.x) * (v1t.x - v4t.x) + (v1t.y - v4t.y) * (v1t.y - v4t.y)/* + (v1t.z - v4t.z) * (v1t.z - v4t.z)*/);
        float r2 = sqrtf((v2t.x - v3t.x) * (v2t.x - v3t.x) + (v2t.y - v3t.y) * (v2t.y - v3t.y)/* + (v2t.z - v3t.z) * (v2t.z - v3t.z)*/);

        float diam = (r1 > r2) ? r1 : r2;

        float cx = (v2t.x + v3t.x) / 2;
        float cy = (v2t.y + v3t.y) / 2;

        GLdouble left = cx - diam;
        GLdouble right = cx + diam;
        GLdouble bottom = cy - diam;
        GLdouble top = cy + diam;

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(left, right, bottom, top, -1000, 1000);
        glMatrixMode(GL_MODELVIEW);

        updateGL();
    }
}