source: branches/port5/RNA3D/RNA3D_OpenGLGraphics.cxx

Last change on this file was 5868, checked in by westram, 16 years ago
  • separated openGL parts from WINDOW library (it's nonsense to link openGL into all applications when only one application actually uses openGL)
    • created GL/glWINDOW library (extending normal WINDOW library) and moved relevant code there
    • created an interface header for applications needing access to Motif/X11 internals (aw_window_Xm_interface.hxx)
    • RNA3D uses that interface header (instead of hardcoded links into ../WINDOW)
    • AW_variable_update_struct went local (AW_button.cxx)
    • some functions exported (aw_create_help_entry, aw_create_shell, aw_realize_widget). Needed by libglWINDOW
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.8 KB
Line 
1#include "RNA3D_GlobalHeader.hxx"
2#include "RNA3D_Global.hxx"
3#include "RNA3D_OpenGLGraphics.hxx"
4#include "RNA3D_Renderer.hxx"
5#include "RNA3D_Textures.hxx"
6#include "RNA3D_StructureData.hxx"
7
8#include <cmath>
9#include <cstdlib>
10
11#include <awt_canvas.hxx>
12#include <aw_window_Xm_interface.hxx>
13
14using namespace std;
15
16OpenGLGraphics::OpenGLGraphics(void)
17    : displayGrid(false)
18    , ApplicationBGColor(0, 0, 0)
19{
20}
21
22OpenGLGraphics::~OpenGLGraphics(void){
23}
24
25// Sets the Background Color for the OpenGL Window
26void OpenGLGraphics::SetOpenGLBackGroundColor() {
27
28    unsigned long bgColor;
29    XtVaGetValues( RNA3D->glw, XmNbackground, &bgColor, NULL );
30
31    Widget w = AW_get_AreaWidget(RNA3D->gl_Canvas->aww, AW_MIDDLE_AREA);
32
33    XColor xcolor;
34    xcolor.pixel = bgColor;
35    Colormap colormap = DefaultColormapOfScreen( XtScreen( w ) );
36    XQueryColor( XtDisplay( w ), colormap, &xcolor );
37
38    float r, g, b; r = g = b = 0.0;
39    r = (float) xcolor.red / 65535.0;
40    g = (float) xcolor.green / 65535.0;
41    b = (float) xcolor.blue / 65535.0;
42
43    // set OpenGL Backgroud Color to the widget's backgroud     
44    glClearColor(r, g, b, 1);
45
46    // extern ColorRGBf ApplicationBGColor;
47    ApplicationBGColor = ColorRGBf(r, g, b);
48}
49
50// Converts the GC into RGB values
51ColorRGBf OpenGLGraphics::ConvertGCtoRGB(int gc) {
52    ColorRGBf clr = ColorRGBf(0,0,0);
53    float r, g, b; r = g = b = 0.0;
54
55    Widget w   = AW_get_AreaWidget(RNA3D->gl_Canvas->aww, AW_MIDDLE_AREA);
56    GC     xgc = AW_map_AreaGC(RNA3D->gl_Canvas->aww, AW_MIDDLE_AREA, gc);
57
58    XGCValues     xGCValues;
59    XGetGCValues( XtDisplay( w ), xgc, GCForeground, &xGCValues );
60    unsigned long color = xGCValues.foreground;
61
62    XColor xcolor;
63    xcolor.pixel = color;
64
65    Colormap colormap = DefaultColormapOfScreen( XtScreen( w ) );
66    XQueryColor( XtDisplay( w ), colormap, &xcolor );
67       
68    r = (float) xcolor.red / 65535.0;
69    g = (float) xcolor.green / 65535.0;
70    b = (float) xcolor.blue / 65535.0;
71
72    clr = ColorRGBf(r,g,b);
73    return clr;
74}
75
76// Converts the GC into RGB values and sets the glColor
77void OpenGLGraphics::SetColor(int gc) {
78    ColorRGBf color = ConvertGCtoRGB(gc);
79    glColor4f(color.red, color.green, color.blue, 1);
80}
81
82// Converts the GC into RGB values and returns them
83ColorRGBf OpenGLGraphics::GetColor(int gc) {
84    ColorRGBf color = ConvertGCtoRGB(gc);
85    return color;
86}
87
88void OpenGLGraphics::WinToScreenCoordinates(int x, int y, GLdouble *screenPos) {
89    //project window coords to gl coord
90    glLoadIdentity();
91    GLdouble modelMatrix[16];
92    glGetDoublev(GL_MODELVIEW_MATRIX,modelMatrix);
93    GLdouble projMatrix[16];
94    glGetDoublev(GL_PROJECTION_MATRIX,projMatrix);
95    GLint viewport[4];
96    glGetIntegerv(GL_VIEWPORT,viewport);
97    gluUnProject(x, y, 0,
98                 modelMatrix,
99                 projMatrix,
100                 viewport,
101                 //the next 3 parameters are the pointers to the final object coordinates.(double)
102                 &screenPos[0], &screenPos[1], &screenPos[2] 
103                 );
104}
105
106//=========== Functions related to rendering FONTS ========================//
107
108static GLuint font_base;
109
110void OpenGLGraphics::init_font(GLuint base, char* f)
111{
112    Display* display;
113    XFontStruct* font_info;
114    int first;
115    int last;
116
117    /* Need an X Display before calling any Xlib routines. */
118    //  display = XOpenDisplay(0); // glxFont problem : changed XOpenDisplay(0) to glXGetCurrentDisplay()
119    display = glXGetCurrentDisplay();
120    if (display == 0) {
121        fprintf(stderr, "XOpenDisplay() failed.  Exiting.\n");
122        exit(-1);
123    } 
124    else {
125        /* Load the font. */
126        font_info = XLoadQueryFont(display, f);
127        if (!font_info) {
128            fprintf(stderr, "XLoadQueryFont() failed - Exiting.\n");
129            exit(-1);
130        }
131        else {
132            /* Tell GLX which font & glyphs to use. */
133            first = font_info->min_char_or_byte2;
134            last  = font_info->max_char_or_byte2;
135            glXUseXFont(font_info->fid, first, last-first+1, base+first);
136        }
137        //XCloseDisplay(display); // glxFont problem
138        display = 0;
139    }
140}
141
142void OpenGLGraphics::print_string(GLuint base, char* s)
143{
144    if (!glIsList(font_base)) {
145        fprintf(stderr, "print_string(): Bad display list. - Exiting.\n");
146        exit (-1);
147    }
148    else if (s && strlen(s)) {
149        glPushAttrib(GL_LIST_BIT);
150        glListBase(base);
151        glCallLists(strlen(s), GL_UNSIGNED_BYTE, (GLubyte *)s);
152        glPopAttrib();
153    }
154}
155
156void OpenGLGraphics::InitMainFont(char* f)
157{
158    font_base = glGenLists(256);
159    if (!glIsList(font_base)) {
160        fprintf(stderr, "InitMainFont(): Out of display lists. - Exiting.\n");
161        exit (-1);
162    }
163    else {
164        init_font(font_base, f);
165    }
166}
167
168void OpenGLGraphics::PrintString(float x, float y, float z, char *s, void * /*font */) {
169    glRasterPos3f(x, y, z);
170    print_string(font_base, s);
171    //       for (unsigned int i = 0; i < strlen(s); i++) {
172    //          glutBitmapCharacter(font, s[i]);
173    //     }
174}
175
176void OpenGLGraphics::PrintComment(float /* x */, float /* y */, float /* z */, char */*s */){
177    // if comments are longer break them and display in next line
178    //     int col  = 35;
179
180    //         glRasterPos3f(x, y, z);
181    //         for (unsigned int i = 0; i < strlen(s); i++) {
182    //             if(i%col==0) {
183    //                 y -= 10;
184    //                 glRasterPos3f(x, y, z);
185    //             }
186    //             glutBitmapCharacter(GLUT_BITMAP_8_BY_13, s[i]);
187    //         }
188}
189
190//===============================================================================
191
192void  OpenGLGraphics::DrawBox(float x, float y,  float w, float h)
193{
194    glBegin(GL_QUADS);             
195    glVertex2f(x-w/2, y-h/2);
196    glVertex2f(x+w/2, y-h/2);
197    glVertex2f(x+w/2, y+h/2);
198    glVertex2f(x-w/2, y+h/2);
199    glEnd();
200}
Note: See TracBrowser for help on using the repository browser.