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 "PlaneRenderer.h" |
---|
17 | |
---|
18 | PlaneRenderer::PlaneRenderer(CGcontext _context, int _width, int _height): |
---|
19 | n_planes(0), |
---|
20 | g_context(_context), |
---|
21 | render_width(_width), |
---|
22 | render_height(_height), |
---|
23 | active_plane(-1) |
---|
24 | { |
---|
25 | plane.clear(); |
---|
26 | tf.clear(); |
---|
27 | init_shaders(); |
---|
28 | } |
---|
29 | |
---|
30 | |
---|
31 | PlaneRenderer::~PlaneRenderer(){} |
---|
32 | |
---|
33 | //initialize the render shader |
---|
34 | void PlaneRenderer::init_shaders(){ |
---|
35 | |
---|
36 | //plane rendering shader |
---|
37 | m_fprog = loadProgram(g_context, CG_PROFILE_FP30, CG_SOURCE, "/opt/nanovis/lib/shaders/one_plane.cg"); |
---|
38 | m_data_param = cgGetNamedParameter(m_fprog, "data"); |
---|
39 | m_tf_param = cgGetNamedParameter(m_fprog, "tf"); |
---|
40 | m_render_param = cgGetNamedParameter(m_fprog, "render_param"); |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | int PlaneRenderer::add_plane(Texture2D* _p, TransferFunction* _tf){ |
---|
45 | |
---|
46 | int ret = n_planes; |
---|
47 | |
---|
48 | plane.push_back(_p); |
---|
49 | tf.push_back(_tf); |
---|
50 | |
---|
51 | if(ret==0) |
---|
52 | active_plane = ret; |
---|
53 | |
---|
54 | n_planes++; |
---|
55 | return ret; |
---|
56 | } |
---|
57 | |
---|
58 | void |
---|
59 | PlaneRenderer::remove_plane(int index) { |
---|
60 | vector<Texture2D*>::iterator piter = plane.begin()+index; |
---|
61 | vector<TransferFunction*>::iterator tfiter = tf.begin()+index; |
---|
62 | |
---|
63 | plane.erase(piter); |
---|
64 | tf.erase(tfiter); |
---|
65 | |
---|
66 | n_planes--; |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | void PlaneRenderer::render(){ |
---|
71 | if (n_planes == 0) |
---|
72 | return; |
---|
73 | |
---|
74 | glEnable(GL_TEXTURE_2D); |
---|
75 | glEnable(GL_BLEND); |
---|
76 | |
---|
77 | glViewport(0, 0, render_width, render_height); |
---|
78 | glMatrixMode(GL_PROJECTION); |
---|
79 | glLoadIdentity(); |
---|
80 | gluOrtho2D(0, render_width, 0, render_height); |
---|
81 | glMatrixMode(GL_MODELVIEW); |
---|
82 | glLoadIdentity(); |
---|
83 | |
---|
84 | //glColor3f(1.,1.,1.); //MUST HAVE THIS LINE!!! |
---|
85 | |
---|
86 | //if no active plane |
---|
87 | if(active_plane == -1) |
---|
88 | return; |
---|
89 | |
---|
90 | activate_shader(active_plane); |
---|
91 | glBegin(GL_QUADS); |
---|
92 | glTexCoord2f(0, 0); glVertex2f(0, 0); |
---|
93 | glTexCoord2f(1, 0); glVertex2f(render_width, 0); |
---|
94 | glTexCoord2f(1, 1); glVertex2f(render_width, render_height); |
---|
95 | glTexCoord2f(0, 1); glVertex2f(0, render_height); |
---|
96 | glEnd(); |
---|
97 | deactivate_shader(); |
---|
98 | |
---|
99 | } |
---|
100 | |
---|
101 | void PlaneRenderer::activate_shader(int index){ |
---|
102 | |
---|
103 | cgGLSetTextureParameter(m_data_param, plane[index]->id); |
---|
104 | cgGLSetTextureParameter(m_tf_param, tf[index]->id); |
---|
105 | cgGLEnableTextureParameter(m_data_param); |
---|
106 | cgGLEnableTextureParameter(m_tf_param); |
---|
107 | |
---|
108 | cgGLSetParameter4f(m_render_param, 0., 0., 0., 0.); |
---|
109 | |
---|
110 | cgGLBindProgram(m_fprog); |
---|
111 | cgGLEnableProfile(CG_PROFILE_FP30); |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | void PlaneRenderer::deactivate_shader(){ |
---|
116 | cgGLDisableProfile(CG_PROFILE_FP30); |
---|
117 | cgGLDisableTextureParameter(m_data_param); |
---|
118 | cgGLDisableTextureParameter(m_tf_param); |
---|
119 | } |
---|
120 | |
---|
121 | void |
---|
122 | PlaneRenderer::set_active_plane(int index) { |
---|
123 | active_plane = index; |
---|
124 | } |
---|
125 | |
---|
126 | void |
---|
127 | PlaneRenderer::set_screen_size(int w, int h) { |
---|
128 | render_width = w; |
---|
129 | render_height = h; |
---|
130 | } |
---|