mirror of https://github.com/mode777/rayjs.git
Fix lightmapping
This commit is contained in:
parent
9341e134a1
commit
4f061d0ab8
File diff suppressed because it is too large
Load Diff
Binary file not shown.
After Width: | Height: | Size: 250 KiB |
|
@ -2,14 +2,16 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <stddef.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
//#include "glad/glad.h"
|
//#include "glad/glad.h"
|
||||||
#define GLAD_MALLOC(sz) malloc(sz)
|
#define GLAD_MALLOC(sz) malloc(sz)
|
||||||
#define GLAD_FREE(sz) free(sz)
|
#define GLAD_FREE(sz) free(sz)
|
||||||
//#define GLAD_GL_IMPLEMENTATION
|
//#define GLAD_GL_IMPLEMENTATION
|
||||||
#include "../thirdparty/raylib/src/external/glad.h"
|
#include "../thirdparty/raylib/src/external/glad.h"
|
||||||
#include "GLFW/glfw3.h"
|
//#include "GLFW/glfw3.h"
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
#include "rlgl.h"
|
||||||
|
|
||||||
#define LIGHTMAPPER_IMPLEMENTATION
|
#define LIGHTMAPPER_IMPLEMENTATION
|
||||||
#define LM_DEBUG_INTERPOLATION
|
#define LM_DEBUG_INTERPOLATION
|
||||||
|
@ -34,10 +36,10 @@ typedef struct
|
||||||
GLuint lightmap;
|
GLuint lightmap;
|
||||||
int w, h;
|
int w, h;
|
||||||
Model raylib_model;
|
Model raylib_model;
|
||||||
GLuint vao, vbo, ibo;
|
//GLuint vao, vbo, ibo;
|
||||||
vertex_t *vertices;
|
//vertex_t *vertices;
|
||||||
unsigned short *indices;
|
//unsigned short *indices;
|
||||||
unsigned int vertexCount, indexCount;
|
//unsigned int vertexCount, indexCount;
|
||||||
} scene_t;
|
} scene_t;
|
||||||
|
|
||||||
static int initScene(scene_t *scene);
|
static int initScene(scene_t *scene);
|
||||||
|
@ -65,11 +67,13 @@ static int bake(scene_t *scene)
|
||||||
float *data = calloc(w * h * 4, sizeof(float));
|
float *data = calloc(w * h * 4, sizeof(float));
|
||||||
lmSetTargetLightmap(ctx, data, w, h, 4);
|
lmSetTargetLightmap(ctx, data, w, h, 4);
|
||||||
|
|
||||||
|
Mesh m = scene->raylib_model.meshes[0];
|
||||||
|
|
||||||
lmSetGeometry(ctx, NULL, // no transformation in this example
|
lmSetGeometry(ctx, NULL, // no transformation in this example
|
||||||
LM_FLOAT, (unsigned char*)scene->vertices + offsetof(vertex_t, p), sizeof(vertex_t),
|
LM_FLOAT, (unsigned char*)m.vertices, 0,
|
||||||
LM_NONE , NULL , 0 , // no interpolated normals in this example
|
LM_NONE , NULL , 0 , // no interpolated normals in this example
|
||||||
LM_FLOAT, (unsigned char*)scene->vertices + offsetof(vertex_t, t), sizeof(vertex_t),
|
LM_FLOAT, (unsigned char*)m.texcoords, 0,
|
||||||
scene->indexCount, LM_UNSIGNED_SHORT, scene->indices);
|
m.vertexCount, LM_NONE, 0);
|
||||||
|
|
||||||
int vp[4];
|
int vp[4];
|
||||||
float view[16], projection[16];
|
float view[16], projection[16];
|
||||||
|
@ -77,11 +81,11 @@ static int bake(scene_t *scene)
|
||||||
while (lmBegin(ctx, vp, view, projection))
|
while (lmBegin(ctx, vp, view, projection))
|
||||||
{
|
{
|
||||||
// render to lightmapper framebuffer
|
// render to lightmapper framebuffer
|
||||||
glViewport(vp[0], vp[1], vp[2], vp[3]);
|
rlViewport(vp[0], vp[1], vp[2], vp[3]);
|
||||||
drawScene(scene, view, projection);
|
drawScene(scene, view, projection);
|
||||||
|
|
||||||
// display progress every second (printf is expensive)
|
// display progress every second (printf is expensive)
|
||||||
double time = glfwGetTime();
|
double time = GetTime();
|
||||||
if (time - lastUpdateTime > 1.0)
|
if (time - lastUpdateTime > 1.0)
|
||||||
{
|
{
|
||||||
lastUpdateTime = time;
|
lastUpdateTime = time;
|
||||||
|
@ -91,7 +95,7 @@ static int bake(scene_t *scene)
|
||||||
|
|
||||||
lmEnd(ctx);
|
lmEnd(ctx);
|
||||||
}
|
}
|
||||||
printf("\rFinished baking %d triangles.\n", scene->indexCount / 3);
|
//printf("\rFinished baking %d triangles.\n", scene->indexCount / 3);
|
||||||
|
|
||||||
lmDestroy(ctx);
|
lmDestroy(ctx);
|
||||||
|
|
||||||
|
@ -107,13 +111,23 @@ static int bake(scene_t *scene)
|
||||||
lmImagePower(data, w, h, 4, 1.0f / 2.2f, 0x7); // gamma correct color channels
|
lmImagePower(data, w, h, 4, 1.0f / 2.2f, 0x7); // gamma correct color channels
|
||||||
free(temp);
|
free(temp);
|
||||||
|
|
||||||
|
unsigned char *tempub = (unsigned char*)calloc(w * h * 4, sizeof(unsigned char));
|
||||||
|
lmImageFtoUB(data, tempub, w, h, 4, 1.0f);
|
||||||
|
Image im;
|
||||||
|
im.data = tempub;
|
||||||
|
im.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
|
||||||
|
im.height = h;
|
||||||
|
im.width = w;
|
||||||
|
ExportImage(im,"result.png");
|
||||||
|
|
||||||
|
// upload result
|
||||||
|
rlUnloadTexture(scene->lightmap);
|
||||||
|
scene->lightmap = rlLoadTexture(tempub, w, h, RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, 1);
|
||||||
|
free(tempub);
|
||||||
|
|
||||||
// save result to a file
|
// save result to a file
|
||||||
if (lmImageSaveTGAf("result.tga", data, w, h, 4, 1.0f))
|
if (lmImageSaveTGAf("result.tga", data, w, h, 4, 1.0f))
|
||||||
printf("Saved result.tga\n");
|
printf("Saved result.tga\n");
|
||||||
|
|
||||||
// upload result
|
|
||||||
glBindTexture(GL_TEXTURE_2D, scene->lightmap);
|
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_FLOAT, data);
|
|
||||||
free(data);
|
free(data);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -192,29 +206,33 @@ static GLuint loadProgram(const char *vp, const char *fp, const char **attribute
|
||||||
static int initScene(scene_t *scene)
|
static int initScene(scene_t *scene)
|
||||||
{
|
{
|
||||||
// load mesh
|
// load mesh
|
||||||
//scene->raylib_model = LoadModel("thirdparty/lightmapper/example/gazebo.obj");
|
scene->raylib_model = LoadModel("thirdparty/lightmapper/example/gazebo.obj");
|
||||||
|
Mesh m = scene->raylib_model.meshes[0];
|
||||||
|
if(m.normals != NULL) puts("Has normals");
|
||||||
|
if(m.texcoords != NULL) puts("Has texcoords");
|
||||||
|
if(m.texcoords2 != NULL) puts("Has texcoords2");
|
||||||
//scene->vertices = myModel.meshes[0].
|
//scene->vertices = myModel.meshes[0].
|
||||||
if (!loadSimpleObjFile("thirdparty/lightmapper/example/gazebo.obj", &scene->vertices, &scene->vertexCount, &scene->indices, &scene->indexCount))
|
// if (!loadSimpleObjFile("thirdparty/lightmapper/example/gazebo.obj", &scene->vertices, &scene->vertexCount, &scene->indices, &scene->indexCount))
|
||||||
{
|
// {
|
||||||
fprintf(stderr, "Error loading obj file\n");
|
// fprintf(stderr, "Error loading obj file\n");
|
||||||
return 0;
|
// return 0;
|
||||||
}
|
// }
|
||||||
|
|
||||||
glGenVertexArrays(1, &scene->vao);
|
// glGenVertexArrays(1, &scene->vao);
|
||||||
glBindVertexArray(scene->vao);
|
// glBindVertexArray(scene->vao);
|
||||||
|
|
||||||
glGenBuffers(1, &scene->vbo);
|
// glGenBuffers(1, &scene->vbo);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, scene->vbo);
|
// glBindBuffer(GL_ARRAY_BUFFER, scene->vbo);
|
||||||
glBufferData(GL_ARRAY_BUFFER, scene->vertexCount * sizeof(vertex_t), scene->vertices, GL_STATIC_DRAW);
|
// glBufferData(GL_ARRAY_BUFFER, scene->vertexCount * sizeof(vertex_t), scene->vertices, GL_STATIC_DRAW);
|
||||||
|
|
||||||
glGenBuffers(1, &scene->ibo);
|
// glGenBuffers(1, &scene->ibo);
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, scene->ibo);
|
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, scene->ibo);
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, scene->indexCount * sizeof(unsigned short), scene->indices, GL_STATIC_DRAW);
|
// glBufferData(GL_ELEMENT_ARRAY_BUFFER, scene->indexCount * sizeof(unsigned short), scene->indices, GL_STATIC_DRAW);
|
||||||
|
|
||||||
glEnableVertexAttribArray(0);
|
// glEnableVertexAttribArray(0);
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (void*)offsetof(vertex_t, p));
|
// glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (void*)offsetof(vertex_t, p));
|
||||||
glEnableVertexAttribArray(1);
|
// glEnableVertexAttribArray(1);
|
||||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (void*)offsetof(vertex_t, t));
|
// glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (void*)offsetof(vertex_t, t));
|
||||||
|
|
||||||
// create lightmap texture
|
// create lightmap texture
|
||||||
scene->w = 654;
|
scene->w = 654;
|
||||||
|
@ -240,7 +258,7 @@ static int initScene(scene_t *scene)
|
||||||
"void main()\n"
|
"void main()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
"gl_Position = u_projection * (u_view * vec4(a_position, 1.0));\n"
|
"gl_Position = u_projection * (u_view * vec4(a_position, 1.0));\n"
|
||||||
"v_texcoord = a_texcoord;\n"
|
"v_texcoord = vec2(a_texcoord.x, a_texcoord.y);\n"
|
||||||
"}\n";
|
"}\n";
|
||||||
|
|
||||||
const char *fp =
|
const char *fp =
|
||||||
|
@ -275,6 +293,7 @@ static int initScene(scene_t *scene)
|
||||||
|
|
||||||
static void drawScene(scene_t *scene, float *view, float *projection)
|
static void drawScene(scene_t *scene, float *view, float *projection)
|
||||||
{
|
{
|
||||||
|
Mesh m = scene->raylib_model.meshes[0];
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
glDisable(GL_BLEND);
|
glDisable(GL_BLEND);
|
||||||
|
|
||||||
|
@ -285,17 +304,19 @@ static void drawScene(scene_t *scene, float *view, float *projection)
|
||||||
|
|
||||||
glBindTexture(GL_TEXTURE_2D, scene->lightmap);
|
glBindTexture(GL_TEXTURE_2D, scene->lightmap);
|
||||||
|
|
||||||
glBindVertexArray(scene->vao);
|
//glBindVertexArray(scene->vao);
|
||||||
glDrawElements(GL_TRIANGLES, scene->indexCount, GL_UNSIGNED_SHORT, 0);
|
//glDrawElements(GL_TRIANGLES, scene->indexCount, GL_UNSIGNED_SHORT, 0);
|
||||||
|
glBindVertexArray(m.vaoId);
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, m.vertexCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void destroyScene(scene_t *scene)
|
static void destroyScene(scene_t *scene)
|
||||||
{
|
{
|
||||||
free(scene->vertices);
|
// free(scene->vertices);
|
||||||
free(scene->indices);
|
// free(scene->indices);
|
||||||
glDeleteVertexArrays(1, &scene->vao);
|
// glDeleteVertexArrays(1, &scene->vao);
|
||||||
glDeleteBuffers(1, &scene->vbo);
|
// glDeleteBuffers(1, &scene->vbo);
|
||||||
glDeleteBuffers(1, &scene->ibo);
|
// glDeleteBuffers(1, &scene->ibo);
|
||||||
glDeleteTextures(1, &scene->lightmap);
|
glDeleteTextures(1, &scene->lightmap);
|
||||||
glDeleteProgram(scene->program);
|
glDeleteProgram(scene->program);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue