Skybox Implementation #1
60
main.c
@@ -6,6 +6,7 @@
|
||||
#include "stdlib.h"
|
||||
#include "stdint.h"
|
||||
#include "time.h"
|
||||
#include "rlgl.h"
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Defines and Macros
|
||||
@@ -214,6 +215,49 @@ int main(void)
|
||||
int locCutoff = GetShaderLocation(shAlphaClip, "u_alphaCutoff");
|
||||
float cutoff = 0.3f; // tweak to taste; 0.3 - 0.5 works well for tree PNGs
|
||||
SetShaderValue(shAlphaClip, locCutoff, &cutoff, SHADER_UNIFORM_FLOAT);
|
||||
|
||||
|
||||
|
||||
// --- Skybox setup ---
|
||||
// Generate cube mesh
|
||||
Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f);
|
||||
Model skybox = LoadModelFromMesh(cube);
|
||||
|
||||
// Load skybox shader
|
||||
// Make sure the .vs/.fs are in your resources/shaders/glsl330/ folder
|
||||
Shader skyboxShader = LoadShader(
|
||||
"resources/shaders/skybox.vs",
|
||||
"resources/shaders/skybox.fs"
|
||||
);
|
||||
|
||||
// Attach shader to skybox
|
||||
skybox.materials[0].shader = skyboxShader;
|
||||
|
||||
// Load your cubemap (cross layout image)
|
||||
Image img = LoadImage("resources/cubemap/cubemap.png");
|
||||
// AUTO_DETECT will figure out layout (works for cross 4x3, panorama, etc.)
|
||||
skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture =
|
||||
LoadTextureCubemap(img, CUBEMAP_LAYOUT_AUTO_DETECT);
|
||||
UnloadImage(img);
|
||||
|
||||
// Tell the shader which material map is the environmentMap
|
||||
int mapType = MATERIAL_MAP_CUBEMAP;
|
||||
SetShaderValue(skyboxShader,
|
||||
GetShaderLocation(skyboxShader, "environmentMap"),
|
||||
&mapType, SHADER_UNIFORM_INT);
|
||||
|
||||
// Flip/gamma options (set to 0 for PNG cross)
|
||||
int vflipped = 0;
|
||||
int doGamma = 0;
|
||||
SetShaderValue(skyboxShader, GetShaderLocation(skyboxShader, "vflipped"), &vflipped, SHADER_UNIFORM_INT);
|
||||
SetShaderValue(skyboxShader, GetShaderLocation(skyboxShader, "doGamma"), &doGamma, SHADER_UNIFORM_INT);
|
||||
|
||||
// Done – now you can draw it
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
@@ -333,6 +377,22 @@ int main(void)
|
||||
ClearBackground(BLACK);
|
||||
|
||||
BeginMode3D(g.player.camera);
|
||||
|
||||
rlDisableBackfaceCulling();
|
||||
rlDisableDepthMask();
|
||||
|
||||
// keep it centered on the camera
|
||||
skybox.transform = MatrixTranslate(
|
||||
g.player.camera.position.x,
|
||||
g.player.camera.position.y,
|
||||
g.player.camera.position.z
|
||||
);
|
||||
|
||||
DrawModel(skybox, (Vector3){0}, 1.0f, WHITE);
|
||||
|
||||
rlEnableDepthMask();
|
||||
rlEnableBackfaceCulling();
|
||||
|
||||
DrawLevel(mdlTile);
|
||||
|
||||
BeginShaderMode(shAlphaClip);
|
||||
|
||||
BIN
resources/cubemap/back.png
Normal file
|
After Width: | Height: | Size: 746 KiB |
BIN
resources/cubemap/bottom.png
Normal file
|
After Width: | Height: | Size: 753 KiB |
BIN
resources/cubemap/cubemap.png
Normal file
|
After Width: | Height: | Size: 5.1 MiB |
BIN
resources/cubemap/front.png
Normal file
|
After Width: | Height: | Size: 1017 KiB |
BIN
resources/cubemap/left.png
Normal file
|
After Width: | Height: | Size: 807 KiB |
BIN
resources/cubemap/right.png
Normal file
|
After Width: | Height: | Size: 838 KiB |
BIN
resources/cubemap/top.png
Normal file
|
After Width: | Height: | Size: 830 KiB |
30
resources/shaders/skybox.fs
Normal file
@@ -0,0 +1,30 @@
|
||||
#version 330
|
||||
|
||||
// Input vertex attributes (from vertex shader)
|
||||
in vec3 fragPosition;
|
||||
|
||||
// Input uniform values
|
||||
uniform samplerCube environmentMap;
|
||||
uniform bool vflipped;
|
||||
uniform bool doGamma;
|
||||
|
||||
// Output fragment color
|
||||
out vec4 finalColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Fetch color from texture map
|
||||
vec3 color = vec3(0.0);
|
||||
|
||||
if (vflipped) color = texture(environmentMap, vec3(fragPosition.x, -fragPosition.y, fragPosition.z)).rgb;
|
||||
else color = texture(environmentMap, fragPosition).rgb;
|
||||
|
||||
if (doGamma)// Apply gamma correction
|
||||
{
|
||||
color = color/(color + vec3(1.0));
|
||||
color = pow(color, vec3(1.0/2.2));
|
||||
}
|
||||
|
||||
// Calculate final fragment color
|
||||
finalColor = vec4(color, 1.0);
|
||||
}
|
||||
24
resources/shaders/skybox.vs
Normal file
@@ -0,0 +1,24 @@
|
||||
#version 330
|
||||
|
||||
// Input vertex attributes
|
||||
in vec3 vertexPosition;
|
||||
|
||||
// Input uniform values
|
||||
uniform mat4 matProjection;
|
||||
uniform mat4 matView;
|
||||
|
||||
// Output vertex attributes (to fragment shader)
|
||||
out vec3 fragPosition;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Calculate fragment position based on model transformations
|
||||
fragPosition = vertexPosition;
|
||||
|
||||
// Remove translation from the view matrix
|
||||
mat4 rotView = mat4(mat3(matView));
|
||||
vec4 clipPos = matProjection*rotView*vec4(vertexPosition, 1.0);
|
||||
|
||||
// Calculate final vertex position
|
||||
gl_Position = clipPos;
|
||||
}
|
||||