MythTV  master
gltexture.cpp
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 /* ============================================================
3  * File : gltexture.cpp
4  * Author: Renchi Raju <renchi@pooh.tam.uiuc.edu>
5  * Date : 2006-07-24
6  * Description :
7  *
8  * Copyright 2004-2006 Renchi Raju, Daniel Kristjansson
9  *
10  * This program is free software; you can redistribute it
11  * and/or modify it under the terms of the GNU General
12  * Public License as published bythe Free Software Foundation;
13  * either version 2, or (at your option)
14  * any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * ============================================================ */
22 
23 // Qt headers
24 #include <QImage>
25 
26 // MythGallery headers
27 #include "thumbview.h"
28 #include "gltexture.h"
29 #include "galleryutil.h"
30 
34 void GLTexture::Init(const QImage &image)
35 {
36  Deinit();
37  glGenTextures(1, &tex);
38  glBindTexture(GL_TEXTURE_2D, tex);
39 
40  /* actually generate the texture */
41  glTexImage2D(GL_TEXTURE_2D, 0, 3, image.width(), image.height(), 0,
42  GL_RGBA, GL_UNSIGNED_BYTE, image.bits());
43 
44  /* enable linear filtering */
45  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
46  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
47 }
48 
53 {
54  if (tex)
55  glDeleteTextures(1, &tex);
56 }
57 
58 void GLTexture::Bind(void)
59 {
60  glMatrixMode(GL_TEXTURE);
61  glLoadIdentity();
62  glRotatef(GetAngle(), 0.0f, 0.0f, 1.0f);
63  glBindTexture(GL_TEXTURE_2D, tex);
64 }
65 
66 void GLTexture::MakeQuad(float alpha, float scale)
67 {
68  Bind();
69 
70  glBegin(GL_QUADS);
71  glColor4f(1.0f, 1.0f, 1.0f, alpha);
72 
73  glTexCoord2f(0.0f, 0.0f);
74  glVertex3f(-GetTextureX() * scale, -GetTextureY() * scale, 0.0f);
75 
76  glTexCoord2f(1.0f, 0.0f);
77  glVertex3f(+GetTextureX() * scale, -GetTextureY() * scale, 0.0f);
78 
79  glTexCoord2f(1.0f, 1.0f);
80  glVertex3f(+GetTextureX() * scale, +GetTextureY() * scale, 0.0f);
81 
82  glTexCoord2f(0.0f, 1.0f);
83  glVertex3f(-GetTextureX() * scale, +GetTextureY() * scale, 0.0f);
84  glEnd();
85 }
86 
87 void GLTexture::ScaleTo(const QSize &dest, ScaleMax scaleMax)
88 {
89  QSize sz = GalleryUtil::ScaleToDest(GetSize(), dest, scaleMax);
90  if ((sz.width() > 0) && (sz.height() > 0) &&
91  (dest.width() > 0) && (dest.height() > 0))
92  {
93  SetScale((float)sz.width() / (float)dest.width(),
94  (float)sz.height() / (float)dest.height());
95  }
96 }
97 
98 void GLTexture::SetItem(ThumbItem *thumbItem, const QSize &sz)
99 {
100  item = thumbItem;
101  if (item)
102  {
104  SetSize(sz);
105 
106  if (angle % 180 != 0)
107  SwapWidthHeight();
108  }
109 }
110 
111 QString GLTexture::GetDescription(const QString &status) const
112 {
113  if (item)
114  return item->GetDescription(status, GetSize(), angle);
115 
116  return QString();
117 }
float GetTextureY(void) const
Definition: gltexture.h:63
void SetItem(ThumbItem *, const QSize &sz)
Definition: gltexture.cpp:98
QString GetDescription(const QString &status, const QSize &sz, int angle) const
Definition: thumbview.cpp:113
GLuint tex
Definition: gltexture.h:68
float GetTextureX(void) const
Definition: gltexture.h:62
void Bind(void)
Definition: gltexture.cpp:58
int angle
Definition: gltexture.h:69
int GetAngle(void) const
Definition: gltexture.h:64
long GetRotationAngle(void)
Definition: thumbview.cpp:81
void MakeQuad(float alpha=1.0f, float scale=1.0f)
Definition: gltexture.cpp:66
ThumbItem * item
Definition: gltexture.h:70
void SetScale(float x, float y)
Definition: gltexture.h:54
ScaleMax
Definition: galleryutil.h:28
static QSize ScaleToDest(const QSize &sz, const QSize &dest, ScaleMax scaleMax)
void Deinit(void)
Delete the texture.
Definition: gltexture.cpp:52
void SetSize(const QSize &sz)
Definition: gltexture.h:52
#define GL_RGBA
QString GetDescription(const QString &status) const
Definition: gltexture.cpp:111
QSize GetSize(void) const
Definition: gltexture.h:60
void ScaleTo(const QSize &dest, ScaleMax scaleMax)
Definition: gltexture.cpp:87
void SwapWidthHeight(void)
Definition: gltexture.h:47
void Init(const QImage &image)
Create the texture initialized with QImage.
Definition: gltexture.cpp:34