00001
00002
00003
00004
00005
00006
00007
00009
00010 #include <GL/glew.h>
00011 #include <GL/glut.h>
00012 #include <Cg/cgGL.h>
00013
00014
00015
00016
00017
00018 #include "VolumeRender.h"
00019
00020
00021
00022
00023
00024
00025
00026
00027 VolumeRender::VolumeRender(CGcontext cg_context, VolumeBuffer *volume)
00028 : m_cg_context(cg_context),
00029 m_volume(volume),
00030 m_density(0.01),
00031 m_brightness(2.0)
00032 {
00033
00034 loadPrograms();
00035 }
00036
00037 VolumeRender::~VolumeRender()
00038 {
00039
00040
00041
00042
00043 cgDestroyProgram(m_raymarch_fprog);
00044 }
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 void
00073 VolumeRender::loadPrograms()
00074 {
00075
00076 m_cg_fprofile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
00077
00078
00079
00080
00081
00082
00083 m_raymarch_fprog = cgCreateProgramFromFile( m_cg_context, CG_SOURCE, "post2d.cg", m_cg_fprofile , "main", 0);
00084 cgGLLoadProgram(m_raymarch_fprog);
00085
00086 cgGLSetOptimalOptions(m_cg_fprofile);
00087
00088
00089
00090
00091 volume_texture = cgGetNamedParameter(m_raymarch_fprog, "volume_texture");
00092 volume_texture_s1 = cgGetNamedParameter(m_raymarch_fprog, "volume_texture_s1");
00093 volume_texture_s2 = cgGetNamedParameter(m_raymarch_fprog, "volume_texture_s2");
00094 volume_texture_s3 = cgGetNamedParameter(m_raymarch_fprog, "volume_texture_s3");
00095
00096 transfer_function = cgGetNamedParameter(m_raymarch_fprog, "transfer_function");
00097 transfer_function_s1 = cgGetNamedParameter(m_raymarch_fprog, "transfer_function_s1");
00098 transfer_function_s2 = cgGetNamedParameter(m_raymarch_fprog, "transfer_function_s2");
00099 transfer_function_s3 = cgGetNamedParameter(m_raymarch_fprog, "transfer_function_s3");
00100 }
00101
00102
00103 void
00104 VolumeRender::render()
00105 {
00106 int iz;
00107 float z;
00108 f3dVectorF quad[4];
00109
00110 GLuint texture_id;
00111 GLuint texture_id_s1;
00112 GLuint texture_id_s2;
00113 GLuint texture_id_s3;
00114
00115 float depth;
00116 int depth2;
00117 float inc;
00118 int loop;
00119 int front;
00120 front = 0;
00121
00122
00123 depth = 0;
00124 depth2 = 0;
00125 inc=(float)(1.0/255);
00126 loop=(int)(255);
00127
00128 for (iz=0; iz < loop; iz++)
00129 {
00130 switch(front)
00131 {
00132
00133
00134 case 0: z=depth*(m_volume->vertex[0].z-m_volume->vertex[4].z)+m_volume->vertex[4].z;
00135 quad[0] = f3dVectorF(m_volume->vertex[0].x,m_volume->vertex[0].y,z);
00136 quad[1] = f3dVectorF(m_volume->vertex[1].x,m_volume->vertex[1].y,z);
00137 quad[2] = f3dVectorF(m_volume->vertex[2].x,m_volume->vertex[2].y,z);
00138 quad[3] = f3dVectorF(m_volume->vertex[3].x,m_volume->vertex[3].y,z);
00139
00140 depth2=floor(depth*(m_volume->getDepth()));
00141
00142 texture_id = m_volume->m_tex_z[m_volume->getDepth()-1-depth2];
00143
00144 texture_id_s1 = m_volume->m_tex_z_s1[m_volume->getDepth()-1-depth2];
00145 texture_id_s2 = m_volume->m_tex_z_s2[m_volume->getDepth()-1-depth2];
00146 texture_id_s3 = m_volume->m_tex_z_s3[m_volume->getDepth()-1-depth2];
00147
00148
00149
00150
00151 break;
00152 }
00153
00154 depth+=inc;
00155
00156
00157 cgGLBindProgram(m_raymarch_fprog);
00158 cgGLEnableProfile(m_cg_fprofile);
00159
00160 cgGLSetTextureParameter(volume_texture, texture_id);
00161 cgGLSetTextureParameter(volume_texture_s1, texture_id_s1);
00162 cgGLSetTextureParameter(volume_texture_s2, texture_id_s2);
00163 cgGLSetTextureParameter(volume_texture_s3, texture_id_s3);
00164
00165
00166 cgGLSetTextureParameter(transfer_function, m_tfid);
00167 cgGLSetTextureParameter(transfer_function_s1, m_tfid_s1);
00168 cgGLSetTextureParameter(transfer_function_s2, m_tfid_s2);
00169 cgGLSetTextureParameter(transfer_function_s3, m_tfid_s3);
00170
00171 cgGLEnableTextureParameter(volume_texture);
00172 cgGLEnableTextureParameter(volume_texture_s1);
00173 cgGLEnableTextureParameter(volume_texture_s2);
00174 cgGLEnableTextureParameter(volume_texture_s3);
00175
00176 cgGLEnableTextureParameter(transfer_function);
00177 cgGLEnableTextureParameter(transfer_function_s1);
00178 cgGLEnableTextureParameter(transfer_function_s2);
00179 cgGLEnableTextureParameter(transfer_function_s3);
00180
00181 glBegin(GL_QUADS);
00182 glNormal3f(m_volume->normal[front].x,m_volume->normal[front].y,m_volume->normal[front].z);
00183 glTexCoord2f(0,0);
00184 glVertex3f(quad[0].x,quad[0].y,quad[0].z);
00185 glTexCoord2f(1.0,0);
00186 glVertex3f(quad[1].x,quad[1].y,quad[1].z);
00187 glTexCoord2f(1.0,1.0);
00188 glVertex3f(quad[2].x,quad[2].y,quad[2].z);
00189 glTexCoord2f(0,1.0);
00190 glVertex3f(quad[3].x,quad[3].y,quad[3].z);
00191 glEnd();
00192
00193 cgGLDisableProfile(m_cg_fprofile);
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220 }
00221
00222 }
00223
00224
00225 void
00226 VolumeRender::renderSlice(int sn)
00227 {
00228 f3dVectorF quad[4];
00229
00230 GLuint texture_id;
00231 GLuint texture_id_s1;
00232 GLuint texture_id_s2;
00233 GLuint texture_id_s3;
00234
00235 texture_id = m_volume->m_tex_z[sn];
00236 texture_id_s1 = m_volume->m_tex_z_s1[sn];
00237 texture_id_s2 = m_volume->m_tex_z_s1[sn];
00238 texture_id_s3 = m_volume->m_tex_z_s1[sn];
00239
00240 glBindTexture(GL_TEXTURE_2D, texture_id);
00241
00242 cgGLBindProgram(m_raymarch_fprog);
00243 cgGLEnableProfile(m_cg_fprofile);
00244
00245 cgGLSetTextureParameter(volume_texture, texture_id);
00246 cgGLSetTextureParameter(volume_texture_s1, texture_id_s1);
00247 cgGLSetTextureParameter(volume_texture_s2, texture_id_s2);
00248 cgGLSetTextureParameter(volume_texture_s3, texture_id_s3);
00249
00250
00251 cgGLSetTextureParameter(transfer_function, m_tfid);
00252 cgGLSetTextureParameter(transfer_function_s1, m_tfid_s1);
00253 cgGLSetTextureParameter(transfer_function_s2, m_tfid_s2);
00254 cgGLSetTextureParameter(transfer_function_s3, m_tfid_s3);
00255
00256 cgGLEnableTextureParameter(volume_texture);
00257 cgGLEnableTextureParameter(volume_texture_s1);
00258 cgGLEnableTextureParameter(volume_texture_s2);
00259 cgGLEnableTextureParameter(volume_texture_s3);
00260
00261 cgGLEnableTextureParameter(transfer_function);
00262 cgGLEnableTextureParameter(transfer_function_s1);
00263 cgGLEnableTextureParameter(transfer_function_s2);
00264 cgGLEnableTextureParameter(transfer_function_s3);
00265
00266 glEnable(GL_TEXTURE_2D);
00267 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
00268 glBegin(GL_QUADS);
00269 glTexCoord3f(0.0f, 0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f);
00270 glTexCoord3f(0.0f, 1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 0.0f);
00271 glTexCoord3f(1.0f, 1.0f, 0.0f); glVertex3f(1.0f, 1.0f, 0.0f);
00272 glTexCoord3f(1.0f, 0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
00273 glEnd();
00274
00275 cgGLDisableProfile(m_cg_fprofile);
00276 }
00277
00278
00279
00280 void
00281 VolumeRender::loaddata()
00282 {
00283 m_volume->create2DTextures(GL_UNSIGNED_BYTE);
00284 }