prosperon/source/engine/3d/skybox.c

127 lines
3.1 KiB
C
Raw Normal View History

2021-11-30 21:29:18 -06:00
#include "skybox.h"
#include "shader.h"
#include "camera.h"
#include <string.h>
2022-02-06 10:14:57 -06:00
#include <stdlib.h>
2021-11-30 21:29:18 -06:00
#include "openglrender.h"
static const float skyboxVertices[216] = {
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, -1.0f,
1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f
};
struct mSkybox *MakeSkybox(const char *cubemap)
{
struct mSkybox *newskybox =
(struct mSkybox *) malloc(sizeof(struct mSkybox));
newskybox->shader = MakeShader("skyvert.glsl", "skyfrag.glsl");
shader_compile(newskybox->shader);
glGenVertexArrays(1, &newskybox->VAO);
glGenBuffers(1, &newskybox->VBO);
glBindVertexArray(newskybox->VAO);
glBindBuffer(GL_ARRAY_BUFFER, newskybox->VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices,
GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float),
(void *) 0);
shader_use(newskybox->shader);
shader_setint(newskybox->shader, "skybox", 0);
2022-02-06 10:14:57 -06:00
/*
2021-11-30 21:29:18 -06:00
const char *faces[6] =
{ "right.jpg", "left.jpg", "top.jpg", "bottom.jpg", "front.jpg",
"back.jpg"
};
2022-02-06 10:14:57 -06:00
*/
2021-11-30 21:29:18 -06:00
glGenTextures(1, &newskybox->id);
glBindTexture(GL_TEXTURE_CUBE_MAP, newskybox->id);
2022-02-06 10:14:57 -06:00
/*char buf[100] = { '\0' };*/
2021-11-30 21:29:18 -06:00
2022-06-28 22:13:39 -05:00
for (int i = 0; i < 6; i++) {
2022-02-06 10:14:57 -06:00
/*
2021-11-30 21:29:18 -06:00
buf[0] = '\0';
strcat(buf, cubemap);
strcat(buf, "/");
strcat(buf, faces[i]);
2022-02-06 10:14:57 -06:00
IMG_Load(buf);
2021-11-30 21:29:18 -06:00
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, 2048,
2048, 0, GL_RGB, GL_UNSIGNED_BYTE, data->pixels);
2022-02-06 10:14:57 -06:00
*/
2021-11-30 21:29:18 -06:00
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S,
GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T,
GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R,
GL_CLAMP_TO_EDGE);
return newskybox;
}
void skybox_draw(const struct mSkybox *skybox,
const struct mCamera *camera)
{
shader_use(skybox->shader);
mfloat_t view[16] = { 0.f };
getviewmatrix(view, camera);
shader_setmat4(skybox->shader, "skyview", view);
// skybox cube
glBindVertexArray(skybox->VAO);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, skybox->id);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
}