From d8bd8634ab7d5179cb1481206176af1f8e592e75 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sat, 5 Mar 2016 13:05:45 +0100 Subject: 3d Camera: Added support for field-of-view Y --- examples/shaders_postprocessing.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/shaders_postprocessing.c') diff --git a/examples/shaders_postprocessing.c b/examples/shaders_postprocessing.c index 0f851658..44829648 100644 --- a/examples/shaders_postprocessing.c +++ b/examples/shaders_postprocessing.c @@ -30,7 +30,7 @@ int main() InitWindow(screenWidth, screenHeight, "raylib [shaders] example - postprocessing shader"); // Define the camera to look into our 3d world - Camera camera = {{ 3.0f, 3.0f, 3.0f }, { 0.0f, 1.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }}; + Camera camera = {{ 3.0f, 3.0f, 3.0f }, { 0.0f, 1.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f }; Model dwarf = LoadModel("resources/model/dwarf.obj"); // Load OBJ model Texture2D texture = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model texture -- cgit v1.2.3 From 66b096d97848eb096a043781f1f305e7189f2a00 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Wed, 30 Mar 2016 20:09:16 +0200 Subject: Added support for render to texture (use RenderTexture2D) Now it's possible to render to texture, old postprocessing system will be removed on next raylib version. --- examples/resources/shaders/base.vs | 3 + examples/resources/shaders/bloom.fs | 3 +- examples/shaders_postprocessing.c | 36 ++++++++--- src/core.c | 22 ++++++- src/raylib.h | 11 ++++ src/rlgl.c | 118 +++++++++++++++++++++++++++++++++++- src/rlgl.h | 11 ++++ src/textures.c | 19 ++++++ 8 files changed, 210 insertions(+), 13 deletions(-) (limited to 'examples/shaders_postprocessing.c') diff --git a/examples/resources/shaders/base.vs b/examples/resources/shaders/base.vs index b0f930b7..9b8cca69 100644 --- a/examples/resources/shaders/base.vs +++ b/examples/resources/shaders/base.vs @@ -3,8 +3,10 @@ in vec3 vertexPosition; in vec2 vertexTexCoord; in vec3 vertexNormal; +in vec4 vertexColor; out vec2 fragTexCoord; +out vec4 fragTintColor; uniform mat4 mvpMatrix; @@ -13,6 +15,7 @@ uniform mat4 mvpMatrix; void main() { fragTexCoord = vertexTexCoord; + fragTintColor = vertexColor; gl_Position = mvpMatrix*vec4(vertexPosition, 1.0); } \ No newline at end of file diff --git a/examples/resources/shaders/bloom.fs b/examples/resources/shaders/bloom.fs index 2833ce33..0a73161a 100644 --- a/examples/resources/shaders/bloom.fs +++ b/examples/resources/shaders/bloom.fs @@ -1,11 +1,12 @@ #version 330 in vec2 fragTexCoord; +in vec4 fragTintColor; out vec4 fragColor; uniform sampler2D texture0; -uniform vec4 fragTintColor; +//uniform vec4 fragTintColor; // NOTE: Add here your custom variables diff --git a/examples/shaders_postprocessing.c b/examples/shaders_postprocessing.c index 44829648..fabf5131 100644 --- a/examples/shaders_postprocessing.c +++ b/examples/shaders_postprocessing.c @@ -41,7 +41,11 @@ int main() Shader shader = LoadShader("resources/shaders/base.vs", "resources/shaders/bloom.fs"); // Load postpro shader - SetPostproShader(shader); // Set fullscreen postprocessing shader + // NOTE: Old postprocessing system is not flexible enough despite being very easy to use + //SetPostproShader(shader); // Set fullscreen postprocessing shader + + // New postprocessing system let the user create multiple RenderTexture2D and perform multiple render passes + RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight); // Setup orbital camera SetCameraMode(CAMERA_ORBITAL); // Set an orbital camera mode @@ -65,15 +69,26 @@ int main() ClearBackground(RAYWHITE); - Begin3dMode(camera); + BeginTextureMode(target); // Enable render to texture RenderTexture2D + + Begin3dMode(camera); - DrawModel(dwarf, position, 2.0f, WHITE); // Draw 3d model with texture + DrawModel(dwarf, position, 2.0f, WHITE); // Draw 3d model with texture - DrawGrid(10, 1.0f); // Draw a grid + DrawGrid(10, 1.0f); // Draw a grid - End3dMode(); + End3dMode(); + + DrawText("HELLO TEXTURE!!!", 120, 200, 60, RED); + + EndTextureMode(); // End drawing to texture (now we have a texture available for next passes) + + SetCustomShader(shader); + // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom) + DrawTextureRec(target.texture, (Rectangle){ 0, target.texture.height, target.texture.width, -target.texture.height }, (Vector2){ 0, 0 }, WHITE); + SetDefaultShader(); - DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, BLACK); + DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, DARKGRAY); DrawFPS(10, 10); @@ -83,11 +98,12 @@ int main() // De-Initialization //-------------------------------------------------------------------------------------- - UnloadShader(shader); // Unload shader - UnloadTexture(texture); // Unload texture - UnloadModel(dwarf); // Unload model + UnloadShader(shader); // Unload shader + UnloadTexture(texture); // Unload texture + UnloadModel(dwarf); // Unload model + UnloadRenderTexture(target); // Unload render texture - CloseWindow(); // Close window and OpenGL context + CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; diff --git a/src/core.c b/src/core.c index 5150389c..5a794376 100644 --- a/src/core.c +++ b/src/core.c @@ -545,7 +545,7 @@ void BeginDrawing(void) if (IsPosproShaderEnabled()) rlEnablePostproFBO(); - rlClearScreenBuffers(); + rlClearScreenBuffers(); // Clear current framebuffers rlLoadIdentity(); // Reset current matrix (MODELVIEW) @@ -656,6 +656,26 @@ void End3dMode(void) rlDisableDepthTest(); // Disable DEPTH_TEST for 2D } +// Initializes render texture for drawing +void BeginTextureMode(RenderTexture2D target) +{ + rlglDraw(); // Draw Buffers (Only OpenGL 3+ and ES2) + + rlEnableRenderTexture(target.id); + + rlClearScreenBuffers(); // Clear render texture buffers + + rlLoadIdentity(); // Reset current matrix (MODELVIEW) +} + +// Ends drawing to render texture +void EndTextureMode(void) +{ + rlglDraw(); // Draw Buffers (Only OpenGL 3+ and ES2) + + rlDisableRenderTexture(); +} + // Set target FPS for the game void SetTargetFPS(int fps) { diff --git a/src/raylib.h b/src/raylib.h index 2b65df9e..c8f2c2a2 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -324,6 +324,13 @@ typedef struct Texture2D { int format; // Data format (TextureFormat) } Texture2D; +// RenderTexture2D type, for texture rendering +typedef struct RenderTexture2D { + unsigned int id; // Render texture (fbo) id + Texture2D texture; // Color buffer attachment texture + Texture2D depth; // Depth buffer attachment texture +} RenderTexture2D; + // SpriteFont type, includes texture and charSet array data typedef struct SpriteFont { Texture2D texture; // Font texture @@ -565,6 +572,8 @@ void EndDrawing(void); // End canvas drawin void Begin3dMode(Camera camera); // Initializes 3D mode for drawing (Camera setup) void End3dMode(void); // Ends 3D mode and returns to default 2D orthographic mode +void BeginTextureMode(RenderTexture2D target); // Initializes render texture for drawing +void EndTextureMode(void); // Ends drawing to render texture Ray GetMouseRay(Vector2 mousePosition, Camera camera); // Returns a ray trace from mouse position Vector2 WorldToScreen(Vector3 position, Camera camera); // Returns the screen space position from a 3d world space position @@ -714,8 +723,10 @@ Texture2D LoadTexture(const char *fileName); Texture2D LoadTextureEx(void *data, int width, int height, int textureFormat); // Load a texture from raw data into GPU memory Texture2D LoadTextureFromRES(const char *rresName, int resId); // Load an image as texture from rRES file (raylib Resource) Texture2D LoadTextureFromImage(Image image); // Load a texture from image data +RenderTexture2D LoadRenderTexture(int width, int height); // Load a texture to be used for rendering void UnloadImage(Image image); // Unload image from CPU memory (RAM) void UnloadTexture(Texture2D texture); // Unload texture from GPU memory +void UnloadRenderTexture(RenderTexture2D target); // Unload render texture from GPU memory Color *GetImageData(Image image); // Get pixel data from image as a Color struct array Image GetTextureData(Texture2D texture); // Get pixel data from GPU texture and return an Image void ImageToPOT(Image *image, Color fillColor); // Convert image to POT (power-of-two) diff --git a/src/rlgl.c b/src/rlgl.c index 39a34095..2153221e 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -785,6 +785,20 @@ void rlDisableTexture(void) #endif } +void rlEnableRenderTexture(unsigned int id) +{ +#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) + glBindFramebuffer(GL_FRAMEBUFFER, id); +#endif +} + +void rlDisableRenderTexture(void) +{ +#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) + glBindFramebuffer(GL_FRAMEBUFFER, 0); +#endif +} + // Enable depth test void rlEnableDepthTest(void) { @@ -803,6 +817,16 @@ void rlDeleteTextures(unsigned int id) glDeleteTextures(1, &id); } +// Unload render texture from GPU memory +void rlDeleteRenderTextures(RenderTexture2D target) +{ +#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) + glDeleteFramebuffers(1, &target.id); + glDeleteTextures(1, &target.texture.id); + glDeleteTextures(1, &target.depth.id); +#endif +} + // Enable rendering to postprocessing FBO void rlEnablePostproFBO() { @@ -1163,7 +1187,7 @@ FBO rlglLoadFBO(int width, int height) glDeleteTextures(1, &fbo.colorTextureId); glDeleteTextures(1, &fbo.depthTextureId); } - else TraceLog(INFO, "[FBO ID %i] Framebuffer object created successfully", fbo); + else TraceLog(INFO, "[FBO ID %i] Framebuffer object created successfully", fbo.id); glBindFramebuffer(GL_FRAMEBUFFER, 0); #endif @@ -1833,6 +1857,98 @@ unsigned int rlglLoadTexture(void *data, int width, int height, int textureForma return id; } +// Load a texture to be used for rendering (fbo with color and depth attachments) +RenderTexture2D rlglLoadRenderTexture(int width, int height) +{ + RenderTexture2D target; + + target.id = 0; + + target.texture.id = 0; + target.texture.width = width; + target.texture.height = height; + target.texture.format = UNCOMPRESSED_R8G8B8; + target.texture.mipmaps = 1; + + target.depth.id = 0; + target.depth.width = width; + target.depth.height = height; + target.depth.format = 19; //DEPTH_COMPONENT_16BIT + target.depth.mipmaps = 1; + +#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) + // Create the texture that will serve as the color attachment for the framebuffer + glGenTextures(1, &target.texture.id); + glBindTexture(GL_TEXTURE_2D, target.texture.id); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); + glBindTexture(GL_TEXTURE_2D, 0); + +#define USE_DEPTH_RENDERBUFFER +#if defined(USE_DEPTH_RENDERBUFFER) + // Create the renderbuffer that will serve as the depth attachment for the framebuffer. + glGenRenderbuffers(1, &target.depth.id); + glBindRenderbuffer(GL_RENDERBUFFER, target.depth.id); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height); + +#elif defined(USE_DEPTH_TEXTURE) + // NOTE: We can also use a texture for depth buffer (GL_ARB_depth_texture/GL_OES_depth_texture extension required) + // A renderbuffer is simpler than a texture and could offer better performance on embedded devices + glGenTextures(1, &target.depth.id); + glBindTexture(GL_TEXTURE_2D, target.depth.id); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL); + glBindTexture(GL_TEXTURE_2D, 0); +#endif + + // Create the framebuffer object + glGenFramebuffers(1, &target.id); + glBindFramebuffer(GL_FRAMEBUFFER, target.id); + + // Attach color texture and depth renderbuffer to FBO + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, target.texture.id, 0); +#if defined(USE_DEPTH_RENDERBUFFER) + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, target.depth.id); +#elif defined(USE_DEPTH_TEXTURE) + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, target.depth.id, 0); +#endif + + GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); + + if (status != GL_FRAMEBUFFER_COMPLETE) + { + TraceLog(WARNING, "Framebuffer object could not be created..."); + + switch(status) + { + case GL_FRAMEBUFFER_UNSUPPORTED: TraceLog(WARNING, "Framebuffer is unsupported"); break; + case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: TraceLog(WARNING, "Framebuffer incomplete attachment"); break; +#if defined(GRAPHICS_API_OPENGL_ES2) + case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS: TraceLog(WARNING, "Framebuffer incomplete dimensions"); break; +#endif + case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: TraceLog(WARNING, "Framebuffer incomplete missing attachment"); break; + default: break; + } + + glDeleteTextures(1, &target.texture.id); + glDeleteTextures(1, &target.depth.id); + glDeleteFramebuffers(1, &target.id); + } + else TraceLog(INFO, "[FBO ID %i] Framebuffer object created successfully", target.id); + + glBindFramebuffer(GL_FRAMEBUFFER, 0); +#endif + + return target; +} + +// Update already loaded texture in GPU with new data void rlglUpdateTexture(unsigned int id, int width, int height, int format, void *data) { glBindTexture(GL_TEXTURE_2D, id); diff --git a/src/rlgl.h b/src/rlgl.h index 1a5260eb..679cb590 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -183,6 +183,13 @@ typedef enum { OPENGL_11 = 1, OPENGL_33, OPENGL_ES_20 } GlVersion; int format; // Data format (TextureFormat) } Texture2D; + // RenderTexture2D type, for texture rendering + typedef struct RenderTexture2D { + unsigned int id; // Render texture (fbo) id + Texture2D texture; // Color buffer attachment texture + Texture2D depth; // Depth buffer attachment texture + } RenderTexture2D; + // Material type typedef struct Material { Shader shader; @@ -248,9 +255,12 @@ void rlColor4f(float x, float y, float z, float w); // Define one vertex (color) //------------------------------------------------------------------------------------ void rlEnableTexture(unsigned int id); // Enable texture usage void rlDisableTexture(void); // Disable texture usage +void rlEnableRenderTexture(unsigned int id); // Enable render texture (fbo) +void rlDisableRenderTexture(void); // Disable render texture (fbo), return to default framebuffer void rlEnableDepthTest(void); // Enable depth test void rlDisableDepthTest(void); // Disable depth test void rlDeleteTextures(unsigned int id); // Delete OpenGL texture from GPU +void rlDeleteRenderTextures(RenderTexture2D target); // Delete render textures (fbo) from GPU void rlDeleteShader(unsigned int id); // Delete OpenGL shader program from GPU void rlDeleteVertexArrays(unsigned int id); // Unload vertex data (VAO) from GPU memory void rlDeleteBuffers(unsigned int id); // Unload vertex data (VBO) from GPU memory @@ -268,6 +278,7 @@ void rlglDraw(void); // Draw VAO/VBO void rlglInitGraphics(int offsetX, int offsetY, int width, int height); // Initialize Graphics (OpenGL stuff) unsigned int rlglLoadTexture(void *data, int width, int height, int textureFormat, int mipmapCount); // Load texture in GPU +RenderTexture2D rlglLoadRenderTexture(int width, int height); // Load a texture to be used for rendering (fbo with color and depth attachments) void rlglUpdateTexture(unsigned int id, int width, int height, int format, void *data); // Update GPU texture with new data void rlglGenerateMipmaps(Texture2D texture); // Generate mipmap data for selected texture diff --git a/src/textures.c b/src/textures.c index 9d0d13b6..67264afb 100644 --- a/src/textures.c +++ b/src/textures.c @@ -389,6 +389,14 @@ Texture2D LoadTextureFromImage(Image image) return texture; } +// Load a texture to be used for rendering +RenderTexture2D LoadRenderTexture(int width, int height) +{ + RenderTexture2D target = rlglLoadRenderTexture(width, height); + + return target; +} + // Unload image from CPU memory (RAM) void UnloadImage(Image image) { @@ -409,6 +417,17 @@ void UnloadTexture(Texture2D texture) } } +// Unload render texture from GPU memory +void UnloadRenderTexture(RenderTexture2D target) +{ + if (target.id != 0) + { + rlDeleteRenderTextures(target); + + TraceLog(INFO, "[FBO ID %i] Unloaded render texture data from VRAM (GPU)", target.id); + } +} + // Get pixel data from image in the form of Color struct array Color *GetImageData(Image image) { -- cgit v1.2.3 From 06a8d7eb06e1573d5cce991ca39f6cb1067ea6fb Mon Sep 17 00:00:00 2001 From: raysan5 Date: Fri, 1 Apr 2016 10:39:33 +0200 Subject: Remove old postprocessing system --- examples/shaders_custom_uniform.c | 7 +- examples/shaders_postprocessing.c | 7 +- src/core.c | 9 +- src/raylib.h | 2 - src/rlgl.c | 244 ++------------------------------------ src/rlgl.h | 6 - 6 files changed, 13 insertions(+), 262 deletions(-) (limited to 'examples/shaders_postprocessing.c') diff --git a/examples/shaders_custom_uniform.c b/examples/shaders_custom_uniform.c index 05e4dcfd..af59dc3c 100644 --- a/examples/shaders_custom_uniform.c +++ b/examples/shaders_custom_uniform.c @@ -47,10 +47,7 @@ int main() float swirlCenter[2] = { (float)screenWidth/2, (float)screenHeight/2 }; - // NOTE: Old postprocessing system is not flexible enough despite being very easy to use - //SetPostproShader(shader); // Set fullscreen postprocessing shader - - // New postprocessing system let the user create multiple RenderTexture2D and perform multiple render passes + // Create a RenderTexture2D to be used for render to texture RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight); // Setup orbital camera @@ -83,7 +80,7 @@ int main() ClearBackground(RAYWHITE); - BeginTextureMode(target); // Enable render to texture RenderTexture2D + BeginTextureMode(target); // Enable drawing to texture Begin3dMode(camera); diff --git a/examples/shaders_postprocessing.c b/examples/shaders_postprocessing.c index fabf5131..0bcd5156 100644 --- a/examples/shaders_postprocessing.c +++ b/examples/shaders_postprocessing.c @@ -41,10 +41,7 @@ int main() Shader shader = LoadShader("resources/shaders/base.vs", "resources/shaders/bloom.fs"); // Load postpro shader - // NOTE: Old postprocessing system is not flexible enough despite being very easy to use - //SetPostproShader(shader); // Set fullscreen postprocessing shader - - // New postprocessing system let the user create multiple RenderTexture2D and perform multiple render passes + // Create a RenderTexture2D to be used for render to texture RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight); // Setup orbital camera @@ -69,7 +66,7 @@ int main() ClearBackground(RAYWHITE); - BeginTextureMode(target); // Enable render to texture RenderTexture2D + BeginTextureMode(target); // Enable drawing to texture Begin3dMode(camera); diff --git a/src/core.c b/src/core.c index 5a794376..ae6f4cad 100644 --- a/src/core.c +++ b/src/core.c @@ -543,12 +543,8 @@ void BeginDrawing(void) updateTime = currentTime - previousTime; previousTime = currentTime; - if (IsPosproShaderEnabled()) rlEnablePostproFBO(); - rlClearScreenBuffers(); // Clear current framebuffers - rlLoadIdentity(); // Reset current matrix (MODELVIEW) - rlMultMatrixf(MatrixToFloat(downscaleView)); // If downscale required, apply it here //rlTranslatef(0.375, 0.375, 0); // HACK to have 2D pixel-perfect drawing on OpenGL 1.1 @@ -578,7 +574,7 @@ void BeginDrawingPro(int blendMode, Shader shader, Matrix transform) BeginDrawing(); SetBlendMode(blendMode); - SetPostproShader(shader); + SetCustomShader(shader); rlMultMatrixf(MatrixToFloat(transform)); } @@ -588,12 +584,11 @@ void EndDrawing(void) { rlglDraw(); // Draw Buffers (Only OpenGL 3+ and ES2) - if (IsPosproShaderEnabled()) rlglDrawPostpro(); // Draw postprocessing effect (shader) - SwapBuffers(); // Copy back buffer to front buffer PollInputEvents(); // Poll user events + // Frame time control system currentTime = GetTime(); drawTime = currentTime - previousTime; previousTime = currentTime; diff --git a/src/raylib.h b/src/raylib.h index c8f2c2a2..22fcb4ac 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -829,11 +829,9 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p Shader LoadShader(char *vsFileName, char *fsFileName); // Load a custom shader and bind default locations unsigned int LoadShaderProgram(char *vShaderStr, char *fShaderStr); // Load custom shaders strings and return program id void UnloadShader(Shader shader); // Unload a custom shader from memory -void SetPostproShader(Shader shader); // Set fullscreen postproduction shader void SetCustomShader(Shader shader); // Set custom shader to be used in batch draw void SetDefaultShader(void); // Set default shader to be used in batch draw void SetModelShader(Model *model, Shader shader); // Link a shader to a model -bool IsPosproShaderEnabled(void); // Check if postprocessing shader is enabled int GetShaderLocation(Shader shader, const char *uniformName); // Get shader uniform location void SetShaderValue(Shader shader, int uniformLoc, float *value, int size); // Set shader uniform value (float) diff --git a/src/rlgl.c b/src/rlgl.c index 2153221e..809077e3 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -176,24 +176,6 @@ typedef struct { // TODO: Store draw state -> blending mode, shader } DrawCall; -// pixel type (same as Color type) -// NOTE: Used exclusively in mipmap generation functions -typedef struct { - unsigned char r; - unsigned char g; - unsigned char b; - unsigned char a; -} pixel; - -// Framebuffer Object type -typedef struct { - GLuint id; - int width; - int height; - GLuint colorTextureId; - GLuint depthTextureId; -} FBO; - #if defined(RLGL_STANDALONE) typedef enum { INFO = 0, ERROR, WARNING, DEBUG, OTHER } TraceLogType; #endif @@ -248,13 +230,6 @@ static bool texCompETC1Supported = false; // ETC1 texture compression support static bool texCompETC2Supported = false; // ETC2/EAC texture compression support static bool texCompPVRTSupported = false; // PVR texture compression support static bool texCompASTCSupported = false; // ASTC texture compression support - -// Framebuffer object and texture -static FBO postproFbo; -static Model postproQuad; - -// Shaders related variables -static bool enabledPostpro = false; #endif // Compressed textures support flags @@ -269,9 +244,6 @@ static PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArrays; //static PFNGLISVERTEXARRAYOESPROC glIsVertexArray; // NOTE: Fails in WebGL, omitted #endif -// Save screen size data (render size), required for postpro quad -static int screenWidth, screenHeight; - static int blendMode = 0; // White texture useful for plain color polys (required by shader) @@ -290,14 +262,11 @@ static void UpdateBuffers(void); static char *TextFileRead(char *fn); static void LoadCompressedTexture(unsigned char *data, int width, int height, int mipmapCount, int compressedFormat); - -FBO rlglLoadFBO(int width, int height); -void rlglUnloadFBO(FBO fbo); #endif #if defined(GRAPHICS_API_OPENGL_11) static int GenerateMipmaps(unsigned char *data, int baseWidth, int baseHeight); -static pixel *GenNextMipmap(pixel *srcData, int srcWidth, int srcHeight); +static Color *GenNextMipmap(Color *srcData, int srcWidth, int srcHeight); #endif #if defined(RLGL_STANDALONE) @@ -827,14 +796,6 @@ void rlDeleteRenderTextures(RenderTexture2D target) #endif } -// Enable rendering to postprocessing FBO -void rlEnablePostproFBO() -{ -#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - glBindFramebuffer(GL_FRAMEBUFFER, postproFbo.id); -#endif -} - // Unload shader from GPU memory void rlDeleteShader(unsigned int id) { @@ -1088,125 +1049,6 @@ void rlglInit(void) #endif } -// Init postpro system -// NOTE: Uses global variables screenWidth and screenHeight -// Modifies global variables: postproFbo, postproQuad -void rlglInitPostpro(void) -{ -#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - postproFbo = rlglLoadFBO(screenWidth, screenHeight); - - if (postproFbo.id > 0) - { - // Create a simple quad model to render fbo texture - Mesh quad; - - quad.vertexCount = 6; - - float w = (float)postproFbo.width; - float h = (float)postproFbo.height; - - float quadPositions[6*3] = { w, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, h, 0.0f, 0.0f, h, 0.0f, w, h, 0.0f, w, 0.0f, 0.0f }; - float quadTexcoords[6*2] = { 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f }; - float quadNormals[6*3] = { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f }; - unsigned char quadColors[6*4] = { 255 }; - - quad.vertices = quadPositions; - quad.texcoords = quadTexcoords; - quad.normals = quadNormals; - quad.colors = quadColors; - - postproQuad = rlglLoadModel(quad); - - // NOTE: postproFbo.colorTextureId must be assigned to postproQuad model shader - } -#endif -} - -// Load a framebuffer object -FBO rlglLoadFBO(int width, int height) -{ - FBO fbo; - fbo.id = 0; - fbo.width = width; - fbo.height = height; - -#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - // Create the texture that will serve as the color attachment for the framebuffer - glGenTextures(1, &fbo.colorTextureId); - glBindTexture(GL_TEXTURE_2D, fbo.colorTextureId); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); - glBindTexture(GL_TEXTURE_2D, 0); - - // Create the renderbuffer that will serve as the depth attachment for the framebuffer. - glGenRenderbuffers(1, &fbo.depthTextureId); - glBindRenderbuffer(GL_RENDERBUFFER, fbo.depthTextureId); - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height); - - // NOTE: We can also use a texture for depth buffer (GL_ARB_depth_texture/GL_OES_depth_texture extensions) - // A renderbuffer is simpler than a texture and could offer better performance on embedded devices -/* - glGenTextures(1, &fbo.depthTextureId); - glBindTexture(GL_TEXTURE_2D, fbo.depthTextureId); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL); - glBindTexture(GL_TEXTURE_2D, 0); -*/ - // Create the framebuffer object - glGenFramebuffers(1, &fbo.id); - glBindFramebuffer(GL_FRAMEBUFFER, fbo.id); - - // Attach color texture and depth renderbuffer to FBO - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fbo.colorTextureId, 0); - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fbo.depthTextureId); - - GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); - - if (status != GL_FRAMEBUFFER_COMPLETE) - { - TraceLog(WARNING, "Framebuffer object could not be created..."); - - switch(status) - { - case GL_FRAMEBUFFER_UNSUPPORTED: TraceLog(WARNING, "Framebuffer is unsupported"); break; - case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: TraceLog(WARNING, "Framebuffer incomplete attachment"); break; -#if defined(GRAPHICS_API_OPENGL_ES2) - case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS: TraceLog(WARNING, "Framebuffer incomplete dimensions"); break; -#endif - case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: TraceLog(WARNING, "Framebuffer incomplete missing attachment"); break; - default: break; - } - - glDeleteTextures(1, &fbo.colorTextureId); - glDeleteTextures(1, &fbo.depthTextureId); - } - else TraceLog(INFO, "[FBO ID %i] Framebuffer object created successfully", fbo.id); - - glBindFramebuffer(GL_FRAMEBUFFER, 0); -#endif - - return fbo; -} - -// Unload framebuffer object -void rlglUnloadFBO(FBO fbo) -{ -#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - glDeleteFramebuffers(1, &fbo.id); - glDeleteTextures(1, &fbo.colorTextureId); - glDeleteTextures(1, &fbo.depthTextureId); - - TraceLog(INFO, "[FBO ID %i] Unloaded framebuffer object successfully", fbo.id); -#endif -} - // Vertex Buffer Object deinitialization (memory free) void rlglClose(void) { @@ -1263,20 +1105,6 @@ void rlglClose(void) glDeleteTextures(1, &whiteTexture); TraceLog(INFO, "[TEX ID %i] Unloaded texture data (base white texture) from VRAM", whiteTexture); - if (postproFbo.id != 0) - { - rlglUnloadFBO(postproFbo); - - // Unload postpro quad model data - rlDeleteBuffers(postproQuad.mesh.vboId[0]); - rlDeleteBuffers(postproQuad.mesh.vboId[1]); - rlDeleteBuffers(postproQuad.mesh.vboId[2]); - - rlDeleteVertexArrays(postproQuad.mesh.vaoId); - - TraceLog(INFO, "Unloaded postprocessing data"); - } - free(draws); #endif } @@ -1443,16 +1271,6 @@ void rlglDraw(void) #endif } -// Draw with postprocessing shader -void rlglDrawPostpro(void) -{ -#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - glBindFramebuffer(GL_FRAMEBUFFER, 0); - - rlglDrawModel(postproQuad, (Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, 0.0f, 0.0f }, 0.0f, (Vector3){1.0f, 1.0f, 1.0f}, (Color){ 255, 255, 255, 255 }, false); -#endif -} - // Draw a 3d model // NOTE: Model transform can come within model struct void rlglDrawModel(Model model, Vector3 position, Vector3 rotationAxis, float rotationAngle, Vector3 scale, Color color, bool wires) @@ -1606,12 +1424,7 @@ void rlglDrawModel(Model model, Vector3 position, Vector3 rotationAxis, float ro // Initialize Graphics Device (OpenGL stuff) // NOTE: Stores global variables screenWidth and screenHeight void rlglInitGraphics(int offsetX, int offsetY, int width, int height) -{ - // Save screen size data (global vars), required on postpro quad - // NOTE: Size represents render size, it could differ from screen size! - screenWidth = width; - screenHeight = height; - +{ // NOTE: Required! viewport must be recalculated if screen resized! glViewport(offsetX/2, offsetY/2, width - offsetX, height - offsetY); // Set viewport width and height @@ -2458,44 +2271,11 @@ void SetCustomShader(Shader shader) #endif } -// Set postprocessing shader -void SetPostproShader(Shader shader) -{ -#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - if (!enabledPostpro) - { - enabledPostpro = true; - rlglInitPostpro(); // Lazy initialization on postprocessing usage - } - - SetModelShader(&postproQuad, shader); - - Texture2D texture; - texture.id = postproFbo.colorTextureId; - texture.width = postproFbo.width; - texture.height = postproFbo.height; - - postproQuad.material.texDiffuse = texture; - - //TraceLog(DEBUG, "Postproquad texture id: %i", postproQuad.texture.id); - //TraceLog(DEBUG, "Postproquad shader diffuse map id: %i", postproQuad.shader.texDiffuseId); - //TraceLog(DEBUG, "Shader diffuse map id: %i", shader.texDiffuseId); -#elif defined(GRAPHICS_API_OPENGL_11) - TraceLog(WARNING, "Shaders not supported on OpenGL 1.1"); -#endif -} - // Set default shader to be used in batch draw void SetDefaultShader(void) { #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) SetCustomShader(defaultShader); - - if (enabledPostpro) - { - SetPostproShader(defaultShader); - enabledPostpro = false; - } #endif } @@ -2529,16 +2309,6 @@ void SetModelShader(Model *model, Shader shader) #endif } -// Check if postprocessing is enabled (used in module: core) -bool IsPosproShaderEnabled(void) -{ -#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - return enabledPostpro; -#elif defined(GRAPHICS_API_OPENGL_11) - return false; -#endif -} - // Get shader uniform location int GetShaderLocation(Shader shader, const char *uniformName) { @@ -3120,8 +2890,8 @@ static int GenerateMipmaps(unsigned char *data, int baseWidth, int baseHeight) // Generate mipmaps // NOTE: Every mipmap data is stored after data - pixel *image = (pixel *)malloc(width*height*sizeof(pixel)); - pixel *mipmap = NULL; + Color *image = (Color *)malloc(width*height*sizeof(Color)); + Color *mipmap = NULL; int offset = 0; int j = 0; @@ -3169,15 +2939,15 @@ static int GenerateMipmaps(unsigned char *data, int baseWidth, int baseHeight) } // Manual mipmap generation (basic scaling algorithm) -static pixel *GenNextMipmap(pixel *srcData, int srcWidth, int srcHeight) +static Color *GenNextMipmap(Color *srcData, int srcWidth, int srcHeight) { int x2, y2; - pixel prow, pcol; + Color prow, pcol; int width = srcWidth/2; int height = srcHeight/2; - pixel *mipmap = (pixel *)malloc(width*height*sizeof(pixel)); + Color *mipmap = (Color *)malloc(width*height*sizeof(Color)); // Scaling algorithm works perfectly (box-filter) for (int y = 0; y < height; y++) diff --git a/src/rlgl.h b/src/rlgl.h index 679cb590..714961e1 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -267,7 +267,6 @@ void rlDeleteBuffers(unsigned int id); // Unload vertex data (VBO) from void rlClearColor(byte r, byte g, byte b, byte a); // Clear color buffer with color void rlClearScreenBuffers(void); // Clear used screen buffers (color and depth) int rlGetVersion(void); // Returns current OpenGL version -void rlEnablePostproFBO(void); // Enable rendering to postprocessing FBO //------------------------------------------------------------------------------------ // Functions Declaration - rlgl functionality @@ -285,9 +284,6 @@ void rlglGenerateMipmaps(Texture2D texture); // Gene // NOTE: There is a set of shader related functions that are available to end user, // to avoid creating function wrappers through core module, they have been directly declared in raylib.h -void rlglInitPostpro(void); // Initialize postprocessing system -void rlglDrawPostpro(void); // Draw with postprocessing shader - Model rlglLoadModel(Mesh mesh); // Upload vertex data into GPU and provided VAO/VBO ids void rlglDrawModel(Model model, Vector3 position, Vector3 rotationAxis, float rotationAngle, Vector3 scale, Color color, bool wires); @@ -309,11 +305,9 @@ void PrintModelviewMatrix(void); // DEBUG: Print modelview matrix Shader LoadShader(char *vsFileName, char *fsFileName); // Load a custom shader and bind default locations unsigned int LoadShaderProgram(char *vShaderStr, char *fShaderStr); // Load custom shader strings and return program id void UnloadShader(Shader shader); // Unload a custom shader from memory -void SetPostproShader(Shader shader); // Set fullscreen postproduction shader void SetCustomShader(Shader shader); // Set custom shader to be used in batch draw void SetDefaultShader(void); // Set default shader to be used in batch draw void SetModelShader(Model *model, Shader shader); // Link a shader to a model -bool IsPosproShaderEnabled(void); // Check if postprocessing shader is enabled int GetShaderLocation(Shader shader, const char *uniformName); // Get shader uniform location void SetShaderValue(Shader shader, int uniformLoc, float *value, int size); // Set shader uniform value (float) -- cgit v1.2.3 From 1d545449bb19148b45d2d92f213e1001a8eb527c Mon Sep 17 00:00:00 2001 From: raysan5 Date: Thu, 7 Apr 2016 12:32:32 +0200 Subject: Reviewed shaders and added comments --- examples/resources/shaders/base.vs | 21 ------- examples/resources/shaders/bloom.fs | 43 -------------- examples/resources/shaders/glsl100/base.vs | 26 +++++++++ examples/resources/shaders/glsl100/bloom.fs | 37 ++++++++++++ examples/resources/shaders/glsl100/grayscale.fs | 25 ++++++++ examples/resources/shaders/glsl100/swirl.fs | 45 +++++++++++++++ examples/resources/shaders/glsl330/base.vs | 26 +++++++++ examples/resources/shaders/glsl330/bloom.fs | 38 +++++++++++++ examples/resources/shaders/glsl330/grayscale.fs | 26 +++++++++ examples/resources/shaders/glsl330/phong.fs | 76 +++++++++++++++++++++++++ examples/resources/shaders/glsl330/phong.vs | 29 ++++++++++ examples/resources/shaders/glsl330/swirl.fs | 46 +++++++++++++++ examples/resources/shaders/grayscale.fs | 20 ------- examples/resources/shaders/phong.fs | 76 ------------------------- examples/resources/shaders/phong.vs | 29 ---------- examples/resources/shaders/shapes_base.vs | 18 ------ examples/resources/shaders/shapes_grayscale.fs | 15 ----- examples/resources/shaders/swirl.fs | 41 ------------- examples/shaders_custom_uniform.c | 6 +- examples/shaders_model_shader.c | 4 +- examples/shaders_postprocessing.c | 6 +- examples/shaders_shapes_textures.c | 7 +-- 22 files changed, 385 insertions(+), 275 deletions(-) delete mode 100644 examples/resources/shaders/base.vs delete mode 100644 examples/resources/shaders/bloom.fs create mode 100644 examples/resources/shaders/glsl100/base.vs create mode 100644 examples/resources/shaders/glsl100/bloom.fs create mode 100644 examples/resources/shaders/glsl100/grayscale.fs create mode 100644 examples/resources/shaders/glsl100/swirl.fs create mode 100644 examples/resources/shaders/glsl330/base.vs create mode 100644 examples/resources/shaders/glsl330/bloom.fs create mode 100644 examples/resources/shaders/glsl330/grayscale.fs create mode 100644 examples/resources/shaders/glsl330/phong.fs create mode 100644 examples/resources/shaders/glsl330/phong.vs create mode 100644 examples/resources/shaders/glsl330/swirl.fs delete mode 100644 examples/resources/shaders/grayscale.fs delete mode 100644 examples/resources/shaders/phong.fs delete mode 100644 examples/resources/shaders/phong.vs delete mode 100644 examples/resources/shaders/shapes_base.vs delete mode 100644 examples/resources/shaders/shapes_grayscale.fs delete mode 100644 examples/resources/shaders/swirl.fs (limited to 'examples/shaders_postprocessing.c') diff --git a/examples/resources/shaders/base.vs b/examples/resources/shaders/base.vs deleted file mode 100644 index 9b8cca69..00000000 --- a/examples/resources/shaders/base.vs +++ /dev/null @@ -1,21 +0,0 @@ -#version 330 - -in vec3 vertexPosition; -in vec2 vertexTexCoord; -in vec3 vertexNormal; -in vec4 vertexColor; - -out vec2 fragTexCoord; -out vec4 fragTintColor; - -uniform mat4 mvpMatrix; - -// NOTE: Add here your custom variables - -void main() -{ - fragTexCoord = vertexTexCoord; - fragTintColor = vertexColor; - - gl_Position = mvpMatrix*vec4(vertexPosition, 1.0); -} \ No newline at end of file diff --git a/examples/resources/shaders/bloom.fs b/examples/resources/shaders/bloom.fs deleted file mode 100644 index 0a73161a..00000000 --- a/examples/resources/shaders/bloom.fs +++ /dev/null @@ -1,43 +0,0 @@ -#version 330 - -in vec2 fragTexCoord; -in vec4 fragTintColor; - -out vec4 fragColor; - -uniform sampler2D texture0; -//uniform vec4 fragTintColor; - -// NOTE: Add here your custom variables - -void main() -{ - vec4 sum = vec4(0); - vec4 tc = vec4(0); - - for (int i = -4; i < 4; i++) - { - for (int j = -3; j < 3; j++) - { - sum += texture2D(texture0, fragTexCoord + vec2(j, i)*0.004) * 0.25; - } - } - - if (texture2D(texture0, fragTexCoord).r < 0.3) - { - tc = sum*sum*0.012 + texture2D(texture0, fragTexCoord); - } - else - { - if (texture2D(texture0, fragTexCoord).r < 0.5) - { - tc = sum*sum*0.009 + texture2D(texture0, fragTexCoord); - } - else - { - tc = sum*sum*0.0075 + texture2D(texture0, fragTexCoord); - } - } - - fragColor = tc; -} \ No newline at end of file diff --git a/examples/resources/shaders/glsl100/base.vs b/examples/resources/shaders/glsl100/base.vs new file mode 100644 index 00000000..e9386939 --- /dev/null +++ b/examples/resources/shaders/glsl100/base.vs @@ -0,0 +1,26 @@ +#version 100 + +// Input vertex attributes +attribute vec3 vertexPosition; +attribute vec2 vertexTexCoord; +attribute vec3 vertexNormal; +attribute vec4 vertexColor; + +// Input uniform values +uniform mat4 mvpMatrix; + +// Output vertex attributes (to fragment shader) +varying vec2 fragTexCoord; +varying 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 = mvpMatrix*vec4(vertexPosition, 1.0); +} \ No newline at end of file diff --git a/examples/resources/shaders/glsl100/bloom.fs b/examples/resources/shaders/glsl100/bloom.fs new file mode 100644 index 00000000..5a08843d --- /dev/null +++ b/examples/resources/shaders/glsl100/bloom.fs @@ -0,0 +1,37 @@ +#version 100 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 fragTintColor; + +// NOTE: Add here your custom variables + +void main() +{ + vec4 sum = vec4(0); + vec4 tc = vec4(0); + + for (int i = -4; i < 4; i++) + { + for (int j = -3; j < 3; j++) + { + sum += texture2D(texture0, fragTexCoord + vec2(j, i)*0.004) * 0.25; + } + } + + // Texel color fetching from texture sampler + vec4 texelColor = texture(texture0, fragTexCoord); + + // Calculate final fragment color + if (texelColor.r < 0.3) tc = sum*sum*0.012 + texelColor; + else if (texelColor.r < 0.5) tc = sum*sum*0.009 + texelColor; + else tc = sum*sum*0.0075 + texelColor; + + finalColor = tc; +} \ No newline at end of file diff --git a/examples/resources/shaders/glsl100/grayscale.fs b/examples/resources/shaders/glsl100/grayscale.fs new file mode 100644 index 00000000..f92ec335 --- /dev/null +++ b/examples/resources/shaders/glsl100/grayscale.fs @@ -0,0 +1,25 @@ +#version 100 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 fragTintColor; + +// NOTE: Add here your custom variables + +void main() +{ + // Texel color fetching from texture sampler + vec4 texelColor = texture(texture0, fragTexCoord)*fragTintColor*fragColor; + + // Convert texel color to grayscale using NTSC conversion weights + float gray = dot(texelColor.rgb, vec3(0.299, 0.587, 0.114)); + + // Calculate final fragment color + gl_FragColor = vec4(gray, gray, gray, texelColor.a); +} \ No newline at end of file diff --git a/examples/resources/shaders/glsl100/swirl.fs b/examples/resources/shaders/glsl100/swirl.fs new file mode 100644 index 00000000..e77d4f87 --- /dev/null +++ b/examples/resources/shaders/glsl100/swirl.fs @@ -0,0 +1,45 @@ +#version 100 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 fragTintColor; + +// NOTE: Add here your custom variables + +const float renderWidth = 800.0; // HARDCODED for example! +const float renderHeight = 480.0; // Use uniforms instead... + +float radius = 250.0; +float angle = 0.8; + +uniform vec2 center = vec2(200.0, 200.0); + +void main (void) +{ + vec2 texSize = vec2(renderWidth, renderHeight); + vec2 tc = fragTexCoord*texSize; + tc -= center; + + float dist = length(tc); + + if (dist < radius) + { + float percent = (radius - dist)/radius; + float theta = percent*percent*angle*8.0; + float s = sin(theta); + float c = cos(theta); + + tc = vec2(dot(tc, vec2(c, -s)), dot(tc, vec2(s, c))); + } + + tc += center; + vec3 color = texture2D(texture0, tc/texSize).rgb; + + gl_FragColor = vec4(color, 1.0);; +} \ No newline at end of file diff --git a/examples/resources/shaders/glsl330/base.vs b/examples/resources/shaders/glsl330/base.vs new file mode 100644 index 00000000..638cb8ae --- /dev/null +++ b/examples/resources/shaders/glsl330/base.vs @@ -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 mvpMatrix; + +// 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 = mvpMatrix*vec4(vertexPosition, 1.0); +} \ No newline at end of file diff --git a/examples/resources/shaders/glsl330/bloom.fs b/examples/resources/shaders/glsl330/bloom.fs new file mode 100644 index 00000000..c8cb0d32 --- /dev/null +++ b/examples/resources/shaders/glsl330/bloom.fs @@ -0,0 +1,38 @@ +#version 330 + +// Input vertex attributes (from vertex shader) +in vec2 fragTexCoord; +in vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 fragTintColor; + +// Output fragment color +out vec4 finalColor; + +// NOTE: Add here your custom variables + +void main() +{ + vec4 sum = vec4(0); + vec4 tc = vec4(0); + + for (int i = -4; i < 4; i++) + { + for (int j = -3; j < 3; j++) + { + sum += texture2D(texture0, fragTexCoord + vec2(j, i)*0.004)*0.25; + } + } + + // Texel color fetching from texture sampler + vec4 texelColor = texture(texture0, fragTexCoord); + + // Calculate final fragment color + if (texelColor.r < 0.3) tc = sum*sum*0.012 + texelColor; + else if (texelColor.r < 0.5) tc = sum*sum*0.009 + texelColor; + else tc = sum*sum*0.0075 + texelColor; + + finalColor = tc; +} \ No newline at end of file diff --git a/examples/resources/shaders/glsl330/grayscale.fs b/examples/resources/shaders/glsl330/grayscale.fs new file mode 100644 index 00000000..d4a8824f --- /dev/null +++ b/examples/resources/shaders/glsl330/grayscale.fs @@ -0,0 +1,26 @@ +#version 330 + +// Input vertex attributes (from vertex shader) +in vec2 fragTexCoord; +in vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 fragTintColor; + +// 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)*fragTintColor*fragColor; + + // Convert texel color to grayscale using NTSC conversion weights + float gray = dot(texelColor.rgb, vec3(0.299, 0.587, 0.114)); + + // Calculate final fragment color + finalColor = vec4(gray, gray, gray, texelColor.a); +} \ No newline at end of file diff --git a/examples/resources/shaders/glsl330/phong.fs b/examples/resources/shaders/glsl330/phong.fs new file mode 100644 index 00000000..2e7a95f6 --- /dev/null +++ b/examples/resources/shaders/glsl330/phong.fs @@ -0,0 +1,76 @@ +#version 330 + +// Input vertex attributes (from vertex shader) +in vec2 fragTexCoord; +in vec3 fragNormal; + +// Diffuse data +uniform sampler2D texture0; +uniform vec4 fragTintColor; + +// Light attributes +uniform vec3 light_ambientColor = vec3(0.6, 0.3, 0.0); +uniform vec3 light_diffuseColor = vec3(1.0, 0.5, 0.0); +uniform vec3 light_specularColor = vec3(0.0, 1.0, 0.0); +uniform float light_intensity = 1.0; +uniform float light_specIntensity = 1.0; + +// Material attributes +uniform vec3 mat_ambientColor = vec3(1.0, 1.0, 1.0); +uniform vec3 mat_specularColor = vec3(1.0, 1.0, 1.0); +uniform float mat_glossiness = 50.0; + +// World attributes +uniform vec3 lightPos; +uniform vec3 cameraPos; + +// Fragment shader output data +out vec4 fragColor; + +vec3 AmbientLighting() +{ + return (mat_ambientColor*light_ambientColor); +} + +vec3 DiffuseLighting(in vec3 N, in vec3 L) +{ + // Lambertian reflection calculation + float diffuse = clamp(dot(N, L), 0, 1); + + return (fragTintColor.xyz*light_diffuseColor*light_intensity*diffuse); +} + +vec3 SpecularLighting(in vec3 N, in vec3 L, in vec3 V) +{ + float specular = 0.0; + + // Calculate specular reflection only if the surface is oriented to the light source + if (dot(N, L) > 0) + { + // Calculate half vector + vec3 H = normalize(L + V); + + // Calculate specular intensity + specular = pow(dot(N, H), 3 + mat_glossiness); + } + + return (mat_specularColor*light_specularColor*light_specIntensity*specular); +} + +void main() +{ + // Normalize input vectors + vec3 L = normalize(lightPos); + vec3 V = normalize(cameraPos); + vec3 N = normalize(fragNormal); + + vec3 ambient = AmbientLighting(); + vec3 diffuse = DiffuseLighting(N, L); + vec3 specular = SpecularLighting(N, L, V); + + // Get base color from texture + vec4 textureColor = texture(texture0, fragTexCoord); + vec3 finalColor = textureColor.rgb; + + fragColor = vec4(finalColor * (ambient + diffuse + specular), textureColor.a); +} \ No newline at end of file diff --git a/examples/resources/shaders/glsl330/phong.vs b/examples/resources/shaders/glsl330/phong.vs new file mode 100644 index 00000000..d68d9b3f --- /dev/null +++ b/examples/resources/shaders/glsl330/phong.vs @@ -0,0 +1,29 @@ +#version 330 + +// Input vertex attributes +in vec3 vertexPosition; +in vec2 vertexTexCoord; +in vec3 vertexNormal; + +// Input uniform values +uniform mat4 mvpMatrix; + +// Output vertex attributes (to fragment shader) +out vec2 fragTexCoord; +out vec3 fragNormal; + +// NOTE: Add here your custom variables +uniform mat4 modelMatrix; + +void main() +{ + // Send vertex attributes to fragment shader + fragTexCoord = vertexTexCoord; + + // Calculate view vector normal from model + mat3 normalMatrix = transpose(inverse(mat3(modelMatrix))); + fragNormal = normalize(normalMatrix*vertexNormal); + + // Calculate final vertex position + gl_Position = mvpMatrix*vec4(vertexPosition, 1.0); +} \ No newline at end of file diff --git a/examples/resources/shaders/glsl330/swirl.fs b/examples/resources/shaders/glsl330/swirl.fs new file mode 100644 index 00000000..b1dc82f0 --- /dev/null +++ b/examples/resources/shaders/glsl330/swirl.fs @@ -0,0 +1,46 @@ +#version 330 + +// Input vertex attributes (from vertex shader) +in vec2 fragTexCoord; +in vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 fragTintColor; + +// Output fragment color +out vec4 finalColor; + +// NOTE: Add here your custom variables + +const float renderWidth = 800.0; // HARDCODED for example! +const float renderHeight = 480.0; // Use uniforms instead... + +float radius = 250.0; +float angle = 0.8; + +uniform vec2 center = vec2(200.0, 200.0); + +void main (void) +{ + vec2 texSize = vec2(renderWidth, renderHeight); + vec2 tc = fragTexCoord*texSize; + tc -= center; + + float dist = length(tc); + + if (dist < radius) + { + float percent = (radius - dist)/radius; + float theta = percent*percent*angle*8.0; + float s = sin(theta); + float c = cos(theta); + + tc = vec2(dot(tc, vec2(c, -s)), dot(tc, vec2(s, c))); + } + + tc += center; + vec3 color = texture2D(texture0, tc/texSize).rgb; + + finalColor = vec4(color, 1.0);; +} \ No newline at end of file diff --git a/examples/resources/shaders/grayscale.fs b/examples/resources/shaders/grayscale.fs deleted file mode 100644 index af50b8c1..00000000 --- a/examples/resources/shaders/grayscale.fs +++ /dev/null @@ -1,20 +0,0 @@ -#version 330 - -in vec2 fragTexCoord; - -out vec4 fragColor; - -uniform sampler2D texture0; -uniform vec4 fragTintColor; - -// NOTE: Add here your custom variables - -void main() -{ - vec4 base = texture2D(texture0, fragTexCoord)*fragTintColor; - - // Convert to grayscale using NTSC conversion weights - float gray = dot(base.rgb, vec3(0.299, 0.587, 0.114)); - - fragColor = vec4(gray, gray, gray, fragTintColor.a); -} \ No newline at end of file diff --git a/examples/resources/shaders/phong.fs b/examples/resources/shaders/phong.fs deleted file mode 100644 index f79413d9..00000000 --- a/examples/resources/shaders/phong.fs +++ /dev/null @@ -1,76 +0,0 @@ -#version 330 - -// Vertex shader input data -in vec2 fragTexCoord; -in vec3 fragNormal; - -// Diffuse data -uniform sampler2D texture0; -uniform vec4 fragTintColor; - -// Light attributes -uniform vec3 light_ambientColor = vec3(0.6, 0.3, 0.0); -uniform vec3 light_diffuseColor = vec3(1.0, 0.5, 0.0); -uniform vec3 light_specularColor = vec3(0.0, 1.0, 0.0); -uniform float light_intensity = 1.0; -uniform float light_specIntensity = 1.0; - -// Material attributes -uniform vec3 mat_ambientColor = vec3(1.0, 1.0, 1.0); -uniform vec3 mat_specularColor = vec3(1.0, 1.0, 1.0); -uniform float mat_glossiness = 50.0; - -// World attributes -uniform vec3 lightPos; -uniform vec3 cameraPos; - -// Fragment shader output data -out vec4 fragColor; - -vec3 AmbientLighting() -{ - return (mat_ambientColor*light_ambientColor); -} - -vec3 DiffuseLighting(in vec3 N, in vec3 L) -{ - // Lambertian reflection calculation - float diffuse = clamp(dot(N, L), 0, 1); - - return (fragTintColor.xyz*light_diffuseColor*light_intensity*diffuse); -} - -vec3 SpecularLighting(in vec3 N, in vec3 L, in vec3 V) -{ - float specular = 0.0; - - // Calculate specular reflection only if the surface is oriented to the light source - if (dot(N, L) > 0) - { - // Calculate half vector - vec3 H = normalize(L + V); - - // Calculate specular intensity - specular = pow(dot(N, H), 3 + mat_glossiness); - } - - return (mat_specularColor*light_specularColor*light_specIntensity*specular); -} - -void main() -{ - // Normalize input vectors - vec3 L = normalize(lightPos); - vec3 V = normalize(cameraPos); - vec3 N = normalize(fragNormal); - - vec3 ambient = AmbientLighting(); - vec3 diffuse = DiffuseLighting(N, L); - vec3 specular = SpecularLighting(N, L, V); - - // Get base color from texture - vec4 textureColor = texture(texture0, fragTexCoord); - vec3 finalColor = textureColor.rgb; - - fragColor = vec4(finalColor * (ambient + diffuse + specular), textureColor.a); -} \ No newline at end of file diff --git a/examples/resources/shaders/phong.vs b/examples/resources/shaders/phong.vs deleted file mode 100644 index 52cc2227..00000000 --- a/examples/resources/shaders/phong.vs +++ /dev/null @@ -1,29 +0,0 @@ -#version 330 - -// Vertex input data -in vec3 vertexPosition; -in vec2 vertexTexCoord; -in vec3 vertexNormal; - -// Projection and model data -uniform mat4 mvpMatrix; - -uniform mat4 modelMatrix; -//uniform mat4 viewMatrix; // Not used - -// Attributes to fragment shader -out vec2 fragTexCoord; -out vec3 fragNormal; - -void main() -{ - // Send texture coord to fragment shader - fragTexCoord = vertexTexCoord; - - // Calculate view vector normal from model - mat3 normalMatrix = transpose(inverse(mat3(modelMatrix))); - fragNormal = normalize(normalMatrix*vertexNormal); - - // Calculate final vertex position - gl_Position = mvpMatrix*vec4(vertexPosition, 1.0); -} \ No newline at end of file diff --git a/examples/resources/shaders/shapes_base.vs b/examples/resources/shaders/shapes_base.vs deleted file mode 100644 index ad272dc1..00000000 --- a/examples/resources/shaders/shapes_base.vs +++ /dev/null @@ -1,18 +0,0 @@ -#version 330 - -attribute vec3 vertexPosition; -attribute vec2 vertexTexCoord; -attribute vec4 vertexColor; - -uniform mat4 mvpMatrix; - -varying vec2 fragTexCoord; -varying vec4 fragTintColor; - -void main() -{ - fragTexCoord = vertexTexCoord; - fragTintColor = vertexColor; - - gl_Position = mvpMatrix*vec4(vertexPosition, 1.0); -} \ No newline at end of file diff --git a/examples/resources/shaders/shapes_grayscale.fs b/examples/resources/shaders/shapes_grayscale.fs deleted file mode 100644 index 0698e1bf..00000000 --- a/examples/resources/shaders/shapes_grayscale.fs +++ /dev/null @@ -1,15 +0,0 @@ -#version 330 - -uniform sampler2D texture0; -varying vec2 fragTexCoord; -varying vec4 fragTintColor; - -void main() -{ - vec4 base = texture2D(texture0, fragTexCoord)*fragTintColor; - - // Convert to grayscale using NTSC conversion weights - float gray = dot(base.rgb, vec3(0.299, 0.587, 0.114)); - - gl_FragColor = vec4(gray, gray, gray, base.a); -} \ No newline at end of file diff --git a/examples/resources/shaders/swirl.fs b/examples/resources/shaders/swirl.fs deleted file mode 100644 index f89ef406..00000000 --- a/examples/resources/shaders/swirl.fs +++ /dev/null @@ -1,41 +0,0 @@ -#version 330 - -in vec2 fragTexCoord; - -out vec4 fragColor; - -uniform sampler2D texture0; -uniform vec4 fragTintColor; - -// NOTE: Add here your custom variables - -const float renderWidth = 800.0; // HARDCODED for example! -const float renderHeight = 480.0; // Use uniforms instead... - -float radius = 250.0; -float angle = 0.8; - -uniform vec2 center = vec2(200.0, 200.0); - -void main (void) -{ - vec2 texSize = vec2(renderWidth, renderHeight); - vec2 tc = fragTexCoord*texSize; - tc -= center; - float dist = length(tc); - - if (dist < radius) - { - float percent = (radius - dist)/radius; - float theta = percent*percent*angle*8.0; - float s = sin(theta); - float c = cos(theta); - - tc = vec2(dot(tc, vec2(c, -s)), dot(tc, vec2(s, c))); - } - - tc += center; - vec3 color = texture2D(texture0, tc/texSize).rgb; - - fragColor = vec4(color, 1.0);; -} \ No newline at end of file diff --git a/examples/shaders_custom_uniform.c b/examples/shaders_custom_uniform.c index af59dc3c..ceaa86df 100644 --- a/examples/shaders_custom_uniform.c +++ b/examples/shaders_custom_uniform.c @@ -36,10 +36,10 @@ int main() Texture2D texture = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model texture SetModelTexture(&dwarf, texture); // Bind texture to model - Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position + Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position - Shader shader = LoadShader("resources/shaders/base.vs", - "resources/shaders/swirl.fs"); // Load postpro shader + Shader shader = LoadShader("resources/shaders/glsl330/base.vs", + "resources/shaders/glsl330/swirl.fs"); // Load postpro shader // Get variable (uniform) location on the shader to connect with the program // NOTE: If uniform variable could not be found in the shader, function returns -1 diff --git a/examples/shaders_model_shader.c b/examples/shaders_model_shader.c index a10ec235..b302f631 100644 --- a/examples/shaders_model_shader.c +++ b/examples/shaders_model_shader.c @@ -34,8 +34,8 @@ int main() Model dwarf = LoadModel("resources/model/dwarf.obj"); // Load OBJ model Texture2D texture = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model texture - Shader shader = LoadShader("resources/shaders/base.vs", - "resources/shaders/grayscale.fs"); // Load model shader + Shader shader = LoadShader("resources/shaders/glsl330/base.vs", + "resources/shaders/glsl330/grayscale.fs"); // Load model shader SetModelShader(&dwarf, shader); // Set shader effect to 3d model SetModelTexture(&dwarf, texture); // Bind texture to model diff --git a/examples/shaders_postprocessing.c b/examples/shaders_postprocessing.c index 0bcd5156..632a6371 100644 --- a/examples/shaders_postprocessing.c +++ b/examples/shaders_postprocessing.c @@ -38,8 +38,8 @@ int main() Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position - Shader shader = LoadShader("resources/shaders/base.vs", - "resources/shaders/bloom.fs"); // Load postpro shader + Shader shader = LoadShader("resources/shaders/glsl330/base.vs", + "resources/shaders/glsl330/bloom.fs"); // Load postpro shader // Create a RenderTexture2D to be used for render to texture RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight); @@ -76,7 +76,7 @@ int main() End3dMode(); - DrawText("HELLO TEXTURE!!!", 120, 200, 60, RED); + DrawText("HELLO POSTPROCESSING!", 70, 190, 50, RED); EndTextureMode(); // End drawing to texture (now we have a texture available for next passes) diff --git a/examples/shaders_shapes_textures.c b/examples/shaders_shapes_textures.c index 37180cec..1b1142fa 100644 --- a/examples/shaders_shapes_textures.c +++ b/examples/shaders_shapes_textures.c @@ -32,10 +32,9 @@ int main() Texture2D sonic = LoadTexture("resources/texture_formats/sonic.png"); - // NOTE: This shader is a bit different than model/postprocessing shaders, - // it requires the color data for every vertice to use it in every shape or texture independently - Shader shader = LoadShader("resources/shaders/shapes_base.vs", - "resources/shaders/shapes_grayscale.fs"); + // NOTE: Using GLSL 330 shader version, on OpenGL ES 2.0 use GLSL 100 shader version + Shader shader = LoadShader("resources/shaders/glsl330/base.vs", + "resources/shaders/glsl330/grayscale.fs"); // Shader usage is also different than models/postprocessing, shader is just activated when required -- cgit v1.2.3 From aa22d979834eeddb849f45bba7c0f467b575e6db Mon Sep 17 00:00:00 2001 From: raysan5 Date: Thu, 7 Apr 2016 13:31:53 +0200 Subject: Simplified texture flip and added comments --- examples/shaders_custom_uniform.c | 2 +- examples/shaders_postprocessing.c | 2 +- src/rlgl.c | 10 +++++++--- src/textures.c | 4 ++++ 4 files changed, 13 insertions(+), 5 deletions(-) (limited to 'examples/shaders_postprocessing.c') diff --git a/examples/shaders_custom_uniform.c b/examples/shaders_custom_uniform.c index ceaa86df..32dd7ff1 100644 --- a/examples/shaders_custom_uniform.c +++ b/examples/shaders_custom_uniform.c @@ -94,7 +94,7 @@ int main() SetCustomShader(shader); // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom) - DrawTextureRec(target.texture, (Rectangle){ 0, target.texture.height, target.texture.width, -target.texture.height }, (Vector2){ 0, 0 }, WHITE); + DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height }, (Vector2){ 0, 0 }, WHITE); SetDefaultShader(); DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, GRAY); diff --git a/examples/shaders_postprocessing.c b/examples/shaders_postprocessing.c index 632a6371..e9fafe15 100644 --- a/examples/shaders_postprocessing.c +++ b/examples/shaders_postprocessing.c @@ -82,7 +82,7 @@ int main() SetCustomShader(shader); // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom) - DrawTextureRec(target.texture, (Rectangle){ 0, target.texture.height, target.texture.width, -target.texture.height }, (Vector2){ 0, 0 }, WHITE); + DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height }, (Vector2){ 0, 0 }, WHITE); SetDefaultShader(); DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, DARKGRAY); diff --git a/src/rlgl.c b/src/rlgl.c index b2a36351..f0acf124 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -1897,6 +1897,9 @@ Model rlglLoadModel(Mesh mesh) // Create buffers for our vertex data (positions, texcoords, normals) glGenBuffers(3, vertexBuffer); + + // NOTE: Default shader is assigned to model, so vbo buffers are properly linked to vertex attribs + // If model shader is changed, vbo buffers must be re-assigned to new location points (previously loaded) // Enable vertex attributes: position glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[0]); @@ -2489,13 +2492,14 @@ static Shader LoadDefaultShader(void) } // Get location handlers to for shader attributes and uniforms +// NOTE: If any location is not found, loc point becomes -1 static void LoadDefaultShaderLocations(Shader *shader) { // Get handles to GLSL input attibute locations shader->vertexLoc = glGetAttribLocation(shader->id, "vertexPosition"); shader->texcoordLoc = glGetAttribLocation(shader->id, "vertexTexCoord"); shader->normalLoc = glGetAttribLocation(shader->id, "vertexNormal"); - shader->colorLoc = glGetAttribLocation(shader->id, "vertexColor"); // -1 if not found + shader->colorLoc = glGetAttribLocation(shader->id, "vertexColor"); // Get handles to GLSL uniform locations (vertex shader) shader->mvpLoc = glGetUniformLocation(shader->id, "mvpMatrix"); @@ -2503,8 +2507,8 @@ static void LoadDefaultShaderLocations(Shader *shader) // Get handles to GLSL uniform locations (fragment shader) shader->tintColorLoc = glGetUniformLocation(shader->id, "fragTintColor"); shader->mapDiffuseLoc = glGetUniformLocation(shader->id, "texture0"); - shader->mapNormalLoc = glGetUniformLocation(shader->id, "texture1"); // -1 if not found - shader->mapSpecularLoc = glGetUniformLocation(shader->id, "texture2"); // -1 if not found + shader->mapNormalLoc = glGetUniformLocation(shader->id, "texture1"); + shader->mapSpecularLoc = glGetUniformLocation(shader->id, "texture2"); } // Read text file diff --git a/src/textures.c b/src/textures.c index 67264afb..79047ab7 100644 --- a/src/textures.c +++ b/src/textures.c @@ -1385,6 +1385,10 @@ void DrawTextureEx(Texture2D texture, Vector2 position, float rotation, float sc void DrawTextureRec(Texture2D texture, Rectangle sourceRec, Vector2 position, Color tint) { Rectangle destRec = { (int)position.x, (int)position.y, abs(sourceRec.width), abs(sourceRec.height) }; + + if (sourceRec.width < 0) sourceRec.x -= sourceRec.width; + if (sourceRec.height < 0) sourceRec.y -= sourceRec.height; + Vector2 origin = { 0, 0 }; DrawTexturePro(texture, sourceRec, destRec, origin, 0.0f, tint); -- cgit v1.2.3 From cac2a66debd0f2d3ef8195940f8e2744d539d19a Mon Sep 17 00:00:00 2001 From: raysan5 Date: Tue, 31 May 2016 17:11:02 +0200 Subject: Improved library consistency Functions renamed to improve library consistency --- examples/shaders_custom_uniform.c | 12 +++++++----- examples/shaders_postprocessing.c | 12 +++++++----- examples/shaders_shapes_textures.c | 18 +++++++++--------- examples/textures_particles_trail_blending.c | 24 +++++++++++++----------- src/raylib.h | 8 +++++--- src/rlgl.c | 18 ++++++++++++------ src/rlgl.h | 9 ++++++--- 7 files changed, 59 insertions(+), 42 deletions(-) (limited to 'examples/shaders_postprocessing.c') diff --git a/examples/shaders_custom_uniform.c b/examples/shaders_custom_uniform.c index 74077143..516d5087 100644 --- a/examples/shaders_custom_uniform.c +++ b/examples/shaders_custom_uniform.c @@ -33,7 +33,7 @@ int main() Camera camera = {{ 3.0f, 3.0f, 3.0f }, { 0.0f, 1.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f }; Model dwarf = LoadModel("resources/model/dwarf.obj"); // Load OBJ model - Texture2D texture = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model texture + Texture2D texture = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model texture (diffuse map) SetModelTexture(&dwarf, texture); // Bind texture to model Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position @@ -94,10 +94,12 @@ int main() EndTextureMode(); // End drawing to texture (now we have a texture available for next passes) - SetCustomShader(shader); - // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom) - DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height }, (Vector2){ 0, 0 }, WHITE); - SetDefaultShader(); + BeginShaderMode(shader); + + // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom) + DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height }, (Vector2){ 0, 0 }, WHITE); + + EndShaderMode(); DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, GRAY); diff --git a/examples/shaders_postprocessing.c b/examples/shaders_postprocessing.c index e9fafe15..5e8b5a80 100644 --- a/examples/shaders_postprocessing.c +++ b/examples/shaders_postprocessing.c @@ -33,7 +33,7 @@ int main() Camera camera = {{ 3.0f, 3.0f, 3.0f }, { 0.0f, 1.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f }; Model dwarf = LoadModel("resources/model/dwarf.obj"); // Load OBJ model - Texture2D texture = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model texture + Texture2D texture = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model texture (diffuse map) SetModelTexture(&dwarf, texture); // Bind texture to model Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position @@ -80,10 +80,12 @@ int main() EndTextureMode(); // End drawing to texture (now we have a texture available for next passes) - SetCustomShader(shader); - // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom) - DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height }, (Vector2){ 0, 0 }, WHITE); - SetDefaultShader(); + BeginShaderMode(shader); + + // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom) + DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height }, (Vector2){ 0, 0 }, WHITE); + + EndShaderMode(); DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, DARKGRAY); diff --git a/examples/shaders_shapes_textures.c b/examples/shaders_shapes_textures.c index 1b1142fa..0a14469f 100644 --- a/examples/shaders_shapes_textures.c +++ b/examples/shaders_shapes_textures.c @@ -65,16 +65,16 @@ int main() // Activate our custom shader to be applied on next shapes/textures drawings - SetCustomShader(shader); + BeginShaderMode(shader); - DrawText("USING CUSTOM SHADER", 190, 40, 10, RED); + DrawText("USING CUSTOM SHADER", 190, 40, 10, RED); - DrawRectangle(250 - 60, 90, 120, 60, RED); - DrawRectangleGradient(250 - 90, 170, 180, 130, MAROON, GOLD); - DrawRectangleLines(250 - 40, 320, 80, 60, ORANGE); + DrawRectangle(250 - 60, 90, 120, 60, RED); + DrawRectangleGradient(250 - 90, 170, 180, 130, MAROON, GOLD); + DrawRectangleLines(250 - 40, 320, 80, 60, ORANGE); // Activate our default shader for next drawings - SetDefaultShader(); + EndShaderMode(); DrawText("USING DEFAULT SHADER", 370, 40, 10, RED); @@ -89,12 +89,12 @@ int main() DrawPoly((Vector2){430, 320}, 6, 80, 0, BROWN); // Activate our custom shader to be applied on next shapes/textures drawings - SetCustomShader(shader); + BeginShaderMode(shader); - DrawTexture(sonic, 380, -10, WHITE); // Using custom shader + DrawTexture(sonic, 380, -10, WHITE); // Using custom shader // Activate our default shader for next drawings - SetDefaultShader(); + EndShaderMode(); EndDrawing(); //---------------------------------------------------------------------------------- diff --git a/examples/textures_particles_trail_blending.c b/examples/textures_particles_trail_blending.c index 76cd0423..0b47c790 100644 --- a/examples/textures_particles_trail_blending.c +++ b/examples/textures_particles_trail_blending.c @@ -102,20 +102,22 @@ int main() ClearBackground(DARKGRAY); - SetBlendMode(blending); + BeginBlendMode(blending); - // Draw active particles - for (int i = 0; i < MAX_PARTICLES; i++) - { - if (mouseTail[i].active) DrawTexturePro(smoke, (Rectangle){ 0, 0, smoke.width, smoke.height }, - (Rectangle){ mouseTail[i].position.x, mouseTail[i].position.y, smoke.width*mouseTail[i].size, smoke.height*mouseTail[i].size }, - (Vector2){ smoke.width*mouseTail[i].size/2, smoke.height*mouseTail[i].size/2 }, mouseTail[i].rotation, - Fade(mouseTail[i].color, mouseTail[i].alpha)); - } + // Draw active particles + for (int i = 0; i < MAX_PARTICLES; i++) + { + if (mouseTail[i].active) DrawTexturePro(smoke, (Rectangle){ 0, 0, smoke.width, smoke.height }, + (Rectangle){ mouseTail[i].position.x, mouseTail[i].position.y, smoke.width*mouseTail[i].size, smoke.height*mouseTail[i].size }, + (Vector2){ smoke.width*mouseTail[i].size/2, smoke.height*mouseTail[i].size/2 }, mouseTail[i].rotation, + Fade(mouseTail[i].color, mouseTail[i].alpha)); + } + + EndBlendMode(); - DrawText("PRESS SPACE to CHANGE BLENDING MODE", 180, 20, 20, RAYWHITE); + DrawText("PRESS SPACE to CHANGE BLENDING MODE", 180, 20, 20, BLACK); - if (blending == BLEND_ALPHA) DrawText("ALPHA BLENDING", 290, screenHeight - 40, 20, RAYWHITE); + if (blending == BLEND_ALPHA) DrawText("ALPHA BLENDING", 290, screenHeight - 40, 20, BLACK); else DrawText("ADDITIVE BLENDING", 280, screenHeight - 40, 20, RAYWHITE); EndDrawing(); diff --git a/src/raylib.h b/src/raylib.h index cba73e52..5bef3698 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -860,8 +860,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p //------------------------------------------------------------------------------------ Shader LoadShader(char *vsFileName, char *fsFileName); // Load a custom shader and bind default locations void UnloadShader(Shader shader); // Unload a custom shader from memory -void SetDefaultShader(void); // Set default shader to be used in batch draw -void SetCustomShader(Shader shader); // Set custom shader to be used in batch draw + Shader GetDefaultShader(void); // Get default shader Shader GetStandardShader(void); // Get default shader Texture2D GetDefaultTexture(void); // Get default texture @@ -871,7 +870,10 @@ void SetShaderValue(Shader shader, int uniformLoc, float *value, int size); // S void SetShaderValuei(Shader shader, int uniformLoc, int *value, int size); // Set shader uniform value (int) void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix mat); // Set shader uniform value (matrix 4x4) -void SetBlendMode(int mode); // Set blending mode (alpha, additive, multiplied) +void BeginShaderMode(Shader shader); // Begin custom shader drawing +void EndShaderMode(void); // End custom shader drawing (use default shader) +void BeginBlendMode(int mode); // Begin blending mode (alpha, additive, multiplied) +void EndBlendMode(void); // End blending mode (reset to default: alpha blending) Light CreateLight(int type, Vector3 position, Color diffuse); // Create a new light, initialize it and add to pool void DestroyLight(Light light); // Destroy a light and take it out of the list diff --git a/src/rlgl.c b/src/rlgl.c index 97a92a4d..5c4c9c01 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -2157,7 +2157,7 @@ void UnloadShader(Shader shader) } // Set custom shader to be used on batch draw -void SetCustomShader(Shader shader) +void BeginShaderMode(Shader shader) { #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) if (currentShader.id != shader.id) @@ -2169,10 +2169,10 @@ void SetCustomShader(Shader shader) } // Set default shader to be used in batch draw -void SetDefaultShader(void) +void EndShaderMode(void) { #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - SetCustomShader(defaultShader); + BeginShaderMode(defaultShader); #endif } @@ -2254,9 +2254,9 @@ void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix mat) #endif } -// Set blending mode (alpha, additive, multiplied) -// NOTE: Only 3 blending modes predefined -void SetBlendMode(int mode) +// Begin blending mode (alpha, additive, multiplied) +// NOTE: Only 3 blending modes supported, default blend mode is alpha +void BeginBlendMode(int mode) { if ((blendMode != mode) && (mode < 3)) { @@ -2274,6 +2274,12 @@ void SetBlendMode(int mode) } } +// End blending mode (reset to default: alpha blending) +void EndBlendMode(void) +{ + BeginBlendMode(BLEND_ALPHA); +} + // Create a new light, initialize it and add to pool Light CreateLight(int type, Vector3 position, Color diffuse) { diff --git a/src/rlgl.h b/src/rlgl.h index 23ad29fb..ccf2b36a 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -317,9 +317,9 @@ void *rlglReadTexturePixels(Texture2D texture); // Read text //------------------------------------------------------------------------------------ Shader LoadShader(char *vsFileName, char *fsFileName); // Load a custom shader and bind default locations void UnloadShader(Shader shader); // Unload a custom shader from memory -void SetCustomShader(Shader shader); // Set custom shader to be used in batch draw -void SetDefaultShader(void); // Set default shader to be used in batch draw + Shader GetDefaultShader(void); // Get default shader +Shader GetStandardShader(void); // Get default shader Texture2D GetDefaultTexture(void); // Get default texture int GetShaderLocation(Shader shader, const char *uniformName); // Get shader uniform location @@ -327,7 +327,10 @@ void SetShaderValue(Shader shader, int uniformLoc, float *value, int size); // S void SetShaderValuei(Shader shader, int uniformLoc, int *value, int size); // Set shader uniform value (int) void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix mat); // Set shader uniform value (matrix 4x4) -void SetBlendMode(int mode); // Set blending mode (alpha, additive, multiplied) +void BeginShaderMode(Shader shader); // Begin custom shader drawing +void EndShaderMode(void); // End custom shader drawing (use default shader) +void BeginBlendMode(int mode); // Begin blending mode (alpha, additive, multiplied) +void EndBlendMode(void); // End blending mode (reset to default: alpha blending) Light CreateLight(int type, Vector3 position, Color diffuse); // Create a new light, initialize it and add to pool void DestroyLight(Light light); // Destroy a light and take it out of the list -- cgit v1.2.3 From 5f7ac64c44543383b10ec6a56e5ec1db5706276e Mon Sep 17 00:00:00 2001 From: raysan5 Date: Fri, 24 Jun 2016 19:49:36 +0200 Subject: Removed function SetModelTexture() It's more educational to go through new material system, so, I decide to remove this function to avoid students confusion... --- CHANGELOG | 7 ++++--- examples/models_cubicmap.c | 2 +- examples/models_heightmap.c | 2 +- examples/models_obj_loading.c | 2 +- examples/shaders_custom_uniform.c | 2 +- examples/shaders_postprocessing.c | 2 +- games/raylib_demo/raylib_demo.c | 4 ++-- src/models.c | 7 ------- src/raylib.h | 1 - 9 files changed, 11 insertions(+), 18 deletions(-) (limited to 'examples/shaders_postprocessing.c') diff --git a/CHANGELOG b/CHANGELOG index 5024dc6e..300c9089 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,20 +1,20 @@ changelog --------- -Current Release: raylib 1.5.0 (23 June 2016) +Current Release: raylib 1.5.0 (xx June 2016) NOTE: Only versions marked as 'Release' are available in installer, updates are only available as source. NOTE: Current Release includes all previous updates. ----------------------------------------------- -Release: raylib 1.5.0 (23 June 2016) +Release: raylib 1.5.0 (xx June 2016) ----------------------------------------------- NOTE: Probably this new version is the biggest boost of the library ever, lots of parts of the library have been redesigned, lots of bugs have been solved and some **AMAZING** new features have been added. HUGE changes: -[core] OCULUS RIFT CV1: Added support for VR witha bunch of Oculus-specific functions to init/close device and Oculus rendering. +[rlgl] OCULUS RIFT CV1: Added support for VR witha bunch of Oculus-specific functions to init/close device and Oculus rendering. [rlgl] MATERIALS SYSTEM: Added support for Materials (.mtl) and multiple material properties: diffuse, specular, normal. [rlgl] LIGHTING SYSTEM: Added support for up to 8 lights of 3 different types: Omni, Directional and Spot [physac] REDESIGNED: Improved performance and simplified usage, physic objects are managed internally @@ -65,6 +65,7 @@ other changes: [models] Updated BoundingBox collision detections [models] Added color parameter to DrawBoundigBox() [models] Removed function: DrawQuad() +[models] Removed function: SetModelTexture() [models] Redesigned DrawPlane() to use RL_TRIANGLES [models] Redesigned DrawRectangleV() to use RL_TRIANGLES [models] Redesign to accomodate new materials system: LoadMaterial() diff --git a/examples/models_cubicmap.c b/examples/models_cubicmap.c index 1ca27dfd..89bc75cf 100644 --- a/examples/models_cubicmap.c +++ b/examples/models_cubicmap.c @@ -29,7 +29,7 @@ int main() // NOTE: By default each cube is mapped to one part of texture atlas Texture2D texture = LoadTexture("resources/cubicmap_atlas.png"); // Load map texture - SetModelTexture(&map, texture); // Bind texture to map model + map.material.texDiffuse = texture; // Set map diffuse texture Vector3 mapPosition = { -16.0f, 0.0f, -8.0f }; // Set model position diff --git a/examples/models_heightmap.c b/examples/models_heightmap.c index c8e5ff35..90e5f5bb 100644 --- a/examples/models_heightmap.c +++ b/examples/models_heightmap.c @@ -26,7 +26,7 @@ int main() Image image = LoadImage("resources/heightmap.png"); // Load heightmap image (RAM) Texture2D texture = LoadTextureFromImage(image); // Convert image to texture (VRAM) Model map = LoadHeightmap(image, (Vector3){ 16, 8, 16 }); // Load heightmap model with defined size - SetModelTexture(&map, texture); // Bind texture to model + map.material.texDiffuse = texture; // Set map diffuse texture Vector3 mapPosition = { -8.0f, 0.0f, -8.0f }; // Set model position (depends on model scaling!) UnloadImage(image); // Unload heightmap image from RAM, already uploaded to VRAM diff --git a/examples/models_obj_loading.c b/examples/models_obj_loading.c index e8dd0adc..a6969f70 100644 --- a/examples/models_obj_loading.c +++ b/examples/models_obj_loading.c @@ -25,7 +25,7 @@ int main() Model dwarf = LoadModel("resources/model/dwarf.obj"); // Load OBJ model Texture2D texture = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model texture - SetModelTexture(&dwarf, texture); // Bind texture to model + dwarf.material.texDiffuse = texture; // Set dwarf model diffuse texture Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position SetTargetFPS(60); // Set our game to run at 60 frames-per-second diff --git a/examples/shaders_custom_uniform.c b/examples/shaders_custom_uniform.c index 516d5087..c4f87259 100644 --- a/examples/shaders_custom_uniform.c +++ b/examples/shaders_custom_uniform.c @@ -34,7 +34,7 @@ int main() Model dwarf = LoadModel("resources/model/dwarf.obj"); // Load OBJ model Texture2D texture = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model texture (diffuse map) - SetModelTexture(&dwarf, texture); // Bind texture to model + dwarf.material.texDiffuse = texture; // Set dwarf model diffuse texture Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position diff --git a/examples/shaders_postprocessing.c b/examples/shaders_postprocessing.c index 5e8b5a80..43d21e08 100644 --- a/examples/shaders_postprocessing.c +++ b/examples/shaders_postprocessing.c @@ -34,7 +34,7 @@ int main() Model dwarf = LoadModel("resources/model/dwarf.obj"); // Load OBJ model Texture2D texture = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model texture (diffuse map) - SetModelTexture(&dwarf, texture); // Bind texture to model + dwarf.material.texDiffuse = texture; // Set dwarf model diffuse texture Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position diff --git a/games/raylib_demo/raylib_demo.c b/games/raylib_demo/raylib_demo.c index 7f6f291a..22213b46 100644 --- a/games/raylib_demo/raylib_demo.c +++ b/games/raylib_demo/raylib_demo.c @@ -202,8 +202,8 @@ int main() camera = (Camera){{ 0.0, 12.0, 15.0 }, { 0.0, 3.0, 0.0 }, { 0.0, 1.0, 0.0 }}; catTexture = LoadTexture("resources/catsham.png"); // Load model texture - cat = LoadModel("resources/cat.obj"); // Load OBJ model - SetModelTexture(&cat, catTexture); + cat = LoadModel("resources/cat.obj"); // Load OBJ model + cat.material.texDiffuse = texture; // Set cat model diffuse texture fxWav = LoadSound("resources/audio/weird.wav"); // Load WAV audio file fxOgg = LoadSound("resources/audio/tanatana.ogg"); // Load OGG audio file diff --git a/src/models.c b/src/models.c index 8deabcb0..a4bcde8f 100644 --- a/src/models.c +++ b/src/models.c @@ -808,13 +808,6 @@ void UnloadMaterial(Material material) rlDeleteTextures(material.texSpecular.id); } -// Link a texture to a model -void SetModelTexture(Model *model, Texture2D texture) -{ - if (texture.id <= 0) model->material.texDiffuse = GetDefaultTexture(); // Use default white texture - else model->material.texDiffuse = texture; -} - // Generate a mesh from heightmap static Mesh GenMeshHeightmap(Image heightmap, Vector3 size) { diff --git a/src/raylib.h b/src/raylib.h index ed787892..641f4c09 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -795,7 +795,6 @@ Model LoadModelFromRES(const char *rresName, int resId); // Load a 3d mod Model LoadHeightmap(Image heightmap, Vector3 size); // Load a heightmap image as a 3d model Model LoadCubicmap(Image cubicmap); // Load a map image as a 3d model (cubes based) void UnloadModel(Model model); // Unload 3d model from memory -void SetModelTexture(Model *model, Texture2D texture); // Link a texture to a model Material LoadMaterial(const char *fileName); // Load material data (from file) Material LoadDefaultMaterial(void); // Load default material (uses default models shader) -- cgit v1.2.3 From 648676f46b01327f0fbd6f017292a3159ea9ab2f Mon Sep 17 00:00:00 2001 From: raysan5 Date: Mon, 10 Oct 2016 19:43:27 +0200 Subject: Update examples to new camera system --- examples/core_3d_camera_first_person.c | 2 +- examples/core_3d_camera_free.c | 2 +- examples/core_3d_picking.c | 8 +++----- examples/core_oculus_rift.c | 12 ++++++------ examples/core_world_screen.c | 9 +++------ examples/models_billboard.c | 11 ++++------- examples/models_cubicmap.c | 2 +- examples/models_heightmap.c | 11 +++++------ examples/shaders_custom_uniform.c | 6 ++---- examples/shaders_model_shader.c | 5 ++++- examples/shaders_postprocessing.c | 8 +++----- examples/shaders_standard_lighting.c | 6 ++---- 12 files changed, 35 insertions(+), 47 deletions(-) (limited to 'examples/shaders_postprocessing.c') diff --git a/examples/core_3d_camera_first_person.c b/examples/core_3d_camera_first_person.c index 27ff5135..3998af81 100644 --- a/examples/core_3d_camera_first_person.c +++ b/examples/core_3d_camera_first_person.c @@ -47,7 +47,7 @@ int main() { // Update //---------------------------------------------------------------------------------- - UpdateCamera(&camera); // Update camera and player position + UpdateCamera(&camera); // Update camera //---------------------------------------------------------------------------------- // Draw diff --git a/examples/core_3d_camera_free.c b/examples/core_3d_camera_free.c index c798f225..d446e14a 100644 --- a/examples/core_3d_camera_free.c +++ b/examples/core_3d_camera_free.c @@ -39,7 +39,7 @@ int main() { // Update //---------------------------------------------------------------------------------- - UpdateCamera(&camera); // Update internal camera and our camera + UpdateCamera(&camera); // Update camera if (IsKeyDown('Z')) camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; //---------------------------------------------------------------------------------- diff --git a/examples/core_3d_picking.c b/examples/core_3d_picking.c index 7f904f7f..bd5c3347 100644 --- a/examples/core_3d_picking.c +++ b/examples/core_3d_picking.c @@ -22,7 +22,7 @@ int main() // Define the camera to look into our 3d world Camera camera; - camera.position = (Vector3){ 0.0f, 10.0f, 10.0f }; // Camera position + camera.position = (Vector3){ 10.0f, 10.0f, 10.0f }; // Camera position 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.fovy = 45.0f; // Camera field-of-view Y @@ -34,9 +34,7 @@ int main() bool collision = false; - SetCameraMode(CAMERA_FREE); // Set a free camera mode - SetCameraPosition(camera.position); // Set internal camera position to match our camera position - SetCameraFovy(camera.fovy); // Set internal camera field-of-view Y + SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- @@ -46,7 +44,7 @@ int main() { // Update //---------------------------------------------------------------------------------- - UpdateCamera(&camera); // Update internal camera and our camera + UpdateCamera(&camera); // Update camera if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) { diff --git a/examples/core_oculus_rift.c b/examples/core_oculus_rift.c index 3d8bb278..7276e3de 100644 --- a/examples/core_oculus_rift.c +++ b/examples/core_oculus_rift.c @@ -30,14 +30,14 @@ int main() // Define the camera to look into our 3d world Camera camera; - camera.position = (Vector3){ 5.0f, 5.0f, 5.0f }; // Camera position - camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point + camera.position = (Vector3){ 5.0f, 2.0f, 5.0f }; // Camera position + camera.target = (Vector3){ 0.0f, 2.0f, 0.0f }; // Camera looking at point camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target) camera.fovy = 60.0f; // Camera field-of-view Y Vector3 cubePosition = { 0.0f, 0.0f, 0.0f }; - SetCameraMode(camera, CAMERA_FIRST_PERSON); + SetCameraMode(camera, CAMERA_FIRST_PERSON); // Set first person camera mode SetTargetFPS(90); // Set our game to run at 90 frames-per-second //-------------------------------------------------------------------------------------- @@ -47,10 +47,10 @@ int main() { // Update //---------------------------------------------------------------------------------- - if (IsVrSimulator()) UpdateCamera(&camera); - else UpdateVrTracking(); + if (IsVrSimulator()) UpdateCamera(&camera); // Update camera (simulator mode) + else UpdateVrTracking(&camera); // Update camera with device tracking data - if (IsKeyPressed(KEY_SPACE)) ToggleVrMode(); + if (IsKeyPressed(KEY_SPACE)) ToggleVrMode(); // Toggle VR mode //---------------------------------------------------------------------------------- // Draw diff --git a/examples/core_world_screen.c b/examples/core_world_screen.c index aa9505e8..f8c53c70 100644 --- a/examples/core_world_screen.c +++ b/examples/core_world_screen.c @@ -21,16 +21,13 @@ int main() InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera free"); // Define the camera to look into our 3d world - Camera camera = {{ 0.0f, 10.0f, 10.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f }; + Camera camera = {{ 10.0f, 10.0f, 10.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f }; Vector3 cubePosition = { 0.0f, 0.0f, 0.0f }; Vector2 cubeScreenPosition; - SetCameraMode(CAMERA_FREE); // Set a free camera mode - SetCameraPosition(camera.position); // Set internal camera position to match our camera position - SetCameraTarget(camera.target); // Set internal camera target to match our camera target - SetCameraFovy(camera.fovy); // Set internal camera field-of-view Y + SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- @@ -40,7 +37,7 @@ int main() { // Update //---------------------------------------------------------------------------------- - UpdateCamera(&camera); // Update internal camera and our camera + UpdateCamera(&camera); // Update camera // Calculate cube screen space position (with a little offset to be in top) cubeScreenPosition = GetWorldToScreen((Vector3){cubePosition.x, cubePosition.y + 2.5f, cubePosition.z}, camera); diff --git a/examples/models_billboard.c b/examples/models_billboard.c index 654b3618..bca9faf8 100644 --- a/examples/models_billboard.c +++ b/examples/models_billboard.c @@ -26,20 +26,17 @@ int main() Texture2D bill = LoadTexture("resources/billboard.png"); // Our texture billboard Vector3 billPosition = { 0.0f, 2.0f, 0.0f }; // Position where draw billboard - SetCameraMode(CAMERA_ORBITAL); // Set an orbital camera mode - SetCameraPosition(camera.position); // Set internal camera position to match our camera position - SetCameraTarget(camera.target); // Set internal camera target to match our camera target - SetCameraFovy(camera.fovy); // Set internal camera field-of-view Y + SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode - SetTargetFPS(60); // Set our game to run at 60 frames-per-second + SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop - while (!WindowShouldClose()) // Detect window close button or ESC key + while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- - UpdateCamera(&camera); // Update internal camera and our camera + UpdateCamera(&camera); // Update camera //---------------------------------------------------------------------------------- // Draw diff --git a/examples/models_cubicmap.c b/examples/models_cubicmap.c index df700d65..0e613029 100644 --- a/examples/models_cubicmap.c +++ b/examples/models_cubicmap.c @@ -45,7 +45,7 @@ int main() { // Update //---------------------------------------------------------------------------------- - UpdateCamera(&camera); // Update internal camera and our camera + UpdateCamera(&camera); // Update camera //---------------------------------------------------------------------------------- // Draw diff --git a/examples/models_heightmap.c b/examples/models_heightmap.c index 90e5f5bb..10069e03 100644 --- a/examples/models_heightmap.c +++ b/examples/models_heightmap.c @@ -29,20 +29,19 @@ int main() map.material.texDiffuse = texture; // Set map diffuse texture Vector3 mapPosition = { -8.0f, 0.0f, -8.0f }; // Set model position (depends on model scaling!) - UnloadImage(image); // Unload heightmap image from RAM, already uploaded to VRAM + UnloadImage(image); // Unload heightmap image from RAM, already uploaded to VRAM - SetCameraMode(CAMERA_ORBITAL); // Set an orbital camera mode - SetCameraPosition(camera.position); // Set internal camera position to match our custom camera position + SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode - SetTargetFPS(60); // Set our game to run at 60 frames-per-second + SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop - while (!WindowShouldClose()) // Detect window close button or ESC key + while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- - UpdateCamera(&camera); // Update internal camera and our camera + UpdateCamera(&camera); // Update camera //---------------------------------------------------------------------------------- // Draw diff --git a/examples/shaders_custom_uniform.c b/examples/shaders_custom_uniform.c index c4f87259..89f87df9 100644 --- a/examples/shaders_custom_uniform.c +++ b/examples/shaders_custom_uniform.c @@ -51,9 +51,7 @@ int main() RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight); // Setup orbital camera - SetCameraMode(CAMERA_ORBITAL); // Set an orbital camera mode - SetCameraPosition(camera.position); // Set internal camera position to match our camera position - SetCameraTarget(camera.target); // Set internal camera target to match our camera target + SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- @@ -71,7 +69,7 @@ int main() // Send new value to the shader to be used on drawing SetShaderValue(shader, swirlCenterLoc, swirlCenter, 2); - UpdateCamera(&camera); // Update internal camera and our camera + UpdateCamera(&camera); // Update camera //---------------------------------------------------------------------------------- // Draw diff --git a/examples/shaders_model_shader.c b/examples/shaders_model_shader.c index a5516eba..26de4922 100644 --- a/examples/shaders_model_shader.c +++ b/examples/shaders_model_shader.c @@ -42,7 +42,7 @@ int main() Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position - SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode + SetCameraMode(camera, CAMERA_FREE); // Set an orbital camera mode SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- @@ -70,6 +70,9 @@ int main() End3dMode(); DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, GRAY); + + DrawText(FormatText("Camera position: (%.2f, %.2f, %.2f)", camera.position.x, camera.position.y, camera.position.z), 600, 20, 10, BLACK); + DrawText(FormatText("Camera target: (%.2f, %.2f, %.2f)", camera.target.x, camera.target.y, camera.target.z), 600, 40, 10, GRAY); DrawFPS(10, 10); diff --git a/examples/shaders_postprocessing.c b/examples/shaders_postprocessing.c index 43d21e08..43d1af72 100644 --- a/examples/shaders_postprocessing.c +++ b/examples/shaders_postprocessing.c @@ -45,9 +45,7 @@ int main() RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight); // Setup orbital camera - SetCameraMode(CAMERA_ORBITAL); // Set an orbital camera mode - SetCameraPosition(camera.position); // Set internal camera position to match our camera position - SetCameraTarget(camera.target); // Set internal camera target to match our camera target + SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- @@ -57,7 +55,7 @@ int main() { // Update //---------------------------------------------------------------------------------- - UpdateCamera(&camera); // Update internal camera and our camera + UpdateCamera(&camera); // Update camera //---------------------------------------------------------------------------------- // Draw @@ -67,7 +65,7 @@ int main() ClearBackground(RAYWHITE); BeginTextureMode(target); // Enable drawing to texture - + Begin3dMode(camera); DrawModel(dwarf, position, 2.0f, WHITE); // Draw 3d model with texture diff --git a/examples/shaders_standard_lighting.c b/examples/shaders_standard_lighting.c index f2b35171..e539ec47 100644 --- a/examples/shaders_standard_lighting.c +++ b/examples/shaders_standard_lighting.c @@ -64,9 +64,7 @@ int main() pointLight->radius = 3.0f; // Setup orbital camera - SetCameraMode(CAMERA_ORBITAL); // Set an orbital camera mode - SetCameraPosition(camera.position); // Set internal camera position to match our camera position - SetCameraTarget(camera.target); // Set internal camera target to match our camera target + SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- @@ -76,7 +74,7 @@ int main() { // Update //---------------------------------------------------------------------------------- - UpdateCamera(&camera); // Update internal camera and our camera + UpdateCamera(&camera); // Update camera //---------------------------------------------------------------------------------- // Draw -- cgit v1.2.3