summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Rodin <mrodin@de.adit-jv.com>2018-11-15 09:16:45 +0100
committerMichael Rodin <mrodin@de.adit-jv.com>2018-11-27 16:51:25 +0100
commitc2fca1e3e7e554826096fd179c1e5c72d403edb0 (patch)
tree6eb56894b6ac9eb9bf8279f2eab148182b84f6b6
parent74e7682c720555178bdd5317b71d5a3c43edf310 (diff)
downloadwayland-ivi-extension-c2fca1e3e7e554826096fd179c1e5c72d403edb0.tar.gz
EGLWLMockNavigation: add a texture loader
Add the class TextureLoader for loading textures in different formats. TextureLoader can currently load textures in two formats: BMP and byte array. Also update CMakeLists.txt to include: include/TextureLoader.h src/TextureLoader.cpp Signed-off-by: Michael Rodin <mrodin@de.adit-jv.com>
-rw-r--r--ivi-layermanagement-examples/EGLWLMockNavigation/CMakeLists.txt3
-rw-r--r--ivi-layermanagement-examples/EGLWLMockNavigation/include/TextureLoader.h44
-rw-r--r--ivi-layermanagement-examples/EGLWLMockNavigation/src/TextureLoader.cpp133
3 files changed, 180 insertions, 0 deletions
diff --git a/ivi-layermanagement-examples/EGLWLMockNavigation/CMakeLists.txt b/ivi-layermanagement-examples/EGLWLMockNavigation/CMakeLists.txt
index 91c9237..5b92848 100644
--- a/ivi-layermanagement-examples/EGLWLMockNavigation/CMakeLists.txt
+++ b/ivi-layermanagement-examples/EGLWLMockNavigation/CMakeLists.txt
@@ -2,6 +2,7 @@
#
# Copyright 2010-2014 BMW Car IT GmbH
# Copyright (C) 2011 DENSO CORPORATION and Robert Bosch Car Multimedia Gmbh
+# Copyright (C) 2018 Advanced Driver Information Technology Joint Venture GmbH
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -74,6 +75,7 @@ set (HEADER_FILES
include/Street.h
include/ShaderBase.h
include/ShaderLighting.h
+ include/TextureLoader.h
)
set (SRC_FILES
@@ -88,6 +90,7 @@ set (SRC_FILES
src/ShaderBase.cpp
src/ShaderLighting.cpp
src/main.cpp
+ src/TextureLoader.cpp
)
add_executable(${PROJECT_NAME}
diff --git a/ivi-layermanagement-examples/EGLWLMockNavigation/include/TextureLoader.h b/ivi-layermanagement-examples/EGLWLMockNavigation/include/TextureLoader.h
new file mode 100644
index 0000000..8a1dfdf
--- /dev/null
+++ b/ivi-layermanagement-examples/EGLWLMockNavigation/include/TextureLoader.h
@@ -0,0 +1,44 @@
+/***************************************************************************
+ *
+ * Copyright (C) 2018 Advanced Driver Information Technology Joint Venture GmbH
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ****************************************************************************/
+#ifndef _TEXTURE_LOADER_H
+#define _TEXTURE_LOADER_H
+
+#include <string.h>
+
+#include <iostream>
+using std::cout;
+using std::endl;
+
+#include <GLES2/gl2.h>
+
+class TextureLoader
+{
+public:
+ TextureLoader();
+
+ bool loadBMP(const char * imagePath);
+ bool loadArray(void * data, unsigned int width, unsigned int height, unsigned int pixelSizeBits);
+ GLuint getId();
+
+private:
+ GLuint textureID;
+ bool loaded = false;
+};
+
+#endif
diff --git a/ivi-layermanagement-examples/EGLWLMockNavigation/src/TextureLoader.cpp b/ivi-layermanagement-examples/EGLWLMockNavigation/src/TextureLoader.cpp
new file mode 100644
index 0000000..30c170f
--- /dev/null
+++ b/ivi-layermanagement-examples/EGLWLMockNavigation/src/TextureLoader.cpp
@@ -0,0 +1,133 @@
+/***************************************************************************
+ *
+ * Copyright (C) 2018 Advanced Driver Information Technology Joint Venture GmbH
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ****************************************************************************/
+#include "TextureLoader.h"
+
+TextureLoader::TextureLoader(){
+ ;
+}
+
+// source: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-5-a-textured-cube/
+bool TextureLoader::loadBMP(const char * imagePath) {
+ // Data read from the header of the BMP file
+ const unsigned char headerSize = 54; // Each BMP file begins by a 54-bytes header
+ unsigned char header[headerSize];
+ GLsizei width, height;
+ uint16_t pixelSizeBits;
+ uint8_t pixelSizeBytes;
+ // imageSize == width*height*pixelSize, because each pixel consists of pixelSize bytes (red, green and blue)
+ unsigned int imageSize;
+ unsigned char * data;
+ FILE * file = fopen(imagePath,"rb");
+ if (!file) {
+ //cout << "Image file could not be opened: " << imagePath << endl;
+ return false;
+ }
+ if (fread(header, 1, headerSize, file) != headerSize) {
+ cout << "Not a correct BMP file (could not read the header): " << imagePath << endl;
+ fclose(file);
+ return false;
+ }
+ if (header[0]!='B' || header[1]!='M' ) {
+ cout << "Not a correct BMP file (wrong header format) :" << imagePath << endl;
+ fclose(file);
+ return false;
+ }
+ // Read fields from the BMP file header
+ imageSize = *(int*)(header + 0x22);
+ width = *(GLsizei*)(header + 0x12);
+ height = *(GLsizei*)(header + 0x16);
+ pixelSizeBits = *(uint16_t*)(header + 0x1C);
+ pixelSizeBytes = pixelSizeBits / 8;
+
+ if ((pixelSizeBits != 24) and (pixelSizeBits != 32)) {
+ cout << "unsupported pixel size of " << pixelSizeBits << " bits" << endl;
+ fclose(file);
+ return false;
+ }
+
+ // calculate the image size if there is no information in the header
+ if (imageSize==0) {
+ imageSize=width*height*pixelSizeBytes;
+ }
+
+ data = new unsigned char [imageSize];
+ fread(data,1,imageSize,file);
+ fclose(file);
+
+ if (pixelSizeBits == 32) {
+ // BMP files with an alpha channel use ABGR format for storing pixels
+ // OpenGLES does not have an ABGR format specifier
+ // therefore change pixel format from ABGR to RGBA
+ for (long long i = 0; i < width*height; i++) {
+ unsigned char a = data[i*pixelSizeBytes + 0];
+ unsigned char b = data[i*pixelSizeBytes + 1];
+ unsigned char g = data[i*pixelSizeBytes + 2];
+ unsigned char r = data[i*pixelSizeBytes + 3];
+ data[i*pixelSizeBytes] = r;
+ data[i*pixelSizeBytes + 1] = g;
+ data[i*pixelSizeBytes + 2] = b;
+ data[i*pixelSizeBytes + 3] = a;
+ }
+ } else if (pixelSizeBits == 24) {
+ for (long long i = 0; i < width*height; i++) {
+ unsigned char b = data[i*pixelSizeBytes];
+ unsigned char g = data[i*pixelSizeBytes + 1];
+ unsigned char r = data[i*pixelSizeBytes + 2];
+ data[i*pixelSizeBytes] = r;
+ data[i*pixelSizeBytes + 1] = g;
+ data[i*pixelSizeBytes + 2] = b;
+ }
+ }
+
+ bool loaded = loadArray(data, width, height, pixelSizeBits);
+ delete[] data;
+ return loaded;
+}
+
+// This function loads an array with image data
+bool TextureLoader::loadArray(void * data, unsigned int width, unsigned int height, unsigned int pixelSizeBits) {
+ if (loaded)
+ // don't load the thexture if a texture was already loaded for this object
+ return false;
+ else
+ loaded = true;
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+ glGenTextures(1, &textureID);
+ glBindTexture(GL_TEXTURE_2D, textureID);
+ if (pixelSizeBits == 32) {
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ } else if (pixelSizeBits == 24) {
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ }
+ glGenerateMipmap(GL_TEXTURE_2D);
+ // set texture parameters
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+ return loaded;
+}
+
+GLuint TextureLoader::getId() {
+ return textureID;
+}