From 35172430c6b5929e8f6781e0d92b4bc1f9fcc2a2 Mon Sep 17 00:00:00 2001 From: Ray Date: Tue, 18 Apr 2017 11:49:02 +0200 Subject: Added SUPPORT_VR_SIMULATOR flag --- src/rlgl.c | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) (limited to 'src/rlgl.c') diff --git a/src/rlgl.c b/src/rlgl.c index 5f7b6cb2..3b23d91a 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -22,7 +22,7 @@ * #define RLGL_STANDALONE * Use rlgl as standalone library (no raylib dependency) * -* #define SUPPORT_VR_SIMULATION / SUPPORT_STEREO_RENDERING +* #define SUPPORT_VR_SIMULATOR * Support VR simulation functionality (stereo rendering) * * #define SUPPORT_DISTORTION_SHADER @@ -56,7 +56,7 @@ // Default configuration flags (supported features) //------------------------------------------------- -#define SUPPORT_VR_SIMULATION +#define SUPPORT_VR_SIMULATOR #define SUPPORT_DISTORTION_SHADER //------------------------------------------------- @@ -222,6 +222,7 @@ typedef struct DrawCall { //Guint fboId; } DrawCall; +#if defined(SUPPORT_VR_SIMULATOR) // Head-Mounted-Display device parameters typedef struct VrDeviceInfo { int hResolution; // HMD horizontal resolution in pixels @@ -244,6 +245,7 @@ typedef struct VrStereoConfig { Matrix eyesProjection[2]; // VR stereo rendering eyes projection matrices Matrix eyesViewOffset[2]; // VR stereo rendering eyes view offset matrices } VrStereoConfig; +#endif //---------------------------------------------------------------------------------- // Global Variables Definition @@ -287,13 +289,16 @@ static bool texCompETC2Supported = false; // ETC2/EAC texture compression supp static bool texCompPVRTSupported = false; // PVR texture compression support static bool texCompASTCSupported = false; // ASTC texture compression support +#if defined(SUPPORT_VR_SIMULATOR) // VR global variables static VrDeviceInfo hmd; // Current VR device info static VrStereoConfig vrConfig; // VR stereo configuration for simulator static bool vrSimulatorReady = false; // VR simulator ready flag static bool vrStereoRender = false; // VR stereo rendering enabled/disabled flag // NOTE: This flag is useful to render data over stereo image (i.e. FPS) -#endif +#endif // defined(SUPPORT_VR_SIMULATOR) + +#endif // defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) // Extension supported flag: Anisotropic filtering static bool texAnisotropicFilterSupported = false; // Anisotropic texture filtering support @@ -339,13 +344,13 @@ static void UpdateDefaultBuffers(void); // Update default internal buffers ( static void DrawDefaultBuffers(void); // Draw default internal buffers vertex data static void UnloadDefaultBuffers(void); // Unload default internal buffers vertex data from CPU and GPU -// Configure stereo rendering (including distortion shader) with HMD device parameters -static void SetStereoConfig(VrDeviceInfo info); - -// Set internal projection and modelview matrix depending on eyes tracking data -static void SetStereoView(int eye, Matrix matProjection, Matrix matModelView); +#if defined(SUPPORT_VR_SIMULATOR) +static void SetStereoConfig(VrDeviceInfo info); // Configure stereo rendering (including distortion shader) with HMD device parameters +static void SetStereoView(int eye, Matrix matProjection, Matrix matModelView); // Set internal projection and modelview matrix depending on eye #endif +#endif // defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) + #if defined(GRAPHICS_API_OPENGL_11) static int GenerateMipmaps(unsigned char *data, int baseWidth, int baseHeight); static Color *GenNextMipmap(Color *srcData, int srcWidth, int srcHeight); @@ -2068,12 +2073,16 @@ void rlglDrawMesh(Mesh mesh, Material material, Matrix transform) } int eyesCount = 1; +#if defined(SUPPORT_VR_SIMULATOR) if (vrStereoRender) eyesCount = 2; +#endif for (int eye = 0; eye < eyesCount; eye++) { - if (eyesCount == 2) SetStereoView(eye, matProjection, matModelView); - else modelview = matModelView; + if (eyesCount == 1) modelview = matModelView; + #if defined(SUPPORT_VR_SIMULATOR) + else SetStereoView(eye, matProjection, matModelView); + #endif // Calculate model-view-projection matrix (MVP) Matrix matMVP = MatrixMultiply(modelview, projection); // Transform to screen-space coordinates @@ -2534,6 +2543,7 @@ void EndBlendMode(void) BeginBlendMode(BLEND_ALPHA); } +#if defined(SUPPORT_VR_SIMULATOR) // Init VR simulator for selected device // NOTE: It modifies the global variable: VrDeviceInfo hmd void InitVrSimulator(int vrDevice) @@ -2761,6 +2771,7 @@ void EndVrDrawing(void) } #endif } +#endif // SUPPORT_VR_SIMULATOR //---------------------------------------------------------------------------------- // Module specific Functions Definition @@ -3306,11 +3317,15 @@ static void DrawDefaultBuffers() Matrix matModelView = modelview; int eyesCount = 1; +#if defined(SUPPORT_VR_SIMULATOR) if (vrStereoRender) eyesCount = 2; +#endif for (int eye = 0; eye < eyesCount; eye++) { + #if defined(SUPPORT_VR_SIMULATOR) if (eyesCount == 2) SetStereoView(eye, matProjection, matModelView); + #endif // Set current shader and upload current MVP matrix if ((lines.vCounter > 0) || (triangles.vCounter > 0) || (quads.vCounter > 0)) @@ -3516,6 +3531,7 @@ static void UnloadDefaultBuffers(void) free(quads.indices); } +#if defined(SUPPORT_VR_SIMULATOR) // Configure stereo rendering (including distortion shader) with HMD device parameters static void SetStereoConfig(VrDeviceInfo hmd) { @@ -3607,6 +3623,8 @@ static void SetStereoView(int eye, Matrix matProjection, Matrix matModelView) SetMatrixModelview(eyeModelView); SetMatrixProjection(eyeProjection); } +#endif // defined(SUPPORT_VR_SIMULATOR) + #endif //defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) #if defined(GRAPHICS_API_OPENGL_11) -- cgit v1.2.3 From 247da006ae81ee66fe0239a1c9bbbfcb0f79aa6f Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sat, 22 Apr 2017 22:35:04 +0200 Subject: Rename parameter --- src/rlgl.c | 20 ++++++++++---------- src/rlgl.h | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) (limited to 'src/rlgl.c') diff --git a/src/rlgl.c b/src/rlgl.c index 3b23d91a..b336571f 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -1331,7 +1331,7 @@ Vector3 rlglUnproject(Vector3 source, Matrix proj, Matrix view) } // Convert image data to OpenGL texture (returns OpenGL valid Id) -unsigned int rlglLoadTexture(void *data, int width, int height, int textureFormat, int mipmapCount) +unsigned int rlglLoadTexture(void *data, int width, int height, int format, int mipmapCount) { glBindTexture(GL_TEXTURE_2D, 0); // Free any old binding @@ -1339,39 +1339,39 @@ unsigned int rlglLoadTexture(void *data, int width, int height, int textureForma // Check texture format support by OpenGL 1.1 (compressed textures not supported) #if defined(GRAPHICS_API_OPENGL_11) - if (textureFormat >= 8) + if (format >= COMPRESSED_DXT1_RGB) { TraceLog(WARNING, "OpenGL 1.1 does not support GPU compressed texture formats"); return id; } #endif - if ((!texCompDXTSupported) && ((textureFormat == COMPRESSED_DXT1_RGB) || (textureFormat == COMPRESSED_DXT1_RGBA) || - (textureFormat == COMPRESSED_DXT3_RGBA) || (textureFormat == COMPRESSED_DXT5_RGBA))) + if ((!texCompDXTSupported) && ((format == COMPRESSED_DXT1_RGB) || (format == COMPRESSED_DXT1_RGBA) || + (format == COMPRESSED_DXT3_RGBA) || (format == COMPRESSED_DXT5_RGBA))) { TraceLog(WARNING, "DXT compressed texture format not supported"); return id; } #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - if ((!texCompETC1Supported) && (textureFormat == COMPRESSED_ETC1_RGB)) + if ((!texCompETC1Supported) && (format == COMPRESSED_ETC1_RGB)) { TraceLog(WARNING, "ETC1 compressed texture format not supported"); return id; } - if ((!texCompETC2Supported) && ((textureFormat == COMPRESSED_ETC2_RGB) || (textureFormat == COMPRESSED_ETC2_EAC_RGBA))) + if ((!texCompETC2Supported) && ((format == COMPRESSED_ETC2_RGB) || (format == COMPRESSED_ETC2_EAC_RGBA))) { TraceLog(WARNING, "ETC2 compressed texture format not supported"); return id; } - if ((!texCompPVRTSupported) && ((textureFormat == COMPRESSED_PVRT_RGB) || (textureFormat == COMPRESSED_PVRT_RGBA))) + if ((!texCompPVRTSupported) && ((format == COMPRESSED_PVRT_RGB) || (format == COMPRESSED_PVRT_RGBA))) { TraceLog(WARNING, "PVRT compressed texture format not supported"); return id; } - if ((!texCompASTCSupported) && ((textureFormat == COMPRESSED_ASTC_4x4_RGBA) || (textureFormat == COMPRESSED_ASTC_8x8_RGBA))) + if ((!texCompASTCSupported) && ((format == COMPRESSED_ASTC_4x4_RGBA) || (format == COMPRESSED_ASTC_8x8_RGBA))) { TraceLog(WARNING, "ASTC compressed texture format not supported"); return id; @@ -1399,7 +1399,7 @@ unsigned int rlglLoadTexture(void *data, int width, int height, int textureForma // GL_RGBA8 GL_RGBA GL_UNSIGNED_BYTE // GL_RGB8 GL_RGB GL_UNSIGNED_BYTE - switch (textureFormat) + switch (format) { case UNCOMPRESSED_GRAYSCALE: { @@ -1440,7 +1440,7 @@ unsigned int rlglLoadTexture(void *data, int width, int height, int textureForma } #elif defined(GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_ES2) // NOTE: on OpenGL ES 2.0 (WebGL), internalFormat must match format and options allowed are: GL_LUMINANCE, GL_RGB, GL_RGBA - switch (textureFormat) + switch (format) { case UNCOMPRESSED_GRAYSCALE: glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, (unsigned char *)data); break; case UNCOMPRESSED_GRAY_ALPHA: glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, width, height, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, (unsigned char *)data); break; diff --git a/src/rlgl.h b/src/rlgl.h index a870a907..f3fd6b22 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -366,7 +366,7 @@ void rlglClose(void); // De-init rlgl void rlglDraw(void); // Draw VAO/VBO void rlglLoadExtensions(void *loader); // Load OpenGL extensions -unsigned int rlglLoadTexture(void *data, int width, int height, int textureFormat, int mipmapCount); // Load texture in GPU +unsigned int rlglLoadTexture(void *data, int width, int height, int format, 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, const void *data); // Update GPU texture with new data void rlglGenerateMipmaps(Texture2D *texture); // Generate mipmap data for selected texture -- cgit v1.2.3 From d593bd0081ea2dcafe3182ffc874882b5b7110b4 Mon Sep 17 00:00:00 2001 From: Ray Date: Sun, 30 Apr 2017 13:03:31 +0200 Subject: Some code tweaks --- src/raylib.h | 2 +- src/rlgl.c | 2 +- src/rlgl.h | 118 +++++++++++++++++++++++++++++---------------------------- src/textures.c | 39 ++++--------------- 4 files changed, 70 insertions(+), 91 deletions(-) (limited to 'src/rlgl.c') diff --git a/src/raylib.h b/src/raylib.h index 18d442d1..6f510f9f 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -351,7 +351,7 @@ typedef struct Image { int format; // Data format (TextureFormat type) } Image; -// Texture2D type, bpp always RGBA (32bit) +// Texture2D type // NOTE: Data stored in GPU memory typedef struct Texture2D { unsigned int id; // OpenGL texture id diff --git a/src/rlgl.c b/src/rlgl.c index b336571f..1dc83314 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -64,7 +64,7 @@ #include // Required for: fopen(), fclose(), fread()... [Used only on LoadText()] #include // Required for: malloc(), free(), rand() -#include // Required for: strcmp(), strlen(), strtok() +#include // Required for: strcmp(), strlen(), strtok() [Used only in extensions loading] #include // Required for: atan2() #ifndef RLGL_STANDALONE diff --git a/src/rlgl.h b/src/rlgl.h index f3fd6b22..8358efb8 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -17,7 +17,9 @@ * #define GRAPHICS_API_OPENGL_21 * #define GRAPHICS_API_OPENGL_33 * #define GRAPHICS_API_OPENGL_ES2 -* Use selected OpenGL backend +* Use selected OpenGL graphics backend, should be supported by platform +* Those preprocessor defines are only used on rlgl module, if OpenGL version is +* required by any other module, use rlGetVersion() tocheck it * * #define RLGL_STANDALONE * Use rlgl as standalone library (no raylib dependency) @@ -57,11 +59,8 @@ #ifndef RLGL_H #define RLGL_H -//#define RLGL_STANDALONE // NOTE: To use rlgl as standalone lib, just uncomment this line - #ifndef RLGL_STANDALONE - #include "raylib.h" // Required for: Model, Shader, Texture2D - #include "utils.h" // Required for: TraceLog() + #include "raylib.h" // Required for: Model, Shader, Texture2D, TraceLog() #endif #ifdef RLGL_STANDALONE @@ -70,15 +69,6 @@ #include "raymath.h" // Required for: Vector3, Matrix -// Select desired OpenGL version -// NOTE: Those preprocessor defines are only used on rlgl module, -// if OpenGL version is required by any other module, it uses rlGetVersion() - -// Choose opengl version here or just define it at compile time: -DGRAPHICS_API_OPENGL_33 -//#define GRAPHICS_API_OPENGL_11 // Only available on PLATFORM_DESKTOP -//#define GRAPHICS_API_OPENGL_33 // Only available on PLATFORM_DESKTOP and RLGL_OCULUS_SUPPORT -//#define GRAPHICS_API_OPENGL_ES2 // Only available on PLATFORM_ANDROID or PLATFORM_RPI or PLATFORM_WEB - // Security check in case no GRAPHICS_API_OPENGL_* defined #if !defined(GRAPHICS_API_OPENGL_11) && !defined(GRAPHICS_API_OPENGL_21) && !defined(GRAPHICS_API_OPENGL_33) && !defined(GRAPHICS_API_OPENGL_ES2) #define GRAPHICS_API_OPENGL_11 @@ -165,28 +155,23 @@ typedef unsigned char byte; unsigned char b; unsigned char a; } Color; + + // Texture2D type + // NOTE: Data stored in GPU memory + typedef struct Texture2D { + unsigned int id; // OpenGL texture id + int width; // Texture base width + int height; // Texture base height + int mipmaps; // Mipmap levels, 1 by default + int format; // Data format (TextureFormat) + } Texture2D; - // Texture formats (support depends on OpenGL version) - typedef enum { - UNCOMPRESSED_GRAYSCALE = 1, // 8 bit per pixel (no alpha) - UNCOMPRESSED_GRAY_ALPHA, - UNCOMPRESSED_R5G6B5, // 16 bpp - UNCOMPRESSED_R8G8B8, // 24 bpp - UNCOMPRESSED_R5G5B5A1, // 16 bpp (1 bit alpha) - UNCOMPRESSED_R4G4B4A4, // 16 bpp (4 bit alpha) - UNCOMPRESSED_R8G8B8A8, // 32 bpp - COMPRESSED_DXT1_RGB, // 4 bpp (no alpha) - COMPRESSED_DXT1_RGBA, // 4 bpp (1 bit alpha) - COMPRESSED_DXT3_RGBA, // 8 bpp - COMPRESSED_DXT5_RGBA, // 8 bpp - COMPRESSED_ETC1_RGB, // 4 bpp - COMPRESSED_ETC2_RGB, // 4 bpp - COMPRESSED_ETC2_EAC_RGBA, // 8 bpp - COMPRESSED_PVRT_RGB, // 4 bpp - COMPRESSED_PVRT_RGBA, // 4 bpp - COMPRESSED_ASTC_4x4_RGBA, // 8 bpp - COMPRESSED_ASTC_8x8_RGBA // 2 bpp - } TextureFormat; + // 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; // Vertex data definning a mesh typedef struct Mesh { @@ -228,23 +213,6 @@ typedef unsigned char byte; int mapTexture2Loc; // Map texture uniform location point (default-texture-unit = 2) } Shader; - // Texture2D type - // NOTE: Data stored in GPU memory - typedef struct Texture2D { - unsigned int id; // OpenGL texture id - int width; // Texture base width - int height; // Texture base height - int mipmaps; // Mipmap levels, 1 by default - 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; // Standard shader (supports 3 map types: diffuse, normal, specular) @@ -267,6 +235,37 @@ typedef unsigned char byte; Vector3 up; // Camera up vector (rotation over its axis) float fovy; // Camera field-of-view apperture in Y (degrees) } Camera; + + // TraceLog message types + typedef enum { + INFO = 0, + ERROR, + WARNING, + DEBUG, + OTHER + } TraceLogType; + + // Texture formats (support depends on OpenGL version) + typedef enum { + UNCOMPRESSED_GRAYSCALE = 1, // 8 bit per pixel (no alpha) + UNCOMPRESSED_GRAY_ALPHA, + UNCOMPRESSED_R5G6B5, // 16 bpp + UNCOMPRESSED_R8G8B8, // 24 bpp + UNCOMPRESSED_R5G5B5A1, // 16 bpp (1 bit alpha) + UNCOMPRESSED_R4G4B4A4, // 16 bpp (4 bit alpha) + UNCOMPRESSED_R8G8B8A8, // 32 bpp + COMPRESSED_DXT1_RGB, // 4 bpp (no alpha) + COMPRESSED_DXT1_RGBA, // 4 bpp (1 bit alpha) + COMPRESSED_DXT3_RGBA, // 8 bpp + COMPRESSED_DXT5_RGBA, // 8 bpp + COMPRESSED_ETC1_RGB, // 4 bpp + COMPRESSED_ETC2_RGB, // 4 bpp + COMPRESSED_ETC2_EAC_RGBA, // 8 bpp + COMPRESSED_PVRT_RGB, // 4 bpp + COMPRESSED_PVRT_RGBA, // 4 bpp + COMPRESSED_ASTC_4x4_RGBA, // 8 bpp + COMPRESSED_ASTC_8x8_RGBA // 2 bpp + } TextureFormat; // Texture parameters: filter mode // NOTE 1: Filtering considers mipmaps if available in the texture @@ -281,13 +280,18 @@ typedef unsigned char byte; } TextureFilterMode; // Texture parameters: wrap mode - typedef enum { WRAP_REPEAT = 0, WRAP_CLAMP, WRAP_MIRROR } TextureWrapMode; + typedef enum { + WRAP_REPEAT = 0, + WRAP_CLAMP, + WRAP_MIRROR + } TextureWrapMode; // Color blending modes (pre-defined) - typedef enum { BLEND_ALPHA = 0, BLEND_ADDITIVE, BLEND_MULTIPLIED } BlendMode; - - // TraceLog message types - typedef enum { INFO = 0, ERROR, WARNING, DEBUG, OTHER } TraceLogType; + typedef enum { + BLEND_ALPHA = 0, + BLEND_ADDITIVE, + BLEND_MULTIPLIED + } BlendMode; // VR Head Mounted Display devices typedef enum { diff --git a/src/textures.c b/src/textures.c index 6c56d6c5..2b61c241 100644 --- a/src/textures.c +++ b/src/textures.c @@ -154,14 +154,7 @@ static Image LoadASTC(const char *fileName); // Load ASTC file // Load image from file into CPU memory (RAM) Image LoadImage(const char *fileName) { - Image image; - - // Initialize image default values - image.data = NULL; - image.width = 0; - image.height = 0; - image.mipmaps = 0; - image.format = 0; + Image image = { 0 }; if (IsFileExtension(fileName, ".rres")) { @@ -282,13 +275,7 @@ Image LoadImagePro(void *data, int width, int height, int format) // Load an image from RAW file data Image LoadImageRaw(const char *fileName, int width, int height, int format, int headerSize) { - Image image; - - image.data = NULL; - image.width = 0; - image.height = 0; - image.mipmaps = 0; - image.format = 0; + Image image = { 0 }; FILE *rawFile = fopen(fileName, "rb"); @@ -342,7 +329,7 @@ Image LoadImageRaw(const char *fileName, int width, int height, int format, int // Load texture from file into GPU memory (VRAM) Texture2D LoadTexture(const char *fileName) { - Texture2D texture; + Texture2D texture = { 0 }; Image image = LoadImage(fileName); @@ -351,11 +338,7 @@ Texture2D LoadTexture(const char *fileName) texture = LoadTextureFromImage(image); UnloadImage(image); } - else - { - TraceLog(WARNING, "Texture could not be created"); - texture.id = 0; - } + else TraceLog(WARNING, "Texture could not be created"); return texture; } @@ -364,14 +347,7 @@ Texture2D LoadTexture(const char *fileName) // NOTE: image is not unloaded, it must be done manually Texture2D LoadTextureFromImage(Image image) { - Texture2D texture; - - // Init texture to default values - texture.id = 0; - texture.width = 0; - texture.height = 0; - texture.mipmaps = 0; - texture.format = 0; + Texture2D texture = { 0 }; texture.id = rlglLoadTexture(image.data, image.width, image.height, image.format, image.mipmaps); @@ -510,9 +486,8 @@ Color *GetImageData(Image image) // NOTE: Compressed texture formats not supported Image GetTextureData(Texture2D texture) { - Image image; - image.data = NULL; - + Image image = { 0 }; + if (texture.format < 8) { image.data = rlglReadTexturePixels(texture); -- cgit v1.2.3