REVIEWED: Ligthmap example
This commit is contained in:
parent
abcbd9817e
commit
5573f0f1c7
4 changed files with 81 additions and 72 deletions
|
@ -1,4 +1,5 @@
|
||||||
#version 330
|
#version 330
|
||||||
|
|
||||||
// Input vertex attributes (from vertex shader)
|
// Input vertex attributes (from vertex shader)
|
||||||
in vec2 fragTexCoord;
|
in vec2 fragTexCoord;
|
||||||
in vec2 fragTexCoord2;
|
in vec2 fragTexCoord2;
|
||||||
|
@ -12,9 +13,11 @@ uniform sampler2D texture1;
|
||||||
// Output fragment color
|
// Output fragment color
|
||||||
out vec4 finalColor;
|
out vec4 finalColor;
|
||||||
|
|
||||||
void main() {
|
void main()
|
||||||
// Texel color fetching from texture sampler
|
{
|
||||||
vec4 texelColor = texture( texture0, fragTexCoord );
|
// Texel color fetching from texture sampler
|
||||||
vec4 texelColor2 = texture( texture1, fragTexCoord2 );
|
vec4 texelColor = texture(texture0, fragTexCoord);
|
||||||
finalColor = texelColor * texelColor2;
|
vec4 texelColor2 = texture(texture1, fragTexCoord2);
|
||||||
|
|
||||||
|
finalColor = texelColor * texelColor2;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#version 330
|
#version 330
|
||||||
|
|
||||||
// Input vertex attributes
|
// Input vertex attributes
|
||||||
in vec3 vertexPosition;
|
in vec3 vertexPosition;
|
||||||
in vec2 vertexTexCoord;
|
in vec2 vertexTexCoord;
|
||||||
|
@ -15,13 +16,14 @@ out vec2 fragTexCoord;
|
||||||
out vec2 fragTexCoord2;
|
out vec2 fragTexCoord2;
|
||||||
out vec4 fragColor;
|
out vec4 fragColor;
|
||||||
|
|
||||||
void main() {
|
void main()
|
||||||
|
{
|
||||||
// Send vertex attributes to fragment shader
|
// Send vertex attributes to fragment shader
|
||||||
fragPosition = vec3( matModel * vec4( vertexPosition, 1.0 ) );
|
fragPosition = vec3(matModel*vec4(vertexPosition, 1.0));
|
||||||
fragTexCoord = vertexTexCoord;
|
fragTexCoord = vertexTexCoord;
|
||||||
fragTexCoord2 = vertexTexCoord2;
|
fragTexCoord2 = vertexTexCoord2;
|
||||||
fragColor = vertexColor;
|
fragColor = vertexColor;
|
||||||
|
|
||||||
// Calculate final vertex position
|
// Calculate final vertex position
|
||||||
gl_Position = mvp * vec4( vertexPosition, 1.0 );
|
gl_Position = mvp*vec4(vertexPosition, 1.0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,81 +46,82 @@ int main(void)
|
||||||
|
|
||||||
// Define the camera to look into our 3d world
|
// Define the camera to look into our 3d world
|
||||||
Camera camera = { 0 };
|
Camera camera = { 0 };
|
||||||
camera.position = (Vector3){ 2.0f, 4.0f, 6.0f }; // Camera position
|
camera.position = (Vector3){ 4.0f, 6.0f, 8.0f }; // Camera position
|
||||||
camera.target = (Vector3){ 0.0f, 0.5f, 0.0f }; // Camera looking at point
|
camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
|
||||||
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
|
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
|
||||||
camera.fovy = 45.0f; // Camera field-of-view Y
|
camera.fovy = 45.0f; // Camera field-of-view Y
|
||||||
camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
|
camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
|
||||||
|
|
||||||
Mesh mesh = GenMeshPlane((float)MAP_SIZE, (float)MAP_SIZE, 1, 1);
|
Mesh mesh = GenMeshPlane((float)MAP_SIZE, (float)MAP_SIZE, 1, 1);
|
||||||
|
|
||||||
// GenMeshPlane doesn't generate texcoords2 so we will upload them separately
|
// GenMeshPlane doesn't generate texcoords2 so we will upload them separately
|
||||||
mesh.texcoords2 = (float *)RL_MALLOC(mesh.vertexCount * 2 * sizeof(float));
|
mesh.texcoords2 = (float *)RL_MALLOC(mesh.vertexCount*2*sizeof(float));
|
||||||
|
|
||||||
// X // Y
|
// X // Y
|
||||||
mesh.texcoords2[0] = 0.0f; mesh.texcoords2[1] = 0.0f;
|
mesh.texcoords2[0] = 0.0f; mesh.texcoords2[1] = 0.0f;
|
||||||
mesh.texcoords2[2] = 1.0f; mesh.texcoords2[3] = 0.0f;
|
mesh.texcoords2[2] = 1.0f; mesh.texcoords2[3] = 0.0f;
|
||||||
mesh.texcoords2[4] = 0.0f; mesh.texcoords2[5] = 1.0f;
|
mesh.texcoords2[4] = 0.0f; mesh.texcoords2[5] = 1.0f;
|
||||||
mesh.texcoords2[6] = 1.0f; mesh.texcoords2[7] = 1.0f;
|
mesh.texcoords2[6] = 1.0f; mesh.texcoords2[7] = 1.0f;
|
||||||
|
|
||||||
// Load a new texcoords2 attributes buffer
|
// Load a new texcoords2 attributes buffer
|
||||||
mesh.vboId[SHADER_LOC_VERTEX_TEXCOORD02] = rlLoadVertexBuffer(mesh.texcoords2, mesh.vertexCount*2*sizeof(float), false);
|
mesh.vboId[SHADER_LOC_VERTEX_TEXCOORD02] = rlLoadVertexBuffer(mesh.texcoords2, mesh.vertexCount*2*sizeof(float), false);
|
||||||
rlEnableVertexArray(mesh.vaoId);
|
rlEnableVertexArray(mesh.vaoId);
|
||||||
// Index 5 is for texcoords2
|
|
||||||
rlSetVertexAttribute(5, 2, RL_FLOAT, 0, 0, 0);
|
// Index 5 is for texcoords2
|
||||||
rlEnableVertexAttribute(5);
|
rlSetVertexAttribute(5, 2, RL_FLOAT, 0, 0, 0);
|
||||||
rlDisableVertexArray();
|
rlEnableVertexAttribute(5);
|
||||||
|
rlDisableVertexArray();
|
||||||
|
|
||||||
// Load lightmap shader
|
// Load lightmap shader
|
||||||
Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/lightmap.vs", GLSL_VERSION),
|
Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/lightmap.vs", GLSL_VERSION),
|
||||||
TextFormat("resources/shaders/glsl%i/lightmap.fs", GLSL_VERSION));
|
TextFormat("resources/shaders/glsl%i/lightmap.fs", GLSL_VERSION));
|
||||||
|
|
||||||
Texture texture = LoadTexture("resources/cubicmap_atlas.png");
|
Texture texture = LoadTexture("resources/cubicmap_atlas.png");
|
||||||
Texture light = LoadTexture("resources/spark_flame.png");
|
Texture light = LoadTexture("resources/spark_flame.png");
|
||||||
|
|
||||||
GenTextureMipmaps(&texture);
|
GenTextureMipmaps(&texture);
|
||||||
SetTextureFilter(texture, TEXTURE_FILTER_TRILINEAR);
|
SetTextureFilter(texture, TEXTURE_FILTER_TRILINEAR);
|
||||||
|
|
||||||
RenderTexture lightmap = LoadRenderTexture(MAP_SIZE, MAP_SIZE);
|
RenderTexture lightmap = LoadRenderTexture(MAP_SIZE, MAP_SIZE);
|
||||||
|
|
||||||
SetTextureFilter(lightmap.texture, TEXTURE_FILTER_TRILINEAR);
|
SetTextureFilter(lightmap.texture, TEXTURE_FILTER_TRILINEAR);
|
||||||
|
|
||||||
Material material = LoadMaterialDefault();
|
Material material = LoadMaterialDefault();
|
||||||
material.shader = shader;
|
material.shader = shader;
|
||||||
material.maps[MATERIAL_MAP_ALBEDO].texture = texture;
|
material.maps[MATERIAL_MAP_ALBEDO].texture = texture;
|
||||||
material.maps[MATERIAL_MAP_METALNESS].texture = lightmap.texture;
|
material.maps[MATERIAL_MAP_METALNESS].texture = lightmap.texture;
|
||||||
|
|
||||||
// Drawing to lightmap
|
// Drawing to lightmap
|
||||||
BeginTextureMode(lightmap);
|
BeginTextureMode(lightmap);
|
||||||
ClearBackground(BLACK);
|
ClearBackground(BLACK);
|
||||||
|
|
||||||
BeginBlendMode(BLEND_ADDITIVE);
|
BeginBlendMode(BLEND_ADDITIVE);
|
||||||
DrawTexturePro(
|
DrawTexturePro(
|
||||||
light,
|
light,
|
||||||
(Rectangle){ 0, 0, light.width, light.height },
|
(Rectangle){ 0, 0, light.width, light.height },
|
||||||
(Rectangle){ 0, 0, 20, 20 },
|
(Rectangle){ 0, 0, 20, 20 },
|
||||||
(Vector2){ 10.0, 10.0 },
|
(Vector2){ 10.0, 10.0 },
|
||||||
0.0,
|
0.0,
|
||||||
RED
|
RED
|
||||||
);
|
);
|
||||||
DrawTexturePro(
|
DrawTexturePro(
|
||||||
light,
|
light,
|
||||||
(Rectangle){ 0, 0, light.width, light.height },
|
(Rectangle){ 0, 0, light.width, light.height },
|
||||||
(Rectangle){ 8, 4, 20, 20 },
|
(Rectangle){ 8, 4, 20, 20 },
|
||||||
(Vector2){ 10.0, 10.0 },
|
(Vector2){ 10.0, 10.0 },
|
||||||
0.0,
|
0.0,
|
||||||
BLUE
|
BLUE
|
||||||
);
|
);
|
||||||
DrawTexturePro(
|
DrawTexturePro(
|
||||||
light,
|
light,
|
||||||
(Rectangle){ 0, 0, light.width, light.height },
|
(Rectangle){ 0, 0, light.width, light.height },
|
||||||
(Rectangle){ 8, 8, 10, 10 },
|
(Rectangle){ 8, 8, 10, 10 },
|
||||||
(Vector2){ 5.0, 5.0 },
|
(Vector2){ 5.0, 5.0 },
|
||||||
0.0,
|
0.0,
|
||||||
GREEN
|
GREEN
|
||||||
);
|
);
|
||||||
BeginBlendMode(BLEND_ALPHA);
|
BeginBlendMode(BLEND_ALPHA);
|
||||||
EndTextureMode();
|
EndTextureMode();
|
||||||
|
|
||||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
|
@ -139,26 +140,29 @@ int main(void)
|
||||||
ClearBackground(RAYWHITE);
|
ClearBackground(RAYWHITE);
|
||||||
|
|
||||||
BeginMode3D(camera);
|
BeginMode3D(camera);
|
||||||
DrawMesh(mesh, material, MatrixIdentity());
|
DrawMesh(mesh, material, MatrixIdentity());
|
||||||
EndMode3D();
|
EndMode3D();
|
||||||
|
|
||||||
DrawFPS(10, 10);
|
DrawFPS(10, 10);
|
||||||
|
|
||||||
DrawTexturePro(
|
DrawTexturePro(
|
||||||
lightmap.texture,
|
lightmap.texture,
|
||||||
(Rectangle){ 0, 0, MAP_SIZE, MAP_SIZE },
|
(Rectangle){ 0, 0, -MAP_SIZE, -MAP_SIZE },
|
||||||
(Rectangle){ 0, 36, MAP_SIZE * 4, MAP_SIZE * 4 },
|
(Rectangle){ GetRenderWidth() - MAP_SIZE*8 - 10, 10, MAP_SIZE*8, MAP_SIZE*8 },
|
||||||
(Vector2){ 0.0, 0.0 },
|
(Vector2){ 0.0, 0.0 },
|
||||||
0.0,
|
0.0,
|
||||||
WHITE
|
WHITE);
|
||||||
);
|
|
||||||
|
DrawText("lightmap", GetRenderWidth() - 66, 16 + MAP_SIZE*8, 10, GRAY);
|
||||||
|
DrawText("10x10 pixels", GetRenderWidth() - 76, 30 + MAP_SIZE*8, 10, GRAY);
|
||||||
|
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
}
|
}
|
||||||
|
|
||||||
// De-Initialization
|
// De-Initialization
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
UnloadMesh(mesh); // Unload the mesh
|
UnloadMesh(mesh); // Unload the mesh
|
||||||
UnloadShader(shader); // Unload shader
|
UnloadShader(shader); // Unload shader
|
||||||
|
|
||||||
CloseWindow(); // Close window and OpenGL context
|
CloseWindow(); // Close window and OpenGL context
|
||||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 224 KiB After Width: | Height: | Size: 205 KiB |
Loading…
Add table
Add a link
Reference in a new issue