INFO804 Introduction à l'informatique graphique
Loading...
Searching...
No Matches
Viewer.cpp
Go to the documentation of this file.
1/**
2@file Viewer.cpp
3@author JOL
4*/
5#include <fstream>
6#include "Viewer.h"
7#include "Scene.h"
8#include "Renderer.h"
9#include "Image2D.h"
10#include "Image2DWriter.h"
11
12using namespace std;
13
14// Draws a tetrahedron with 4 colors.
15void
17{
18 // Set up lights
19 if ( ptrScene != 0 )
20 ptrScene->light( *this );
21 // Draw all objects
22 if ( ptrScene != 0 )
23 ptrScene->draw( *this );
24}
25
26
27void
29{
30 // Restore previous viewer state.
31 restoreStateFromFile();
32
33 // Add custom key description (see keyPressEvent).
34 setKeyDescription(Qt::Key_R, "Renders the scene with a ray-tracer (low resolution)");
35 setKeyDescription(Qt::SHIFT+Qt::Key_R, "Renders the scene with a ray-tracer (medium resolution)");
36 setKeyDescription(Qt::CTRL+Qt::Key_R, "Renders the scene with a ray-tracer (high resolution)");
37 setKeyDescription(Qt::Key_D, "Augments the max depth of ray-tracing algorithm");
38 setKeyDescription(Qt::SHIFT+Qt::Key_D, "Decreases the max depth of ray-tracing algorithm");
39
40 // Opens help window
41 help();
42
43 // To move lights around
44 setMouseTracking(true);
45
46 // Inits the scene
47 if ( ptrScene != 0 )
48 ptrScene->init( *this );
49
50 // Gives a bounding box to the camera
51 camera()->setSceneBoundingBox( qglviewer::Vec( -12, -12, -2 ),qglviewer::Vec( 12, 12, 22 ) );
52
53}
54
55void
57{
58 // Get event modifiers key
59 const Qt::KeyboardModifiers modifiers = e->modifiers();
60 bool handled = false;
61 if ((e->key()==Qt::Key_R) && ptrScene != 0 )
62 {
63 int w = camera()->screenWidth();
64 int h = camera()->screenHeight();
65 Renderer renderer( *ptrScene );
66 qglviewer::Vec orig, dir;
67 camera()->convertClickToLine( QPoint( 0,0 ), orig, dir );
68 Vector3 origin( orig );
69 Vector3 dirUL( dir );
70 camera()->convertClickToLine( QPoint( w,0 ), orig, dir );
71 Vector3 dirUR( dir );
72 camera()->convertClickToLine( QPoint( 0, h ), orig, dir );
73 Vector3 dirLL( dir );
74 camera()->convertClickToLine( QPoint( w, h ), orig, dir );
75 Vector3 dirLR( dir );
76 renderer.setViewBox( origin, dirUL, dirUR, dirLL, dirLR );
77 if ( modifiers == Qt::ShiftModifier ) { w /= 2; h /= 2; }
78 else if ( modifiers == Qt::NoModifier ) { w /= 8; h /= 8; }
79 Image2D<Color> image( w, h );
80 renderer.setResolution( image.w(), image.h() );
81 renderer.render( image, maxDepth );
82 ofstream output( "output.ppm" );
83 Image2DWriter<Color>::write( image, output, true );
84 output.close();
85 handled = true;
86 }
87 if (e->key()==Qt::Key_D)
88 {
89 if ( modifiers == Qt::ShiftModifier )
90 { maxDepth = std::max( 1, maxDepth - 1 ); handled = true; }
91 if ( modifiers == Qt::NoModifier )
92 { maxDepth = std::min( 20, maxDepth + 1 ); handled = true; }
93 std::cout << "Max depth is " << maxDepth << std::endl;
94 }
95
96 if (!handled) QGLViewer::keyPressEvent(e);
97}
98
99QString
101{
102 QString text("<h2>S i m p l e V i e w e r</h2>");
103 text += "Use the mouse to move the camera around the object. ";
104 text += "You can respectively revolve around, zoom and translate with the three mouse buttons. ";
105 text += "Left and middle buttons pressed together rotate around the camera view direction axis<br><br>";
106 text += "Pressing <b>Alt</b> and one of the function keys (<b>F1</b>..<b>F12</b>) defines a camera keyFrame. ";
107 text += "Simply press the function key again to restore it. Several keyFrames define a ";
108 text += "camera path. Paths are saved when you quit the application and restored at next start.<br><br>";
109 text += "Press <b>F</b> to display the frame rate, <b>A</b> for the world axis, ";
110 text += "<b>Alt+Return</b> for full screen mode and <b>Control+S</b> to save a snapshot. ";
111 text += "See the <b>Keyboard</b> tab in this window for a complete shortcut list.<br><br>";
112 text += "Double clicks automates single click actions: A left button double click aligns the closer axis with the camera (if close enough). ";
113 text += "A middle button double click fits the zoom of the camera and the right button re-centers the scene.<br><br>";
114 text += "A left button double click while holding right button pressed defines the camera <i>Revolve Around Point</i>. ";
115 text += "See the <b>Mouse</b> tab and the documentation web pages for details.<br><br>";
116 text += "Press <b>Escape</b> to exit the viewer.";
117 text += "Press <b>R</b> to render the scene (low resolution).";
118 text += "Press <b>Shift+R</b> to render the scene (medium resolution).";
119 text += "Press <b>Ctrl+R</b> to render the scene (high resolution).";
120 return text;
121}
Classe générique pour représenter des images 2D.
Definition Image2D.h:10
int h() const
Definition Image2D.h:180
int w() const
Definition Image2D.h:175
virtual void keyPressEvent(QKeyEvent *e)
Celled when pressing a key.
Definition Viewer.cpp:56
virtual void init()
Called before the first draw.
Definition Viewer.cpp:28
virtual void draw()
Called at each draw of the window.
Definition Viewer.cpp:16
virtual QString helpString() const
Called when pressing help.
Definition Viewer.cpp:100
rt::Scene * ptrScene
Stores the scene.
Definition Viewer.h:54
This structure takes care of rendering a scene.
Definition Renderer.h:49
void render(Image2D< Color > &image, int max_depth)
The main rendering routine.
Definition Renderer.h:93
void light(Viewer &viewer)
This function calls the light method of each of its lights.
Definition Scene.h:62
void draw(Viewer &viewer)
This function calls the draw method of each of its objects.
Definition Scene.h:54