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