Added some texture functionality (view details)
LoadTextureEx() - Simplified parameters UpdateTexture() - Added, allows updating GPU texture data
This commit is contained in:
parent
002dacef40
commit
76024b5036
4 changed files with 76 additions and 10 deletions
|
@ -579,7 +579,7 @@ Image LoadImageEx(Color *pixels, int width, int height);
|
||||||
Image LoadImageRaw(const char *fileName, int width, int height, int format, int headerSize); // Load image data from RAW file
|
Image LoadImageRaw(const char *fileName, int width, int height, int format, int headerSize); // Load image data from RAW file
|
||||||
Image LoadImageFromRES(const char *rresName, int resId); // Load an image from rRES file (raylib Resource)
|
Image LoadImageFromRES(const char *rresName, int resId); // Load an image from rRES file (raylib Resource)
|
||||||
Texture2D LoadTexture(const char *fileName); // Load an image as texture into GPU memory
|
Texture2D LoadTexture(const char *fileName); // Load an image as texture into GPU memory
|
||||||
Texture2D LoadTextureEx(void *data, int width, int height, int textureFormat, int mipmapCount); // Load a texture from raw data into GPU memory
|
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 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
|
Texture2D LoadTextureFromImage(Image image); // Load a texture from image data
|
||||||
void UnloadImage(Image image); // Unload image from CPU memory (RAM)
|
void UnloadImage(Image image); // Unload image from CPU memory (RAM)
|
||||||
|
@ -602,6 +602,7 @@ void ImageColorGrayscale(Image *image);
|
||||||
void ImageColorContrast(Image *image, float contrast); // Modify image color: contrast (-100 to 100)
|
void ImageColorContrast(Image *image, float contrast); // Modify image color: contrast (-100 to 100)
|
||||||
void ImageColorBrightness(Image *image, int brightness); // Modify image color: brightness (-255 to 255)
|
void ImageColorBrightness(Image *image, int brightness); // Modify image color: brightness (-255 to 255)
|
||||||
void GenTextureMipmaps(Texture2D texture); // Generate GPU mipmaps for a texture
|
void GenTextureMipmaps(Texture2D texture); // Generate GPU mipmaps for a texture
|
||||||
|
void UpdateTexture(Texture2D texture, void *pixels); // Update GPU texture with new data
|
||||||
|
|
||||||
void DrawTexture(Texture2D texture, int posX, int posY, Color tint); // Draw a Texture2D
|
void DrawTexture(Texture2D texture, int posX, int posY, Color tint); // Draw a Texture2D
|
||||||
void DrawTextureV(Texture2D texture, Vector2 position, Color tint); // Draw a Texture2D with position defined as Vector2
|
void DrawTextureV(Texture2D texture, Vector2 position, Color tint); // Draw a Texture2D with position defined as Vector2
|
||||||
|
|
66
src/rlgl.c
66
src/rlgl.c
|
@ -1880,6 +1880,38 @@ unsigned int rlglLoadTexture(void *data, int width, int height, int textureForma
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void rlglUpdateTexture(unsigned int id, int width, int height, int format, void *data)
|
||||||
|
{
|
||||||
|
glBindTexture(GL_TEXTURE_2D, id);
|
||||||
|
|
||||||
|
#if defined(GRAPHICS_API_OPENGL_33)
|
||||||
|
switch (format)
|
||||||
|
{
|
||||||
|
case UNCOMPRESSED_GRAYSCALE: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RED, GL_UNSIGNED_BYTE, (unsigned char *)data); break;
|
||||||
|
case UNCOMPRESSED_GRAY_ALPHA: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RG, GL_UNSIGNED_BYTE, (unsigned char *)data); break;
|
||||||
|
case UNCOMPRESSED_R5G6B5: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, (unsigned short *)data); break;
|
||||||
|
case UNCOMPRESSED_R8G8B8: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, (unsigned char *)data); break;
|
||||||
|
case UNCOMPRESSED_R5G5B5A1: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, (unsigned short *)data); break;
|
||||||
|
case UNCOMPRESSED_R4G4B4A4: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, (unsigned short *)data); break;
|
||||||
|
case UNCOMPRESSED_R8G8B8A8: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, (unsigned char *)data); break;
|
||||||
|
default: TraceLog(WARNING, "Texture format updating not supported"); break;
|
||||||
|
}
|
||||||
|
#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 (format)
|
||||||
|
{
|
||||||
|
case UNCOMPRESSED_GRAYSCALE: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_LUMINANCE, GL_UNSIGNED_BYTE, (unsigned char *)data); break;
|
||||||
|
case UNCOMPRESSED_GRAY_ALPHA: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, (unsigned char *)data); break;
|
||||||
|
case UNCOMPRESSED_R5G6B5: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, (unsigned short *)data); break;
|
||||||
|
case UNCOMPRESSED_R8G8B8: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, (unsigned char *)data); break;
|
||||||
|
case UNCOMPRESSED_R5G5B5A1: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, (unsigned short *)data); break;
|
||||||
|
case UNCOMPRESSED_R4G4B4A4: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, (unsigned short *)data); break;
|
||||||
|
case UNCOMPRESSED_R8G8B8A8: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, (unsigned char *)data); break;
|
||||||
|
default: TraceLog(WARNING, "Texture format updating not supported"); break;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
// Generate mipmap data for selected texture
|
// Generate mipmap data for selected texture
|
||||||
void rlglGenerateMipmaps(unsigned int textureId)
|
void rlglGenerateMipmaps(unsigned int textureId)
|
||||||
{
|
{
|
||||||
|
@ -2046,6 +2078,7 @@ unsigned char *rlglReadScreenPixels(int width, int height)
|
||||||
|
|
||||||
// Read texture pixel data
|
// Read texture pixel data
|
||||||
// NOTE: Retrieving pixel data from GPU (glGetTexImage()) not supported on OpenGL ES 2.0
|
// NOTE: Retrieving pixel data from GPU (glGetTexImage()) not supported on OpenGL ES 2.0
|
||||||
|
//void *rlglReadTexturePixels(Texture2D texture) // Required to know texture size! It could not be retrieved on OpenGL ES 2.0
|
||||||
void *rlglReadTexturePixels(unsigned int textureId, unsigned int format)
|
void *rlglReadTexturePixels(unsigned int textureId, unsigned int format)
|
||||||
{
|
{
|
||||||
void *pixels = NULL;
|
void *pixels = NULL;
|
||||||
|
@ -2108,14 +2141,32 @@ void *rlglReadTexturePixels(unsigned int textureId, unsigned int format)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(GRAPHICS_API_OPENGL_ES2)
|
#if defined(GRAPHICS_API_OPENGL_ES2)
|
||||||
// TODO: Look for some way to retrieve texture width and height from id
|
// TODO: Look for some way to retrieve texture width and height from id -> NO WAY AVAILABLE
|
||||||
int width = 1024;
|
int width = 1024;
|
||||||
int height = 1024;
|
int height = 1024;
|
||||||
|
|
||||||
FBO fbo = rlglLoadFBO(width, height);
|
FBO fbo = rlglLoadFBO(width, height);
|
||||||
|
|
||||||
// NOTE: Altenatively we can bind texture to color fbo and glReadPixels()
|
// NOTE: Two possible Options:
|
||||||
|
// 1 - Bind texture to color fbo attachment and glReadPixels()
|
||||||
|
// 2 - Create an fbo, activate it, render quad with texture, glReadPixels()
|
||||||
|
|
||||||
|
#define GET_TEXTURE_FBO_OPTION_1
|
||||||
|
|
||||||
|
#if defined(GET_TEXTURE_FBO_OPTION_1)
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, fbo.id);
|
||||||
|
|
||||||
|
// Attach color texture and depth renderbuffer to FBO
|
||||||
|
// NOTE: texture must RGB
|
||||||
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureId, 0);
|
||||||
|
|
||||||
|
pixels = (unsigned char *)malloc(width*height*3*sizeof(unsigned char));
|
||||||
|
|
||||||
|
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
|
||||||
|
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
|
|
||||||
|
#elif defined(GET_TEXTURE_FBO_OPTION_2)
|
||||||
// Render texture to fbo
|
// Render texture to fbo
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, fbo.id);
|
glBindFramebuffer(GL_FRAMEBUFFER, fbo.id);
|
||||||
glClearColor(0.0, 0.0, 0.0, 0.0);
|
glClearColor(0.0, 0.0, 0.0, 0.0);
|
||||||
|
@ -2131,8 +2182,12 @@ void *rlglReadTexturePixels(unsigned int textureId, unsigned int format)
|
||||||
//glDisable(GL_BLEND);
|
//glDisable(GL_BLEND);
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
|
||||||
//Model quad = GenModelQuad(width, height);
|
Model quad;
|
||||||
//DrawModel(quad, (Vector3){ 0, 0, 0 }, 1.0f, WHITE);
|
//quad.mesh = GenMeshQuad(width, height);
|
||||||
|
quad.transform = MatrixIdentity();
|
||||||
|
quad.shader = simpleShader;
|
||||||
|
|
||||||
|
DrawModel(quad, (Vector3){ 0, 0, 0 }, 1.0f, WHITE);
|
||||||
|
|
||||||
pixels = (unsigned char *)malloc(width*height*4*sizeof(unsigned char));
|
pixels = (unsigned char *)malloc(width*height*4*sizeof(unsigned char));
|
||||||
|
|
||||||
|
@ -2140,7 +2195,8 @@ void *rlglReadTexturePixels(unsigned int textureId, unsigned int format)
|
||||||
|
|
||||||
// Bind framebuffer 0, which means render to back buffer
|
// Bind framebuffer 0, which means render to back buffer
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
|
#endif // GET_TEXTURE_FBO_OPTION
|
||||||
|
|
||||||
// Clean up temporal fbo
|
// Clean up temporal fbo
|
||||||
rlglUnloadFBO(fbo);
|
rlglUnloadFBO(fbo);
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -242,7 +242,8 @@ void rlglClose(void); // De-init rlgl
|
||||||
void rlglDraw(void); // Draw VAO/VBO
|
void rlglDraw(void); // Draw VAO/VBO
|
||||||
void rlglInitGraphics(int offsetX, int offsetY, int width, int height); // Initialize Graphics (OpenGL stuff)
|
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 in GPU OpenGL texture
|
unsigned int rlglLoadTexture(void *data, int width, int height, int textureFormat, int mipmapCount); // Load texture in GPU
|
||||||
|
void rlglUpdateTexture(unsigned int id, int width, int height, int format, void *data); // Update GPU texture with new data
|
||||||
void rlglGenerateMipmaps(unsigned int textureId); // Generate mipmap data for selected texture
|
void rlglGenerateMipmaps(unsigned int textureId); // Generate mipmap data for selected texture
|
||||||
|
|
||||||
// NOTE: There is a set of shader related functions that are available to end user,
|
// NOTE: There is a set of shader related functions that are available to end user,
|
||||||
|
|
|
@ -338,16 +338,17 @@ Texture2D LoadTexture(const char *fileName)
|
||||||
return texture;
|
return texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
Texture2D LoadTextureEx(void *data, int width, int height, int textureFormat, int mipmapCount)
|
// Load a texture from raw data into GPU memory
|
||||||
|
Texture2D LoadTextureEx(void *data, int width, int height, int textureFormat)
|
||||||
{
|
{
|
||||||
Texture2D texture;
|
Texture2D texture;
|
||||||
|
|
||||||
texture.width = width;
|
texture.width = width;
|
||||||
texture.height = height;
|
texture.height = height;
|
||||||
texture.mipmaps = mipmapCount;
|
texture.mipmaps = 1;
|
||||||
texture.format = textureFormat;
|
texture.format = textureFormat;
|
||||||
|
|
||||||
texture.id = rlglLoadTexture(data, width, height, textureFormat, mipmapCount);
|
texture.id = rlglLoadTexture(data, width, height, textureFormat, 1);
|
||||||
|
|
||||||
return texture;
|
return texture;
|
||||||
}
|
}
|
||||||
|
@ -1172,6 +1173,13 @@ void GenTextureMipmaps(Texture2D texture)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update GPU texture with new data
|
||||||
|
// NOTE: pixels data must match texture.format
|
||||||
|
void UpdateTexture(Texture2D texture, void *pixels)
|
||||||
|
{
|
||||||
|
rlglUpdateTexture(texture.id, texture.width, texture.height, texture.format, pixels);
|
||||||
|
}
|
||||||
|
|
||||||
// Draw a Texture2D
|
// Draw a Texture2D
|
||||||
void DrawTexture(Texture2D texture, int posX, int posY, Color tint)
|
void DrawTexture(Texture2D texture, int posX, int posY, Color tint)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue