Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:37

0001 #pragma once
0002 /**
0003 SGLFW_Event_TO_BE_REMOVED.h : manage scene data and OpenGL render pipelines
0004 =============================================================================
0005 
0006 REPLACED BY SGLFW_Evt.h
0007 
0008 Yuxiang started from SGLFW_Scene and added rendering of record arrays
0009 together with the OpenGL mesh based render.
0010 The changes between SGLFW_Scene.h and SGLFW_Event.h
0011 are not big enough to merit the duplication in the longterm.
0012 
0013 Hence have extracted the non-duplicated parts into SGLGW_Evt.h
0014 making it likely that can remove this.
0015 
0016 
0017 ::
0018 
0019     (ok) A[blyth@localhost opticks]$ opticks-fl SGLFW_Event
0020     ./sysrap/SGLFW_Event_TO_BE_REMOVED.h
0021     ./sysrap/SGLFW_Evt.h
0022 
0023 
0024 
0025 Primary members
0026 -----------------
0027 
0028 (SScene)sc
0029     source of SMesh data
0030 (SGLM)gm
0031      view/projection maths using glm
0032 (SGLFW)gl
0033      OpenGL render top level
0034 
0035 
0036 SGLFW_Program render pipelines
0037 --------------------------------
0038 
0039 wire
0040     wireframe
0041 iwire
0042     instanced wireframe
0043 norm
0044     normal shader
0045 inorm
0046     instanced normal shader
0047 
0048 
0049 **/
0050 
0051 #include "spath.h"
0052 #include "SScene.h"
0053 #include "SGLFW.h"
0054 
0055 #include "SRecord.h"
0056 #include "SGLFW_Record.h"
0057 
0058 struct SGLFW_Event
0059 {
0060     SGLFW&        gl ;
0061     SGLM&         gm ;
0062 
0063     SGLFW_Record*   ar ;
0064     SGLFW_Record*   br ;
0065 
0066     const char* shader_fold ;
0067     SGLFW_Program* wire ;
0068     SGLFW_Program* iwire ;
0069     SGLFW_Program* norm ;
0070     SGLFW_Program* inorm ;
0071 
0072     SGLFW_Program*  rec_prog;
0073 
0074     std::vector<SGLFW_Mesh*> mesh ;
0075 
0076     SGLFW_Event(SGLFW& _gl );
0077     void init();
0078     void initProg();
0079     std::string  desc() const;
0080 
0081     void initMesh();
0082     SGLFW_Program* getIProg() const ;
0083     SGLFW_Program* getProg() const ;
0084 
0085     void render();
0086     void render_mesh();
0087     void renderloop();
0088 };
0089 
0090 
0091 
0092 inline SGLFW_Program* SGLFW_Event::getIProg() const
0093 {
0094     return gm.toggle.norm ? inorm : iwire ;
0095 }
0096 inline SGLFW_Program* SGLFW_Event::getProg() const
0097 {
0098     return gm.toggle.norm ? norm : wire ;
0099 }
0100 
0101 
0102 
0103 
0104 inline SGLFW_Event::SGLFW_Event(SGLFW& _gl )
0105     :
0106     gl(_gl),
0107     gm(gl.gm),
0108     ar(SGLFW_Record::Create(gm.ar, gm.timeparam_ptr)),
0109     br(SGLFW_Record::Create(gm.br, gm.timeparam_ptr)),
0110     shader_fold("${SGLFW_Event__shader_fold:-$OPTICKS_PREFIX/gl}"),
0111     wire(nullptr),
0112     iwire(nullptr),
0113     norm(nullptr),
0114     inorm(nullptr),
0115     rec_prog(nullptr)
0116 {
0117     init();
0118 }
0119 
0120 inline void SGLFW_Event::init()
0121 {
0122     initProg();
0123     initMesh();
0124 }
0125 
0126 
0127 
0128 /**
0129 SGLFW_Event::initProg
0130 ----------------------
0131 
0132 Create the shaders
0133 
0134 **/
0135 
0136 inline void SGLFW_Event::initProg()
0137 {
0138     wire = new SGLFW_Program(spath::Resolve(shader_fold,  "wireframe"), "vPos", "vNrm", nullptr, "MVP", gm.MVP_ptr );
0139     iwire = new SGLFW_Program(spath::Resolve(shader_fold, "iwireframe"), "vPos", "vNrm", "vInstanceTransform", "MVP", gm.MVP_ptr );
0140 
0141     norm = new SGLFW_Program(spath::Resolve(shader_fold, "normal"), "vPos", "vNrm", nullptr, "MVP", gm.MVP_ptr );
0142     inorm = new SGLFW_Program(spath::Resolve(shader_fold, "inormal"), "vPos", "vNrm", "vInstanceTransform", "MVP", gm.MVP_ptr );
0143 
0144     rec_prog = new SGLFW_Program(spath::Resolve(shader_fold, "rec_flying_point_persist"), nullptr, nullptr, nullptr, "ModelViewProjection", gm.MVP_ptr );
0145 }
0146 
0147 
0148 inline std::string  SGLFW_Event::desc() const
0149 {
0150     std::stringstream ss;
0151     ss
0152         << "[SGLFW_Event::desc\n"
0153         << "  shader_fold [" << ( shader_fold ? shader_fold : "-" ) << "]\n"
0154         << "]SGLFW_Event::desc\n"
0155         ;
0156     std::string str = ss.str();
0157     return str ;
0158 }
0159 
0160 
0161 
0162 /**
0163 
0164 SGLFW_Event::initMesh
0165 -----------------------
0166 
0167 Traverses the meshmerge vector from SScene
0168 passing them to SGLFW_Mesh instances
0169 which do the OpenGL uploads
0170 
0171 HMM: how to match ray trace IAS/GAS handle selection ?
0172 
0173 **/
0174 
0175 inline void SGLFW_Event::initMesh()
0176 {
0177     int num_meshmerge = gm.scene->meshmerge.size();
0178 
0179     const std::vector<glm::tmat4x4<float>>& inst_tran = gm.scene->inst_tran ;
0180     const float* values = (const float*)inst_tran.data() ;
0181     int item_values = 4*4 ;
0182 
0183     for(int i=0 ; i < num_meshmerge ; i++)
0184     {
0185         const int4&  _inst_info = gm.scene->inst_info[i] ;
0186 
0187         int num_inst = _inst_info.y ;
0188         int offset   = _inst_info.z ;
0189         bool is_instanced = num_inst > 1 ;
0190 
0191         const SMesh* _mm = gm.scene->meshmerge[i] ;
0192 
0193         SGLFW_Mesh* _mesh = new SGLFW_Mesh(_mm);
0194         if( is_instanced )
0195         {
0196             _mesh->set_inst( num_inst, values + offset*item_values );
0197             //std::cout << _mesh->desc() << std::endl ;
0198         }
0199         mesh.push_back(_mesh);
0200     }
0201 }
0202 
0203 
0204 
0205 /**
0206 SGLFW_Event::render
0207 --------------------
0208 
0209 **/
0210 
0211 inline void SGLFW_Event::render()
0212 {
0213     if(      gm.option.M) render_mesh() ;
0214     if(ar && gm.option.A) ar->render(rec_prog);   //record.npy render
0215     if(br && gm.option.B) br->render(rec_prog);   //record.npy render
0216 }
0217 
0218 
0219 /**
0220 SGLFW_Event::render_mesh
0221 -------------------------
0222 
0223 Possibility: indirect OpenGL to avoid the draw loop
0224 but while the number of meshes is small the
0225 motivation is not strong.
0226 
0227 Note the draw loop does have the advantage of
0228 being able to use different shader pipeline
0229 for different mesh (eg to highlight things).
0230 
0231 **/
0232 
0233 inline void SGLFW_Event::render_mesh()
0234 {
0235     int num_mesh = mesh.size();
0236     for(int i=0 ; i < num_mesh ; i++)
0237     {
0238         bool viz = gm.is_vizmask_set(i);
0239         if(!viz) continue ;
0240 
0241         SGLFW_Mesh* _mesh = mesh[i] ;
0242         SGLFW_Program* _prog = _mesh->has_inst() ? getIProg() : getProg() ;
0243         _mesh->render(_prog);
0244     }
0245 }
0246 
0247 
0248 
0249 /**
0250 SGLFW_Event::renderloop
0251 ------------------------
0252 
0253 For ease of integration with alternative renders (eg raytrace)
0254 it is often preferable to directly implement the renderloop
0255 in the main or elsewhere and not use this simple renderloop.
0256 
0257 **/
0258 
0259 inline void SGLFW_Event::renderloop()
0260 {
0261     while(gl.renderloop_proceed())
0262     {
0263         gl.renderloop_head();  // clears
0264         render();
0265         gl.renderloop_tail();      // swap buffers, poll events
0266     }
0267 }
0268 
0269 
0270 
0271 
0272 
0273