xglcomp

view src/texture.cc @ 6:3f908f812ec7

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