rev |
line source |
nuclear@4
|
1 #include <GL/gl.h>
|
nuclear@4
|
2 #include <X11/Xlib.h>
|
nuclear@4
|
3 #include "texture.h"
|
nuclear@4
|
4 #include "logger.h"
|
nuclear@4
|
5
|
nuclear@4
|
6 Texture::Texture()
|
nuclear@4
|
7 {
|
nuclear@4
|
8 glGenTextures(1, &tex);
|
nuclear@4
|
9 glBindTexture(GL_TEXTURE_2D, tex);
|
nuclear@4
|
10 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
nuclear@4
|
11 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
nuclear@4
|
12
|
nuclear@4
|
13 width = height = 0;
|
nuclear@4
|
14 }
|
nuclear@4
|
15
|
nuclear@4
|
16 Texture::~Texture()
|
nuclear@4
|
17 {
|
nuclear@4
|
18 if(tex) {
|
nuclear@4
|
19 glDeleteTextures(1, &tex);
|
nuclear@4
|
20 }
|
nuclear@4
|
21 }
|
nuclear@4
|
22
|
nuclear@4
|
23 void Texture::set_image(int x, int y, unsigned char *pix)
|
nuclear@4
|
24 {
|
nuclear@4
|
25 glBindTexture(GL_TEXTURE_2D, tex);
|
nuclear@4
|
26
|
nuclear@4
|
27 if(x == width && y == height) {
|
nuclear@4
|
28 if(pix) {
|
nuclear@4
|
29 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, x, y, GL_BGRA, GL_UNSIGNED_BYTE, pix);
|
nuclear@4
|
30 }
|
nuclear@4
|
31
|
nuclear@4
|
32 } else {
|
nuclear@4
|
33 width = x;
|
nuclear@4
|
34 height = y;
|
nuclear@4
|
35 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_BGRA, GL_UNSIGNED_BYTE,
|
nuclear@4
|
36 pix ? pix : 0);
|
nuclear@4
|
37 }
|
nuclear@4
|
38 }
|
nuclear@4
|
39
|
nuclear@4
|
40 void Texture::set_image(Display *dpy, Pixmap pixmap)
|
nuclear@4
|
41 {
|
nuclear@4
|
42 Window root_ret;
|
nuclear@4
|
43 int x, y;
|
nuclear@4
|
44 unsigned int w, h, border, depth;
|
nuclear@4
|
45 XGetGeometry(dpy, pixmap, &root_ret, &x, &y, &w, &h, &border, &depth);
|
nuclear@4
|
46
|
nuclear@4
|
47 XImage *ximg = XGetImage(dpy, pixmap, 0, 0, w, h, 0xffffffff, ZPixmap);
|
nuclear@4
|
48 if(!ximg) {
|
nuclear@4
|
49 log_error("XGetImage failed to create an image from the window pixmap\n");
|
nuclear@4
|
50 return;
|
nuclear@4
|
51 }
|
nuclear@4
|
52
|
nuclear@4
|
53 glBindTexture(GL_TEXTURE_2D, tex);
|
nuclear@4
|
54
|
nuclear@4
|
55 if((int)w == width && (int)h == height) {
|
nuclear@4
|
56 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_BGRA, GL_UNSIGNED_BYTE, ximg->data);
|
nuclear@4
|
57 } else {
|
nuclear@4
|
58 width = w;
|
nuclear@4
|
59 height = h;
|
nuclear@4
|
60 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, ximg->data);
|
nuclear@4
|
61 }
|
nuclear@4
|
62 }
|
nuclear@4
|
63
|
nuclear@4
|
64 int Texture::get_width() const
|
nuclear@4
|
65 {
|
nuclear@4
|
66 return width;
|
nuclear@4
|
67 }
|
nuclear@4
|
68
|
nuclear@4
|
69 int Texture::get_height() const
|
nuclear@4
|
70 {
|
nuclear@4
|
71 return height;
|
nuclear@4
|
72 }
|
nuclear@4
|
73
|
nuclear@4
|
74 unsigned int Texture::get_id() const
|
nuclear@4
|
75 {
|
nuclear@4
|
76 return tex;
|
nuclear@4
|
77 }
|