source: branches/stable/RNA3D/RNA3D_OpenGLGraphics.cxx

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