glVertex  5.5.2
glvertex_qt.h
Go to the documentation of this file.
1 // (c) by Stefan Roettger, licensed under MIT license
2 
5 #ifndef GLVERTEX_QT_H
6 #define GLVERTEX_QT_H
7 
8 #include <qglobal.h>
9 #include <qdebug.h>
10 
11 #ifdef Q_OS_ANDROID
12 # define LGL_USE_GLES
13 #endif
14 
15 #include "glvertex.h"
16 
17 #include <QString>
18 #include <QFile>
19 #include <QFileDialog>
20 #include <QImage>
21 
22 inline QString lglBrowseQtFile(QString title = "Browse file",
23  QString path = "",
24  bool newfile = false)
25 {
26  QString file;
27 
28  QFileDialog* fd = new QFileDialog(NULL, title);
29 
30  if (!newfile) fd->setFileMode(QFileDialog::ExistingFile);
31  else fd->setFileMode(QFileDialog::AnyFile);
32  fd->setViewMode(QFileDialog::List);
33  if (newfile) fd->setAcceptMode(QFileDialog::AcceptSave);
34 #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
35  fd->setFilter(QDir::Dirs|QDir::Files);
36  fd->setNameFilter("All Text Files (*.txt);;All Files (*)");
37 #else
38  fd->setFilter("All Text Files (*.txt);;All Files (*)");
39 #endif
40 
41  if (path!="") fd->setDirectory(path);
42 
43  if (fd->exec() == QDialog::Accepted)
44  {
45  QStringList selected = fd->selectedFiles();
46 
47  if (selected.size() > 0)
48  file = selected.at(0);
49  }
50 
51  delete fd;
52 
53  setlocale(LC_NUMERIC, "C");
54 
55  return(file);
56 }
57 
59 inline lglVBO *lglLoadObj(QString qrc, bool silent = false, lgl_texgenmode_enum texgen = LGL_TEXGEN_MIXED_HEDGEHOG, bool flip_v = true)
60 {
61  lglVBO *vbo = NULL;
62 
63  QFile file(qrc);
64  if (file.open(QIODevice::ReadOnly))
65  {
66  QByteArray a = file.readAll();
67  vbo = lglLoadObjData(QString(a).toStdString().c_str(), texgen, flip_v);
68  }
69  else
70  {
71  if (!silent) lglError("cannot open obj file");
72  }
73 
74  return(vbo);
75 }
76 
78 inline GLuint lglLoadQtTextureInto(QString qrc, int *iwidth, int *iheight, int *twidth, int *theight, bool mipmapping = true)
79 {
80  GLuint texid = 0;
81 
82  QImage image(qrc);
83 
84  if (!image.isNull())
85  {
86  *iwidth = *twidth = image.width();
87  *iheight = *theight = image.height();
88 
89  image = image.convertToFormat(QImage::Format_ARGB32);
90 
91  unsigned int size = 4*(*twidth)*(*theight);
92 
93  unsigned char *argb = image.bits();
94  unsigned char *rgba = new unsigned char[size];
95 
96  for (unsigned int i=0; i<size; i+=4)
97  {
98  rgba[i] = argb[i+2];
99  rgba[i+1] = argb[i+1];
100  rgba[i+2] = argb[i];
101  rgba[i+3] = argb[i+3];
102  }
103 
104  if (mipmapping)
105  texid = lglCreateMipmap2D(twidth, theight, LGL_RGBA, rgba);
106  else
107  texid = lglCreateTexmap2D(twidth, theight, LGL_RGBA, rgba);
108 
109  delete[] rgba;
110  }
111 
112  return(texid);
113 }
114 
116 inline GLuint lglLoadQtTexture(QString qrc, int *iwidth, int *iheight, int *twidth, int *theight, bool mipmapping = true, bool silent = false)
117 {
118  GLuint texid = lglLoadQtTextureInto(qrc, iwidth, iheight, twidth, theight, mipmapping);
119 
120  if (texid == 0) texid = lglLoadQtTextureInto(QString("../") + qrc, iwidth, iheight, twidth, theight, mipmapping);
121  if (texid == 0) texid = lglLoadQtTextureInto(QString("data/") + qrc, iwidth, iheight, twidth, theight, mipmapping);
122  if (texid == 0) texid = lglLoadQtTextureInto(QString("../data/") + qrc, iwidth, iheight, twidth, theight, mipmapping);
123 #ifndef _WIN32
124  if (texid == 0) texid = lglLoadQtTextureInto(QString("/usr/local/share/") + qrc, iwidth, iheight, twidth, theight, mipmapping);
125 #else
126  if (texid == 0) texid = lglLoadQtTextureInto(QString("C:\\glvertex\\") + qrc, iwidth, iheight, twidth, theight, mipmapping);
127 #endif
128 
129  if (texid == 0)
130  if (!silent) lglError("cannot open texture file");
131 
132  return(texid);
133 }
134 
136 inline GLuint lglLoadQtTexture(QString qrc, bool mipmapping = true, bool silent = false)
137 {
138  int iwidth, iheight, twidth, theight;
139  return(lglLoadQtTexture(qrc, &iwidth, &iheight, &twidth, &theight, mipmapping, silent));
140 }
141 
143 inline GLuint lglLoadQtTexture(QString qrc, int *iwidth, int *iheight, bool mipmapping = true, bool silent = false)
144 {
145  int twidth, theight;
146  return(lglLoadQtTexture(qrc, iwidth, iheight, &twidth, &theight, mipmapping, silent));
147 }
148 
150 inline lglVBO *lglLoadObjWithQtTexture(QString qrc, bool silent = false, lgl_texgenmode_enum texgen = LGL_TEXGEN_MIXED_HEDGEHOG, bool flip_v = true, bool mipmapping = true)
151 {
152  // load obj file
153  lglVBO *vbo = lglLoadObj(qrc, silent, texgen, flip_v);
154 
155  // load jpg file
156  std::string jpgname = lgl_string(qrc.toStdString()).chop(".obj").append(".jpg");
157  int jpgid = lglLoadQtTexture(jpgname.c_str(), mipmapping, true);
158  vbo->lglTexture2D(jpgid, true);
159 
160  // load png file
161  std::string pngname = lgl_string(qrc.toStdString()).chop(".obj").append(".png");
162  int pngid = lglLoadQtTexture(pngname.c_str(), mipmapping, silent);
163  vbo->lglTexture2D(pngid, true);
164 
165  return(vbo);
166 }
167 
168 #endif
lglCreateTexmap2D
GLuint lglCreateTexmap2D(int *width, int *height, lgl_texmap_type type, unsigned char *data)
create a 2D texture map
Definition: glvertex_texture.h:299
lgl_string::chop
lgl_string chop(const std::string &match) const
chop match from end
Definition: glvertex_string.h:82
lglLoadObjWithQtTexture
lglVBO * lglLoadObjWithQtTexture(QString qrc, bool silent=false, lgl_texgenmode_enum texgen=LGL_TEXGEN_MIXED_HEDGEHOG, bool flip_v=true, bool mipmapping=true)
load OBJ and JPG/PNG from file path (and from default installation paths)
Definition: glvertex_qt.h:150
glvertex.h
lgl_string
LGL string class that extends std::string.
Definition: glvertex_string.h:12
lglLoadQtTexture
GLuint lglLoadQtTexture(QString qrc, int *iwidth, int *iheight, int *twidth, int *theight, bool mipmapping=true, bool silent=false)
load qt image into texture object from qt resource path (and from default installation paths)
Definition: glvertex_qt.h:116
lglError
void lglError(std::string e)
generate an error message
Definition: glvertex_api.h:928
lglLoadQtTextureInto
GLuint lglLoadQtTextureInto(QString qrc, int *iwidth, int *iheight, int *twidth, int *theight, bool mipmapping=true)
load qt image into texture object from qt resource path
Definition: glvertex_qt.h:78
lgl::lglTexture2D
void lglTexture2D(GLuint texid, bool owner=false)
bind a 2D texture for the default GLSL program
Definition: glvertex_core.h:4266
lglVBO
LGL API: vbo class definition.
Definition: glvertex_core.h:5827
lglLoadObjData
bool lglLoadObjData(lglVBO *vbo, const char *data, lgl_texgenmode_enum texgen=LGL_TEXGEN_NONE, bool flip_v=true, const char *name="obj data")
load OBJ into vbo from memory chunk
Definition: glvertex_objformat.h:357
lglCreateMipmap2D
GLuint lglCreateMipmap2D(int *width, int *height, lgl_texmap_type type, unsigned char *data)
create a 2D texture mip-map
Definition: glvertex_texture.h:386
lgl_texgenmode_enum
lgl_texgenmode_enum
texgen mode enum
Definition: glvertex_core.h:56
lglLoadObj
lglVBO * lglLoadObj(QString qrc, bool silent=false, lgl_texgenmode_enum texgen=LGL_TEXGEN_MIXED_HEDGEHOG, bool flip_v=true)
load OBJ into vbo from qt resource path
Definition: glvertex_qt.h:59