2021-06-06 01:26:18 +00:00
|
|
|
/**
|
|
|
|
* Looking Glass
|
2024-02-01 06:16:31 +00:00
|
|
|
* Copyright © 2017-2024 The Looking Glass Authors
|
2021-06-06 01:26:18 +00:00
|
|
|
* https://looking-glass.io
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2 of the License, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., 59
|
|
|
|
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
2018-09-22 06:26:10 +00:00
|
|
|
|
2018-12-12 05:39:04 +00:00
|
|
|
#include "model.h"
|
|
|
|
#include "shader.h"
|
|
|
|
#include "texture.h"
|
2018-09-23 05:48:44 +00:00
|
|
|
|
2019-04-11 01:12:59 +00:00
|
|
|
#include "common/debug.h"
|
2022-01-12 01:22:18 +00:00
|
|
|
#include "common/ll.h"
|
2018-09-22 06:26:10 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
struct EGL_Model
|
|
|
|
{
|
2018-12-15 13:54:37 +00:00
|
|
|
bool rebuild;
|
|
|
|
struct ll * verticies;
|
|
|
|
size_t vertexCount;
|
|
|
|
bool finish;
|
2018-09-22 06:26:10 +00:00
|
|
|
|
2018-12-15 13:54:37 +00:00
|
|
|
GLuint buffer;
|
2021-07-18 01:44:59 +00:00
|
|
|
GLuint vao;
|
2018-09-22 06:26:10 +00:00
|
|
|
|
2018-09-23 05:48:44 +00:00
|
|
|
EGL_Shader * shader;
|
|
|
|
EGL_Texture * texture;
|
2018-09-22 06:26:10 +00:00
|
|
|
};
|
|
|
|
|
2018-12-15 13:54:37 +00:00
|
|
|
struct FloatList
|
|
|
|
{
|
|
|
|
GLfloat * v;
|
|
|
|
GLfloat * u;
|
|
|
|
size_t count;
|
|
|
|
};
|
|
|
|
|
2018-09-23 10:45:20 +00:00
|
|
|
void update_uniform_bindings(EGL_Model * model);
|
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
bool egl_modelInit(EGL_Model ** model)
|
2018-09-22 06:26:10 +00:00
|
|
|
{
|
2021-08-15 22:55:19 +00:00
|
|
|
*model = malloc(sizeof(**model));
|
2018-09-22 06:26:10 +00:00
|
|
|
if (!*model)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to malloc EGL_Model");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-15 16:18:22 +00:00
|
|
|
memset(*model, 0, sizeof(**model));
|
2018-12-15 13:54:37 +00:00
|
|
|
|
|
|
|
(*model)->verticies = ll_new();
|
|
|
|
|
2018-09-22 06:26:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
void egl_modelFree(EGL_Model ** model)
|
2018-09-22 06:26:10 +00:00
|
|
|
{
|
|
|
|
if (!*model)
|
|
|
|
return;
|
|
|
|
|
2018-12-15 13:54:37 +00:00
|
|
|
struct FloatList * fl;
|
|
|
|
while(ll_shift((*model)->verticies, (void **)&fl))
|
|
|
|
{
|
|
|
|
free(fl->u);
|
|
|
|
free(fl->v);
|
|
|
|
free(fl);
|
|
|
|
}
|
|
|
|
ll_free((*model)->verticies);
|
2018-09-22 06:26:10 +00:00
|
|
|
|
2021-07-18 01:40:30 +00:00
|
|
|
if ((*model)->buffer)
|
2018-12-15 13:54:37 +00:00
|
|
|
glDeleteBuffers(1, &(*model)->buffer);
|
2018-09-22 06:26:10 +00:00
|
|
|
|
2021-07-18 01:44:59 +00:00
|
|
|
if ((*model)->vao)
|
|
|
|
glDeleteVertexArrays(1, &(*model)->vao);
|
|
|
|
|
2018-09-22 06:26:10 +00:00
|
|
|
free(*model);
|
|
|
|
*model = NULL;
|
|
|
|
}
|
|
|
|
|
2021-08-09 04:08:10 +00:00
|
|
|
void egl_modelSetDefault(EGL_Model * model, bool flipped)
|
2018-12-12 09:08:52 +00:00
|
|
|
{
|
|
|
|
static const GLfloat square[] =
|
|
|
|
{
|
|
|
|
-1.0f, -1.0f, 0.0f,
|
|
|
|
1.0f, -1.0f, 0.0f,
|
|
|
|
-1.0f, 1.0f, 0.0f,
|
|
|
|
1.0f, 1.0f, 0.0f
|
|
|
|
};
|
|
|
|
|
2021-08-09 04:08:10 +00:00
|
|
|
static const GLfloat uvsNormal[] =
|
|
|
|
{
|
|
|
|
0.0f, 0.0f,
|
|
|
|
1.0f, 0.0f,
|
|
|
|
0.0f, 1.0f,
|
|
|
|
1.0f, 1.0f
|
|
|
|
};
|
|
|
|
|
|
|
|
static const GLfloat uvsFlipped[] =
|
2018-12-12 09:08:52 +00:00
|
|
|
{
|
|
|
|
0.0f, 1.0f,
|
|
|
|
1.0f, 1.0f,
|
|
|
|
0.0f, 0.0f,
|
|
|
|
1.0f, 0.0f
|
|
|
|
};
|
|
|
|
|
2021-08-09 04:08:10 +00:00
|
|
|
egl_modelAddVerts(model, square, flipped ? uvsFlipped : uvsNormal, 4);
|
2018-12-12 09:08:52 +00:00
|
|
|
}
|
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
void egl_modelAddVerts(EGL_Model * model, const GLfloat * verticies, const GLfloat * uvs, const size_t count)
|
2018-09-22 06:26:10 +00:00
|
|
|
{
|
2021-08-15 22:55:19 +00:00
|
|
|
struct FloatList * fl = malloc(sizeof(*fl));
|
2022-03-06 23:13:54 +00:00
|
|
|
if (!fl)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("out of memory");
|
|
|
|
return;
|
|
|
|
}
|
2018-09-22 06:26:10 +00:00
|
|
|
|
2018-12-15 13:54:37 +00:00
|
|
|
fl->count = count;
|
2022-03-06 23:13:54 +00:00
|
|
|
|
|
|
|
fl->v = malloc(sizeof(GLfloat) * count * 3);
|
|
|
|
if (!fl->v)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("out of memory");
|
|
|
|
free(fl);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fl->u = malloc(sizeof(GLfloat) * count * 2);
|
|
|
|
if (!fl->u)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("out of memory");
|
|
|
|
free(fl->v);
|
|
|
|
free(fl);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-15 13:54:37 +00:00
|
|
|
memcpy(fl->v, verticies, sizeof(GLfloat) * count * 3);
|
2018-09-22 06:26:10 +00:00
|
|
|
|
2018-12-15 13:54:37 +00:00
|
|
|
if (uvs)
|
|
|
|
memcpy(fl->u, uvs, sizeof(GLfloat) * count * 2);
|
|
|
|
else
|
|
|
|
memset(fl->u, 0 , sizeof(GLfloat) * count * 2);
|
2018-09-22 06:26:10 +00:00
|
|
|
|
2018-12-15 13:54:37 +00:00
|
|
|
ll_push(model->verticies, fl);
|
|
|
|
model->rebuild = true;
|
|
|
|
model->vertexCount += count;
|
2018-09-22 06:26:10 +00:00
|
|
|
}
|
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
void egl_modelRender(EGL_Model * model)
|
2018-09-22 06:26:10 +00:00
|
|
|
{
|
2018-12-15 13:54:37 +00:00
|
|
|
if (!model->vertexCount)
|
2018-09-22 06:26:10 +00:00
|
|
|
return;
|
2018-12-15 13:54:37 +00:00
|
|
|
|
|
|
|
if (model->rebuild)
|
|
|
|
{
|
2021-07-18 01:40:30 +00:00
|
|
|
if (model->buffer)
|
2018-12-15 13:54:37 +00:00
|
|
|
glDeleteBuffers(1, &model->buffer);
|
|
|
|
|
2021-07-18 01:44:59 +00:00
|
|
|
if (!model->vao)
|
|
|
|
glGenVertexArrays(1, &model->vao);
|
|
|
|
|
|
|
|
glBindVertexArray(model->vao);
|
|
|
|
|
2018-12-15 13:54:37 +00:00
|
|
|
/* create a buffer large enough */
|
|
|
|
glGenBuffers(1, &model->buffer);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, model->buffer);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * (model->vertexCount * 5), NULL, GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
GLintptr offset = 0;
|
|
|
|
|
|
|
|
/* buffer the verticies */
|
|
|
|
struct FloatList * fl;
|
2022-01-12 01:17:29 +00:00
|
|
|
ll_lock(model->verticies);
|
|
|
|
ll_forEachNL(model->verticies, item, fl)
|
2018-12-15 13:54:37 +00:00
|
|
|
{
|
|
|
|
glBufferSubData(GL_ARRAY_BUFFER, offset, sizeof(GLfloat) * fl->count * 3, fl->v);
|
|
|
|
offset += sizeof(GLfloat) * fl->count * 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* buffer the uvs */
|
2022-01-12 01:17:29 +00:00
|
|
|
ll_forEachNL(model->verticies, item, fl)
|
2018-12-15 13:54:37 +00:00
|
|
|
{
|
|
|
|
glBufferSubData(GL_ARRAY_BUFFER, offset, sizeof(GLfloat) * fl->count * 2, fl->u);
|
|
|
|
offset += sizeof(GLfloat) * fl->count * 2;
|
|
|
|
}
|
2022-01-12 01:17:29 +00:00
|
|
|
ll_unlock(model->verticies);
|
2018-12-15 13:54:37 +00:00
|
|
|
|
2021-07-18 01:44:59 +00:00
|
|
|
/* set up vertex arrays in the VAO */
|
|
|
|
glEnableVertexAttribArray(0);
|
|
|
|
glEnableVertexAttribArray(1);
|
|
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
|
|
|
|
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)(sizeof(GLfloat) * model->vertexCount * 3));
|
|
|
|
|
2018-12-15 13:54:37 +00:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
2021-07-18 01:44:59 +00:00
|
|
|
glBindVertexArray(0);
|
2018-12-15 13:54:37 +00:00
|
|
|
model->rebuild = false;
|
2018-09-22 06:26:10 +00:00
|
|
|
}
|
|
|
|
|
2021-07-18 01:44:59 +00:00
|
|
|
glBindVertexArray(model->vao);
|
2018-12-15 13:54:37 +00:00
|
|
|
|
2018-09-22 06:26:10 +00:00
|
|
|
if (model->shader)
|
2021-08-08 07:16:10 +00:00
|
|
|
egl_shaderUse(model->shader);
|
2018-09-22 06:26:10 +00:00
|
|
|
|
2018-12-15 13:54:37 +00:00
|
|
|
if (model->texture)
|
2021-08-08 07:16:10 +00:00
|
|
|
egl_textureBind(model->texture);
|
2018-09-22 06:26:10 +00:00
|
|
|
|
2018-12-15 13:54:37 +00:00
|
|
|
/* draw the arrays */
|
|
|
|
GLint offset = 0;
|
|
|
|
struct FloatList * fl;
|
2022-01-12 01:17:29 +00:00
|
|
|
ll_lock(model->verticies);
|
|
|
|
ll_forEachNL(model->verticies, item, fl)
|
2018-09-22 06:26:10 +00:00
|
|
|
{
|
2018-12-15 13:54:37 +00:00
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, offset, fl->count);
|
|
|
|
offset += fl->count;
|
2018-09-22 06:26:10 +00:00
|
|
|
}
|
2022-01-12 01:17:29 +00:00
|
|
|
ll_unlock(model->verticies);
|
2018-09-22 06:26:10 +00:00
|
|
|
|
2018-12-15 13:54:37 +00:00
|
|
|
/* unbind and cleanup */
|
2018-09-23 05:48:44 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
2021-07-18 01:44:59 +00:00
|
|
|
glBindVertexArray(0);
|
2018-09-23 05:48:44 +00:00
|
|
|
glUseProgram(0);
|
2018-09-22 06:26:10 +00:00
|
|
|
}
|
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
void egl_modelSetShader(EGL_Model * model, EGL_Shader * shader)
|
2018-09-22 06:26:10 +00:00
|
|
|
{
|
|
|
|
model->shader = shader;
|
2018-09-23 10:45:20 +00:00
|
|
|
update_uniform_bindings(model);
|
2018-09-22 06:26:10 +00:00
|
|
|
}
|
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
void egl_modelSetTexture(EGL_Model * model, EGL_Texture * texture)
|
2018-09-22 06:26:10 +00:00
|
|
|
{
|
2018-09-23 05:48:44 +00:00
|
|
|
model->texture = texture;
|
2018-09-23 10:45:20 +00:00
|
|
|
update_uniform_bindings(model);
|
|
|
|
}
|
|
|
|
|
|
|
|
void update_uniform_bindings(EGL_Model * model)
|
|
|
|
{
|
|
|
|
if (!model->shader || !model->texture)
|
|
|
|
return;
|
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
egl_shaderAssocTextures(model->shader, 1);
|
2021-01-25 08:58:36 +00:00
|
|
|
}
|