1#ifndef LIBOSMSCOUT_OPENGLMAPDATA_H
2#define LIBOSMSCOUT_OPENGLMAPDATA_H
36#include <glm/gtc/matrix_transform.hpp>
37#include <glm/gtc/type_ptr.hpp>
47 unsigned char *
data=
nullptr;
74 template <
int TexturePixelType,
unsigned int TexturePixelSize>
78 std::vector<GLfloat> vertices;
79 std::vector<GLfloat> verticesBuffer;
80 std::vector<GLuint> elements;
81 std::vector<GLuint> elementsBuffer;
82 unsigned char *textures=
nullptr;
83 std::vector<OpenGLTextureRef> texturesBuffer;
85 int textureSizeBuffer=0;
87 int textureWidthBuffer=0;
90 GLuint shaderProgram=0;
99 GLuint vertexShader=0;
100 GLuint fragmentShader=0;
104 glm::mat4 projection;
107 glBindBuffer(GL_ARRAY_BUFFER, vbo);
108 glBufferData(GL_ARRAY_BUFFER,
sizeof(GLfloat) * vertices.size(), &vertices[0], GL_DYNAMIC_DRAW);
112 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
113 glBufferData(GL_ELEMENT_ARRAY_BUFFER,
sizeof(GLfloat) * elements.size(), &elements[0], GL_DYNAMIC_DRAW);
123 if (shaderProgram!=0) {
124 glDeleteProgram(shaderProgram);
126 if (fragmentShader!=0) {
127 glDeleteShader(fragmentShader);
129 if (vertexShader!=0) {
130 glDeleteShader(vertexShader);
134 glDeleteBuffers(1, &ebo);
137 glDeleteBuffers(1, &vbo);
141 glDeleteVertexArrays(1, &vao);
152 vertices = std::move(verticesBuffer);
153 verticesBuffer.clear();
154 elements = std::move(elementsBuffer);
155 elementsBuffer.clear();
157 textureSize = textureSizeBuffer;
158 textureSizeBuffer = 0;
159 textureWidth = textureWidthBuffer;
160 textureWidthBuffer = 0;
162 textures =
new unsigned char[textureWidth * textureHeight * TexturePixelSize];
165 for (
int i = 0; i < textureHeight; i++) {
166 for (
unsigned int j = 0; j < texturesBuffer.size(); j++) {
167 int start = i * texturesBuffer[j]->width * TexturePixelSize;
168 for (
unsigned int k = start; k < start + (texturesBuffer[j]->width * TexturePixelSize); k++) {
169 textures[index] = (texturesBuffer[j]->data[k]);
175 texturesBuffer.clear();
181 if (textures !=
nullptr) {
185 texturesBuffer.clear();
191 glBindVertexArray(vao);
195 const std::string &vertexShaderFileName,
196 const std::string &fragmentShaderFileName,
197 GLuint projectionShader) {
198 glGenVertexArrays(1, &vao);
199 glBindVertexArray(vao);
200 glGenBuffers(1, &vbo);
201 glGenBuffers(1, &ebo);
202 glGenTextures(1, &tex);
205 model = glm::mat4(1.0f);
207 textureSizeBuffer = 0;
209 textureWidthBuffer = 0;
212 if (std::string vertexShaderSource;
214 LoadShader(vertexShader, GL_VERTEX_SHADER,
"vertex", vertexShaderSource))) {
218 if (std::string fragmentShaderSource;
219 !(
LoadShaderSource(shaderDir, fragmentShaderFileName, fragmentShaderSource) &&
220 LoadShader(fragmentShader, GL_FRAGMENT_SHADER,
"fragment", fragmentShaderSource))) {
224 glEnable(GL_PROGRAM_POINT_SIZE);
226 shaderProgram = glCreateProgram();
227 glAttachShader(shaderProgram, vertexShader);
228 glAttachShader(shaderProgram, fragmentShader);
229 glAttachShader(shaderProgram, projectionShader);
230 glBindFragDataLocation(shaderProgram, 0,
"outColor");
231 glLinkProgram(shaderProgram);
234 glGetProgramiv(shaderProgram, GL_LINK_STATUS, &isLinked);
235 if (isLinked == GL_FALSE) {
237 glGetProgramiv(shaderProgram, GL_INFO_LOG_LENGTH, &maxLength);
239 std::vector<GLchar> errorLog(maxLength);
240 glGetProgramInfoLog(shaderProgram, maxLength, &maxLength, errorLog.data());
241 assert(!errorLog.empty() && errorLog.back() == 0);
242 log.Error() <<
"Error while linking shader program: " << errorLog.data();
248 glDetachShader(shaderProgram, vertexShader);
249 glDetachShader(shaderProgram, fragmentShader);
250 glDetachShader(shaderProgram, projectionShader);
256 glActiveTexture(GL_TEXTURE0);
257 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
258 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
259 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
260 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
261 glPixelStorei(GL_UNPACK_ALIGNMENT, TexturePixelSize);
262 glBindTexture(GL_TEXTURE_2D, tex);
263 glTexImage2D(GL_TEXTURE_2D, 0, TexturePixelType, textureWidth, textureHeight, 0, TexturePixelType, GL_UNSIGNED_BYTE, textures);
272 this->verticesBuffer.push_back(vertex);
276 this->elementsBuffer.push_back(element);
280 return (this->verticesBuffer.size()) / (this->verticesSize);
284 glUseProgram(shaderProgram);
288 this->verticesSize = size;
292 return this->verticesBuffer.size();
304 texturesBuffer.push_back(texture);
306 textureWidthBuffer += texture->width;
317 return textureHeight;
321 return texturesBuffer[index]->width;
326 for(
int i = 0; i < index+1; i++)
327 sum += texturesBuffer[i]->width;
333 textureHeight = textheight;
337 GLuint uniform = glGetUniformLocation(shaderProgram,
"Model");
338 glUniformMatrix4fv(uniform, 1, GL_FALSE, glm::value_ptr(model));
343 glm::vec3(0.0, 0.0, 1.0f),
344 glm::vec3(0.0f, 0.0f, 0.0f),
345 glm::vec3(0.0f, 1.0f, 0.0f)
347 GLint uniView = glGetUniformLocation(shaderProgram,
"View");
348 glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view));
352 projection = glm::perspective(glm::radians(60.0f), (
float) width / (
float) height, 0.1f, 10.0f);
353 GLint uniProj = glGetUniformLocation(shaderProgram,
"Projection");
354 glUniformMatrix4fv(uniProj, 1, GL_FALSE, glm::value_ptr(projection));
357 void AddAttrib(std::string attribName, GLint length, GLuint type,
size_t positionOffset) {
358 GLint attrib = glGetAttribLocation(shaderProgram, attribName.c_str());
359 glEnableVertexAttribArray(attrib);
360 glVertexAttribPointer(attrib, length, type, GL_FALSE, verticesSize *
sizeof(GLfloat), (
void *) positionOffset);
364 GLuint uniform = glGetUniformLocation(shaderProgram, uniformName);
365 glUniform1f(uniform, value);
382 return this->shaderProgram;
398 glDrawElements(GL_TRIANGLES, (GLsizei) elements.size(), GL_UNSIGNED_INT, 0);
void AddUniform(const char *uniformName, float value)
Definition OpenGLMapData.h:363
OpenGLMapData & operator=(OpenGLMapData &&)=delete
const glm::mat4 & GetView() const
Definition OpenGLMapData.h:389
void AddNewElement(GLuint element)
Definition OpenGLMapData.h:275
int GetTextureHeight()
Definition OpenGLMapData.h:316
const glm::mat4 & GetProjection() const
Definition OpenGLMapData.h:393
void SetTextureHeight(int textheight)
Definition OpenGLMapData.h:332
int GetTextureWidth(int index)
Definition OpenGLMapData.h:320
void SetMapProjection(const OpenGLProjection &mapProjection)
Definition OpenGLMapData.h:368
int GetTextureWidthSum(int index)
Definition OpenGLMapData.h:324
void SetVerticesSize(int size)
Definition OpenGLMapData.h:287
int GetNumOfVertices()
Definition OpenGLMapData.h:291
void UseProgram()
Definition OpenGLMapData.h:283
void clearData()
Definition OpenGLMapData.h:178
void LoadTextures()
Definition OpenGLMapData.h:255
GLuint getVAO()
Definition OpenGLMapData.h:373
void SetModel()
Definition OpenGLMapData.h:336
void BindBuffers()
Definition OpenGLMapData.h:190
OpenGLMapData(const OpenGLMapData &)=delete
OpenGLMapData(OpenGLMapData &&)=delete
void AddNewVertex(GLfloat vertex)
Definition OpenGLMapData.h:271
~OpenGLMapData()
Definition OpenGLMapData.h:119
int GetVerticesNumber()
Definition OpenGLMapData.h:279
OpenGLMapData & operator=(const OpenGLMapData &)=delete
void SwapData()
Definition OpenGLMapData.h:150
int GetTextureWidth()
Definition OpenGLMapData.h:312
bool InitContext(const std::string &shaderDir, const std::string &vertexShaderFileName, const std::string &fragmentShaderFileName, GLuint projectionShader)
Definition OpenGLMapData.h:194
GLuint getShaderProgram()
Definition OpenGLMapData.h:381
const glm::mat4 & GetModel() const
Definition OpenGLMapData.h:385
void AddNewTexture(OpenGLTextureRef texture)
Definition OpenGLMapData.h:303
GLuint GetTexture()
Definition OpenGLMapData.h:377
void SetView(float, float)
Definition OpenGLMapData.h:341
void Draw()
Definition OpenGLMapData.h:397
void AddAttrib(std::string attribName, GLint length, GLuint type, size_t positionOffset)
Definition OpenGLMapData.h:357
void SetProjection(float width, float height)
Definition OpenGLMapData.h:351
void LoadVertices()
Definition OpenGLMapData.h:266
Definition OpenGLProjection.h:33
void SetShaderUniforms(GLuint shaderProgram) const
Definition OpenGLProjection.h:47
OpenGLTexture & operator=(OpenGLTexture &&)=delete
OpenGLTexture(OpenGLTexture &&)=delete
size_t fromOriginY
Definition OpenGLMapData.h:49
size_t width
Definition OpenGLMapData.h:44
size_t size
Definition OpenGLMapData.h:46
OpenGLTexture(const OpenGLTexture &)=delete
~OpenGLTexture()
Definition OpenGLMapData.h:58
OpenGLTexture & operator=(const OpenGLTexture &)=delete
size_t height
Definition OpenGLMapData.h:45
unsigned char * data
Definition OpenGLMapData.h:47
OSMSCOUT_API Log log
Definition LoggerImpl.h:95
bool OSMSCOUT_MAP_OPENGL_API LoadShader(GLuint &shader, GLenum type, const std::string &name, const std::string &shaderSource)
std::shared_ptr< OpenGLTexture > OpenGLTextureRef
Definition OpenGLMapData.h:67
bool OSMSCOUT_MAP_OPENGL_API LoadShaderSource(const std::string &dirPath, const std::string &name, std::string &result)