cgv
Loading...
Searching...
No Matches
gl_context_unix.cxx
1#ifdef NEVER
2#include <cgv_gl/gl/wgl.h>
3#include "gl_context.h"
4#include <iostream>
5
6using namespace cgv::utils;
7
8
9namespace cgv {
10 namespace render {
11 namespace gl {
12
13struct unix_gl_context : public gl_context
14{
15 Display *d_dpy;
16 Window d_win;
17 GLXContext d_ctx;
18 //
19 unsigned int width;
20 //
21 unsigned int height;
22
23 // Function prototypes.
24
25 bool init_application();
26 bool ensure_init_application();
27 bool init_instance();
28 bool set_pixel_format();
29 bool create(const std::string& title, bool show);
30
31 unix_gl_context(unsigned int w = -1, unsigned int h = -1);
32 ~unix_gl_context();
33
35 RenderPass get_render_pass() const { return RP_NONE; }
37 RenderPassFlags get_render_pass_flags() const { return RPF_NONE; }
39 void render_pass(RenderPass render_pass = RP_MAIN,
40 RenderPassFlags render_pass_flags = RPF_ALL) {}
42 RenderPassFlags get_default_render_pass_flags() const { return RPF_NONE; }
44 void set_default_render_pass_flags(RenderPassFlags) { }
46 bool in_render_process() const { return false; }
48 bool is_created() const { return true; }
50 bool is_current() const { return true; }
52 bool make_current() const;
54 void clear_current() const;
56
58 unsigned int get_width() const { return width; }
60 unsigned int get_height() const { return height; }
62 void resize(unsigned int width, unsigned int height) { }
64 void set_bg_color(float r, float g, float b, float a) {}
66 void post_redraw() {}
68 void force_redraw() {}
69 bool is_alpha_buffer_attached() const { return true; }
70 void attach_alpha_buffer() {}
71 void detach_alpha_buffer() {}
72 bool is_stencil_buffer_attached() const { return false; }
73 void attach_stencil_buffer() {}
74 void detach_stencil_buffer() {}
75 bool is_quad_buffer_supported() const { return false; }
76 bool is_quad_buffer_attached() const { return false; }
77 void attach_quad_buffer() {}
78 void detach_quad_buffer() {}
79 bool is_accum_buffer_attached() const { return false; }
80 void attach_accum_buffer() {}
81 void detach_accum_buffer() {}
82 bool is_multisample_enabled() const { return false; }
83 void enable_multisample() {}
84 void disable_multisample() {}
85
89 void enable_font_face(cgv::media::font::font_face_ptr font_face, float font_size) {}
91 float get_current_font_size() const { return 12; }
93 cgv::media::font::font_face_ptr get_current_font_face() const { return cgv::media::font::font_face_ptr();
94 }
96
97 std::ostream& output_stream() { return std::cout; }
98};
99
100bool unix_gl_context::make_current() const
101{
102 bool res;
103 XLockDisplay(d_dpy);
104 res = glXMakeCurrent(d_dpy, d_win, d_ctx);
105 XUnlockDisplay(d_dpy);
106
107 if (!res) {
108 std::cerr << "failed to make current" << std::endl;
109 return false;
110 }
111 return true;
112}
113
114void unix_gl_context::clear_current() const
115{
116 XLockDisplay(d_dpy);
117 glXMakeCurrent(d_dpy, 0, 0);
118 XUnlockDisplay(d_dpy);
119}
120
121unix_gl_context::unix_gl_context(unsigned int w, unsigned int h)
122{
123 width = w;
124 height = h;
125 d_dpy = 0;
126 d_win = 0;
127 d_ctx = 0;
128}
129
130unix_gl_context::~unix_gl_context()
131{
132 XLockDisplay(d_dpy);
133 glXMakeCurrent(d_dpy, d_win, d_ctx);
134 glXDestroyContext(d_dpy, d_ctx);
135 XDestroyWindow(d_dpy, d_win);
136 XUnlockDisplay(d_dpy);
137 XCloseDisplay(d_dpy);
138 d_dpy = 0;
139 d_win = 0;
140 d_ctx = 0;
141}
142
143bool unix_gl_context::create(const std::string& title, bool show)
144{
145
146 // Set to NULL for getting it from the environment variable
147 const char *display = ":0";
148
149 if (!(d_dpy = XOpenDisplay(display))) {
150 std::cerr << "Couldn't open X11 display" << std::endl;
151 return false;
152 }
153 XLockDisplay(d_dpy);
154
155 int attr[] = {
156 GLX_RGBA,
157 GLX_RED_SIZE, 8,
158 GLX_GREEN_SIZE, 8,
159 GLX_BLUE_SIZE, 8,
160 GLX_DOUBLEBUFFER,
161 GLX_DEPTH_SIZE, 16,
162 None
163 };
164
165 int scrnum = DefaultScreen(d_dpy);
166
167 Window root = RootWindow(d_dpy, scrnum);
168
169 /*
170 // We need this for OpenGL3
171 int elemc;
172 GLXFBConfig *fbcfg = glXChooseFBConfig(d_dpy, scrnum, NULL, &elemc);
173 if (!fbcfg) {
174 std::cerr << "Couldn't get FB configs" << std::endl;
175 return false;
176 }
177 else {
178 std::cout << "Got " << elemc << " FB configs" << std::endl;
179 }
180 */
181
182 XVisualInfo *visinfo = glXChooseVisual(d_dpy, scrnum, attr);
183
184 if (!visinfo) {
185 std::cerr << "Couldn't get a visual" << std::endl;
186 }
187 else {
188 d_ctx = glXCreateContext(d_dpy, visinfo, NULL, True);
189
190
191 // Window parameters
192 XSetWindowAttributes winattr;
193 winattr.background_pixel = 0;
194 winattr.border_pixel = 0;
195 winattr.colormap = XCreateColormap(d_dpy, root, visinfo->visual, AllocNone);
196 winattr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
197 unsigned long mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
198
199 //std::cout << "Window depth " << visinfo->depth << ", w " << width << "x" << height << std::endl;
200 d_win = XCreateWindow(d_dpy, root, 0, 0, width, height, 0,
201 visinfo->depth, InputOutput, visinfo->visual, mask, &winattr);
202
203 /*
204 // OpenGL 3.2
205 int gl3attr[] = {
206 GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
207 GLX_CONTEXT_MINOR_VERSION_ARB, 2,
208 GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
209 None
210 };
211
212 d_ctx = glXCreateContextAttribsARB(d_dpy, fbcfg[0], NULL, true, gl3attr);
213 */
214
215
216 XMapWindow(d_dpy, d_win);
217 glXMakeCurrent(d_dpy, d_win, d_ctx);
218 XUnmapWindow(d_dpy, d_win);
219 /*
220 std::cout << "OpenGL:\n\tvendor " << glGetString(GL_VENDOR)
221 << "\n\trenderer " << glGetString(GL_RENDERER)
222 << "\n\tversion " << glGetString(GL_VERSION)
223 << "\n\tshader language " << glGetString(GL_SHADING_LANGUAGE_VERSION)
224 << "\n" << std::endl;
225 */
226 /*
227 int extCount;
228 glGetIntegerv(GL_NUM_EXTENSIONS, &extCount);
229 for (int i = 0; i < extCount; ++i)
230 std::cout << "Extension " << i+1 << "/" << extCount << ": " << glGetStringi(GL_EXTENSIONS, i) << std::endl;
231 */
232 glViewport(0, 0, width, height);
233 }
234
235 XFree(visinfo);
236 XUnlockDisplay(d_dpy);
237 return true;
238}
239
240context* create_unix_gl_context(RenderAPI api, unsigned int w, unsigned int h,
241 const std::string& title, bool show)
242{
243 if (api != RA_OPENGL)
244 return 0;
245 unix_gl_context* ctx_ptr = new unix_gl_context(w,h);
246 if (!ctx_ptr->create(title, show)) {
247 delete ctx_ptr;
248 return 0;
249 }
250 return ctx_ptr;
251}
252
253 }
254 }
255}
256
257#include "lib_begin.h"
258
259namespace cgv {
260 namespace render {
261
262extern CGV_API context_factory_registration create_unix_gl_context_registration(gl::create_unix_gl_context);
263
264 }
265}
266
267
268#endif
data::ref_ptr< font_face > font_face_ptr
always use this ref counted pointer to store font faces
Definition font.h:52
RenderAPI
enumeration of rendering APIs which can be queried from the context
Definition context.h:70
RenderPass
Enumeration of different render passes, which can be queried from the context and used to specify a n...
Definition context.h:77
@ RP_NONE
no renderpass
Definition context.h:78
RenderPassFlags
available flags that can be queried from the context and set for a new render pass
Definition context.h:93
@ RPF_NONE
no frame initialization is performed
Definition context.h:94
namespace that holds tools that dont fit any other namespace
the cgv namespace
Definition print.h:11