1 | /* |
---|
2 | * ---------------------------------------------------------------------- |
---|
3 | * PlaneRenderer.cpp : PlaneRenderer class for volume visualization |
---|
4 | * |
---|
5 | * ====================================================================== |
---|
6 | * AUTHOR: Wei Qiao <qiaow@purdue.edu> |
---|
7 | * Purdue Rendering and Perceptualization Lab (PURPL) |
---|
8 | * |
---|
9 | * Copyright (c) 2004-2006 Purdue Research Foundation |
---|
10 | * |
---|
11 | * See the file "license.terms" for information on usage and |
---|
12 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
13 | * ====================================================================== |
---|
14 | */ |
---|
15 | |
---|
16 | #include <R2/R2FilePath.h> |
---|
17 | #include "PlaneRenderer.h" |
---|
18 | |
---|
19 | PlaneRenderer::PlaneRenderer(CGcontext _context, int _width, int _height): |
---|
20 | n_planes(0), |
---|
21 | g_context(_context), |
---|
22 | render_width(_width), |
---|
23 | render_height(_height), |
---|
24 | active_plane(-1) |
---|
25 | { |
---|
26 | plane.clear(); |
---|
27 | tf.clear(); |
---|
28 | init_shaders(); |
---|
29 | } |
---|
30 | |
---|
31 | |
---|
32 | PlaneRenderer::~PlaneRenderer(){} |
---|
33 | |
---|
34 | //initialize the render shader |
---|
35 | void PlaneRenderer::init_shaders(){ |
---|
36 | |
---|
37 | //plane rendering shader |
---|
38 | R2string path = R2FilePath::getInstance()->getPath("one_plane.cg"); |
---|
39 | m_fprog = loadProgram(g_context, CG_PROFILE_FP30, CG_SOURCE, path); |
---|
40 | m_data_param = cgGetNamedParameter(m_fprog, "data"); |
---|
41 | m_tf_param = cgGetNamedParameter(m_fprog, "tf"); |
---|
42 | m_render_param = cgGetNamedParameter(m_fprog, "render_param"); |
---|
43 | } |
---|
44 | |
---|
45 | |
---|
46 | int PlaneRenderer::add_plane(Texture2D* _p, TransferFunction* _tf){ |
---|
47 | |
---|
48 | int ret = n_planes; |
---|
49 | |
---|
50 | plane.push_back(_p); |
---|
51 | tf.push_back(_tf); |
---|
52 | |
---|
53 | if(ret==0) |
---|
54 | active_plane = ret; |
---|
55 | |
---|
56 | n_planes++; |
---|
57 | return ret; |
---|
58 | } |
---|
59 | |
---|
60 | void |
---|
61 | PlaneRenderer::remove_plane(int index) { |
---|
62 | vector<Texture2D*>::iterator piter = plane.begin()+index; |
---|
63 | vector<TransferFunction*>::iterator tfiter = tf.begin()+index; |
---|
64 | |
---|
65 | plane.erase(piter); |
---|
66 | tf.erase(tfiter); |
---|
67 | |
---|
68 | n_planes--; |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | void PlaneRenderer::render(){ |
---|
73 | if (n_planes == 0) |
---|
74 | return; |
---|
75 | |
---|
76 | glEnable(GL_TEXTURE_2D); |
---|
77 | glEnable(GL_BLEND); |
---|
78 | |
---|
79 | glViewport(0, 0, render_width, render_height); |
---|
80 | glMatrixMode(GL_PROJECTION); |
---|
81 | glLoadIdentity(); |
---|
82 | gluOrtho2D(0, render_width, 0, render_height); |
---|
83 | glMatrixMode(GL_MODELVIEW); |
---|
84 | glLoadIdentity(); |
---|
85 | |
---|
86 | //glColor3f(1.,1.,1.); //MUST HAVE THIS LINE!!! |
---|
87 | |
---|
88 | //if no active plane |
---|
89 | if(active_plane == -1) |
---|
90 | return; |
---|
91 | |
---|
92 | activate_shader(active_plane); |
---|
93 | glBegin(GL_QUADS); |
---|
94 | glTexCoord2f(0, 0); glVertex2f(0, 0); |
---|
95 | glTexCoord2f(1, 0); glVertex2f(render_width, 0); |
---|
96 | glTexCoord2f(1, 1); glVertex2f(render_width, render_height); |
---|
97 | glTexCoord2f(0, 1); glVertex2f(0, render_height); |
---|
98 | glEnd(); |
---|
99 | deactivate_shader(); |
---|
100 | |
---|
101 | } |
---|
102 | |
---|
103 | void PlaneRenderer::activate_shader(int index){ |
---|
104 | |
---|
105 | cgGLSetTextureParameter(m_data_param, plane[index]->id); |
---|
106 | cgGLSetTextureParameter(m_tf_param, tf[index]->id); |
---|
107 | cgGLEnableTextureParameter(m_data_param); |
---|
108 | cgGLEnableTextureParameter(m_tf_param); |
---|
109 | |
---|
110 | cgGLSetParameter4f(m_render_param, 0., 0., 0., 0.); |
---|
111 | |
---|
112 | cgGLBindProgram(m_fprog); |
---|
113 | cgGLEnableProfile(CG_PROFILE_FP30); |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | void PlaneRenderer::deactivate_shader(){ |
---|
118 | cgGLDisableProfile(CG_PROFILE_FP30); |
---|
119 | cgGLDisableTextureParameter(m_data_param); |
---|
120 | cgGLDisableTextureParameter(m_tf_param); |
---|
121 | } |
---|
122 | |
---|
123 | void |
---|
124 | PlaneRenderer::set_active_plane(int index) { |
---|
125 | active_plane = index; |
---|
126 | } |
---|
127 | |
---|
128 | void |
---|
129 | PlaneRenderer::set_screen_size(int w, int h) { |
---|
130 | render_width = w; |
---|
131 | render_height = h; |
---|
132 | } |
---|