Warning, /include/Geant4/toolx/sg/GL_action is written in an unsupported language. File is not indexed.
0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003
0004 #ifndef toolx_sg_GL_action
0005 #define toolx_sg_GL_action
0006
0007 #include "GL_manager"
0008
0009 #include <tools/sg/render_action>
0010
0011 namespace toolx {
0012 namespace sg {
0013
0014 template <class GL_FUNCTIONS>
0015 class GL_action_T : public tools::sg::render_action, protected GL_FUNCTIONS {
0016 using parent_gl_functions = GL_FUNCTIONS;
0017 private:
0018 TOOLS_T_ACTION(GL_FUNCTIONS,GL_action_T,toolx::sg::GL_action_T,tools::sg::render_action)
0019 public:
0020 virtual void draw_vertex_array(tools::gl::mode_t a_mode,size_t a_floatn,const float* a_xyzs){
0021 size_t num = a_floatn/3;
0022 if(!num) return;
0023 _draw_v(a_mode,num,a_xyzs);
0024 }
0025
0026 virtual void draw_vertex_array_xy(tools::gl::mode_t a_mode,size_t a_floatn,const float* a_xys){
0027 size_t num = a_floatn/2;
0028 if(!num) return;
0029
0030 #ifdef TOOLS_USE_GL_VERSION_3_2
0031
0032 GLuint vao_id = 0;
0033 parent_gl_functions::gl__GenVertexArrays(1,&vao_id);
0034 if(!vao_id) {
0035 m_out << "toolx::sg::GL_action_T::draw_vertex_array_xy : glGenVertexArrays failed ()." << std::endl;
0036 return;
0037 }
0038
0039 parent_gl_functions::gl__BindVertexArray(vao_id);
0040 ////////////////////////////////////////////////////////////////////
0041 ////////////////////////////////////////////////////////////////////
0042 unsigned int vbo_id = 0;
0043 parent_gl_functions::gl__GenBuffers(1,&vbo_id);
0044 if(!vbo_id) {
0045 m_out << "toolx::sg::GL_action_T::draw_vertex_array_xy : glGenBuffers failed ()." << std::endl;
0046 parent_gl_functions::gl__DeleteVertexArrays(1,&vao_id);
0047 return;
0048 }
0049 std::vector<float> gsto_data;
0050 size_t ngsto = num*2; //num points * 2 coords
0051 gsto_data.resize(ngsto);
0052 float* _gsto_data = gsto_data.data();
0053 ::memcpy(_gsto_data,a_xys,ngsto*sizeof(float));
0054
0055 parent_gl_functions::gl__BindBuffer(GL_ARRAY_BUFFER,vbo_id);
0056 parent_gl_functions::gl__BufferData(GL_ARRAY_BUFFER,gsto_data.size()*sizeof(float),_gsto_data,GL_STATIC_DRAW);
0057 parent_gl_functions::gl__BindBuffer(GL_ARRAY_BUFFER,0);
0058 ////////////////////////////////////////////////////////////////////
0059 ////////////////////////////////////////////////////////////////////
0060 parent_gl_functions::gl__BindVertexArray(0);
0061
0062 parent_gl_functions::gl__BindVertexArray(vao_id);
0063 parent_gl_functions::gl__BindBuffer(GL_ARRAY_BUFFER,vbo_id);
0064 parent_gl_functions::gl__VertexAttribPointer(m_mgr.g_one_pos_location(),2,GL_FLOAT,GL_FALSE,0,NULL);
0065 parent_gl_functions::gl__BindBuffer(GL_ARRAY_BUFFER,0);
0066 parent_gl_functions::gl__BindVertexArray(0);
0067
0068 parent_gl_functions::gl__BindVertexArray(vao_id);
0069 parent_gl_functions::gl__EnableVertexAttribArray(m_mgr.g_one_pos_location());
0070 parent_gl_functions::gl__DrawArrays(a_mode,0,(GLsizei)num);
0071 parent_gl_functions::gl__DisableVertexAttribArray(m_mgr.g_one_pos_location());
0072 parent_gl_functions::gl__BindVertexArray(0);
0073
0074 ////////////////////////////////////////////////////////////////////
0075 ////////////////////////////////////////////////////////////////////
0076 parent_gl_functions::gl__DeleteBuffers(1,&vbo_id);
0077 parent_gl_functions::gl__DeleteVertexArrays(1,&vao_id);
0078
0079 #else //not TOOLS_USE_GL_VERSION_3_2
0080
0081 #ifdef _WIN32
0082 float* vp = new float[num*3];
0083 if(!vp) return;
0084 float* pos = vp;
0085 float* pda = (float*)a_xys;
0086 for(size_t index=0;index<num;index++){
0087 *pos = *pda;pos++;pda++;
0088 *pos = *pda;pos++;pda++;
0089 *pos = 0;pos++; //Windows GL needs a z = 0.
0090 }
0091 parent_gl_functions::gl__EnableClientState(GL_VERTEX_ARRAY);
0092 parent_gl_functions::gl__VertexPointer(3,GL_FLOAT,0,vp);
0093 parent_gl_functions::gl__DrawArrays(a_mode,0,(GLsizei)num);
0094 parent_gl_functions::gl__DisableClientState(GL_VERTEX_ARRAY);
0095 delete [] vp;
0096 #else //not _WIN32
0097 parent_gl_functions::gl__EnableClientState(GL_VERTEX_ARRAY);
0098 parent_gl_functions::gl__VertexPointer(2,GL_FLOAT,0,a_xys);
0099 parent_gl_functions::gl__DrawArrays(a_mode,0,(GLsizei)num);
0100 parent_gl_functions::gl__DisableClientState(GL_VERTEX_ARRAY);
0101 #endif //not _WIN32
0102
0103 #endif //not TOOLS_USE_GL_VERSION_3_2
0104 }
0105
0106 virtual void draw_vertex_color_array(tools::gl::mode_t a_mode,size_t a_floatn,const float* a_xyzs,const float* a_rgbas){
0107 // Used in atb_vertices.
0108 // We expect a_rgbas of size : 4*(a_floatn/3)
0109 // (then one RGBA color per 3D point).
0110 size_t num = a_floatn/3;
0111 if(!num) return;
0112 _draw_vc(a_mode,num,a_xyzs,a_rgbas);
0113 }
0114
0115 virtual void draw_vertex_normal_array(tools::gl::mode_t a_mode,size_t a_floatn,const float* a_xyzs,const float* a_nms){
0116 // We expect a_nms of size : 3*(a_floatn/3)
0117 // (then one normal per 3D point).
0118 size_t num = a_floatn/3;
0119 if(!num) return;
0120 _draw_vn(a_mode,num,a_xyzs,a_nms);
0121 }
0122
0123 virtual void draw_vertex_color_normal_array(tools::gl::mode_t a_mode,
0124 size_t a_floatn,const float* a_xyzs,const float* a_rgbas,const float* a_nms){
0125 // Used in atb_vertices.
0126 // We expect a_nms of size : 3*(a_floatn/3)
0127 // (then one normal per 3D point).
0128 // We expect a_rgbas of size : 4*(a_floatn/3)
0129 // (then one RGBA color per 3D point).
0130 size_t num = a_floatn/3;
0131 if(!num) return;
0132 _draw_vcn(a_mode,num,a_xyzs,a_rgbas,a_nms);
0133 }
0134
0135 /////////////////////////////////////////////////////////////////
0136 /// texture /////////////////////////////////////////////////////
0137 /////////////////////////////////////////////////////////////////
0138 virtual void draw_vertex_array_texture(tools::gl::mode_t a_mode,
0139 size_t a_floatn,
0140 const float* a_xyzs,
0141 gstoid a_tex,
0142 const float* a_tex_coords) {
0143 size_t num = a_floatn/3;
0144 if(!num) return;
0145
0146 //expect 2*num a_tex_coords.
0147
0148 #ifdef TOOLS_USE_GL_VERSION_3_2
0149
0150 GLuint vao_id = 0;
0151 parent_gl_functions::gl__GenVertexArrays(1,&vao_id);
0152 if(!vao_id) {
0153 m_out << "toolx::sg::GL_action_T::draw_vertex_array_texture : glGenVertexArrays failed ()." << std::endl;
0154 return;
0155 }
0156
0157 size_t nxyzs = a_floatn;
0158
0159 parent_gl_functions::gl__BindVertexArray(vao_id);
0160 ////////////////////////////////////////////////////////////////////
0161 ////////////////////////////////////////////////////////////////////
0162 unsigned int vbo_id = 0;
0163 parent_gl_functions::gl__GenBuffers(1,&vbo_id);
0164 if(!vbo_id) {
0165 m_out << "toolx::sg::GL_action_T::draw_vertex_array_texture : glGenBuffers failed ()." << std::endl;
0166 parent_gl_functions::gl__DeleteVertexArrays(1,&vao_id);
0167 return;
0168 }
0169 {size_t ntcs = num*2;
0170 size_t ngsto = nxyzs+ntcs;
0171 std::vector<float> gsto_data;
0172 gsto_data.resize(ngsto);
0173 float* _gsto_data = gsto_data.data();
0174 ::memcpy(_gsto_data,a_xyzs,a_floatn*sizeof(float));
0175 ::memcpy(_gsto_data+nxyzs,a_tex_coords,ntcs*sizeof(float));
0176
0177 parent_gl_functions::gl__BindBuffer(GL_ARRAY_BUFFER,vbo_id);
0178 parent_gl_functions::gl__BufferData(GL_ARRAY_BUFFER,gsto_data.size()*sizeof(float),_gsto_data,GL_STATIC_DRAW);
0179 parent_gl_functions::gl__BindBuffer(GL_ARRAY_BUFFER,0);
0180 ////////////////////////////////////////////////////////////////////
0181 ////////////////////////////////////////////////////////////////////
0182 parent_gl_functions::gl__BindVertexArray(0);}
0183
0184 ////////////////////////////////////////////////////////////////////
0185 ////////////////////////////////////////////////////////////////////
0186 parent_gl_functions::gl__BindVertexArray(vao_id);
0187 parent_gl_functions::gl__BindBuffer(GL_ARRAY_BUFFER,vbo_id);
0188 {char* pos_xyzs = NULL;
0189 char* pos_tcs = NULL;pos_tcs += nxyzs*sizeof(float);
0190 parent_gl_functions::gl__VertexAttribPointer(m_mgr.g_one_pos_location(),3,GL_FLOAT,GL_FALSE,0,pos_xyzs);
0191 parent_gl_functions::gl__VertexAttribPointer(m_mgr.g_one_tex_location(),2,GL_FLOAT,GL_FALSE,0,pos_tcs);}
0192 parent_gl_functions::gl__BindBuffer(GL_ARRAY_BUFFER,0);
0193 parent_gl_functions::gl__BindVertexArray(0);
0194
0195 m_mgr.bind_gsto(a_tex);
0196 parent_gl_functions::gl__BindVertexArray(vao_id);
0197 parent_gl_functions::gl__Uniform1i(m_mgr.g_tex_on_location(),1);
0198 parent_gl_functions::gl__Uniform1i(m_mgr.g_sampler_location(),0);
0199 parent_gl_functions::gl__EnableVertexAttribArray(m_mgr.g_one_pos_location());
0200 parent_gl_functions::gl__EnableVertexAttribArray(m_mgr.g_one_tex_location());
0201 parent_gl_functions::gl__DrawArrays(a_mode,0,(GLsizei)num);
0202 parent_gl_functions::gl__DisableVertexAttribArray(m_mgr.g_one_pos_location());
0203 parent_gl_functions::gl__DisableVertexAttribArray(m_mgr.g_one_tex_location());
0204 parent_gl_functions::gl__Uniform1i(m_mgr.g_tex_on_location(),0);
0205 parent_gl_functions::gl__BindVertexArray(0);
0206 parent_gl_functions::gl__BindTexture(GL_TEXTURE_2D,0);
0207
0208 ////////////////////////////////////////////////////////////////////
0209 ////////////////////////////////////////////////////////////////////
0210 parent_gl_functions::gl__DeleteBuffers(1,&vbo_id);
0211 parent_gl_functions::gl__DeleteVertexArrays(1,&vao_id);
0212
0213 #else //not TOOLS_USE_GL_VERSION_3_2
0214 parent_gl_functions::gl__Enable(GL_TEXTURE_2D);
0215
0216 m_mgr.bind_gsto(a_tex);
0217
0218 parent_gl_functions::gl__EnableClientState(GL_VERTEX_ARRAY);
0219 parent_gl_functions::gl__EnableClientState(GL_TEXTURE_COORD_ARRAY);
0220 parent_gl_functions::gl__VertexPointer(3,GL_FLOAT,0,a_xyzs);
0221 parent_gl_functions::gl__TexCoordPointer(2,GL_FLOAT,0,a_tex_coords);
0222 parent_gl_functions::gl__DrawArrays(a_mode,0,(GLsizei)num);
0223 parent_gl_functions::gl__DisableClientState(GL_VERTEX_ARRAY);
0224 parent_gl_functions::gl__DisableClientState(GL_TEXTURE_COORD_ARRAY);
0225
0226 parent_gl_functions::gl__BindTexture(GL_TEXTURE_2D,0);
0227
0228 parent_gl_functions::gl__Disable(GL_TEXTURE_2D);
0229 #endif //not TOOLS_USE_GL_VERSION_3_2
0230 }
0231
0232 virtual void draw_vertex_normal_array_texture(tools::gl::mode_t a_mode,
0233 size_t a_floatn,
0234 const float* a_xyzs,
0235 const float* a_nms,
0236 gstoid a_tex,
0237 const float* a_tex_coords) {
0238 size_t num = a_floatn/3;
0239 if(!num) return;
0240
0241 //expect 2*num a_tex_coords.
0242
0243 #ifdef TOOLS_USE_GL_VERSION_3_2
0244
0245 GLuint vao_id = 0;
0246 parent_gl_functions::gl__GenVertexArrays(1,&vao_id);
0247 if(!vao_id) {
0248 m_out << "toolx::sg::GL_action_T::draw_vertex_normal_array_texture : glGenVertexArrays failed ()." << std::endl;
0249 return;
0250 }
0251
0252 size_t nxyzs = a_floatn;
0253 size_t nnms = nxyzs;
0254
0255 parent_gl_functions::gl__BindVertexArray(vao_id);
0256 ////////////////////////////////////////////////////////////////////
0257 ////////////////////////////////////////////////////////////////////
0258 unsigned int vbo_id = 0;
0259 parent_gl_functions::gl__GenBuffers(1,&vbo_id);
0260 if(!vbo_id) {
0261 m_out << "toolx::sg::GL_action_T::draw_vertex_normal_array_texture : glGenBuffers failed ()." << std::endl;
0262 parent_gl_functions::gl__DeleteVertexArrays(1,&vao_id);
0263 return;
0264 }
0265 {size_t ntcs = num*2;
0266 size_t ngsto = nxyzs+nnms+ntcs;
0267 std::vector<float> gsto_data;
0268 gsto_data.resize(ngsto);
0269 float* _gsto_data = gsto_data.data();
0270 ::memcpy(_gsto_data,a_xyzs,nxyzs*sizeof(float));
0271 ::memcpy(_gsto_data+nxyzs,a_nms,nnms*sizeof(float));
0272 ::memcpy(_gsto_data+nxyzs+nnms,a_tex_coords,ntcs*sizeof(float));
0273
0274 parent_gl_functions::gl__BindBuffer(GL_ARRAY_BUFFER,vbo_id);
0275 parent_gl_functions::gl__BufferData(GL_ARRAY_BUFFER,gsto_data.size()*sizeof(float),_gsto_data,GL_STATIC_DRAW);
0276 parent_gl_functions::gl__BindBuffer(GL_ARRAY_BUFFER,0);
0277 ////////////////////////////////////////////////////////////////////
0278 ////////////////////////////////////////////////////////////////////
0279 parent_gl_functions::gl__BindVertexArray(0);}
0280
0281 ////////////////////////////////////////////////////////////////////
0282 ////////////////////////////////////////////////////////////////////
0283 parent_gl_functions::gl__BindVertexArray(vao_id);
0284 parent_gl_functions::gl__BindBuffer(GL_ARRAY_BUFFER,vbo_id);
0285 {char* pos_xyzs = NULL;
0286 char* pos_nms = NULL;pos_nms += nxyzs*sizeof(float);
0287 char* pos_tcs = NULL;pos_tcs += (nxyzs+nnms)*sizeof(float);
0288 parent_gl_functions::gl__VertexAttribPointer(m_mgr.g_one_pos_location(),3,GL_FLOAT,GL_FALSE,0,pos_xyzs);
0289 parent_gl_functions::gl__VertexAttribPointer(m_mgr.g_one_normal_location(),3,GL_FLOAT,GL_FALSE,0,pos_nms);
0290 parent_gl_functions::gl__VertexAttribPointer(m_mgr.g_one_tex_location(),2,GL_FLOAT,GL_FALSE,0,pos_tcs);
0291 parent_gl_functions::gl__BindBuffer(GL_ARRAY_BUFFER,0);
0292 parent_gl_functions::gl__BindVertexArray(0);}
0293
0294 m_mgr.bind_gsto(a_tex);
0295 parent_gl_functions::gl__BindVertexArray(vao_id);
0296 parent_gl_functions::gl__Uniform1i(m_mgr.g_tex_on_location(),1);
0297 parent_gl_functions::gl__Uniform1i(m_mgr.g_sampler_location(),0);
0298 parent_gl_functions::gl__Uniform1i(m_mgr.g_pos_normal_location(),true);
0299 parent_gl_functions::gl__EnableVertexAttribArray(m_mgr.g_one_pos_location());
0300 parent_gl_functions::gl__EnableVertexAttribArray(m_mgr.g_one_normal_location());
0301 parent_gl_functions::gl__EnableVertexAttribArray(m_mgr.g_one_tex_location());
0302 parent_gl_functions::gl__DrawArrays(a_mode,0,(GLsizei)num);
0303 parent_gl_functions::gl__DisableVertexAttribArray(m_mgr.g_one_pos_location());
0304 parent_gl_functions::gl__DisableVertexAttribArray(m_mgr.g_one_normal_location());
0305 parent_gl_functions::gl__DisableVertexAttribArray(m_mgr.g_one_tex_location());
0306 parent_gl_functions::gl__Uniform1i(m_mgr.g_tex_on_location(),0);
0307 parent_gl_functions::gl__Uniform1i(m_mgr.g_pos_normal_location(),false);
0308 parent_gl_functions::gl__BindVertexArray(0);
0309 parent_gl_functions::gl__BindTexture(GL_TEXTURE_2D,0);
0310
0311 ////////////////////////////////////////////////////////////////////
0312 ////////////////////////////////////////////////////////////////////
0313 parent_gl_functions::gl__DeleteVertexArrays(1,&vao_id);
0314 parent_gl_functions::gl__DeleteBuffers(1,&vbo_id);
0315
0316 #else //not TOOLS_USE_GL_VERSION_3_2
0317 parent_gl_functions::gl__Enable(GL_TEXTURE_2D);
0318
0319 m_mgr.bind_gsto(a_tex);
0320
0321 parent_gl_functions::gl__EnableClientState(GL_VERTEX_ARRAY);
0322 parent_gl_functions::gl__EnableClientState(GL_NORMAL_ARRAY);
0323 parent_gl_functions::gl__EnableClientState(GL_TEXTURE_COORD_ARRAY);
0324 parent_gl_functions::gl__VertexPointer(3,GL_FLOAT,0,a_xyzs);
0325 parent_gl_functions::gl__NormalPointer(GL_FLOAT,0,a_nms);
0326 parent_gl_functions::gl__TexCoordPointer(2,GL_FLOAT,0,a_tex_coords);
0327 parent_gl_functions::gl__DrawArrays(a_mode,0,(GLsizei)num);
0328 parent_gl_functions::gl__DisableClientState(GL_NORMAL_ARRAY);
0329 parent_gl_functions::gl__DisableClientState(GL_VERTEX_ARRAY);
0330 parent_gl_functions::gl__DisableClientState(GL_TEXTURE_COORD_ARRAY);
0331
0332 parent_gl_functions::gl__BindTexture(GL_TEXTURE_2D,0);
0333
0334 parent_gl_functions::gl__Disable(GL_TEXTURE_2D);
0335 #endif //not TOOLS_USE_GL_VERSION_3_2
0336 }
0337
0338 /////////////////////////////////////////////////////////////////
0339 /// gsto ////////////////////////////////////////////////////////
0340 /////////////////////////////////////////////////////////////////
0341
0342 virtual void begin_gsto(gstoid a_id) {
0343 switch(m_mgr.get_gsto_mode()){
0344
0345 case tools::sg::gsto_gl_vbo:{
0346 m_mgr.bind_gsto(a_id);
0347 }break;
0348
0349 case tools::sg::gsto_gl_list:{
0350 #ifndef TOOLS_USE_GL_VERSION_3_2
0351 m_gsto = a_id;
0352 m_created = false;
0353 m_gl_id = m_mgr.gsto_gl_list_id(a_id,m_created);
0354 if(m_gl_id && m_created) {
0355 parent_gl_functions::gl__NewList(m_gl_id,GL_COMPILE);
0356 }
0357 #endif
0358 }break;
0359
0360 case tools::sg::gsto_memory:{
0361 m_gsto = a_id;
0362 }break;
0363 }
0364 }
0365
0366 virtual void end_gsto() {
0367 switch(m_mgr.get_gsto_mode()){
0368
0369 case tools::sg::gsto_gl_vbo:{
0370 m_mgr.unbind_gsto();
0371 }break;
0372
0373 case tools::sg::gsto_gl_list:{
0374 #ifndef TOOLS_USE_GL_VERSION_3_2
0375 if(m_gl_id && m_created) {
0376 parent_gl_functions::gl__EndList();
0377 }
0378 if(m_gl_id) parent_gl_functions::gl__CallList(m_gl_id);
0379 m_created = false;
0380 m_gl_id = 0;
0381 m_gsto = 0;
0382 #endif
0383 }break;
0384
0385 case tools::sg::gsto_memory:{
0386 m_gsto = 0;
0387 }break;
0388 }
0389 }
0390
0391 typedef tools::sg::bufpos bufpos;
0392 virtual void draw_gsto_v(tools::gl::mode_t a_mode,size_t a_elems,bufpos a_pos_xyzs){
0393
0394 switch(m_mgr.get_gsto_mode()){
0395
0396 case tools::sg::gsto_gl_vbo:{
0397 #ifdef TOOLS_USE_GL_VERSION_3_2
0398 char* pos_xyzs = NULL;pos_xyzs += a_pos_xyzs;
0399 _draw_shader_v(a_mode,a_elems,pos_xyzs);
0400 #endif
0401 }break;
0402
0403 case tools::sg::gsto_gl_list:{
0404 #ifndef TOOLS_USE_GL_VERSION_3_2
0405 float* buffer = m_mgr.gsto_data(m_gsto);
0406 if(!buffer) return;
0407 char* pos_xyzs = (char*)buffer;pos_xyzs += a_pos_xyzs;
0408 if(m_gl_id && m_created) {
0409 parent_gl_functions::gl__Begin(a_mode);
0410 float* pos = (float*)pos_xyzs;
0411 for(size_t index=0;index<a_elems;index++,pos+=3) {
0412 parent_gl_functions::gl__Vertex3f(*(pos+0),*(pos+1),*(pos+2));
0413 }
0414 parent_gl_functions::gl__End();
0415 }
0416 #endif
0417 }break;
0418
0419 case tools::sg::gsto_memory:{
0420 float* buffer = m_mgr.gsto_data(m_gsto);
0421 if(!buffer) return;
0422 char* pos_xyzs = (char*)buffer;pos_xyzs += a_pos_xyzs;
0423 _draw_v(a_mode,a_elems,pos_xyzs);
0424 }break;
0425 }
0426 }
0427
0428 virtual void draw_gsto_vc(tools::gl::mode_t a_mode,size_t a_elems,bufpos a_pos_xyzs,bufpos a_pos_rgbas){
0429
0430 switch(m_mgr.get_gsto_mode()){
0431
0432 case tools::sg::gsto_gl_vbo:{
0433 #ifdef TOOLS_USE_GL_VERSION_3_2
0434 char* pos_xyzs = NULL;pos_xyzs += a_pos_xyzs;
0435 char* pos_rgbas = NULL;pos_rgbas += a_pos_rgbas;
0436 _draw_shader_vc(a_mode,a_elems,pos_xyzs,pos_rgbas);
0437 #endif
0438 }break;
0439
0440 case tools::sg::gsto_gl_list:{
0441 #ifndef TOOLS_USE_GL_VERSION_3_2
0442 float* buffer = m_mgr.gsto_data(m_gsto);
0443 if(!buffer) return;
0444 char* pos_xyzs = (char*)buffer;pos_xyzs += a_pos_xyzs;
0445 char* pos_rgbas = (char*)buffer;pos_rgbas += a_pos_rgbas;
0446 if(m_gl_id && m_created) {
0447 parent_gl_functions::gl__Begin(a_mode);
0448 float* pos = (float*)pos_xyzs;
0449 float* pco = (float*)pos_rgbas;
0450 for(size_t index=0;index<a_elems;index++,pos+=3,pco+=4) {
0451 parent_gl_functions::gl__Color4f (*(pco+0),*(pco+1),*(pco+2),*(pco+3));
0452 parent_gl_functions::gl__Vertex3f(*(pos+0),*(pos+1),*(pos+2));
0453 }
0454 parent_gl_functions::gl__End();
0455 }
0456 #endif
0457 }break;
0458
0459 case tools::sg::gsto_memory:{
0460 float* buffer = m_mgr.gsto_data(m_gsto);
0461 if(!buffer) return;
0462 char* pos_xyzs = (char*)buffer;pos_xyzs += a_pos_xyzs;
0463 char* pos_rgbas = (char*)buffer;pos_rgbas += a_pos_rgbas;
0464 _draw_vc(a_mode,a_elems,pos_xyzs,pos_rgbas);
0465 }break;
0466 }
0467 }
0468
0469 virtual void draw_gsto_vn(tools::gl::mode_t a_mode,size_t a_elems,bufpos a_pos_xyzs,bufpos a_pos_nms){
0470
0471 switch(m_mgr.get_gsto_mode()){
0472
0473 case tools::sg::gsto_gl_vbo:{
0474 #ifdef TOOLS_USE_GL_VERSION_3_2
0475 char* pos_xyzs = NULL;pos_xyzs += a_pos_xyzs;
0476 char* pos_nms = NULL;pos_nms += a_pos_nms;
0477 _draw_shader_vn(a_mode,a_elems,pos_xyzs,pos_nms);
0478 #endif
0479 }break;
0480
0481 case tools::sg::gsto_gl_list:{
0482 #ifndef TOOLS_USE_GL_VERSION_3_2
0483 float* buffer = m_mgr.gsto_data(m_gsto);
0484 if(!buffer) return;
0485 char* pos_xyzs = (char*)buffer;pos_xyzs += a_pos_xyzs;
0486 if(m_gl_id && m_created) {
0487 parent_gl_functions::gl__Begin(a_mode);
0488 float* pos = (float*)pos_xyzs;
0489 for(size_t index=0;index<a_elems;index++,pos+=3) {
0490 parent_gl_functions::gl__Vertex3f(*(pos+0),*(pos+1),*(pos+2));
0491 }
0492 parent_gl_functions::gl__End();
0493 }
0494 #endif
0495 }break;
0496
0497 case tools::sg::gsto_memory:{
0498 float* buffer = m_mgr.gsto_data(m_gsto);
0499 if(!buffer) return;
0500 char* pos_xyzs = (char*)buffer;pos_xyzs += a_pos_xyzs;
0501 char* pos_nms = (char*)buffer;pos_nms += a_pos_nms;
0502 _draw_vn(a_mode,a_elems,pos_xyzs,pos_nms);
0503 }break;
0504 }
0505 }
0506
0507 virtual void draw_gsto_vcn(tools::gl::mode_t a_mode,size_t a_elems,bufpos a_pos_xyzs,bufpos a_pos_rgbas,bufpos a_pos_nms){
0508
0509 switch(m_mgr.get_gsto_mode()){
0510
0511 case tools::sg::gsto_gl_vbo:{
0512 #ifdef TOOLS_USE_GL_VERSION_3_2
0513 char* pos_xyzs = NULL;pos_xyzs += a_pos_xyzs;
0514 char* pos_rgbas = NULL;pos_rgbas += a_pos_rgbas;
0515 char* pos_nms = NULL;pos_nms += a_pos_nms;
0516 _draw_shader_vcn(a_mode,a_elems,pos_xyzs,pos_rgbas,pos_nms);
0517 #endif
0518 }break;
0519
0520 case tools::sg::gsto_gl_list:{
0521 #ifndef TOOLS_USE_GL_VERSION_3_2
0522 float* buffer = m_mgr.gsto_data(m_gsto);
0523 if(!buffer) return;
0524 char* pos_xyzs = (char*)buffer;pos_xyzs += a_pos_xyzs;
0525 char* pos_rgbas = (char*)buffer;pos_rgbas += a_pos_rgbas;
0526 char* pos_nms = (char*)buffer;pos_nms += a_pos_nms;
0527 if(m_gl_id && m_created) {
0528 parent_gl_functions::gl__Begin(a_mode);
0529 float* pos = (float*)pos_xyzs;
0530 float* pco = (float*)pos_rgbas;
0531 float* pnm = (float*)pos_nms;
0532 for(size_t index=0;index<a_elems;
0533 index++,pos+=3,pco+=4,pnm+=3) {
0534 parent_gl_functions::gl__Vertex3f(*(pos+0),*(pos+1),*(pos+2));
0535 parent_gl_functions::gl__Color4f (*(pco+0),*(pco+1),*(pco+2),*(pco+3));
0536 parent_gl_functions::gl__Normal3f(*(pnm+0),*(pnm+1),*(pnm+2));
0537 }
0538 parent_gl_functions::gl__End();
0539 }
0540 #endif
0541 }break;
0542
0543 case tools::sg::gsto_memory:{
0544 float* buffer = m_mgr.gsto_data(m_gsto);
0545 if(!buffer) return;
0546 char* pos_xyzs = (char*)buffer;pos_xyzs += a_pos_xyzs;
0547 char* pos_rgbas = (char*)buffer;pos_rgbas += a_pos_rgbas;
0548 char* pos_nms = (char*)buffer;pos_nms += a_pos_nms;
0549 _draw_vcn(a_mode,a_elems,pos_xyzs,pos_rgbas,pos_nms);
0550 }break;
0551 }
0552 }
0553
0554 /////////////////////////////////////////////////////////////////
0555 /////////////////////////////////////////////////////////////////
0556 /////////////////////////////////////////////////////////////////
0557
0558 virtual void clear_color(float a_r,float a_g,float a_b,float a_a){
0559 parent_gl_functions::gl__ClearColor(a_r,a_g,a_b,a_a);
0560 parent_gl_functions::gl__Clear(GL_COLOR_BUFFER_BIT);
0561 }
0562 virtual void color4f(float a_r,float a_g,float a_b,float a_a){
0563 #ifdef TOOLS_USE_GL_VERSION_3_2
0564 parent_gl_functions::gl__Uniform4f(m_mgr.g_color_location(),a_r,a_g,a_b,a_a);
0565 #else
0566 parent_gl_functions::gl__Color4f(a_r,a_g,a_b,a_a);
0567 #endif
0568 }
0569 virtual void line_width(float a_v){
0570 #ifdef TOOLS_USE_GL_VERSION_3_2
0571 (void)a_v;
0572 #else
0573 parent_gl_functions::gl__LineWidth(a_v);
0574 #endif
0575 }
0576 virtual void point_size(float a_v){
0577 #ifdef TOOLS_USE_GL_VERSION_3_2
0578 parent_gl_functions::gl__Uniform1f(m_mgr.g_point_size_location(),a_v);
0579 #else
0580 parent_gl_functions::gl__PointSize(a_v);
0581 #endif
0582 }
0583 virtual void set_polygon_offset(bool a_v) {
0584 if(a_v) parent_gl_functions::gl__Enable(GL_POLYGON_OFFSET_FILL);
0585 else parent_gl_functions::gl__Disable(GL_POLYGON_OFFSET_FILL);
0586 parent_gl_functions::gl__PolygonOffset(1.,1.);
0587 }
0588 virtual void normal(float a_x,float a_y,float a_z) {
0589 #ifdef TOOLS_USE_GL_VERSION_3_2
0590 parent_gl_functions::gl__Uniform3f(m_mgr.g_normal_location(),a_x,a_y,a_z);
0591 #else
0592 parent_gl_functions::gl__Normal3f(a_x,a_y,a_z);
0593 #endif
0594 }
0595
0596 virtual void set_winding(tools::sg::winding_type a_v) {
0597 if(a_v==tools::sg::winding_ccw)
0598 parent_gl_functions::gl__FrontFace(GL_CCW);
0599 else
0600 parent_gl_functions::gl__FrontFace(GL_CW);
0601 }
0602
0603 virtual void set_shade_model(tools::sg::shade_type a_v) {
0604 #ifdef TOOLS_USE_GL_VERSION_3_2
0605 (void)a_v;
0606 #else
0607 if(a_v==tools::sg::shade_smooth)
0608 parent_gl_functions::gl__ShadeModel(GL_SMOOTH);
0609 else
0610 parent_gl_functions::gl__ShadeModel(GL_FLAT);
0611 #endif
0612 }
0613
0614 virtual void set_cull_face(bool a_on) {
0615 if(a_on) parent_gl_functions::gl__Enable(GL_CULL_FACE);
0616 else parent_gl_functions::gl__Disable(GL_CULL_FACE);
0617 }
0618
0619 virtual void set_point_smooth(bool a_on) {
0620 #ifdef TOOLS_USE_GL_VERSION_3_2
0621 (void)a_on;
0622 #else
0623 if(a_on) parent_gl_functions::gl__Enable(GL_POINT_SMOOTH);
0624 else parent_gl_functions::gl__Disable(GL_POINT_SMOOTH);
0625 #endif
0626 }
0627
0628 virtual void set_line_smooth(bool a_on) {
0629 #ifdef TOOLS_USE_GL_VERSION_3_2
0630 (void)a_on;
0631 #else
0632 if(a_on) parent_gl_functions::gl__Enable(GL_LINE_SMOOTH);
0633 else parent_gl_functions::gl__Disable(GL_LINE_SMOOTH);
0634 #endif
0635 }
0636
0637 virtual void set_depth_test(bool a_on) {
0638 if(a_on) parent_gl_functions::gl__Enable(GL_DEPTH_TEST);
0639 else parent_gl_functions::gl__Disable(GL_DEPTH_TEST);
0640 }
0641
0642 virtual void load_proj_matrix(const tools::mat4f& a_mtx) {
0643 #ifdef TOOLS_USE_GL_VERSION_3_2
0644 m_proj = a_mtx;
0645 {tools::mat4f mtx(m_proj);
0646 mtx.mul_mtx(m_model);
0647 parent_gl_functions::gl__UniformMatrix4fv(m_mgr.g_model_proj_matrix_location(),1,GL_FALSE,mtx.data());}
0648 #else
0649 parent_gl_functions::gl__MatrixMode(GL_PROJECTION);
0650 parent_gl_functions::gl__LoadMatrixf(a_mtx.data());
0651 #endif
0652 }
0653
0654 virtual void load_model_matrix(const tools::mat4f& a_mtx) {
0655 #ifdef TOOLS_USE_GL_VERSION_3_2
0656 m_model = a_mtx;
0657 {tools::mat4f mtx(m_proj);
0658 mtx.mul_mtx(m_model);
0659 parent_gl_functions::gl__UniformMatrix4fv(m_mgr.g_model_proj_matrix_location(),1,GL_FALSE,mtx.data());}
0660
0661 tools::mat4f tmp(a_mtx);
0662 tmp.no_translate();
0663 tools::mat4f normal_matrix;
0664 if(!tmp.invert(normal_matrix)) {
0665 m_out << "toolx::sg::GL_action_T::load_model_matrix :"
0666 << " can't invert model matrix."
0667 << std::endl;
0668 }
0669 normal_matrix.transpose();
0670
0671 parent_gl_functions::gl__UniformMatrix4fv(m_mgr.g_normal_matrix_location(),1,GL_FALSE,normal_matrix.data());
0672 #else
0673 parent_gl_functions::gl__MatrixMode(GL_MODELVIEW);
0674 parent_gl_functions::gl__LoadMatrixf(a_mtx.data());
0675 #endif
0676 }
0677
0678 virtual unsigned int max_lights() {
0679 #ifdef TOOLS_USE_GL_VERSION_3_2
0680 return 1000;
0681 #else
0682 return GL_MAX_LIGHTS;
0683 #endif
0684 }
0685
0686 virtual void enable_light(unsigned int a_light,
0687 float a_dx,float a_dy,float a_dz,
0688 float a_r,float a_g,float a_b,float a_a,
0689 float a_ar,float a_ag,float a_ab,float a_aa){
0690 #ifdef TOOLS_USE_GL_VERSION_3_2
0691 float dl = ::sqrtf(a_dx*a_dx+a_dy*a_dy+a_dz*a_dz);
0692 if(!dl) {
0693 m_out << "toolx::sg::GL_action_T::enable_light :"
0694 << " null light direction."
0695 << std::endl;
0696 return;
0697 }
0698 parent_gl_functions::gl__Uniform1i(m_mgr.g_light_on_location(),1);
0699 parent_gl_functions::gl__Uniform3f(m_mgr.g_light_direction_location(),a_dx/dl,a_dy/dl,a_dz/dl);
0700 parent_gl_functions::gl__Uniform4f(m_mgr.g_light_color_location(),a_r,a_g,a_b,a_a);
0701 parent_gl_functions::gl__Uniform4f(m_mgr.g_light_ambient_location(),a_ar,a_ag,a_ab,a_aa);
0702
0703 (void)a_light;
0704 #else //not TOOLS_USE_GL_VERSION_3_2
0705 parent_gl_functions::gl__Enable(GL_LIGHTING);
0706 GLenum light = GL_LIGHT0+a_light;
0707
0708 float params[4];
0709 params[0] = -a_dx;
0710 params[1] = -a_dy;
0711 params[2] = -a_dz;
0712 params[3] = 0; //0 tells that it is a directional light.
0713 parent_gl_functions::gl__Lightfv(light,GL_POSITION,params);
0714
0715
0716 params[0] = a_r;
0717 params[1] = a_g;
0718 params[2] = a_b;
0719 params[3] = a_a;
0720 parent_gl_functions::gl__Lightfv(light,GL_DIFFUSE,params);
0721 parent_gl_functions::gl__Lightfv(light,GL_SPECULAR,params); //coin/SoDirectionalLight does that.
0722
0723 params[0] = a_ar;
0724 params[1] = a_ag;
0725 params[2] = a_ab;
0726 params[3] = a_aa;
0727 parent_gl_functions::gl__Lightfv(light,GL_AMBIENT,params); //coin/SoDirectionalLight does that.
0728
0729 // coin/SoDirectionalLight does the below :
0730 parent_gl_functions::gl__Lightf(light, GL_SPOT_EXPONENT, 0.0);
0731 parent_gl_functions::gl__Lightf(light, GL_SPOT_CUTOFF, 180.0);
0732 parent_gl_functions::gl__Lightf(light, GL_CONSTANT_ATTENUATION, 1);
0733 parent_gl_functions::gl__Lightf(light, GL_LINEAR_ATTENUATION, 0);
0734 parent_gl_functions::gl__Lightf(light, GL_QUADRATIC_ATTENUATION, 0);
0735
0736 parent_gl_functions::gl__Enable(light);
0737 #endif //not TOOLS_USE_GL_VERSION_3_2
0738 }
0739
0740 virtual void set_lighting(bool a_on) {
0741 #ifdef TOOLS_USE_GL_VERSION_3_2
0742 parent_gl_functions::gl__Uniform1i(m_mgr.g_light_on_location(),a_on);
0743 #else
0744 if(a_on) parent_gl_functions::gl__Enable(GL_LIGHTING);
0745 else parent_gl_functions::gl__Disable(GL_LIGHTING);
0746 #endif
0747 }
0748 virtual void set_blend(bool a_on) {
0749 if(a_on) parent_gl_functions::gl__Enable(GL_BLEND);
0750 else parent_gl_functions::gl__Disable(GL_BLEND);
0751 }
0752
0753 virtual void restore_state(unsigned int a_ret_num_light) {
0754 const tools::sg::state& _state = state();
0755 #ifdef TOOLS_USE_GL_VERSION_3_2
0756 GL_action_T::load_model_matrix(_state.m_proj);
0757 GL_action_T::load_model_matrix(_state.m_model);
0758
0759 set_lighting(_state.m_GL_LIGHTING);
0760 set_blend(_state.m_GL_BLEND);
0761
0762 set_depth_test(_state.m_GL_DEPTH_TEST);
0763 set_cull_face(_state.m_GL_CULL_FACE);
0764 set_polygon_offset(_state.m_GL_POLYGON_OFFSET_FILL);
0765
0766 set_winding(_state.m_winding);
0767
0768 color4f(_state.m_color.r(),_state.m_color.g(),_state.m_color.b(),_state.m_color.a());
0769
0770 normal(_state.m_normal.x(),_state.m_normal.y(),_state.m_normal.z());
0771
0772 line_width(_state.m_line_width);
0773 point_size(_state.m_point_size);
0774
0775 (void)a_ret_num_light;
0776 #else //not TOOLS_USE_GL_VERSION_3_2
0777 parent_gl_functions::gl__MatrixMode(GL_PROJECTION);
0778 parent_gl_functions::gl__LoadMatrixf(_state.m_proj.data());
0779
0780 parent_gl_functions::gl__MatrixMode(GL_MODELVIEW);
0781 parent_gl_functions::gl__LoadMatrixf(_state.m_model.data());
0782
0783 if(_state.m_GL_LIGHTING) parent_gl_functions::gl__Enable(GL_LIGHTING);
0784 else parent_gl_functions::gl__Disable(GL_LIGHTING);
0785
0786 if(_state.m_GL_POINT_SMOOTH) parent_gl_functions::gl__Enable(GL_POINT_SMOOTH);
0787 else parent_gl_functions::gl__Disable(GL_POINT_SMOOTH);
0788
0789 if(_state.m_shade_model==tools::sg::shade_smooth)
0790 parent_gl_functions::gl__ShadeModel(GL_SMOOTH);
0791 else
0792 parent_gl_functions::gl__ShadeModel(GL_FLAT);
0793
0794 parent_gl_functions::gl__Color4f(_state.m_color.r(),
0795 _state.m_color.g(),
0796 _state.m_color.b(),
0797 _state.m_color.a());
0798
0799 parent_gl_functions::gl__Normal3f(_state.m_normal.x(),
0800 _state.m_normal.y(),
0801 _state.m_normal.z());
0802
0803 // The "return of separator" state had ret_num_light.
0804 // The restored state has m_light.
0805 // We have to glDisable lights with index in [m_light,ret_num_light-1]
0806 for(unsigned int index=_state.m_light;index<a_ret_num_light;index++) {
0807 parent_gl_functions::gl__Disable(GL_LIGHT0+index);
0808 }
0809
0810 if(_state.m_GL_DEPTH_TEST) parent_gl_functions::gl__Enable(GL_DEPTH_TEST);
0811 else parent_gl_functions::gl__Disable(GL_DEPTH_TEST);
0812
0813 if(_state.m_GL_CULL_FACE) parent_gl_functions::gl__Enable(GL_CULL_FACE);
0814 else parent_gl_functions::gl__Disable(GL_CULL_FACE);
0815
0816 if(_state.m_GL_LINE_SMOOTH) parent_gl_functions::gl__Enable(GL_LINE_SMOOTH);
0817 else parent_gl_functions::gl__Disable(GL_LINE_SMOOTH);
0818
0819 if(_state.m_GL_POLYGON_OFFSET_FILL) parent_gl_functions::gl__Enable(GL_POLYGON_OFFSET_FILL);
0820 else parent_gl_functions::gl__Disable(GL_POLYGON_OFFSET_FILL);
0821
0822 if(_state.m_GL_TEXTURE_2D) parent_gl_functions::gl__Enable(GL_TEXTURE_2D);
0823 else parent_gl_functions::gl__Disable(GL_TEXTURE_2D);
0824
0825 if(_state.m_GL_BLEND) parent_gl_functions::gl__Enable(GL_BLEND);
0826 else parent_gl_functions::gl__Disable(GL_BLEND);
0827
0828 if(_state.m_winding==tools::sg::winding_ccw) {
0829 parent_gl_functions::gl__FrontFace(GL_CCW);
0830 } else {
0831 parent_gl_functions::gl__FrontFace(GL_CW);
0832 }
0833
0834 parent_gl_functions::gl__LineWidth(_state.m_line_width);
0835
0836 parent_gl_functions::gl__PointSize(_state.m_point_size);
0837 parent_gl_functions::gl__Disable(GL_POLYGON_STIPPLE);
0838
0839 #endif //not TOOLS_USE_GL_VERSION_3_2
0840 }
0841
0842 virtual tools::sg::render_manager& render_manager() {return m_mgr;}
0843 public:
0844 GL_action_T(GL_manager_T<GL_FUNCTIONS>& a_mgr,std::ostream& a_out,unsigned int a_ww,unsigned int a_wh)
0845 :parent(a_out,a_ww,a_wh)
0846 ,m_mgr(a_mgr)
0847 ,m_gsto(0)
0848 #ifndef TOOLS_USE_GL_VERSION_3_2
0849 ,m_created(false)
0850 ,m_gl_id(0)
0851 #endif
0852 {
0853 if (!parent_gl_functions::initialize()) {
0854 m_out << "toolx::sg::GL_action_T::GL_action_T: warning: parent_gl_functions::initialize() failed." << std::endl;
0855 }
0856 #ifdef TOOLS_USE_GL_VERSION_3_2
0857 m_model.set_identity();
0858 m_proj.set_identity();
0859 #endif
0860 }
0861 virtual ~GL_action_T(){}
0862 public:
0863 GL_action_T(const GL_action_T& a_from)
0864 :parent(a_from)
0865 ,parent_gl_functions(a_from)
0866 ,m_mgr(a_from.m_mgr)
0867 ,m_gsto(0)
0868 #ifndef TOOLS_USE_GL_VERSION_3_2
0869 ,m_created(false)
0870 ,m_gl_id(0)
0871 #endif
0872 {
0873 if (!parent_gl_functions::initialize()) {
0874 m_out << "toolx::sg::GL_action_T::GL_action_T: warning: parent_gl_functions::initialize() failed." << std::endl;
0875 }
0876 }
0877 GL_action_T& operator=(const GL_action_T& a_from){
0878 render_action::operator=(a_from);
0879 m_gsto = 0;
0880 #ifndef TOOLS_USE_GL_VERSION_3_2
0881 m_created = false;
0882 m_gl_id = 0;
0883 #endif
0884 return *this;
0885 }
0886 protected:
0887 #ifdef TOOLS_USE_GL_VERSION_3_2
0888 void _draw_shader_v(tools::gl::mode_t a_mode,size_t a_elems,const void* a_pos_xyzs){
0889 if(!a_elems) return;
0890
0891 parent_gl_functions::gl__VertexAttribPointer(m_mgr.g_one_pos_location(),3,GL_FLOAT,GL_FALSE,0,a_pos_xyzs);
0892
0893 parent_gl_functions::gl__EnableVertexAttribArray(m_mgr.g_one_pos_location());
0894 parent_gl_functions::gl__DrawArrays(a_mode,0,(GLsizei)a_elems);
0895 parent_gl_functions::gl__DisableVertexAttribArray(m_mgr.g_one_pos_location());
0896 }
0897 #endif
0898
0899 void _draw_v(tools::gl::mode_t a_mode,size_t a_elems,const void* a_pos_xyzs){
0900 if(!a_elems) return;
0901 #ifdef TOOLS_USE_GL_VERSION_3_2
0902
0903 GLuint vao_id = 0;
0904 parent_gl_functions::gl__GenVertexArrays(1,&vao_id);
0905 if(!vao_id) {
0906 m_out << "toolx::sg::GL_action_T::_draw_v : glGenVertexArrays failed ()." << std::endl;
0907 return;
0908 }
0909
0910 parent_gl_functions::gl__BindVertexArray(vao_id);
0911 ////////////////////////////////////////////////////////////////////
0912 ////////////////////////////////////////////////////////////////////
0913 unsigned int vbo_id = 0;
0914 parent_gl_functions::gl__GenBuffers(1,&vbo_id);
0915 if(!vbo_id) {
0916 m_out << "toolx::sg::GL_action_T::_draw_v : glGenBuffers failed ()." << std::endl;
0917 parent_gl_functions::gl__DeleteVertexArrays(1,&vao_id);
0918 return;
0919 }
0920 std::vector<float> gsto_data;
0921 size_t ngsto = a_elems*3; //a_elems points * 3 coords
0922 gsto_data.resize(ngsto);
0923 float* _gsto_data = gsto_data.data();
0924 ::memcpy(_gsto_data,a_pos_xyzs,ngsto*sizeof(float));
0925
0926 parent_gl_functions::gl__BindBuffer(GL_ARRAY_BUFFER,vbo_id);
0927 parent_gl_functions::gl__BufferData(GL_ARRAY_BUFFER,gsto_data.size()*sizeof(float),_gsto_data,GL_STATIC_DRAW);
0928 parent_gl_functions::gl__BindBuffer(GL_ARRAY_BUFFER,0);
0929 ////////////////////////////////////////////////////////////////////
0930 ////////////////////////////////////////////////////////////////////
0931 parent_gl_functions::gl__BindVertexArray(0);
0932
0933 ////////////////////////////////////////////////////////////////////
0934 ////////////////////////////////////////////////////////////////////
0935 parent_gl_functions::gl__BindVertexArray(vao_id);
0936 parent_gl_functions::gl__BindBuffer(GL_ARRAY_BUFFER,vbo_id);
0937 parent_gl_functions::gl__VertexAttribPointer(m_mgr.g_one_pos_location(),3,GL_FLOAT,GL_FALSE,0,NULL);
0938 parent_gl_functions::gl__BindBuffer(GL_ARRAY_BUFFER,0);
0939 parent_gl_functions::gl__BindVertexArray(0);
0940
0941 parent_gl_functions::gl__BindVertexArray(vao_id);
0942 parent_gl_functions::gl__EnableVertexAttribArray(m_mgr.g_one_pos_location());
0943 parent_gl_functions::gl__DrawArrays(a_mode,0,(GLsizei)a_elems);
0944 parent_gl_functions::gl__DisableVertexAttribArray(m_mgr.g_one_pos_location());
0945 parent_gl_functions::gl__BindVertexArray(0);
0946
0947 ////////////////////////////////////////////////////////////////////
0948 ////////////////////////////////////////////////////////////////////
0949 parent_gl_functions::gl__DeleteBuffers(1,&vbo_id);
0950 parent_gl_functions::gl__DeleteVertexArrays(1,&vao_id);
0951
0952 #else //not TOOLS_USE_GL_VERSION_3_2
0953 parent_gl_functions::gl__EnableClientState(GL_VERTEX_ARRAY);
0954 parent_gl_functions::gl__VertexPointer(3,GL_FLOAT,0,a_pos_xyzs);
0955 parent_gl_functions::gl__DrawArrays(a_mode,0,(GLsizei)a_elems);
0956 parent_gl_functions::gl__DisableClientState(GL_VERTEX_ARRAY);
0957 #endif //not TOOLS_USE_GL_VERSION_3_2
0958 }
0959
0960 #ifdef TOOLS_USE_GL_VERSION_3_2
0961 void _draw_shader_vc(tools::gl::mode_t a_mode,size_t a_elems,const void* a_pos_xyzs,const void* a_pos_rgbas){
0962 if(!a_elems) return;
0963
0964 parent_gl_functions::gl__VertexAttribPointer(m_mgr.g_one_pos_location(),3,GL_FLOAT,GL_FALSE,0,a_pos_xyzs);
0965 parent_gl_functions::gl__VertexAttribPointer(m_mgr.g_one_color_location(),4,GL_FLOAT,GL_FALSE,0,a_pos_rgbas);
0966 parent_gl_functions::gl__DrawArrays(a_mode,0,(GLsizei)a_elems);
0967
0968 parent_gl_functions::gl__Uniform1i(m_mgr.g_pos_color_location(),true);
0969 parent_gl_functions::gl__EnableVertexAttribArray(m_mgr.g_one_pos_location());
0970 parent_gl_functions::gl__EnableVertexAttribArray(m_mgr.g_one_color_location());
0971 parent_gl_functions::gl__DrawArrays(a_mode,0,(GLsizei)a_elems);
0972 parent_gl_functions::gl__DisableVertexAttribArray(m_mgr.g_one_pos_location());
0973 parent_gl_functions::gl__DisableVertexAttribArray(m_mgr.g_one_color_location());
0974 parent_gl_functions::gl__Uniform1i(m_mgr.g_pos_color_location(),false);
0975 }
0976 #endif
0977
0978 void _draw_vc(tools::gl::mode_t a_mode,size_t a_elems,const void* a_pos_xyzs,const void* a_pos_rgbas){
0979 if(!a_elems) return;
0980
0981 #ifdef TOOLS_USE_GL_VERSION_3_2
0982
0983 GLuint vao_id = 0;
0984 parent_gl_functions::gl__GenVertexArrays(1,&vao_id);
0985 if(!vao_id) {
0986 m_out << "toolx::sg::GL_action_T::_draw_vc : glGenVertexArrays failed ()." << std::endl;
0987 return;
0988 }
0989
0990 parent_gl_functions::gl__BindVertexArray(vao_id);
0991 ////////////////////////////////////////////////////////////////////
0992 ////////////////////////////////////////////////////////////////////
0993 unsigned int vbo_id = 0;
0994 parent_gl_functions::gl__GenBuffers(1,&vbo_id);
0995 if(!vbo_id) {
0996 m_out << "toolx::sg::GL_action_T::_draw_vc : glGenBuffers failed ()." << std::endl;
0997 parent_gl_functions::gl__DeleteVertexArrays(1,&vao_id);
0998 return;
0999 }
1000 std::vector<float> gsto_data;
1001 size_t nxyzs = a_elems*3; //a_elems points * 3 coords
1002 size_t nrgbas = a_elems*4;
1003 size_t ngsto = nxyzs+nrgbas;
1004 gsto_data.resize(ngsto);
1005 float* _gsto_data = gsto_data.data();
1006 ::memcpy(_gsto_data,a_pos_xyzs,nxyzs*sizeof(float));
1007 ::memcpy(_gsto_data+nxyzs,a_pos_rgbas,nrgbas*sizeof(float));
1008
1009 parent_gl_functions::gl__BindBuffer(GL_ARRAY_BUFFER,vbo_id);
1010 parent_gl_functions::gl__BufferData(GL_ARRAY_BUFFER,gsto_data.size()*sizeof(float),_gsto_data,GL_STATIC_DRAW);
1011 parent_gl_functions::gl__BindBuffer(GL_ARRAY_BUFFER,0);
1012 ////////////////////////////////////////////////////////////////////
1013 ////////////////////////////////////////////////////////////////////
1014 parent_gl_functions::gl__BindVertexArray(0);
1015
1016 ////////////////////////////////////////////////////////////////////
1017 ////////////////////////////////////////////////////////////////////
1018 parent_gl_functions::gl__BindVertexArray(vao_id);
1019 parent_gl_functions::gl__BindBuffer(GL_ARRAY_BUFFER,vbo_id);
1020 char* pos_xyzs = NULL;
1021 char* pos_rgbas = NULL;pos_rgbas += nxyzs*sizeof(float);
1022 parent_gl_functions::gl__VertexAttribPointer(m_mgr.g_one_pos_location(),3,GL_FLOAT,GL_FALSE,0,pos_xyzs);
1023 parent_gl_functions::gl__VertexAttribPointer(m_mgr.g_one_color_location(),4,GL_FLOAT,GL_FALSE,0,pos_rgbas);
1024 parent_gl_functions::gl__DrawArrays(a_mode,0,(GLsizei)a_elems);
1025 parent_gl_functions::gl__BindBuffer(GL_ARRAY_BUFFER,0);
1026 parent_gl_functions::gl__BindVertexArray(0);
1027
1028 parent_gl_functions::gl__BindVertexArray(vao_id);
1029 parent_gl_functions::gl__Uniform1i(m_mgr.g_pos_color_location(),true);
1030 parent_gl_functions::gl__EnableVertexAttribArray(m_mgr.g_one_pos_location());
1031 parent_gl_functions::gl__EnableVertexAttribArray(m_mgr.g_one_color_location());
1032 parent_gl_functions::gl__DrawArrays(a_mode,0,(GLsizei)a_elems);
1033 parent_gl_functions::gl__DisableVertexAttribArray(m_mgr.g_one_pos_location());
1034 parent_gl_functions::gl__DisableVertexAttribArray(m_mgr.g_one_color_location());
1035 parent_gl_functions::gl__Uniform1i(m_mgr.g_pos_color_location(),false);
1036 parent_gl_functions::gl__BindVertexArray(0);
1037
1038 ////////////////////////////////////////////////////////////////////
1039 ////////////////////////////////////////////////////////////////////
1040 parent_gl_functions::gl__DeleteBuffers(1,&vbo_id);
1041 parent_gl_functions::gl__DeleteVertexArrays(1,&vao_id);
1042
1043 #else //not TOOLS_USE_GL_VERSION_3_2
1044 parent_gl_functions::gl__EnableClientState(GL_VERTEX_ARRAY);
1045 parent_gl_functions::gl__EnableClientState(GL_COLOR_ARRAY);
1046
1047 parent_gl_functions::gl__VertexPointer(3,GL_FLOAT,0,a_pos_xyzs);
1048 parent_gl_functions::gl__ColorPointer(4,GL_FLOAT,0,a_pos_rgbas);
1049
1050 parent_gl_functions::gl__DrawArrays(a_mode,0,(GLsizei)a_elems);
1051
1052 parent_gl_functions::gl__DisableClientState(GL_COLOR_ARRAY);
1053 parent_gl_functions::gl__DisableClientState(GL_VERTEX_ARRAY);
1054 #endif //not TOOLS_USE_GL_VERSION_3_2
1055 }
1056
1057 #ifdef TOOLS_USE_GL_VERSION_3_2
1058 void _draw_shader_vn(tools::gl::mode_t a_mode,size_t a_elems,const void* a_pos_xyzs,const void* a_pos_nms){
1059 if(!a_elems) return;
1060
1061 parent_gl_functions::gl__VertexAttribPointer(m_mgr.g_one_pos_location(),3,GL_FLOAT,GL_FALSE,0,a_pos_xyzs);
1062 parent_gl_functions::gl__VertexAttribPointer(m_mgr.g_one_normal_location(),3,GL_FLOAT,GL_FALSE,0,a_pos_nms);
1063
1064 parent_gl_functions::gl__Uniform1i(m_mgr.g_pos_normal_location(),true);
1065 parent_gl_functions::gl__EnableVertexAttribArray(m_mgr.g_one_pos_location());
1066 parent_gl_functions::gl__EnableVertexAttribArray(m_mgr.g_one_normal_location());
1067 parent_gl_functions::gl__DrawArrays(a_mode,0,(GLsizei)a_elems);
1068 parent_gl_functions::gl__DisableVertexAttribArray(m_mgr.g_one_pos_location());
1069 parent_gl_functions::gl__DisableVertexAttribArray(m_mgr.g_one_normal_location());
1070 parent_gl_functions::gl__Uniform1i(m_mgr.g_pos_normal_location(),false);
1071 }
1072 #endif
1073
1074 void _draw_vn(tools::gl::mode_t a_mode,size_t a_elems,const void* a_pos_xyzs,const void* a_pos_nms){
1075 if(!a_elems) return;
1076
1077 #ifdef TOOLS_USE_GL_VERSION_3_2
1078
1079 GLuint vao_id = 0;
1080 parent_gl_functions::gl__GenVertexArrays(1,&vao_id);
1081 if(!vao_id) {
1082 m_out << "toolx::sg::GL_action_T::_draw_vn : glGenVertexArrays failed ()." << std::endl;
1083 return;
1084 }
1085
1086 parent_gl_functions::gl__BindVertexArray(vao_id);
1087 ////////////////////////////////////////////////////////////////////
1088 ////////////////////////////////////////////////////////////////////
1089 unsigned int vbo_id = 0;
1090 parent_gl_functions::gl__GenBuffers(1,&vbo_id);
1091 if(!vbo_id) {
1092 m_out << "toolx::sg::GL_action_T::_draw_vn : glGenBuffers failed ()." << std::endl;
1093 parent_gl_functions::gl__DeleteVertexArrays(1,&vao_id);
1094 return;
1095 }
1096 size_t nxyzs = a_elems*3; //a_elems points * 3 coords
1097 {std::vector<float> gsto_data;
1098 size_t nnms = nxyzs;
1099 size_t ngsto = nxyzs+nnms;
1100 gsto_data.resize(ngsto);
1101 float* _gsto_data = gsto_data.data();
1102 ::memcpy(_gsto_data,a_pos_xyzs,nxyzs*sizeof(float));
1103 ::memcpy(_gsto_data+nxyzs,a_pos_nms,nnms*sizeof(float));
1104
1105 parent_gl_functions::gl__BindBuffer(GL_ARRAY_BUFFER,vbo_id);
1106 parent_gl_functions::gl__BufferData(GL_ARRAY_BUFFER,gsto_data.size()*sizeof(float),_gsto_data,GL_STATIC_DRAW);
1107 parent_gl_functions::gl__BindBuffer(GL_ARRAY_BUFFER,0);
1108 ////////////////////////////////////////////////////////////////////
1109 ////////////////////////////////////////////////////////////////////
1110 parent_gl_functions::gl__BindVertexArray(0);}
1111
1112 ////////////////////////////////////////////////////////////////////
1113 ////////////////////////////////////////////////////////////////////
1114 {char* pos_xyzs = NULL;
1115 char* pos_nms = NULL;pos_nms += nxyzs*sizeof(float);
1116 parent_gl_functions::gl__BindVertexArray(vao_id);
1117 parent_gl_functions::gl__BindBuffer(GL_ARRAY_BUFFER,vbo_id);
1118 parent_gl_functions::gl__VertexAttribPointer(m_mgr.g_one_pos_location(),3,GL_FLOAT,GL_FALSE,0,pos_xyzs);
1119 parent_gl_functions::gl__VertexAttribPointer(m_mgr.g_one_normal_location(),3,GL_FLOAT,GL_FALSE,0,pos_nms);
1120 parent_gl_functions::gl__BindBuffer(GL_ARRAY_BUFFER,0);
1121 parent_gl_functions::gl__BindVertexArray(0);}
1122
1123 parent_gl_functions::gl__BindVertexArray(vao_id);
1124 parent_gl_functions::gl__Uniform1i(m_mgr.g_pos_normal_location(),true);
1125 parent_gl_functions::gl__EnableVertexAttribArray(m_mgr.g_one_pos_location());
1126 parent_gl_functions::gl__EnableVertexAttribArray(m_mgr.g_one_normal_location());
1127 parent_gl_functions::gl__DrawArrays(a_mode,0,(GLsizei)a_elems);
1128 parent_gl_functions::gl__DisableVertexAttribArray(m_mgr.g_one_pos_location());
1129 parent_gl_functions::gl__DisableVertexAttribArray(m_mgr.g_one_normal_location());
1130 parent_gl_functions::gl__Uniform1i(m_mgr.g_pos_normal_location(),false);
1131 parent_gl_functions::gl__BindVertexArray(0);
1132
1133 parent_gl_functions::gl__DeleteBuffers(1,&vbo_id);
1134 parent_gl_functions::gl__DeleteVertexArrays(1,&vao_id);
1135
1136 #else //not TOOLS_USE_GL_VERSION_3_2
1137 parent_gl_functions::gl__EnableClientState(GL_VERTEX_ARRAY);
1138 parent_gl_functions::gl__EnableClientState(GL_NORMAL_ARRAY);
1139
1140 parent_gl_functions::gl__VertexPointer(3,GL_FLOAT,0,a_pos_xyzs);
1141 parent_gl_functions::gl__NormalPointer(GL_FLOAT,0,a_pos_nms);
1142
1143 parent_gl_functions::gl__DrawArrays(a_mode,0,(GLsizei)a_elems);
1144
1145 parent_gl_functions::gl__DisableClientState(GL_NORMAL_ARRAY);
1146 parent_gl_functions::gl__DisableClientState(GL_VERTEX_ARRAY);
1147 #endif //not TOOLS_USE_GL_VERSION_3_2
1148 }
1149
1150 #ifdef TOOLS_USE_GL_VERSION_3_2
1151 void _draw_shader_vcn(tools::gl::mode_t a_mode,size_t a_elems,const void* a_pos_xyzs,const void* a_pos_rgbas,const void* a_pos_nms){
1152 if(!a_elems) return;
1153
1154 parent_gl_functions::gl__VertexAttribPointer(m_mgr.g_one_pos_location(),3,GL_FLOAT,GL_FALSE,0,a_pos_xyzs);
1155 parent_gl_functions::gl__VertexAttribPointer(m_mgr.g_one_normal_location(),3,GL_FLOAT,GL_FALSE,0,a_pos_nms);
1156 parent_gl_functions::gl__VertexAttribPointer(m_mgr.g_one_color_location(),4,GL_FLOAT,GL_FALSE,0,a_pos_rgbas);
1157
1158 parent_gl_functions::gl__Uniform1i(m_mgr.g_pos_color_location(),true);
1159 parent_gl_functions::gl__Uniform1i(m_mgr.g_pos_normal_location(),true);
1160 parent_gl_functions::gl__EnableVertexAttribArray(m_mgr.g_one_pos_location());
1161 parent_gl_functions::gl__EnableVertexAttribArray(m_mgr.g_one_normal_location());
1162 parent_gl_functions::gl__EnableVertexAttribArray(m_mgr.g_one_color_location());
1163 parent_gl_functions::gl__DrawArrays(a_mode,0,(GLsizei)a_elems);
1164 parent_gl_functions::gl__DisableVertexAttribArray(m_mgr.g_one_pos_location());
1165 parent_gl_functions::gl__DisableVertexAttribArray(m_mgr.g_one_normal_location());
1166 parent_gl_functions::gl__DisableVertexAttribArray(m_mgr.g_one_color_location());
1167 parent_gl_functions::gl__Uniform1i(m_mgr.g_pos_normal_location(),false);
1168 parent_gl_functions::gl__Uniform1i(m_mgr.g_pos_color_location(),false);
1169 }
1170 #endif
1171
1172 void _draw_vcn(tools::gl::mode_t a_mode,size_t a_elems,const void* a_pos_xyzs,const void* a_pos_rgbas,const void* a_pos_nms){
1173 if(!a_elems) return;
1174
1175 #ifdef TOOLS_USE_GL_VERSION_3_2
1176
1177 GLuint vao_id = 0;
1178 parent_gl_functions::gl__GenVertexArrays(1,&vao_id);
1179 if(!vao_id) {
1180 m_out << "toolx::sg::GL_action_T::_draw_vcn : glGenVertexArrays failed ()." << std::endl;
1181 return;
1182 }
1183
1184 size_t nxyzs = a_elems*3; //a_elems points * 3 coords
1185
1186 parent_gl_functions::gl__BindVertexArray(vao_id);
1187 ////////////////////////////////////////////////////////////////////
1188 ////////////////////////////////////////////////////////////////////
1189 unsigned int vbo_id = 0;
1190 parent_gl_functions::gl__GenBuffers(1,&vbo_id);
1191 if(!vbo_id) {
1192 m_out << "toolx::sg::GL_action_T::_draw_vcn : glGenBuffers failed ()." << std::endl;
1193 parent_gl_functions::gl__DeleteVertexArrays(1,&vao_id);
1194 return;
1195 }
1196 {std::vector<float> gsto_data;
1197 size_t nnms = nxyzs;
1198 size_t nrgbas = a_elems*4;
1199 size_t ngsto = nxyzs+nnms+nrgbas;
1200 gsto_data.resize(ngsto);
1201 float* _gsto_data = gsto_data.data();
1202 ::memcpy(_gsto_data,a_pos_xyzs,nxyzs*sizeof(float));
1203 ::memcpy(_gsto_data+nxyzs,a_pos_nms,nnms*sizeof(float));
1204 ::memcpy(_gsto_data+nxyzs+nnms,a_pos_rgbas,nrgbas*sizeof(float));
1205
1206 parent_gl_functions::gl__BindBuffer(GL_ARRAY_BUFFER,vbo_id);
1207 parent_gl_functions::gl__BufferData(GL_ARRAY_BUFFER,gsto_data.size()*sizeof(float),_gsto_data,GL_STATIC_DRAW);
1208 parent_gl_functions::gl__BindBuffer(GL_ARRAY_BUFFER,0);}
1209 ////////////////////////////////////////////////////////////////////
1210 ////////////////////////////////////////////////////////////////////
1211 parent_gl_functions::gl__BindVertexArray(0);
1212
1213 ////////////////////////////////////////////////////////////////////
1214 ////////////////////////////////////////////////////////////////////
1215 {parent_gl_functions::gl__BindVertexArray(vao_id);
1216 parent_gl_functions::gl__BindBuffer(GL_ARRAY_BUFFER,vbo_id);
1217 char* pos_xyzs = NULL;
1218 char* pos_nms = NULL;pos_nms += nxyzs*sizeof(float);
1219 char* pos_rgbas = NULL;pos_rgbas += 2*nxyzs*sizeof(float);
1220 parent_gl_functions::gl__VertexAttribPointer(m_mgr.g_one_pos_location(),3,GL_FLOAT,GL_FALSE,0,pos_xyzs);
1221 parent_gl_functions::gl__VertexAttribPointer(m_mgr.g_one_normal_location(),3,GL_FLOAT,GL_FALSE,0,pos_nms);
1222 parent_gl_functions::gl__VertexAttribPointer(m_mgr.g_one_color_location(),4,GL_FLOAT,GL_FALSE,0,pos_rgbas);
1223 parent_gl_functions::gl__BindBuffer(GL_ARRAY_BUFFER,0);
1224 parent_gl_functions::gl__BindVertexArray(0);}
1225
1226 parent_gl_functions::gl__BindVertexArray(vao_id);
1227 parent_gl_functions::gl__Uniform1i(m_mgr.g_pos_color_location(),true);
1228 parent_gl_functions::gl__Uniform1i(m_mgr.g_pos_normal_location(),true);
1229 parent_gl_functions::gl__EnableVertexAttribArray(m_mgr.g_one_pos_location());
1230 parent_gl_functions::gl__EnableVertexAttribArray(m_mgr.g_one_normal_location());
1231 parent_gl_functions::gl__EnableVertexAttribArray(m_mgr.g_one_color_location());
1232 parent_gl_functions::gl__DrawArrays(a_mode,0,(GLsizei)a_elems);
1233 parent_gl_functions::gl__DisableVertexAttribArray(m_mgr.g_one_pos_location());
1234 parent_gl_functions::gl__DisableVertexAttribArray(m_mgr.g_one_normal_location());
1235 parent_gl_functions::gl__DisableVertexAttribArray(m_mgr.g_one_color_location());
1236 parent_gl_functions::gl__Uniform1i(m_mgr.g_pos_normal_location(),false);
1237 parent_gl_functions::gl__Uniform1i(m_mgr.g_pos_color_location(),false);
1238 parent_gl_functions::gl__BindVertexArray(0);
1239
1240 ////////////////////////////////////////////////////////////////////
1241 ////////////////////////////////////////////////////////////////////
1242
1243 parent_gl_functions::gl__DeleteBuffers(1,&vbo_id);
1244 parent_gl_functions::gl__DeleteVertexArrays(1,&vao_id);
1245
1246 #else //not TOOLS_USE_GL_VERSION_3_2
1247 parent_gl_functions::gl__EnableClientState(GL_VERTEX_ARRAY);
1248 parent_gl_functions::gl__EnableClientState(GL_COLOR_ARRAY);
1249 parent_gl_functions::gl__EnableClientState(GL_NORMAL_ARRAY);
1250
1251 parent_gl_functions::gl__VertexPointer(3,GL_FLOAT,0,a_pos_xyzs);
1252 parent_gl_functions::gl__ColorPointer(4,GL_FLOAT,0,a_pos_rgbas);
1253 parent_gl_functions::gl__NormalPointer(GL_FLOAT,0,a_pos_nms);
1254
1255 parent_gl_functions::gl__DrawArrays(a_mode,0,(GLsizei)a_elems);
1256
1257 parent_gl_functions::gl__DisableClientState(GL_COLOR_ARRAY);
1258 parent_gl_functions::gl__DisableClientState(GL_NORMAL_ARRAY);
1259 parent_gl_functions::gl__DisableClientState(GL_VERTEX_ARRAY);
1260 #endif //not TOOLS_USE_GL_VERSION_3_2
1261 }
1262
1263 protected:
1264 GL_manager_T<GL_FUNCTIONS>& m_mgr;
1265 gstoid m_gsto;
1266 #ifndef TOOLS_USE_GL_VERSION_3_2
1267 bool m_created;
1268 gstoid m_gl_id;
1269 #endif
1270 #ifdef TOOLS_USE_GL_VERSION_3_2
1271 tools::mat4f m_model;
1272 tools::mat4f m_proj;
1273 #endif
1274 };
1275
1276 }}
1277
1278 #endif