source: tags/svn.1.5.4/RNA3D/RNA3D_OpenGLGraphics.cxx

Last change on this file was 7811, checked in by westram, 14 years ago

merge from dev [7748] [7749] [7750]

  • comments (C→C++ style)
  • fixed umlauts in TREEGEN
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.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 <awt_canvas.hxx>
9#include <aw_window_Xm_interface.hxx>
10
11using namespace std;
12
13OpenGLGraphics::OpenGLGraphics()
14    : displayGrid(false)
15    , ApplicationBGColor(0, 0, 0)
16{
17}
18
19OpenGLGraphics::~OpenGLGraphics() {
20}
21
22// Sets the Background Color for the OpenGL Window
23void OpenGLGraphics::SetOpenGLBackGroundColor() {
24
25    unsigned long bgColor;
26    XtVaGetValues(RNA3D->glw, XmNbackground, &bgColor, NULL);
27
28    Widget w = AW_get_AreaWidget(RNA3D->gl_Canvas->aww, AW_MIDDLE_AREA);
29
30    XColor xcolor;
31    xcolor.pixel = bgColor;
32    Colormap colormap = DefaultColormapOfScreen(XtScreen(w));
33    XQueryColor(XtDisplay(w), colormap, &xcolor);
34
35    float r, g, b; r = g = b = 0.0;
36    r = (float) xcolor.red / 65535.0;
37    g = (float) xcolor.green / 65535.0;
38    b = (float) xcolor.blue / 65535.0;
39
40    // set OpenGL background color to the widget's background
41    glClearColor(r, g, b, 1);
42
43    ApplicationBGColor = ColorRGBf(r, g, b);
44}
45
46// Converts the GC into RGB values
47ColorRGBf OpenGLGraphics::ConvertGCtoRGB(int gc) {
48    ColorRGBf clr = ColorRGBf(0, 0, 0);
49    float r, g, b; r = g = b = 0.0;
50
51    Widget w   = AW_get_AreaWidget(RNA3D->gl_Canvas->aww, AW_MIDDLE_AREA);
52    GC     xgc = AW_map_AreaGC(RNA3D->gl_Canvas->aww, AW_MIDDLE_AREA, gc);
53
54    XGCValues     xGCValues;
55    XGetGCValues(XtDisplay(w), xgc, GCForeground, &xGCValues);
56    unsigned long color = xGCValues.foreground;
57
58    XColor xcolor;
59    xcolor.pixel = color;
60
61    Colormap colormap = DefaultColormapOfScreen(XtScreen(w));
62    XQueryColor(XtDisplay(w), colormap, &xcolor);
63
64    r = (float) xcolor.red / 65535.0;
65    g = (float) xcolor.green / 65535.0;
66    b = (float) xcolor.blue / 65535.0;
67
68    clr = ColorRGBf(r, g, b);
69    return clr;
70}
71
72// Converts the GC into RGB values and sets the glColor
73void OpenGLGraphics::SetColor(int gc) {
74    ColorRGBf color = ConvertGCtoRGB(gc);
75    glColor4f(color.red, color.green, color.blue, 1);
76}
77
78// Converts the GC into RGB values and returns them
79ColorRGBf OpenGLGraphics::GetColor(int gc) {
80    ColorRGBf color = ConvertGCtoRGB(gc);
81    return color;
82}
83
84void OpenGLGraphics::WinToScreenCoordinates(int x, int y, GLdouble *screenPos) {
85    // project window coords to gl coord
86    glLoadIdentity();
87    GLdouble modelMatrix[16];
88    glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
89    GLdouble projMatrix[16];
90    glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
91    GLint viewport[4];
92    glGetIntegerv(GL_VIEWPORT, viewport);
93    gluUnProject(x, y, 0,
94                 modelMatrix,
95                 projMatrix,
96                 viewport,
97                 // the next 3 parameters are the pointers to the final object coordinates.(double)
98                 &screenPos[0], &screenPos[1], &screenPos[2]
99                 );
100}
101
102// =========== Functions related to rendering FONTS ========================//
103
104static GLuint font_base;
105
106void OpenGLGraphics::init_font(GLuint base, char* f)
107{
108    Display* display;
109    XFontStruct* font_info;
110    int first;
111    int last;
112
113    // Need an X Display before calling any Xlib routines.
114    display = glXGetCurrentDisplay();
115    if (display == 0) {
116        fprintf(stderr, "XOpenDisplay() failed.  Exiting.\n");
117        exit(-1);
118    }
119    else {
120        // Load the font.
121        font_info = XLoadQueryFont(display, f);
122        if (!font_info) {
123            fprintf(stderr, "XLoadQueryFont() failed - Exiting.\n");
124            exit(-1);
125        }
126        else {
127            // Tell GLX which font & glyphs to use.
128            first = font_info->min_char_or_byte2;
129            last  = font_info->max_char_or_byte2;
130            glXUseXFont(font_info->fid, first, last-first+1, base+first);
131        }
132        display = 0;
133    }
134}
135
136void OpenGLGraphics::print_string(GLuint base, char* s)
137{
138    if (!glIsList(font_base)) {
139        fprintf(stderr, "print_string(): Bad display list. - Exiting.\n");
140        exit (-1);
141    }
142    else if (s && strlen(s)) {
143        glPushAttrib(GL_LIST_BIT);
144        glListBase(base);
145        glCallLists(strlen(s), GL_UNSIGNED_BYTE, (GLubyte *)s);
146        glPopAttrib();
147    }
148}
149
150void OpenGLGraphics::InitMainFont(char* f)
151{
152    font_base = glGenLists(256);
153    if (!glIsList(font_base)) {
154        fprintf(stderr, "InitMainFont(): Out of display lists. - Exiting.\n");
155        exit (-1);
156    }
157    else {
158        init_font(font_base, f);
159    }
160}
161
162void OpenGLGraphics::PrintString(float x, float y, float z, char *s, void * /* font */) {
163    glRasterPos3f(x, y, z);
164    print_string(font_base, s);
165}
166
167void  OpenGLGraphics::DrawBox(float x, float y,  float w, float h)
168{
169    glBegin(GL_QUADS);
170    glVertex2f(x-w/2, y-h/2);
171    glVertex2f(x+w/2, y-h/2);
172    glVertex2f(x+w/2, y+h/2);
173    glVertex2f(x-w/2, y+h/2);
174    glEnd();
175}
Note: See TracBrowser for help on using the repository browser.