📄 raytrace.cpp
字号:
// ********************************************************// This is demo code. You may derive from, use, modify, and// distribute it without limitation for any purpose.// Obviously you don't get a warranty or an assurance of// fitness for a particular purpose with this code. Your// welcome to remove this header and claim original// authorship. I really don't care.// ********************************************************#include "RayTrace.h"#ifdef WIN32#else // WIN32#include <unistd.h>#endif // !WIN32#include "../GClasses/GTime.h"#include "../GClasses/GMacros.h"#include "../GClasses/GRayTrace.h"#include "../GClasses/GBits.h"#include "../GClasses/GPointerQueue.h"#include "../GClasses/GThread.h"#include "GRibParser.h"#include "Interpolate.h"class RayTraceDialog : public GWidgetDialog{friend class RayTraceController;protected: RayTraceController* m_pController; GWidgetFileSystemBrowser* m_pFileSystemBrowser; GWidgetCheckBox* m_pCheckBox;public: RayTraceDialog(RayTraceController* pController, int w, int h) : GWidgetDialog(w, h, 0xff884466) { m_pController = pController; GString s; s.Copy(L"Begin"); m_pFileSystemBrowser = new GWidgetFileSystemBrowser(this, 100, 400, 500, 190, ".rib"); m_pCheckBox = new GWidgetCheckBox(this, 5, 400, 20, 20); s.Copy(L"High Quality"); new GWidgetTextLabel(this, 26, 400, 70, 20, &s, 0xff002200); m_pFileSystemBrowser->Draw(this); } virtual ~RayTraceDialog() { } virtual void OnReleaseTextButton(GWidgetTextButton* pButton) { } virtual void OnSelectFilename(GWidgetFileSystemBrowser* pBrowser, const char* szFilename) { m_pController->OnSelectFile(szFilename); }};// ----------------------------------------------------------------------------RayTraceView::RayTraceView(RayTraceController* pController): ViewBase(){ m_pDialog = new RayTraceDialog(pController, m_screenRect.w, m_screenRect.h); m_pImage = NULL;}RayTraceView::~RayTraceView(){ delete(m_pDialog);}/*virtual*/ void RayTraceView::Draw(SDL_Surface *pScreen){ GRect r; GImage* pCanvas = m_pDialog->GetImage(&r); if(m_pImage) { r.x = 0; r.y = 0; r.w = m_pImage->GetWidth(); r.h = m_pImage->GetHeight(); pCanvas->Blit(0, 0, m_pImage, &r); } BlitImage(pScreen, m_screenRect.x, m_screenRect.y, pCanvas);}void RayTraceView::OnChar(char c){ m_pDialog->HandleChar(c);}void RayTraceView::OnMouseDown(int x, int y){ x -= m_screenRect.x; y -= m_screenRect.y; GWidgetAtomic* pNewWidget = m_pDialog->FindAtomicWidget(x, y); m_pDialog->GrabWidget(pNewWidget, x, y);}void RayTraceView::OnMouseUp(int x, int y){ m_pDialog->ReleaseWidget();}bool RayTraceView::OnMousePos(int x, int y){ return m_pDialog->HandleMousePos(x - m_screenRect.x, y - m_screenRect.y);}// -------------------------------------------------------------------------------RayTraceController::RayTraceController(): ControllerBase(){ m_pView = new RayTraceView(this); m_bRendering = false; m_pScene = NULL;}RayTraceController::~RayTraceController(){ delete(m_pView);}void RayTraceController::OnSelectFile(const char* szFilename){ m_pScene = RibParser::LoadScene(szFilename); if(!m_pScene) { printf("Failed to create scene\n"); m_bKeepRunning = false; return; } if(((RayTraceView*)m_pView)->m_pDialog->m_pCheckBox->IsChecked()) m_pScene->SetRenderMode(GRayTraceScene::QUALITY_RAY_TRACE); else m_pScene->SetRenderMode(GRayTraceScene::FAST_RAY_TRACE); m_pScene->RenderBegin(); GImage* pImage = m_pScene->GetImage();//m_pScene->RenderSinglePixel(pImage->GetWidth() / 2, pImage->GetHeight() / 2); ((RayTraceView*)m_pView)->SetImage(pImage); m_bRendering = true;}void RayTraceController::RunModal(){ double timeOld = GTime::GetTime(); double time; double timeUpdate = 0; m_pView->Update(); while(m_bKeepRunning) { time = GTime::GetTime(); if(HandleEvents(time - timeOld)) // HandleEvents returns true if it thinks the view needs to be updated { m_pView->Update(); timeUpdate = time; } else if(m_bRendering) { m_bRendering = m_pScene->RenderLine(); if(!m_bRendering || time - timeUpdate > .25) { m_pView->Update(); timeUpdate = time; } } else { GThread::sleep(10); } timeOld = time; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -