add basic shader template

This commit is contained in:
Roy Qu 2022-01-23 20:11:49 +08:00
parent df323d30c8
commit 62d7f7edb4
4 changed files with 146 additions and 0 deletions

View File

@ -0,0 +1,28 @@
[Template]
ver=2
Name=raylib 3D Shader
Name[zh_CN]=raylib 3D 着色器
Icon=raylib.ico
Description=A 3D Shader program using raylib ( https://raylib.com )
Description[zh_CN]=带着色器的raylib 3D程序 ( https://raylib.com )
Category=3D
Category[zh_CN]=3D
[Unit0]
CPPName=main.cpp
C=raylib_3d_shader_cpp.txt
[Unit1]
Source=raylib_base.vs
Target=vertices_shader.vs
[Unit2]
Source=raylib_base.fs
Target=fragment_shader.fs
[Project]
UnitCount=3
Type=0
IsCpp=1
linker=-lraylib -lopengl32 -lgdi32 -lwinmm_@@__@@_

View File

@ -0,0 +1,67 @@
#include <raylib.h>
#include <raymath.h>
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
InitWindow(screenWidth, screenHeight, "raylib shader");
// Define the camera to look into our 3d world
Camera camera = { 0 };
camera.position = (Vector3){ 2.0f, 4.0f, 6.0f }; // Camera position
camera.target = (Vector3){ 0.0f, 0.5f, 0.0f }; // Camera looking at point
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.projection = CAMERA_PERSPECTIVE; // Camera mode type
// Load plane model from a generated mesh
Model model = LoadModelFromMesh(GenMeshCube(10.0f, 10.0f, 3.3));
Shader shader = LoadShader("vertices_shader.vs","raylib_base.fs");
// Assign out lighting shader to model
model.materials[0].shader = shader;
SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
UpdateCamera(&camera); // Update camera
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode3D(camera);
DrawModel(model, Vector3Zero(), 1.0f, WHITE);
DrawGrid(10, 1.0f);
EndMode3D();
EndDrawing();
//----------------------------------------------------------------------------------
}
UnloadModel(model); // Unload the model
UnloadShader(shader); // Unload shader
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}

View File

@ -0,0 +1,25 @@
#version 330
// Input vertex attributes (from vertex shader)
in vec2 fragTexCoord;
in vec4 fragColor;
// Input uniform values
uniform sampler2D texture0;
uniform vec4 colDiffuse;
// Output fragment color
out vec4 finalColor;
// NOTE: Add here your custom variables
void main()
{
// Texel color fetching from texture sampler
vec4 texelColor = texture(texture0, fragTexCoord);
// NOTE: Implement here your fragment shader code
finalColor = texelColor*colDiffuse;
}

View File

@ -0,0 +1,26 @@
#version 330
// Input vertex attributes
in vec3 vertexPosition;
in vec2 vertexTexCoord;
in vec3 vertexNormal;
in vec4 vertexColor;
// Input uniform values
uniform mat4 mvp;
// Output vertex attributes (to fragment shader)
out vec2 fragTexCoord;
out vec4 fragColor;
// NOTE: Add here your custom variables
void main()
{
// Send vertex attributes to fragment shader
fragTexCoord = vertexTexCoord;
fragColor = vertexColor;
// Calculate final vertex position
gl_Position = mvp*vec4(vertexPosition, 1.0);
}