void DrawVolumesLines() { TStreamsDesc streams; streams.AddStream(TStream::Vertex, TDataType::Float, 3, prim_lines_vertex); render->Draw(streams, TPrimitive::Lines, prim_lines_vertex_count); streams.Clear(); }
void DrawVolumes() { TStreamsDesc streams; streams.AddStream(TStream::Vertex, TDataType::Float, 3, prim_vertex); streams.AddStream(TStream::Index, TDataType::UInt, 1, prim_index); render->Draw(streams, TPrimitive::Triangles, prim_index_count); streams.Clear(); }
void TRender::Render(std::vector<TRenderCommand>& render_commands, std::vector<IGUIVisual*>& gui, EngineInterface::IViewport* viewport) { //render->Set.ModelView(TMatrix4::GetOrtho(TVec2(0, 0), TVec2(20, 20), -1, 1)); render->Set.ModelView(TMatrix4::GetOrtho(viewport->GetTransform().position, viewport->GetSize(), -1, 1)); //render->AlphaTest.Enable(true); //render->AlphaTest.Func(TAlphaTestFunc::AT_GREATER, 0.9); render->Blend.Enable(true); render->Blend.Func(TBlendEquation::BE_SRC_ALPHA, TBlendFunc::BF_ADD, TBlendEquation::BE_ONE_MINUS_SRC_ALPHA); //render->Set.PolygonMode(TBaluRenderEnums::TPolygonMode::Line); std::sort(render_commands.begin(), render_commands.end(), [&](TRenderCommand& l, TRenderCommand& r){return l.layer_order > r.layer_order; }); render->Set.Color(1, 1, 1, 1); //int current_layer = render_commands.front().layer; int draw_calls = 0; int vertex_array_setups = 0; int texture_binds = 0; int vertex_count = 0; for (int i = 0; i < render_commands.size(); i++) { auto& c = render_commands[i]; assert(c.vertices_count > 0); { render->Set.Color(1, 1, 1, c.alpha); auto tex = c.material_id->GetTexture(); render->Texture.Bind(*(TTextureId*)&tex); TStreamsDesc streams; streams.AddStream(TStream::Vertex, TDataType::Float, 2, c.vertices); streams.AddStream(TStream::TexCoord, TDataType::Float, 2, c.tex_coords); //streams.AddStream(TStream::Color, TDataType::Float, 4, c.colors); render->Draw(streams, TPrimitive::Triangles, c.vertices_count); vertex_count += c.vertices_count; draw_calls++; vertex_array_setups++; texture_binds++; if (c.draw_triangles_grid) { render->Texture.Enable(false); render->Set.PolygonMode(TPolygonMode::Line); render->Draw(streams, TPrimitive::Triangles, c.vertices_count); render->Set.PolygonMode(TPolygonMode::Fill); render->Texture.Enable(true); vertex_count += c.vertices_count; draw_calls++; vertex_array_setups++; texture_binds++; } } } render->Blend.Enable(false); //glDisable(GL_DEPTH_TEST); render->Set.PolygonMode(TBaluRenderEnums::TPolygonMode::Fill); auto vg_context = GetNanoVGContext(); if (vg_context != nullptr) { //render_test(); //render->Depth.Test(false); begin_frame(render->ScreenSize()); TDrawingHelperContext drawing_context; drawing_context.viewport = viewport; auto screen = TScreen(this->GetInternalRender()->Get.Viewport()); drawing_context.screen = &screen; auto main_viewport_view = TView(TVec2(0.5, 0.5), TVec2(1, 1)); drawing_context.view = &main_viewport_view; auto drawing_helper = TDrawingHelper(drawing_context); for (auto& v : gui) { v->Render(&drawing_helper); } char buf[100]; nvgFontSize(vg_context, 20.2f); nvgFontFace(vg_context, "sans"); nvgFillColor(vg_context, nvgRGBA(255, 255, 255, 255)); nvgTextAlign(vg_context, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE); sprintf_s(buf, "draw_calls %i", draw_calls); nvgText(vg_context, 10, 10, buf, NULL); sprintf_s(buf, "vertex_array_setups %i", vertex_array_setups); nvgText(vg_context, 10, 30, buf, NULL); sprintf_s(buf, "texture_binds %i", texture_binds); nvgText(vg_context, 10, 50, buf, NULL); sprintf_s(buf, "vertices %i", vertex_count); nvgText(vg_context, 10, 70, buf, NULL); sprintf_s(buf, "guis %i", gui.size()); nvgText(vg_context, 10, 90, buf, NULL); end_frame(); } }
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); } }