OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Texture.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2014-2017, ARM Limited and Contributors
2  *
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge,
6  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 #include "Texture.h"
22 
23 #include <GLES2/gl2ext.h>
24 #include <cstdio>
25 #include <cstdlib>
26 #include <android/log.h>
27 
28 #define CHANNELS_PER_PIXEL 3
29 
30 #define LOG_TAG "libNative"
31 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
32 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
33 
34 /* [loadTexture] */
35 void loadTexture( const char * texture, unsigned int level, unsigned int width, unsigned int height)
36 {
37  GLubyte * theTexture;
38  theTexture = (GLubyte *)malloc(sizeof(GLubyte) * width * height * CHANNELS_PER_PIXEL);
39 
40  FILE * theFile = fopen(texture, "r");
41 
42  if(theFile == NULL)
43  {
44  LOGE("Failure to load the texture");
45  return;
46  }
47 
48  fread(theTexture, width * height * CHANNELS_PER_PIXEL, 1, theFile);
49 
50  /* Load the texture. */
51  glTexImage2D(GL_TEXTURE_2D, level, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, theTexture);
52 
53  /* Set the filtering mode. */
54  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
55  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST_MIPMAP_NEAREST);
56 
57  free(theTexture);
58 }
59 /* [loadTexture] */
60 
61 /* [loadCompressedTexture] */
62 void loadCompressedTexture( const char * texture, unsigned int level)
63 {
64  GLushort paddedWidth;
65  GLushort paddedHeight;
66  GLushort width;
67  GLushort height;
68  GLubyte textureHead[16];
69  GLubyte * theTexture;
70 
71  FILE * theFile = fopen(texture, "rb");
72 
73  if(theFile == NULL)
74  {
75  LOGE("Failure to load the texture");
76  return;
77  }
78 
79  fread(textureHead, 16, 1, theFile);
80 
81  paddedWidth = (textureHead[8] << 8) | textureHead[9];
82  paddedHeight = (textureHead[10] << 8) | textureHead[11];
83  width = (textureHead[12] << 8) | textureHead[13];
84  height = (textureHead[14] << 8) | textureHead[15];
85 
86  theTexture = (GLubyte *)malloc(sizeof(GLubyte) * ((paddedWidth * paddedHeight) >> 1));
87  fread(theTexture, (paddedWidth * paddedHeight) >> 1, 1, theFile);
88 
89  /* Load the texture. */
90  glCompressedTexImage2D(GL_TEXTURE_2D, level, GL_ETC1_RGB8_OES, width, height, 0, (paddedWidth * paddedHeight) >> 1, theTexture);
91 
92  /* Set the filtering mode. */
93  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
94  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST_MIPMAP_NEAREST);
95 
96  free(theTexture);
97  fclose(theFile);
98 }
99 /* [loadCompressedTexture] */
GLint GLsizei GLsizei height
Definition: gl2ext.h:179
void loadTexture(const char *texture, unsigned int level, unsigned int width, unsigned int height)
Loads a desired texture into memory at an appropriate mipmap level.
Definition: Texture.cpp:35
void loadCompressedTexture(const char *texture, unsigned int level)
Loads a compressed texture into memory at an appropriate mipmap level.
Definition: Texture.cpp:62
GLubyte * theTexture
Definition: Texture.cpp:36
#define CHANNELS_PER_PIXEL
Definition: Texture.cpp:28
GLenum GLuint texture
Definition: gl2ext.h:385
#define LOGE(...)
Definition: Texture.cpp:32
#define GL_ETC1_RGB8_OES
Definition: gl2ext.h:257
GLenum GLuint GLint level
Definition: gl2ext.h:385
GLint GLsizei width
Definition: gl2ext.h:179