source: tags/arb-6.0/RNA3D/RNA3D_OpenGLEngine.cxx

Last change on this file was 10113, checked in by westram, 11 years ago
  • changed dynamic value-initializations into default-initializations
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.1 KB
Line 
1#include "RNA3D_GlobalHeader.hxx"
2#include "RNA3D_Global.hxx"
3#include "RNA3D_OpenGLEngine.hxx"
4#include "RNA3D_OpenGLGraphics.hxx"
5#include "RNA3D_StructureData.hxx"
6#include "RNA3D_Textures.hxx"
7#include "RNA3D_Renderer.hxx"
8#include "RNA3D_Graphics.hxx"
9#include "RNA3D_Interface.hxx"
10
11#include <iostream>
12
13#include <aw_root.hxx>
14#include <aw_awars.hxx>
15#include <BI_basepos.hxx>
16
17#include <GL/aw_window_ogl.hxx>
18
19// --------------------------------------------------------------------------------
20// global data
21
22RNA3D_Global *RNA3D = 0;
23char globalComment[1000];
24
25void RNA3D_init_global_data(ED4_plugin_host& host) {
26    if (!RNA3D) {
27        RNA3D = new RNA3D_Global(host);
28    }
29}
30
31RNA3D_Global::RNA3D_Global(ED4_plugin_host& host) {
32    OpenGLEngineState = -1;
33    iRotateMolecule   = 0;
34    bPointSpritesSupported           = false;
35    bEColiRefInitialized             = false;
36    bMapSaiDispListCreated           = false;
37    bAutoRotate                      = false;
38    bRotateMolecule                  = false;
39    bDisplayMask                     = false;
40    bDisplayComments                 = false;
41    bMapSearchStringsDispListCreated = false;
42
43    ROTATION_SPEED = 0.5;
44    scale = 0.01;
45
46    cGraphics  = new OpenGLGraphics;
47    cStructure = new Structure3D(host);
48    cTexture   = new Texture2D;
49    cRenderer  = new GLRenderer;
50
51    Viewer = Vector3(0.0, 0.0, -2);
52    Center = Vector3(0.0, 0.0, 0.0);
53    Up     = Vector3(0.0, 1.0, 0.0);
54}
55
56RNA3D_Global::~RNA3D_Global() {
57    delete cGraphics;
58    delete cStructure;
59    delete cTexture;
60    delete cRenderer;
61}
62
63// end of global data section
64// --------------------------------------------------------------------------------
65
66static float fAspectRatio;
67const float fViewAngle = 40.0;
68const float fClipNear  = 0.5f;
69const float fClipFar   = 100;
70
71static Display *dpy;
72static GLXContext glx_context;
73
74static int DoubleBufferWithAlpha[] = { GLX_RGBA,
75                                       GLX_DEPTH_SIZE, 12,
76                                       GLX_RED_SIZE, 4,
77                                       GLX_GREEN_SIZE, 4,
78                                       GLX_BLUE_SIZE, 4,
79                                       GLX_ALPHA_SIZE, 4,
80                                       GLX_DOUBLEBUFFER,
81                                       None };
82
83static int DoubleBuffer[] = { GLX_RGBA,
84                              GLX_DEPTH_SIZE, 12,
85                              GLX_RED_SIZE, 4,
86                              GLX_GREEN_SIZE, 4,
87                              GLX_BLUE_SIZE, 4,
88                              GLX_DOUBLEBUFFER,
89                              None };
90
91static int SingleBuffer[] = { GLX_RGBA,
92                              GLX_DEPTH_SIZE, 12,
93                              GLX_RED_SIZE, 4,
94                              GLX_GREEN_SIZE, 4,
95                              GLX_BLUE_SIZE, 4,
96                              None };
97
98static GLfloat rotation_matrix[16];
99static GLfloat rot_x = 0.0, rot_y = 0.0;
100
101static int iScreenWidth, iScreenHeight;
102
103static bool bMapSpDispListCreated     = false;
104static bool bCursorPosDispListCreated = false;
105static bool bHelixNrDispListCreated   = false;
106
107using namespace std;
108
109static void ShowVendorInformation() {
110    const GLubyte *vendor = NULL;
111    vendor = glGetString(GL_VENDOR);   cout<<"Vendor  : "<<vendor<<endl;
112    vendor = glGetString(GL_RENDERER); cout<<"Renderer: "<<vendor<<endl;
113    vendor = glGetString(GL_VERSION);  cout<<"Version : "<<vendor<<endl;
114}
115
116static void initExtensions() {
117    // check mandatory for extensions
118    char missingExtensions[500]="";
119    if (!GLEW_VERSION_1_2) {
120        strcat(missingExtensions, "\nOpenGL Version 1.2");
121    }
122    if (strlen(missingExtensions) > 0) {
123        GBK_terminatef("ERROR: Some needed extensions are not present:%s\n", missingExtensions);
124    }
125
126#ifdef DEBUG
127    printf("DEBUG: All mandatory extensions seem to be ok.\n");
128#endif // DEBUG
129
130    // the following code checks if point sprites could be used and activates them if possible
131    missingExtensions[0] = 0;
132    if (!GLEW_EXT_point_parameters) strcat(missingExtensions, "\nGL_EXT_point_parameters");
133    if (!GLEW_ARB_point_sprite)     strcat(missingExtensions, "\nGL_ARB_point_sprite");
134    if (strlen(missingExtensions) > 0) {
135        printf("Some extra extensions are not present:%s\n", missingExtensions);
136        printf("Molecule Display: Quality of Rendering is LOW!!\n");
137        RNA3D->bPointSpritesSupported = false;
138    }
139    else {
140#ifdef DEBUG
141        printf("DEBUG: All extra extensions seem to be ok as well.\n");
142#endif // DEBUG
143        RNA3D->bPointSpritesSupported = true;
144    }
145}
146
147void ReshapeOpenGLWindow(GLint width, GLint height) {
148    iScreenWidth  = width;
149    iScreenHeight = height;
150
151    fAspectRatio = (float) width / (float) height;
152
153    glViewport(0, 0, width, height);
154    glMatrixMode(GL_PROJECTION);
155    glLoadIdentity();
156    gluPerspective(fViewAngle, fAspectRatio, fClipNear, fClipFar);
157    glMatrixMode(GL_MODELVIEW);
158    glLoadIdentity();
159}
160
161static void CalculateRotationMatrix() {
162    static int initialized = 0;
163    GLfloat new_rotation_matrix[16];
164
165    // calculate new rotation matrix
166    glPushMatrix();
167    glLoadIdentity();
168    glRotatef(-rot_x, 1.0, 0.0, 0.0);
169    glRotatef(rot_y, 0.0, 1.0, 0.0);
170    glGetFloatv(GL_MODELVIEW_MATRIX, new_rotation_matrix);
171    glPopMatrix();
172
173    // calculate total rotation
174    glPushMatrix();
175    glLoadIdentity();
176    glMultMatrixf(new_rotation_matrix);
177    if (initialized) {
178        glMultMatrixf(rotation_matrix);
179    }
180
181    glGetFloatv(GL_MODELVIEW_MATRIX, rotation_matrix);
182    initialized = 1;
183    glPopMatrix();
184}
185
186void InitializeOpenGLEngine(GLint width, GLint height) {
187    cout<<"RNA3D: Initializing OpenGLEngine : "<<width<<" x "<<height<<endl;
188
189    RNA3D->saved_x = RNA3D->saved_y = 2.0f;
190    ComputeRotationXY(1, 1);
191
192    // Get Information about Vendor & Version
193    ShowVendorInformation();
194
195    GLenum err = glewInit();
196    if (GLEW_OK != err) {
197        // problem: glewInit failed, something is seriously wrong
198        fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
199    }
200    fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
201
202    initExtensions();
203
204    // Prepare the structure Data  and Generate Display Lists
205
206    RNA3D->cStructure->ReadCoOrdinateFile();    // Reading Structure information
207    RNA3D->cStructure->GetSecondaryStructureInfo();  // Getting Secondary Structure Information
208    RNA3D->cStructure->Combine2Dand3DstructureInfo(); // Combining Secondary Structure data with 3D Coordinates
209
210    RNA3D->cStructure->GenerateDisplayLists(); // Generating Display Lists for Rendering
211
212    // Generate Textures
213    RNA3D->cTexture->LoadGLTextures();  // Load The Texture(s)
214
215    glShadeModel(GL_SMOOTH);
216    glClearColor(0, 0, 0, 1);
217    glClearDepth(1.0f);
218
219    glEnable(GL_DEPTH_TEST);         // Enables Depth Testing
220    glDepthFunc(GL_LEQUAL);          // The Type Of Depth Test To Do
221
222    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
223    glEnable(GL_TEXTURE_2D);    // Enable Texture Mapping
224
225    ReshapeOpenGLWindow(width, height);
226
227    CalculateRotationMatrix();
228}
229
230void ComputeRotationXY(int x, int y) {
231    rot_y = (GLfloat)(x - RNA3D->saved_x) * RNA3D->ROTATION_SPEED;
232    rot_x = (GLfloat)(y - RNA3D->saved_y) * RNA3D->ROTATION_SPEED;
233    RNA3D->saved_x = x;
234    RNA3D->saved_y = y;
235}
236
237void MapDisplayParameters(AW_root *root) {
238    GLRenderer  *cRenderer  = RNA3D->cRenderer;
239    Structure3D *cStructure = RNA3D->cStructure;
240
241    // General Molecule Display Section
242    cRenderer->iBackBone       = root->awar(AWAR_3D_MOL_BACKBONE)->read_int();
243    cRenderer->iColorise       = root->awar(AWAR_3D_MOL_COLORIZE)->read_int();
244    cRenderer->fSkeletonSize   = root->awar(AWAR_3D_MOL_SIZE)->read_float();
245    cRenderer->iDispPos        = root->awar(AWAR_3D_MOL_DISP_POS)->read_int();
246    cStructure->iInterval      = root->awar(AWAR_3D_MOL_POS_INTERVAL)->read_int();
247    cRenderer->iDispCursorPos  = root->awar(AWAR_3D_CURSOR_POSITION)->read_int();
248    RNA3D->iRotateMolecule    = root->awar(AWAR_3D_MOL_ROTATE)->read_int();
249
250    // Display Bases Section
251    cRenderer->iDisplayBases     = root->awar(AWAR_3D_DISPLAY_BASES)->read_int();
252    cRenderer->ObjectSize        = root->awar(AWAR_3D_DISPLAY_SIZE)->read_float();
253    cRenderer->iBaseMode         = root->awar(AWAR_3D_BASES_MODE)->read_int();
254    cRenderer->iBaseHelix        = root->awar(AWAR_3D_BASES_HELIX)->read_int();
255    cRenderer->iBaseUnpairHelix  = root->awar(AWAR_3D_BASES_UNPAIRED_HELIX)->read_int();
256    cRenderer->iBaseNonHelix     = root->awar(AWAR_3D_BASES_NON_HELIX)->read_int();
257    cRenderer->iShapeHelix       = root->awar(AWAR_3D_SHAPES_HELIX)->read_int();
258    cRenderer->iShapeUnpairHelix = root->awar(AWAR_3D_SHAPES_UNPAIRED_HELIX)->read_int();
259    cRenderer->iShapeNonHelix    = root->awar(AWAR_3D_SHAPES_NON_HELIX)->read_int();
260
261    // Display Helices Section
262    cRenderer->iDisplayHelix  = root->awar(AWAR_3D_DISPLAY_HELIX)->read_int();
263    cRenderer->iHelixMidPoint = root->awar(AWAR_3D_HELIX_MIDPOINT)->read_int();
264    cRenderer->iHelixNrs      = root->awar(AWAR_3D_HELIX_NUMBER)->read_int();
265    cRenderer->fHelixSize     = root->awar(AWAR_3D_HELIX_SIZE)->read_float();
266    cRenderer->iHelixBackBone = root->awar(AWAR_3D_HELIX_BACKBONE)->read_int();
267    cRenderer->iStartHelix    = root->awar(AWAR_3D_HELIX_FROM)->read_int();
268    cRenderer->iEndHelix      = root->awar(AWAR_3D_HELIX_TO)->read_int();
269    cRenderer->iDispTerInt    = root->awar(AWAR_3D_DISPLAY_TERTIARY_INTRACTIONS)->read_int();
270
271    // Mapping Sequence Data Section
272    cStructure->iMapSAI           = root->awar(AWAR_3D_MAP_SAI)->read_int();
273    cStructure->iMapSearch        = root->awar(AWAR_3D_MAP_SEARCH_STRINGS)->read_int();
274    cStructure->iMapEnable        = root->awar(AWAR_3D_MAP_ENABLE)->read_int();
275    cRenderer->iMapSpecies        = root->awar(AWAR_3D_MAP_SPECIES)->read_int();
276    cRenderer->iMapSpeciesBase    = root->awar(AWAR_3D_MAP_SPECIES_DISP_BASE)->read_int();
277    cRenderer->iMapSpeciesPos     = root->awar(AWAR_3D_MAP_SPECIES_DISP_POS)->read_int();
278    cRenderer->iMapSpeciesDels    = root->awar(AWAR_3D_MAP_SPECIES_DISP_DELETIONS)->read_int();
279    cRenderer->iMapSpeciesMiss    = root->awar(AWAR_3D_MAP_SPECIES_DISP_MISSING)->read_int();
280    cRenderer->iMapSpeciesIns     = root->awar(AWAR_3D_MAP_SPECIES_DISP_INSERTIONS)->read_int();
281    cRenderer->iMapSpeciesInsInfo = root->awar(AWAR_3D_MAP_SPECIES_DISP_INSERTIONS_INFO)->read_int();
282
283    { // Validation of Helix Numbers entered by the User
284        int NoOfHelices = 0;
285        int rnaType = cStructure->FindTypeOfRNA();
286        switch (rnaType) {
287            case LSU_23S: NoOfHelices = 101; break;
288            case SSU_16S: NoOfHelices = 50;  break;
289            case LSU_5S:  NoOfHelices = 5;   break;
290        }
291
292        if (cRenderer->iStartHelix < 1 ||  cRenderer->iStartHelix > NoOfHelices) {
293            cout<<"Invalid Helix NUMBER !!"<<endl;
294            root->awar(AWAR_3D_HELIX_FROM)->write_int(1);
295        }
296        if (cRenderer->iEndHelix < 1 ||  cRenderer->iEndHelix > NoOfHelices) {
297            cout<<"Invalid Helix NUMBER !!"<<endl;
298            root->awar(AWAR_3D_HELIX_TO)->write_int(NoOfHelices);
299        }
300
301        if (cRenderer->iStartHelix > cRenderer->iEndHelix) {
302            root->awar(AWAR_3D_HELIX_FROM)->write_int(cRenderer->iEndHelix - 1);
303        }
304        else if (cRenderer->iEndHelix < cRenderer->iStartHelix) {
305            root->awar(AWAR_3D_HELIX_TO)->write_int(cRenderer->iStartHelix + 1);
306        }
307    }
308
309    // Generation of DisplayLists for displaying Helix Numbers
310    if (!bHelixNrDispListCreated &&
311        (cRenderer->iHelixNrs || cRenderer->iHelixMidPoint))
312    {
313        cStructure->GenerateHelixNrDispList(cRenderer->iStartHelix, cRenderer->iEndHelix);
314        bHelixNrDispListCreated = true;
315    }
316
317    // Validation of Base Position display
318    if (cStructure->iInterval < 1) {
319        cout<<"WARNING: Invalid POSITION Interval!! Setting it to Default Value (25)."<<endl;
320        root->awar(AWAR_3D_MOL_POS_INTERVAL)->write_int(25);
321    }
322
323    if (cStructure->iMapEnable)
324    {
325        if (cStructure->iMapSearch) {
326            if (!RNA3D->bMapSearchStringsDispListCreated) {
327                cStructure->MapSearchStringsToEcoliTemplate(root);
328            }
329        }
330        if (cStructure->iMapSAI) {
331            if (!RNA3D->bMapSaiDispListCreated) {
332                cStructure->MapSaiToEcoliTemplate();
333            }
334        }
335        if (!bMapSpDispListCreated) {
336            cStructure->MapCurrentSpeciesToEcoliTemplate(root);
337            bMapSpDispListCreated = true;
338        }
339    }
340}
341
342
343void DisplayPostionsIntervalChanged_CB(AW_root *awr) {
344    MapDisplayParameters(awr);
345    glDeleteLists(STRUCTURE_POS, 2);
346    RNA3D->cStructure->ComputeBasePositions();
347    RefreshOpenGLDisplay();
348}
349
350void MapSelectedSpeciesChanged_CB(AW_root *awr) {
351
352    // If Selected Species (in Primary Editor) changed and
353    // the MapSpecies display lists created then,
354    //  1. Delete the display lists;
355    //  2. Recalculate the Display lists for current species;
356    //  3. Map it to EColi Template;
357
358    if (RNA3D->cStructure->iMapEnable &&
359        RNA3D->cRenderer->iMapSpecies &&
360        bMapSpDispListCreated)
361    {
362        glDeleteLists(MAP_SPECIES_BASE_DIFFERENCE, 9);
363        RNA3D->cStructure->MapCurrentSpeciesToEcoliTemplate(awr);
364    }
365
366    // If selected species changed then regenerate the SearchStrings DisplayList
367    MapSearchStringsToEcoliTemplateChanged_CB(awr);
368
369    RefreshOpenGLDisplay();
370}
371
372void MapSaiToEcoliTemplateChanged_CB(AW_root */*awr*/) {
373    // if SAI changed in EDIT4 then display lists should be recalculated
374
375    if (RNA3D->cStructure->iMapEnable  &&
376        RNA3D->cStructure->iMapSAI     &&
377        RNA3D->bMapSaiDispListCreated)
378    {
379        RNA3D->bMapSaiDispListCreated = false;
380        glDeleteLists(MAP_SAI_TO_STRUCTURE, 1);
381        RNA3D->cStructure->MapSaiToEcoliTemplate();
382    }
383
384    RefreshOpenGLDisplay();
385}
386
387void MapSearchStringsToEcoliTemplateChanged_CB(AW_root *awr) {
388
389    // If selected species changed then regenerate the
390    // SearchStrings DisplayList
391
392    if (RNA3D->cStructure->iMapEnable  &&
393        RNA3D->cStructure->iMapSearch  &&
394        RNA3D->bMapSearchStringsDispListCreated)
395    {
396        RNA3D->bMapSearchStringsDispListCreated = false;
397        glDeleteLists(MAP_SEARCH_STRINGS_TO_STRUCTURE, 2);
398        RNA3D->cStructure->MapSearchStringsToEcoliTemplate(awr);
399    }
400}
401
402void CursorPositionChanged_CB(AW_root *awr) {
403
404    if (RNA3D->bEColiRefInitialized) {
405        long iCursorPos = awr->awar(AWAR_CURSOR_POSITION)->read_int();
406        long EColiPos   = RNA3D->cStructure->EColiRef->abs_2_rel(iCursorPos); // @@@ calls abs_2_rel with biopos (1..N)! this is wrong
407
408        if (!bCursorPosDispListCreated) {
409            RNA3D->cStructure->GenerateCursorPositionDispList(EColiPos);
410            bMapSpDispListCreated = true;
411        }
412        else {
413            glDeleteLists(ECOLI_CURSOR_POSITION, 1);
414            RNA3D->cStructure->GenerateCursorPositionDispList(EColiPos);
415        }
416        RefreshOpenGLDisplay();
417    }
418}
419
420void DisplayHelixNrsChanged_CB(AW_root *awr) {
421    MapDisplayParameters(awr);
422
423    int iStart = RNA3D->cRenderer->iStartHelix;
424    int iEnd   = RNA3D->cRenderer->iEndHelix;
425    if (!bHelixNrDispListCreated) {
426        RNA3D->cStructure->GenerateHelixNrDispList(iStart, iEnd);
427        bHelixNrDispListCreated = true;
428    }
429    else {
430        glDeleteLists(HELIX_NUMBERS, 2);
431        RNA3D->cStructure->GenerateHelixNrDispList(iStart, iEnd);
432    }
433    RefreshOpenGLDisplay();
434}
435
436static void DrawStructure() {
437    GLRenderer *cRenderer = RNA3D->cRenderer;
438
439    // Drawing Molecule Skeleton
440    cRenderer->DisplayMolecule(RNA3D->cStructure);
441
442    // Mapping Helices to The molecule
443    cRenderer->DoHelixMapping();
444
445    // Texture Mapping
446    cRenderer->BeginTexturizer();
447    cRenderer->TexturizeStructure(RNA3D->cTexture, RNA3D->cStructure);
448    cRenderer->EndTexturizer();
449}
450
451void RenderOpenGLScene(Widget w) {
452    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
453
454    // setting the BackGround Color of the OpenGL Scene
455    RNA3D->cGraphics->SetOpenGLBackGroundColor();
456
457    glLoadIdentity();
458
459    gluLookAt(RNA3D->Viewer.x, RNA3D->Viewer.y, RNA3D->Viewer.z,
460              RNA3D->Center.x, RNA3D->Center.y, RNA3D->Center.z,
461              RNA3D->Up.x,     RNA3D->Up.y,     RNA3D->Up.z);
462
463    { // Displaying Molecule Name
464        RNA3D->cRenderer->DisplayMoleculeName(iScreenWidth, iScreenHeight, RNA3D->cStructure);
465    }
466
467    glScalef(RNA3D->scale, RNA3D->scale, RNA3D->scale);
468
469    RNA3D->cRenderer->DisplayMoleculeMask(iScreenWidth, iScreenHeight);
470
471    if (RNA3D->bRotateMolecule || RNA3D->bAutoRotate) {
472        CalculateRotationMatrix();
473    }
474
475    glMultMatrixf(rotation_matrix);
476
477    Vector3& strCen = *RNA3D->cStructure->strCen;
478    glTranslatef(-strCen.x, -strCen.y, -strCen.z);
479
480    DrawStructure();
481
482    glFlush();
483    glXWaitX();
484    glXSwapBuffers (XtDisplay(w), XtWindow(w));
485}
486
487void InitializeOpenGLWindow(Widget w) {
488
489    if (RNA3D->OpenGLEngineState == CREATED) return;
490
491    Arg args[1];
492    XVisualInfo *vi;
493
494    XtSetArg(args[0], (char *) GLwNvisualInfo, &vi);
495    XtGetValues(w, args, 1);
496
497    dpy = XtDisplay(w);
498    if (!dpy) {
499        fprintf(stderr, "could not open display\n");
500    }
501    else {
502        if (AW_alpha_Size_Supported) {
503            vi = glXChooseVisual(dpy, DefaultScreen(dpy), DoubleBufferWithAlpha);
504            printf("RNA3D: Double Buffered Visual With Alpha Size Supported !\n");
505        }
506        else {
507            vi = glXChooseVisual(dpy, DefaultScreen(dpy), DoubleBuffer);
508            printf("RNA3D: Double Buffered Visual Supported !\n");
509        }
510
511        if (!vi) {
512            fprintf(stderr, "try to get a single buffered visual\n");
513            vi = glXChooseVisual(dpy, DefaultScreen(dpy), SingleBuffer);
514            if (!vi)
515                fprintf(stderr, "could not get visual\n");
516        }
517        else {
518            glx_context = glXCreateContext(dpy, vi, NULL, GL_TRUE);
519            if (!glXIsDirect(dpy, glx_context))
520                fprintf(stderr, "direct rendering not supported\n");
521            else
522                printf("RNA3D: Direct rendering supported\n");
523
524            GLwDrawingAreaMakeCurrent(w, glx_context);
525
526            RNA3D->glw = w;
527
528            RNA3D->OpenGLEngineState = CREATED;
529
530            // Initializing fonts
531            char fontName[] = "fixed";
532            RNA3D->cGraphics->InitMainFont(fontName);
533        }
534    }
535}
Note: See TracBrowser for help on using the repository browser.