Ejemplo n.º 1
0
int main(int argc, char **argv) 
{
	SDL_Surface *screen;
	SDL_Event event;
	struct vec4 cubeCenter, cube[8];
	scalar rota;
	struct mat4 rot;
	int i;

	SDL_Init( SDL_INIT_EVERYTHING ); 

	screen = SDL_SetVideoMode(width, height, 32, 0);

	proj = proj_perspective(45, (scalar) width / (scalar) height, 1, 10);

	cubeCenter = vec4_zero();

	while(1) 
	{
		rota = (scalar) SDL_GetTicks() / 1000.0;
		cubeCenter.z = 5 + sin(rota);
		rot = mat4_aangle(_vec4(0, 1, 0, 1), rota);

		cube[0] = vec4_add(mat4_mulv(rot, _vec4( 1, -1, 1, 0)), cubeCenter);
		cube[1] = vec4_add(mat4_mulv(rot, _vec4(-1, -1, 1, 0)), cubeCenter);
		cube[2] = vec4_add(mat4_mulv(rot, _vec4(-1,  1, 1, 0)), cubeCenter);
		cube[3] = vec4_add(mat4_mulv(rot, _vec4( 1,  1, 1, 0)), cubeCenter);

		cube[4] = vec4_add(mat4_mulv(rot, _vec4( 1, -1, -1, 0)), cubeCenter);
		cube[5] = vec4_add(mat4_mulv(rot, _vec4(-1, -1, -1, 0)), cubeCenter);
		cube[6] = vec4_add(mat4_mulv(rot, _vec4(-1,  1, -1, 0)), cubeCenter);
		cube[7] = vec4_add(mat4_mulv(rot, _vec4( 1,  1, -1, 0)), cubeCenter);

		SDL_FillRect(screen, 0, 0);

		drawLine3d(screen, cube[0], cube[1]);
		drawLine3d(screen, cube[0], cube[3]);
		drawLine3d(screen, cube[1], cube[2]);
		drawLine3d(screen, cube[2], cube[3]);

		drawLine3d(screen, cube[4], cube[5]);
		drawLine3d(screen, cube[4], cube[7]);
		drawLine3d(screen, cube[5], cube[6]);
		drawLine3d(screen, cube[6], cube[7]);

		drawLine3d(screen, cube[0], cube[4]);
		drawLine3d(screen, cube[1], cube[5]);
		drawLine3d(screen, cube[2], cube[6]);
		drawLine3d(screen, cube[3], cube[7]);

		SDL_Flip(screen);

		while(SDL_PollEvent(&event) != 0)
		{
			switch(event.type)
			{
				case (SDL_QUIT):
					SDL_Quit();
					return 0;
				default:
					break;
			}
		}
	}

	SDL_Quit(); 
	return 0;
}
Ejemplo n.º 2
0
void CrystalEditorFrame::OnPaint(wxPaintEvent& event)
{
    wxPaintDC dc(mainPanel);
    dc.SetBackground(*wxBLACK_BRUSH);
    dc.Clear();
    int width, height;
    GetClientSize(&width, &height);
    Matrix combinedMatrix = create3DTransformationMatrix
    (
        target + backward * distance,
        target,
        upward,
        0, 0,
        width, height
    );

    // Render coorinate axes
    Vector3 origin(0,0,0);
    Vector3 right(0.3,0,0);
    Vector3 up(0,0.3,0);
    Vector3 backward(0,0,0.3);

    dc.SetPen(redPen);
    drawLine3d(dc, combinedMatrix, origin, right);
    dc.SetPen(greenPen);
    drawLine3d(dc, combinedMatrix, origin, up);
    dc.SetPen(bluePen);
    drawLine3d(dc, combinedMatrix, origin, backward);

    // Render ray paths
    for (int i = 0; i < COLOR_COUNT; i++)
    {
        Ray &currentPaths = rayPaths[i];
        wxColor pc = currentPaths.pen.GetColour();
        for (size_t j = 0; j < currentPaths.rayPaths.size(); j++)
        {
            vector<pair<Vector3, double> > &currentPath = currentPaths.rayPaths[j];
            for (size_t k = 0; k < currentPath.size() - 1; k++)
            {
                double p = currentPath[k].second;
                if (p < minimumIntensity) p = minimumIntensity;
                wxPen tmpPen(
                    wxColour(
                        clampIn(p * pc.Red(), 0, 255),
                        clampIn(p * pc.Green(), 0, 255),
                        clampIn(p * pc.Blue(), 0, 255)
                    )
                );

                dc.SetPen(tmpPen);
                drawLine3d(dc, combinedMatrix, currentPath[k].first, currentPath[k + 1].first);
            }
        }
    }
    // Render the mesh wireframe
    dc.SetPen(*wxWHITE_PEN);
    renderMeshOnDC(
        dc,
        crystalMesh,
        combinedMatrix,
        RM_WIREFRAME
    );
}