glVertex  5.5.2
glvertex_texture.h
Go to the documentation of this file.
1 // (c) by Stefan Roettger, licensed under MIT license
2 
5 #ifndef GLVERTEX_TEXTURE_H
6 #define GLVERTEX_TEXTURE_H
7 
8 #include "glvertex_core.h"
9 #include "glvertex_texture_gl.h"
10 
13  LGL_RGB,
14  LGL_RGBA,
15  LGL_INTENSITY,
16  LGL_LUMINANCE,
17  LGL_LUMINANCE_ALPHA
18 };
19 
20 inline vec4f lglTexmapGet(int x, int y,
21  int width, int height,
22  lgl_texmap_type type, unsigned char *data)
23 {
24  vec4f value(0);
25 
26  if (x>=0 && x<width && y>=0 && y<height)
27  {
28  int components = 0;
29 
30  switch (type)
31  {
32  case LGL_RGB: components = 3; break;
33  case LGL_RGBA: components = 4; break;
34  case LGL_INTENSITY: case LGL_LUMINANCE: components = 1; break;
35  case LGL_LUMINANCE_ALPHA: components = 2; break;
36  }
37 
38  int offset = (x+y*width)*components;
39 
40  if (components > 0) value.r = data[offset]*1.0f/255;
41  if (components > 1) value.g = data[offset+1]*1.0f/255;
42  if (components > 2) value.b = data[offset+2]*1.0f/255;
43  if (components > 3) value.a = data[offset+3]*1.0f/255;
44  }
45 
46  return(value);
47 }
48 
49 inline void lglTexmapSet(int x, int y, vec4f value,
50  int width, int height,
51  lgl_texmap_type type, unsigned char *data)
52 {
53  if (x>=0 && x<width && y>=0 && y<height)
54  {
55  int components = 0;
56 
57  switch (type)
58  {
59  case LGL_RGB: components = 3; break;
60  case LGL_RGBA: components = 4; break;
61  case LGL_INTENSITY: case LGL_LUMINANCE: components = 1; break;
62  case LGL_LUMINANCE_ALPHA: components = 2; break;
63  }
64 
65  if (value.r < 0) value.r = 0;
66  else if (value.r > 1) value.r = 1;
67 
68  if (value.g < 0) value.g = 0;
69  else if (value.g > 1) value.g = 1;
70 
71  if (value.b < 0) value.b = 0;
72  else if (value.b > 1) value.b = 1;
73 
74  if (value.a < 0) value.a = 0;
75  else if (value.a > 1) value.a = 1;
76 
77  int offset = (x+y*width)*components;
78 
79  if (components > 0) data[offset] = (unsigned char)(value.r*255+0.5f);
80  if (components > 1) data[offset+1] = (unsigned char)(value.g*255+0.5f);
81  if (components > 2) data[offset+2] = (unsigned char)(value.b*255+0.5f);
82  if (components > 3) data[offset+3] = (unsigned char)(value.a*255+0.5f);
83  }
84 }
85 
86 inline vec4f lglSampleTexmap(float s, float t,
87  int width, int height,
88  lgl_texmap_type type, unsigned char *data)
89 {
90  int x, y;
91  float u, v;
92 
93  vec4f value(0);
94 
95  if (width<1 || height<1)
96  {
97  lglError("invalid 2D texture size");
98  return(0);
99  }
100 
101  if (s < 0) s = 0;
102  else if (s > 1) s = 1;
103 
104  if (t < 0) t = 0;
105  else if (t > 1) t = 1;
106 
107  s *= width-1;
108  t *= height-1;
109 
110  x = (int)floor(s);
111  y = (int)floor(t);
112 
113  u = s - x;
114  v = t - y;
115 
116  if (width>1 && height>1)
117  {
118  if (x == width-1)
119  {
120  x = width-2;
121  u = 1.0f;
122  }
123 
124  if (y == height-1)
125  {
126  y = height-2;
127  v = 1.0f;
128  }
129 
130  vec4f v1 = lglTexmapGet(x,y, width,height, type,data);
131  vec4f v2 = lglTexmapGet(x+1,y, width,height, type,data);
132  vec4f v3 = lglTexmapGet(x,y+1, width,height, type,data);
133  vec4f v4 = lglTexmapGet(x+1,y+1, width,height, type,data);
134 
135  value = (1-v)*((1-u)*v1+u*v2)+v*((1-u)*v3+u*v4);
136  }
137  else if (width>1)
138  {
139  if (x == width-1)
140  {
141  x = width-2;
142  u = 1.0f;
143  }
144 
145  vec4f v1 = lglTexmapGet(x,y, width,height, type,data);
146  vec4f v2 = lglTexmapGet(x+1,y, width,height, type,data);
147 
148  value = (1-u)*v1+u*v2;
149  }
150  else
151  {
152  if (y == height-1)
153  {
154  y = height-2;
155  v = 1.0f;
156  }
157 
158  vec4f v1 = lglTexmapGet(x,y, width,height, type,data);
159  vec4f v2 = lglTexmapGet(x,y+1, width,height, type,data);
160 
161  value = (1-v)*v1+v*v2;
162  }
163 
164  return(value);
165 }
166 
167 inline unsigned char *lglScaleTexmap(int inwidth, int inheight,
168  int outwidth, int outheight,
169  lgl_texmap_type type, unsigned char *data)
170 {
171  if (inwidth<1 || inheight<1 || outwidth<1 || outheight<1)
172  {
173  lglError("invalid 2D texture size");
174  return(NULL);
175  }
176 
177  int components = 0;
178 
179  switch (type)
180  {
181  case LGL_RGB: components = 3; break;
182  case LGL_RGBA: components = 4; break;
183  case LGL_INTENSITY: case LGL_LUMINANCE: components = 1; break;
184  case LGL_LUMINANCE_ALPHA: components = 2; break;
185  }
186 
187  unsigned char *data2 = (unsigned char *)malloc(outwidth*outheight*components);
188 
189  if (!data2)
190  {
191  lglError("insufficient memory");
192  return(NULL);
193  }
194 
195  for (int x=0; x<outwidth; x++)
196  for (int y=0; y<outheight; y++)
197  {
198  float s = (float)x/(outwidth-1);
199  float t = (float)y/(outheight-1);
200 
201  vec4f value = lglSampleTexmap(s,t, inwidth,inheight, type,data);
202  lglTexmapSet(x,y, value, outwidth,outheight, type,data2);
203  }
204 
205  return(data2);
206 }
207 
208 inline unsigned char *lglScaleTexmap2POT(int *width, int *height,
209  lgl_texmap_type type, unsigned char *data)
210 {
211  if (*width<1 || *height<1)
212  {
213  lglError("invalid 2D texture size");
214  return(NULL);
215  }
216 
217  int width2, height2;
218 
219  for (width2=2; width2<*width; width2*=2) ;
220  for (height2=2; height2<*height; height2*=2) ;
221 
222  if (width2==*width && height2==*height)
223  return(data);
224  else
225  {
226  unsigned char *data2 = lglScaleTexmap(*width,*height, width2,height2, type,data);
227 
228  *width = width2;
229  *height = height2;
230 
231  return(data2);
232  }
233 }
234 
235 inline unsigned char *lglDownsizeTexmap(int *width, int *height,
236  lgl_texmap_type type, unsigned char *data)
237 {
238  if (*width<2 && *height<2)
239  {
240  lglError("invalid 2D texture size");
241  return(NULL);
242  }
243 
244  int components = 0;
245 
246  switch (type)
247  {
248  case LGL_RGB: components = 3; break;
249  case LGL_RGBA: components = 4; break;
250  case LGL_INTENSITY: case LGL_LUMINANCE: components = 1; break;
251  case LGL_LUMINANCE_ALPHA: components = 2; break;
252  }
253 
254  int width2 = *width;
255  int height2 = *height;
256 
257  if (width2&1) width2++;
258  if (height2&1) height2++;
259 
260  unsigned char *data2 = data;
261 
262  if (width2!=*width || height2!=*height)
263  data2 = lglScaleTexmap(*width,*height, width2,height2, type,data);
264 
265  *width = width2/2;
266  *height = height2/2;
267 
268  unsigned char *down = (unsigned char *)malloc((*width)*(*height)*components);
269 
270  if (!down)
271  {
272  lglError("insufficient memory");
273  return(NULL);
274  }
275 
276  for (int x=0; x<*width; x++)
277  for (int y=0; y<*height; y++)
278  {
279  int x2 = 2*x;
280  int y2 = 2*y;
281 
282  vec4f v1 = lglTexmapGet(x2,y2, width2,height2, type,data2);
283  vec4f v2 = lglTexmapGet(x2+1,y2, width2,height2, type,data2);
284  vec4f v3 = lglTexmapGet(x2,y2+1, width2,height2, type,data2);
285  vec4f v4 = lglTexmapGet(x2+1,y2+1, width2,height2, type,data2);
286 
287  vec4f value = 0.25f*(v1+v2+v3+v4);
288 
289  lglTexmapSet(x,y, value, *width,*height, type,down);
290  }
291 
292  if (data2 != data)
293  free(data2);
294 
295  return(down);
296 }
297 
299 inline GLuint lglCreateTexmap2D(int *width, int *height,
300  lgl_texmap_type type, unsigned char *data)
301 {
302  if (*width<1 || *height<1)
303  {
304  lglError("invalid 2D texture size");
305  return(0);
306  }
307 
308  GLuint texid;
309 
310  GLenum gl_type = GL_RGB;
311 
312  switch (type)
313  {
314  case LGL_RGB: gl_type = GL_RGB; break;
315  case LGL_RGBA: gl_type = GL_RGBA; break;
316 #ifdef GLVERTEX_TEXTURE_SWIZZLE
317  case LGL_INTENSITY: gl_type = GL_RED; break;
318 #else
319  case LGL_INTENSITY: gl_type = GL_INTENSITY; break;
320 #endif
321 #ifdef GLVERTEX_TEXTURE_SWIZZLE
322  case LGL_LUMINANCE: gl_type = GL_RED; break;
323 #else
324  case LGL_LUMINANCE: gl_type = GL_LUMINANCE; break;
325 #endif
326 #ifdef GLVERTEX_TEXTURE_SWIZZLE
327  case LGL_LUMINANCE_ALPHA: gl_type = GL_RG; break;
328 #else
329  case LGL_LUMINANCE_ALPHA: gl_type = GL_LUMINANCE_ALPHA; break;
330 #endif
331  }
332 
333  unsigned char *data2 = lglScaleTexmap2POT(width,height, type,data);
334 
335  if (!data2)
336  {
337  lglError("insufficient memory");
338  return(0);
339  }
340 
341  glGenTextures(1, &texid);
342  glBindTexture(GL_TEXTURE_2D, texid);
343 
344  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
345  lglTexImage2D(GL_TEXTURE_2D, 0, gl_type, *width, *height, 0, gl_type==GL_INTENSITY?GL_LUMINANCE:gl_type, GL_UNSIGNED_BYTE, data2);
346 
347  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
348  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
349 
350  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
351  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
352 
353 #ifdef GLVERTEX_TEXTURE_SWIZZLE
354  if (gl_type == GL_RED)
355  {
356  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_RED);
357  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_RED);
358  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
359  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, type==LGL_LUMINANCE?GL_ONE:GL_RED);
360  }
361  else if (gl_type == GL_RG)
362  {
363  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_RED);
364  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_RED);
365  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
366  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_GREEN);
367  }
368 #endif
369 
370  glBindTexture(GL_TEXTURE_2D, 0);
371 
372  if (data2 != data)
373  free(data2);
374 
375  return(texid);
376 }
377 
379 inline GLuint lglCreateTexmap2D(int width, int height,
380  lgl_texmap_type type, unsigned char *data)
381 {
382  return(lglCreateTexmap2D(&width, &height, type, data));
383 }
384 
386 inline GLuint lglCreateMipmap2D(int *width, int *height,
387  lgl_texmap_type type, unsigned char *data)
388 {
389  if (*width<1 || *height<1)
390  {
391  lglError("invalid 2D texture size");
392  return(0);
393  }
394 
395  GLuint texid;
396 
397  GLenum gl_type = GL_RGB;
398 
399  switch (type)
400  {
401  case LGL_RGB: gl_type = GL_RGB; break;
402  case LGL_RGBA: gl_type = GL_RGBA; break;
403 #ifdef GLVERTEX_TEXTURE_SWIZZLE
404  case LGL_INTENSITY: gl_type = GL_RED; break;
405 #else
406  case LGL_INTENSITY: gl_type = GL_INTENSITY; break;
407 #endif
408 #ifdef GLVERTEX_TEXTURE_SWIZZLE
409  case LGL_LUMINANCE: gl_type = GL_RED; break;
410 #else
411  case LGL_LUMINANCE: gl_type = GL_LUMINANCE; break;
412 #endif
413 #ifdef GLVERTEX_TEXTURE_SWIZZLE
414  case LGL_LUMINANCE_ALPHA: gl_type = GL_RG; break;
415 #else
416  case LGL_LUMINANCE_ALPHA: gl_type = GL_LUMINANCE_ALPHA; break;
417 #endif
418  }
419 
420  unsigned char *data2 = lglScaleTexmap2POT(width,height, type,data);
421 
422  if (!data2)
423  {
424  lglError("insufficient memory");
425  return(0);
426  }
427 
428  glGenTextures(1, &texid);
429  glBindTexture(GL_TEXTURE_2D, texid);
430 
431  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
432  lglTexImage2D(GL_TEXTURE_2D, 0, gl_type, *width, *height, 0, gl_type==GL_INTENSITY?GL_LUMINANCE:gl_type, GL_UNSIGNED_BYTE, data2);
433 
434  int width2 = *width;
435  int height2 = *height;
436 
437  unsigned char *m1 = data2;
438  unsigned char *m2 = NULL;
439 
440  for (int level=1; width2>1 || height2>1; level++)
441  {
442  m2 = lglDownsizeTexmap(&width2,&height2, type,m1);
443 
444  if (!m2)
445  {
446  lglError("insufficient memory");
447  break;
448  }
449 
450  if (level > 1)
451  free(m1);
452 
453  lglTexImage2D(GL_TEXTURE_2D, level, gl_type, width2, height2, 0, gl_type==GL_INTENSITY?GL_LUMINANCE:gl_type, GL_UNSIGNED_BYTE, m2);
454 
455  m1 = m2;
456  }
457 
458  if (m2)
459  free(m2);
460 
461  if (data2 != data)
462  free(data2);
463 
464  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
465  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
466 
467  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
468  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
469 
470 #ifdef GLVERTEX_TEXTURE_SWIZZLE
471  if (gl_type == GL_RED)
472  {
473  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_RED);
474  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_RED);
475  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
476  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, type==LGL_LUMINANCE?GL_ONE:GL_RED);
477  }
478  else if (gl_type == GL_RG)
479  {
480  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_RED);
481  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_RED);
482  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
483  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_GREEN);
484  }
485 #endif
486 
487  glBindTexture(GL_TEXTURE_2D, 0);
488 
489  return(texid);
490 }
491 
493 inline GLuint lglCreateMipmap2D(int width, int height,
494  lgl_texmap_type type, unsigned char *data)
495 {
496  return(lglCreateMipmap2D(&width, &height, type, data));
497 }
498 
500 inline GLuint lglCreateTexmap3D(int width, int height, int depth,
501  lgl_texmap_type type, unsigned char *data)
502 {
503 #if !defined(LGL_GLES) || defined (LGL_GLES3)
504 
505  if (width<1 || height<1 || depth<1)
506  {
507  lglError("invalid 3D texture size");
508  return(0);
509  }
510 
511  if ((width&(width-1))!=0) lglWarning("3D texture map width not a power of two");
512  if ((height&(height-1))!=0) lglWarning("3D texture map height not a power of two");
513  if ((depth&(depth-1))!=0) lglWarning("3D texture map depth not a power of two");
514 
515  GLuint texid;
516 
517  GLenum gl_type = GL_RGB;
518 
519  switch (type)
520  {
521  case LGL_RGB: gl_type = GL_RGB; break;
522  case LGL_RGBA: gl_type = GL_RGBA; break;
523 #ifdef GLVERTEX_TEXTURE_SWIZZLE
524  case LGL_INTENSITY: gl_type = GL_RED; break;
525 #else
526  case LGL_INTENSITY: gl_type = GL_INTENSITY; break;
527 #endif
528 #ifdef GLVERTEX_TEXTURE_SWIZZLE
529  case LGL_LUMINANCE: gl_type = GL_RED; break;
530 #else
531  case LGL_LUMINANCE: gl_type = GL_LUMINANCE; break;
532 #endif
533 #ifdef GLVERTEX_TEXTURE_SWIZZLE
534  case LGL_LUMINANCE_ALPHA: gl_type = GL_RG; break;
535 #else
536  case LGL_LUMINANCE_ALPHA: gl_type = GL_LUMINANCE_ALPHA; break;
537 #endif
538  }
539 
540  glGenTextures(1, &texid);
541  glBindTexture(GL_TEXTURE_3D, texid);
542 
543  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
544  lglTexImage3D(GL_TEXTURE_3D, 0, gl_type, width, height, depth, 0, gl_type==GL_INTENSITY?GL_LUMINANCE:gl_type, GL_UNSIGNED_BYTE, data);
545 
546  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
547  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
548 
549  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
550  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
551  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
552 
553 #ifdef GLVERTEX_TEXTURE_SWIZZLE
554  if (gl_type == GL_RED)
555  {
556  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_SWIZZLE_R, GL_RED);
557  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_SWIZZLE_G, GL_RED);
558  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_SWIZZLE_B, GL_RED);
559  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_SWIZZLE_A, type==LGL_LUMINANCE?GL_ONE:GL_RED);
560  }
561  else if (gl_type == GL_RG)
562  {
563  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_SWIZZLE_R, GL_RED);
564  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_SWIZZLE_G, GL_RED);
565  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_SWIZZLE_B, GL_RED);
566  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_SWIZZLE_A, GL_GREEN);
567  }
568 #endif
569 
570  glBindTexture(GL_TEXTURE_3D, 0);
571 
572  return(texid);
573 
574 #else
575 
576  return(0);
577 
578 #endif
579 }
580 
582 inline GLuint lglCreateNoiseTexmap3D(int width, int height, int depth)
583 {
584 #if !defined(LGL_GLES) || defined (LGL_GLES3)
585 
586  if (width<1 || height<1 || depth<1)
587  {
588  lglError("invalid noise texture size");
589  return(0);
590  }
591 
592  GLuint texid;
593 
594  float *data = glslnoise::perlinnoise::noise(width, height, depth);
595 
596  if (!data)
597  {
598  lglError("invalid noise texture");
599  return(0);
600  }
601 
602  glGenTextures(1, &texid);
603  glBindTexture(GL_TEXTURE_3D, texid);
604 
605  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
606 #ifdef GLVERTEX_TEXTURE_SWIZZLE
607  lglTexImage3D(GL_TEXTURE_3D, 0, GL_RED, width, height, depth, 0, GL_RED, GL_FLOAT, data);
608 #else
609  lglTexImage3D(GL_TEXTURE_3D, 0, GL_INTENSITY, width, height, depth, 0, GL_LUMINANCE, GL_FLOAT, data);
610 #endif
611 
612  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
613  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
614 
615  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
616  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
617  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
618 
619 #ifdef GLVERTEX_TEXTURE_SWIZZLE
620  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_SWIZZLE_R, GL_RED);
621  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_SWIZZLE_G, GL_RED);
622  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_SWIZZLE_B, GL_RED);
623  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_SWIZZLE_A, GL_RED);
624 #endif
625 
626  glBindTexture(GL_TEXTURE_3D, 0);
627 
628  free(data);
629 
630  return(texid);
631 
632 #else
633 
634  return(0);
635 
636 #endif
637 }
638 
640 inline void lglDeleteTexture(GLuint texid)
641 {
642  GLuint GLtexid = texid;
643  if (texid > 0) glDeleteTextures(1, &GLtexid);
644 }
645 
647 inline void lglTexMagFilter2D(GLuint texid, bool linear = true)
648 {
649  glBindTexture(GL_TEXTURE_2D, texid);
650  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, linear?GL_LINEAR:GL_NEAREST);
651  glBindTexture(GL_TEXTURE_2D, 0);
652 }
653 
655 inline void lglTexMagFilter3D(GLuint texid, bool linear = true)
656 {
657 #if !defined(LGL_GLES) || defined (LGL_GLES3)
658 
659  glBindTexture(GL_TEXTURE_3D, texid);
660  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, linear?GL_LINEAR:GL_NEAREST);
661  glBindTexture(GL_TEXTURE_3D, 0);
662 
663 #endif
664 }
665 
667 inline void lglTexMinFilter2D(GLuint texid, bool linear = true, bool mipmap = false)
668 {
669  glBindTexture(GL_TEXTURE_2D, texid);
670  GLuint texmap_filter = linear?GL_LINEAR:GL_NEAREST;
671  GLuint mipmap_filter = linear?GL_LINEAR_MIPMAP_LINEAR:GL_NEAREST_MIPMAP_NEAREST;
672  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mipmap?mipmap_filter:texmap_filter);
673  glBindTexture(GL_TEXTURE_2D, 0);
674 }
675 
677 inline void lglTexMinFilter3D(GLuint texid, bool linear = true, bool mipmap = false)
678 {
679 #if !defined(LGL_GLES) || defined (LGL_GLES3)
680 
681  glBindTexture(GL_TEXTURE_3D, texid);
682  GLuint texmap_filter = linear?GL_LINEAR:GL_NEAREST;
683  GLuint mipmap_filter = linear?GL_LINEAR_MIPMAP_LINEAR:GL_NEAREST_MIPMAP_NEAREST;
684  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, mipmap?mipmap_filter:texmap_filter);
685  glBindTexture(GL_TEXTURE_3D, 0);
686 
687 #endif
688 }
689 
691 inline void lglTexWrap2D(GLuint texid, bool repeat = true)
692 {
693  glBindTexture(GL_TEXTURE_2D, texid);
694 
695 #ifndef GL_CLAMP_TO_EDGE
696  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, repeat?GL_REPEAT:GL_CLAMP);
697  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, repeat?GL_REPEAT:GL_CLAMP);
698 #else
699  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, repeat?GL_REPEAT:GL_CLAMP_TO_EDGE);
700  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, repeat?GL_REPEAT:GL_CLAMP_TO_EDGE);
701 #endif
702 
703  glBindTexture(GL_TEXTURE_2D, texid);
704 }
705 
707 inline void lglTexWrap3D(GLuint texid, bool repeat = true)
708 {
709 #if !defined(LGL_GLES) || defined (LGL_GLES3)
710 
711  glBindTexture(GL_TEXTURE_3D, texid);
712 
713 #ifndef GL_CLAMP_TO_EDGE
714  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, repeat?GL_REPEAT:GL_CLAMP);
715  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, repeat?GL_REPEAT:GL_CLAMP);
716  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, repeat?GL_REPEAT:GL_CLAMP);
717 #else
718  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, repeat?GL_REPEAT:GL_CLAMP_TO_EDGE);
719  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, repeat?GL_REPEAT:GL_CLAMP_TO_EDGE);
720  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, repeat?GL_REPEAT:GL_CLAMP_TO_EDGE);
721 #endif
722 
723  glBindTexture(GL_TEXTURE_3D, texid);
724 
725 #endif
726 }
727 
729 inline void lglTexFilterAnisotropic2D(GLuint texid, bool ansiotropic = true)
730 {
731 #ifdef GL_EXT_texture_filter_anisotropic
732 
733  glBindTexture(GL_TEXTURE_2D, texid);
734 
735  GLfloat maxaniso=1.0f;
736  if (ansiotropic) glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxaniso);
737  glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, &maxaniso);
738 
739  glBindTexture(GL_TEXTURE_2D, texid);
740 
741 #endif
742 }
743 
745 inline void lglTexFilterAnisotropic3D(GLuint texid, bool ansiotropic = true)
746 {
747 #if !defined(LGL_GLES) || defined (LGL_GLES3)
748 
749 #ifdef GL_EXT_texture_filter_anisotropic
750 
751  glBindTexture(GL_TEXTURE_3D, texid);
752 
753  GLfloat maxaniso=1.0f;
754  if (ansiotropic) glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxaniso);
755  glTexParameterfv(GL_TEXTURE_3D, GL_TEXTURE_MAX_ANISOTROPY_EXT, &maxaniso);
756 
757  glBindTexture(GL_TEXTURE_3D, texid);
758 
759 #endif
760 
761 #endif
762 }
763 
765 inline int lglGetMaxTexSize()
766 {
767  GLint param;
768  glGetIntegerv(GL_MAX_TEXTURE_SIZE, &param);
769  return(param);
770 }
771 
772 #endif
lglWarning
void lglWarning(std::string w)
generate a warning message
Definition: glvertex_api.h:932
lglCreateTexmap2D
GLuint lglCreateTexmap2D(int *width, int *height, lgl_texmap_type type, unsigned char *data)
create a 2D texture map
Definition: glvertex_texture.h:299
lglTexWrap2D
void lglTexWrap2D(GLuint texid, bool repeat=true)
specify the 2D texture wrap type
Definition: glvertex_texture.h:691
lglDeleteTexture
void lglDeleteTexture(GLuint texid)
delete a 2D or 3D texture map
Definition: glvertex_texture.h:640
lglCreateTexmap3D
GLuint lglCreateTexmap3D(int width, int height, int depth, lgl_texmap_type type, unsigned char *data)
create a 3D texture map
Definition: glvertex_texture.h:500
lglTexWrap3D
void lglTexWrap3D(GLuint texid, bool repeat=true)
specify the 3D texture wrap type
Definition: glvertex_texture.h:707
lglTexFilterAnisotropic3D
void lglTexFilterAnisotropic3D(GLuint texid, bool ansiotropic=true)
specify the 3D texture anisotropic filter type
Definition: glvertex_texture.h:745
lglError
void lglError(std::string e)
generate an error message
Definition: glvertex_api.h:928
glvertex_core.h
vec4f
4D float vector
Definition: glslmath.h:961
lglTexMinFilter3D
void lglTexMinFilter3D(GLuint texid, bool linear=true, bool mipmap=false)
specify the 3D texture minification filter type
Definition: glvertex_texture.h:677
lglCreateNoiseTexmap3D
GLuint lglCreateNoiseTexmap3D(int width, int height, int depth)
create a 3D noise texture
Definition: glvertex_texture.h:582
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
lglTexMinFilter2D
void lglTexMinFilter2D(GLuint texid, bool linear=true, bool mipmap=false)
specify the 2D texture minification filter type
Definition: glvertex_texture.h:667
lgl_texmap_type
lgl_texmap_type
texture type
Definition: glvertex_texture.h:12
lglTexMagFilter3D
void lglTexMagFilter3D(GLuint texid, bool linear=true)
specify the 3D texture magnification filter type
Definition: glvertex_texture.h:655
lglGetMaxTexSize
int lglGetMaxTexSize()
get the maximum texture size
Definition: glvertex_texture.h:765
lglTexMagFilter2D
void lglTexMagFilter2D(GLuint texid, bool linear=true)
specify the 2D texture magnification filter type
Definition: glvertex_texture.h:647
lglTexFilterAnisotropic2D
void lglTexFilterAnisotropic2D(GLuint texid, bool ansiotropic=true)
specify the 2D texture anisotropic filter type
Definition: glvertex_texture.h:729