示例#1
0
static void draw_scene(GLFWwindow* window, double tt)
{
	balu_time.Tick();

	if (balu_time.ShowFPS())
	{
		char buf[1000];
		sprintf_s(buf, "1 - Nearest 2 - Billenear    %7.1f FPS", balu_time.GetFPS());
		glfwSetWindowTitle(window, buf);
	}

	cam->UpdateView();

	CheckGLError();

	{
		//render->Set.ClearColor(0, 0, 1);
		render->Clear(1, 1);
		render->Set.Projection(perspective_matrix);
		render->Set.ModelView(cam->GetView());

		TMatrix<float, 4> inv_mvp;
		inv_mvp = perspective_matrix*cam->GetView();
		inv_mvp.Invert();

		{
			render->Blend.Enable(false);

			render->Set.Color(1, 1, 0.7);
			//DrawVolumesLines();
			render->Set.Color(0.3, 0.9, 0.2, 0.7);
			//DrawVolumes();
		}
		//render->Clear(1, 1);
		render->Set.Projection(TMatrix<float, 4>::GetIdentity());
		render->Set.ModelView(TMatrix<float, 4>::GetIdentity());

		if (true)
		{
			float pixel_size_x = 2.0 / (viewport_width);
			float pixel_size_y = 2.0 / (viewport_height);
			//float size = 0.9;
			//float step = 0.002;
			//render->Set.PointSize(1);
			//render->Set.PointSmooth(true);

			//glBegin(GL_POINTS);

//#pragma omp parallel for
			for (int x = 1; x < viewport_width-1; x += 1)
			{
				//break;
				for (int y = 1 ; y < viewport_height-1; y += 1)
				{
					float i = x*pixel_size_x - 1;
					float j = y*pixel_size_y - 1;
					TVec4 v0(i, j, 0, 1);
					TVec4 v1(i, j, 1, 1);
					v0 = inv_mvp*v0;
					v1 = inv_mvp*v1;
					TRay<float, 3> ray;
					ray.pos = v0.GetHomogen();
					ray.dir = v1.GetHomogen() - ray.pos;
					ray.dir.Normalize();
					//ray.dir = -ray.dir;

					//render->Set.Color(0, 0, 1, 1);

					//TVec3 color(0, 0, 1);
					TVec<unsigned char, 4> color(0, 0, 1);
					color = TVec<unsigned char, 4>(0, 0.2 * 255, 0.2 * 255, 255);
					for (int k = 0; k < volumes.size(); k++)
					{
						//float t, t0, t1, t2, t3;
						//TVec3 n, n0, n1;
						//bool c0 = volumes[k]->CollideWith(ray);
						//bool c1 = volumes[k]->CollideWith(ray, t, n);
						//bool c2 = volumes[k]->CollideWith(ray, t0, n0, t1, n1);
						//bool c3 = volumes[k]->CollideWith(ray, t2, t3);

						TRayCollisionInfo<float, 3> info, info2;

						//bool c2 = volumes[k]->RayCollide(ray);
						//bool c1 = volumes[k]->RayCollide(ray, info);

						//bool c1 = volumes[k]->SegmentCollide(TSegment<float, 3>(ray.pos, ray.pos + ray.dir * 1000));
						//bool c1 = volumes[k]->SegmentCollide(TSegment<float, 3>(ray.pos + ray.dir * 40, ray.pos));
						//bool c1 = volumes[k]->SegmentCollide(TSegment<float, 3>(ray.pos, ray.pos + ray.dir * 30), info);

						//bool c1 = volumes[k]->LineCollide(TLine<float, 3>(ray.pos + ray.dir * 40, ray.pos));
						bool c1 = volumes[k]->LineCollide(TLine<float, 3>(ray.pos, ray.pos + ray.dir * 40), info);
						
						//assert(c0 == c1);

						//if (c2)
						//{
						//	color = TVec<unsigned char, 4>(1*255, 1*255, 0, 255);
						//}else
						//	color = TVec<unsigned char, 4>(0, 0.2*255, 0, 255);

						if (c1)
						{
							float col = 1;
							if (info.have_in)
								col = abs(Clamp<float>(0, 1, -info.in_normal*ray.dir));
							else if (info.have_out)
								col = abs(Clamp<float>(0, 1, info.out_normal*ray.dir));
							//else assert(false);
							color = TVec<unsigned char,4>(0, col*255, 0,255);
							//render->Set.Color(0, col, 0, 1);
						}
						else
						{
							//color = TVec<unsigned char, 4>(0, 0.2 * 255, 0.2 * 255,255);
							//render->Set.Color(0, 0.2, 0.2, 1);
							//break;
						}

						//if (c0&&c1&&c2&&c3)
						//{
						//	//render->Set.Color(0,n*TVec3(0.5,1,1).GetNormalized(),0,255);
						//	render->Set.Color(0, 1, 0, 1);
						//	break;
						//}
						//else if ((!c0) && (!c1) && (!c2) && (!c3))
						//{
						//	render->Set.Color(0, 0.2, 0.2, 1);
						//}
						//else
						//{
						//	render->Set.Color(1, 0, 0, 1);
						//	break;
						//}
					}

					
					//glVertex3fv((GLfloat*)&(ray.pos + ray.dir));
					//glColor3fv((GLfloat*)&color);
					//glVertex2fv((GLfloat*)&(TVec<float, 2>(i,j)));
					raytracer_color_buffer[y*viewport_width + x] = color;
					
				}
			}

			glRasterPos2d(-1, -1);
			
			glDrawPixels(viewport_width, viewport_height, GL_RGBA, GL_UNSIGNED_BYTE, (GLfloat*)&raytracer_color_buffer[0]);

			//glEnd();

			//render->Set.PointSmooth(false);
			//render->Set.PointSize(1);
		}

		render->Blend.Enable(false);

	}
	if (false)
		{
			TStreamsDesc streams;
			TGeomLine<float, 3> line[3];
			TGeomLine<unsigned char, 4> color[3];
			line[0][0] = TVec3(0, 0, 0);
			line[0][1] = TVec3(1, 0, 0);
			color[0].Set(TVec<unsigned char, 4>(255, 0, 0, 255));
			line[1][0] = TVec3(0, 0, 0);
			line[1][1] = TVec3(0, 1, 0);
			color[1].Set(TVec<unsigned char, 4>(0, 255, 0, 255));
			line[2][0] = TVec3(0, 0, 0);
			line[2][1] = TVec3(0, 0, 1);
			color[2].Set(TVec<unsigned char, 4>(0, 0, 255, 255));
			streams.AddStream(TStream::Vertex, TDataType::Float, 3, &line);
			streams.AddStream(TStream::Color, TDataType::UByte, 4, &color);
			render->Draw(streams, TPrimitive::Lines, 6);
			streams.Clear();

			render->Set.Color(1, 1, 1);

		}
}