Adapt multiple functions to rlgl

Nearly a complete rework of Models module
Some teaks on multiple functions
This commit is contained in:
raysan5 2014-04-04 20:11:57 +02:00
parent 79cf87d91d
commit c04f37d0f5
8 changed files with 760 additions and 454 deletions

View File

@ -131,7 +131,7 @@ Sound LoadSound(char *fileName)
// NOTE: Buffer space is allocated inside LoadWAV, Wave must be freed // NOTE: Buffer space is allocated inside LoadWAV, Wave must be freed
Wave wave = LoadWAV(fileName); Wave wave = LoadWAV(fileName);
ALenum format; ALenum format = 0;
// The OpenAL format is worked out by looking at the number of channels and the bits per sample // The OpenAL format is worked out by looking at the number of channels and the bits per sample
if (wave.channels == 1) if (wave.channels == 1)
{ {
@ -257,7 +257,7 @@ Sound LoadSoundFromRES(const char *rresName, int resId)
free(data); free(data);
// Convert wave to Sound (OpenAL) // Convert wave to Sound (OpenAL)
ALenum format; ALenum format = 0;
// The OpenAL format is worked out by looking at the number of channels and the bits per sample // The OpenAL format is worked out by looking at the number of channels and the bits per sample
if (wave.channels == 1) if (wave.channels == 1)

File diff suppressed because it is too large Load Diff

View File

@ -355,15 +355,17 @@ const char *FormatText(const char *text, ...);
void DrawCube(Vector3 position, float width, float height, float lenght, Color color); // Draw cube void DrawCube(Vector3 position, float width, float height, float lenght, Color color); // Draw cube
void DrawCubeV(Vector3 position, Vector3 size, Color color); // Draw cube (Vector version) void DrawCubeV(Vector3 position, Vector3 size, Color color); // Draw cube (Vector version)
void DrawCubeWires(Vector3 position, float width, float height, float lenght, Color color); // Draw cube wires void DrawCubeWires(Vector3 position, float width, float height, float lenght, Color color); // Draw cube wires
void DrawCubeTexture(Texture2D texture, Vector3 position, float width, float height, float lenght, Color color); // Draw cube textured
void DrawSphere(Vector3 centerPos, float radius, Color color); // Draw sphere void DrawSphere(Vector3 centerPos, float radius, Color color); // Draw sphere
void DrawSphereEx(Vector3 centerPos, float radius, int rings, int slices, Color color); // Draw sphere with extended parameters void DrawSphereEx(Vector3 centerPos, float radius, int rings, int slices, Color color); // Draw sphere with extended parameters
void DrawSphereWires(Vector3 centerPos, float radius, Color color); // Draw sphere wires void DrawSphereWires(Vector3 centerPos, float radius, int rings, int slices, Color color); // Draw sphere wires
void DrawCylinder(Vector3 position, float radiusTop, float radiusBottom, float height, int slices, Color color); // Draw a cylinder/cone void DrawCylinder(Vector3 position, float radiusTop, float radiusBottom, float height, int slices, Color color); // Draw a cylinder/cone
void DrawCylinderWires(Vector3 position, float radiusTop, float radiusBottom, float height, int slices, Color color); // Draw a cylinder/cone wires void DrawCylinderWires(Vector3 position, float radiusTop, float radiusBottom, float height, int slices, Color color); // Draw a cylinder/cone wires
void DrawPlane(Vector3 centerPos, Vector2 size, Vector3 rotation, Color color); // Draw a plane void DrawPlane(Vector3 centerPos, Vector2 size, Vector3 rotation, Color color); // Draw a plane
void DrawPlaneEx(Vector3 centerPos, Vector2 size, Vector3 rotation, int slicesX, int slicesZ, Color color); // Draw a plane with divisions void DrawPlaneEx(Vector3 centerPos, Vector2 size, Vector3 rotation, int slicesX, int slicesZ, Color color); // Draw a plane with divisions
void DrawGrid(int slices, float spacing); // Draw a grid (centered at (0, 0, 0)) void DrawGrid(int slices, float spacing); // Draw a grid (centered at (0, 0, 0))
void DrawGizmo(Vector3 position, bool orbits); // Draw gizmo (with or without orbits) void DrawGizmo(Vector3 position); // Draw simple gizmo
void DrawGizmoEx(Vector3 position, Vector3 rotation, float scale, bool orbits); // Draw gizmo with extended parameters
//DrawTorus(), DrawTeapot() are useless... //DrawTorus(), DrawTeapot() are useless...
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------

View File

@ -49,9 +49,9 @@
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Defines and Macros // Defines and Macros
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
#define MATRIX_STACK_SIZE 16 // TODO: REVIEW: Matrix stack required? #define MATRIX_STACK_SIZE 16 // Matrix stack max size
#define MAX_DRAWS_BY_TEXTURE 256 // Draws are organized by texture changes #define MAX_DRAWS_BY_TEXTURE 256 // Draws are organized by texture changes
#define TEMP_VERTEX_BUFFER_SIZE 1024 // Temporal Vertex Buffer (required for post-transformations) #define TEMP_VERTEX_BUFFER_SIZE 1024 // Temporal Vertex Buffer (required for vertex-transformations)
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Types and Structures Definition // Types and Structures Definition
@ -67,7 +67,7 @@ typedef struct {
float *vertices; // 3 components per vertex float *vertices; // 3 components per vertex
float *colors; // 4 components per vertex float *colors; // 4 components per vertex
} VertexPositionColorBuffer; } VertexPositionColorBuffer;
/*
typedef struct { typedef struct {
int vCounter; int vCounter;
int tcCounter; int tcCounter;
@ -76,7 +76,17 @@ typedef struct {
float *texcoords; // 2 components per vertex float *texcoords; // 2 components per vertex
float *colors; // 4 components per vertex float *colors; // 4 components per vertex
} VertexPositionColorTextureBuffer; } VertexPositionColorTextureBuffer;
*/
/*
typedef struct {
int vCounter;
int tcCounter;
int nCounter;
float *vertices; // 3 components per vertex
float *texcoords; // 2 components per vertex
float *normals; // 3 components per vertex
} VertexPositionTextureNormalBuffer;
*/
typedef struct { typedef struct {
int vCounter; int vCounter;
int tcCounter; int tcCounter;
@ -108,8 +118,8 @@ static int currentMatrixMode;
static DrawMode currentDrawMode; static DrawMode currentDrawMode;
// Vertex arrays for lines, triangles and quads // Vertex arrays for lines, triangles and quads
static VertexPositionColorBuffer lines; static VertexPositionColorBuffer lines; // No texture support
static VertexPositionColorTextureBuffer triangles; static VertexPositionColorBuffer triangles; // No texture support
static VertexPositionColorTextureIndexBuffer quads; static VertexPositionColorTextureIndexBuffer quads;
// Vetex-Fragment Shader Program ID // Vetex-Fragment Shader Program ID
@ -614,6 +624,14 @@ void rlDeleteTextures(unsigned int id)
glDeleteTextures(1, &id); glDeleteTextures(1, &id);
} }
// Unload vertex data from GPU memory
void rlDeleteVertexArrays(unsigned int id)
{
#if defined(USE_OPENGL_33) || defined(USE_OPENGL_ES2)
glDeleteVertexArrays(1, &id);
#endif
}
// Clear color buffer with color // Clear color buffer with color
void rlClearColor(byte r, byte g, byte b, byte a) void rlClearColor(byte r, byte g, byte b, byte a)
{ {
@ -655,10 +673,11 @@ void rlglInit()
if (glewIsSupported("GL_VERSION_3_3")) printf("OpenGL 3.3 initialized\n"); if (glewIsSupported("GL_VERSION_3_3")) printf("OpenGL 3.3 initialized\n");
/* /*
// TODO: GLEW is a big library that loads ALL extensions, maybe using glad we can only load required ones...
if (!gladLoadGL()) if (!gladLoadGL())
{ {
printf("Something went wrong!\n"); fprintf(stderr, printf("Failed to initialize glad.\n");
exit(-1); exit(1);
} }
*/ */
// Set default draw mode // Set default draw mode
@ -694,7 +713,7 @@ void rlglInit()
printf("Vendor: %s\n", glGetString(GL_VENDOR)); printf("Vendor: %s\n", glGetString(GL_VENDOR));
printf("Renderer: %s\n", glGetString(GL_RENDERER)); printf("Renderer: %s\n", glGetString(GL_RENDERER));
printf("Version: %s\n", glGetString(GL_VERSION)); printf("Version: %s\n", glGetString(GL_VERSION));
printf("GLSL: %s\n\n", glGetString(0x8B8C)); // GL_SHADING_LANGUAGE_VERSION printf("GLSL: %s\n\n", glGetString(0x8B8C)); //GL_SHADING_LANGUAGE_VERSION
InitializeBuffers(); // Init vertex arrays InitializeBuffers(); // Init vertex arrays
InitializeVAOs(); // Init VBO and VAO InitializeVAOs(); // Init VBO and VAO
@ -707,7 +726,7 @@ void rlglInit()
// Create default white texture for plain colors (required by shader) // Create default white texture for plain colors (required by shader)
unsigned char pixels[4] = { 255, 255, 255, 255 }; // 1 pixel RGBA (4 bytes) unsigned char pixels[4] = { 255, 255, 255, 255 }; // 1 pixel RGBA (4 bytes)
whiteTexture = rlglTexture(1, 1, pixels); whiteTexture = rlglLoadTexture(1, 1, pixels);
// Init draw calls tracking system // Init draw calls tracking system
draws = (DrawCall *)malloc(sizeof(DrawCall)*MAX_DRAWS_BY_TEXTURE); draws = (DrawCall *)malloc(sizeof(DrawCall)*MAX_DRAWS_BY_TEXTURE);
@ -794,8 +813,12 @@ void rlglDraw()
if (triangles.vCounter > 0) if (triangles.vCounter > 0)
{ {
glBindTexture(GL_TEXTURE_2D, whiteTexture);
glBindVertexArray(vaoTriangles); glBindVertexArray(vaoTriangles);
glDrawArrays(GL_TRIANGLES, 0, triangles.vCounter); glDrawArrays(GL_TRIANGLES, 0, triangles.vCounter);
glBindTexture(GL_TEXTURE_2D, 0);
} }
if (quads.vCounter > 0) if (quads.vCounter > 0)
@ -830,8 +853,6 @@ void rlglDraw()
glDrawElements(GL_TRIANGLES, numIndicesToProcess, GL_UNSIGNED_INT, (GLvoid*) (sizeof(GLuint) * indicesOffset)); glDrawElements(GL_TRIANGLES, numIndicesToProcess, GL_UNSIGNED_INT, (GLvoid*) (sizeof(GLuint) * indicesOffset));
indicesOffset += draws[i].vCount/4*6; indicesOffset += draws[i].vCount/4*6;
//printf("-------Next vertex offset: %i\n", indicesOffset/6*4);
} }
} }
@ -849,7 +870,7 @@ void rlglDraw()
lines.cCounter = 0; lines.cCounter = 0;
triangles.vCounter = 0; triangles.vCounter = 0;
triangles.vCounter = 0; triangles.cCounter = 0;
quads.vCounter = 0; quads.vCounter = 0;
quads.tcCounter = 0; quads.tcCounter = 0;
@ -861,6 +882,58 @@ void rlglDraw()
#endif #endif
} }
void rlglDrawModel(Model model, bool wires)
{
if (wires) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
#ifdef USE_OPENGL_11
// NOTE: For models we use Vertex Arrays (OpenGL 1.1)
glEnableClientState(GL_VERTEX_ARRAY); // Enable vertex array
glEnableClientState(GL_TEXTURE_COORD_ARRAY); // Enable texture coords array
glEnableClientState(GL_NORMAL_ARRAY); // Enable normals array
glVertexPointer(3, GL_FLOAT, 0, model.vertices); // Pointer to vertex coords array
glTexCoordPointer(2, GL_FLOAT, 0, model.texcoords); // Pointer to texture coords array
glNormalPointer(GL_FLOAT, 0, model.normals); // Pointer to normals array
//glColorPointer(4, GL_UNSIGNED_BYTE, 0, model.colors); // Pointer to colors array (NOT USED)
rlPushMatrix();
rlTranslatef(position.x, position.y, position.z);
//glRotatef(rotation * GetFrameTime(), 0, 1, 0);
rlScalef(scale, scale, scale);
rlColor4ub(color.r, color.g, color.b, color.a);
glDrawArrays(GL_TRIANGLES, 0, model.numVertices);
rlPopMatrix();
glDisableClientState(GL_VERTEX_ARRAY); // Disable vertex array
glDisableClientState(GL_TEXTURE_COORD_ARRAY); // Disable texture coords array
glDisableClientState(GL_NORMAL_ARRAY); // Disable normals array
#endif
#ifdef USE_OPENGL_33
glUseProgram(shaderProgram); // Use our shader
Matrix modelview2 = MatrixMultiply(model.transform, modelview);
// NOTE: Drawing in OpenGL 3.3+, transform is passed to shader
glUniformMatrix4fv(projectionMatrixLoc, 1, false, GetMatrixVector(projection));
glUniformMatrix4fv(modelviewMatrixLoc, 1, false, GetMatrixVector(modelview2));
glUniform1i(textureLoc, 0);
glBindVertexArray(model.vaoId);
//glBindTexture(GL_TEXTURE_2D, model.textureId);
glDrawArrays(GL_TRIANGLES, 0, model.numVertices);
//glBindTexture(GL_TEXTURE_2D, 0); // Unbind textures
glBindVertexArray(0); // Unbind VAO
#endif
if (wires) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
#endif #endif
// Initialize Graphics Device (OpenGL stuff) // Initialize Graphics Device (OpenGL stuff)
@ -909,7 +982,7 @@ void rlglInitGraphicsDevice(int fbWidth, int fbHeight)
// Convert image data to OpenGL texture (returns OpenGL valid Id) // Convert image data to OpenGL texture (returns OpenGL valid Id)
// NOTE: Image is not unloaded, it should be done manually... // NOTE: Image is not unloaded, it should be done manually...
unsigned int rlglTexture(int width, int height, unsigned char *pixels) unsigned int rlglLoadTexture(int width, int height, unsigned char *pixels)
{ {
glBindTexture(GL_TEXTURE_2D,0); // Free any old binding glBindTexture(GL_TEXTURE_2D,0); // Free any old binding
@ -926,7 +999,7 @@ unsigned int rlglTexture(int width, int height, unsigned char *pixels)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // Filter for pixel-perfect drawing, alternative: GL_LINEAR glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // Filter for pixel-perfect drawing, alternative: GL_LINEAR
#ifdef USE_OPENGL_33 #ifdef USE_OPENGL_33
// Trilinear filtering! // Trilinear filtering
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); // Activate use of mipmaps (must be available) //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); // Activate use of mipmaps (must be available)
//glGenerateMipmap(GL_TEXTURE_2D); // OpenGL 3.3! //glGenerateMipmap(GL_TEXTURE_2D); // OpenGL 3.3!
@ -947,6 +1020,39 @@ unsigned int rlglTexture(int width, int height, unsigned char *pixels)
return id; return id;
} }
#ifdef USE_OPENGL_33
unsigned int rlglLoadModel(VertexData data)
{
GLuint vaoModel; // Vertex Array Objects (VAO)
GLuint vertexBuffer[3]; // Vertex Buffer Objects (VBO)
// Initialize Quads VAO (Buffer A)
glGenVertexArrays(1, &vaoModel);
glBindVertexArray(vaoModel);
// Create buffers for our vertex data (positions, texcoords, normals)
glGenBuffers(3, vertexBuffer);
// Enable vertex attributes
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*data.numVertices, data.vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(vertexLoc);
glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, 0, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*data.numVertices, data.texcoords, GL_STATIC_DRAW);
glEnableVertexAttribArray(texcoordLoc);
glVertexAttribPointer(texcoordLoc, 2, GL_FLOAT, 0, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*data.numVertices, data.normals, GL_STATIC_DRAW);
//glEnableVertexAttribArray(normalLoc);
//glVertexAttribPointer(normalLoc, 3, GL_FLOAT, 0, 0, 0);
return vaoModel;
}
#endif
// Read screen pixel data (color buffer) // Read screen pixel data (color buffer)
unsigned char *rlglReadScreenPixels(int width, int height) unsigned char *rlglReadScreenPixels(int width, int height)
{ {
@ -1303,11 +1409,13 @@ static void UpdateBuffers()
// Triangles - vertex positions buffer // Triangles - vertex positions buffer
glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[0]); glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*3*MAX_TRIANGLES_BATCH, triangles.vertices, GL_DYNAMIC_DRAW); //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*3*MAX_TRIANGLES_BATCH, triangles.vertices, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*3*triangles.vCounter, triangles.vertices);
// Triangles - colors buffer // Triangles - colors buffer
glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[1]); glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*3*MAX_TRIANGLES_BATCH, triangles.colors, GL_DYNAMIC_DRAW); //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*3*MAX_TRIANGLES_BATCH, triangles.colors, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*4*triangles.cCounter, triangles.colors);
//-------------------------------------------------------------- //--------------------------------------------------------------
@ -1355,6 +1463,12 @@ static void UpdateBuffers()
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*4*quads.vCounter, quads.colors); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*4*quads.vCounter, quads.colors);
} }
// Another option would be using buffer mapping...
//triangles.vertices = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
// Now we can modify vertices
//glUnmapBuffer(GL_ARRAY_BUFFER);
//-------------------------------------------------------------- //--------------------------------------------------------------
// Unbind the current VAO // Unbind the current VAO

View File

@ -29,9 +29,15 @@
#ifndef RLGL_H #ifndef RLGL_H
#define 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 typedef: Model
#endif
// Select desired OpenGL version // Select desired OpenGL version
//#define USE_OPENGL_11 #define USE_OPENGL_11
#define USE_OPENGL_33 //#define USE_OPENGL_33
//#define USE_OPENGL_ES2 //#define USE_OPENGL_ES2
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -50,6 +56,29 @@ typedef enum { RL_PROJECTION, RL_MODELVIEW, RL_TEXTURE } MatrixMode;
typedef enum { RL_LINES, RL_TRIANGLES, RL_QUADS } DrawMode; typedef enum { RL_LINES, RL_TRIANGLES, RL_QUADS } DrawMode;
#ifdef RLGL_STANDALONE
typedef struct Model Model;
#endif
typedef struct {
int numVertices;
float *vertices; // 3 components per vertex
float *texcoords; // 2 components per vertex
float *normals; // 3 components per vertex
} VertexData;
#ifdef USE_OPENGL_11
struct Model {
VertexData data;
};
#else
struct Model {
unsigned int vaoId;
Matrix transform;
int numVertices;
};
#endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { // Prevents name mangling of functions extern "C" { // Prevents name mangling of functions
#endif #endif
@ -88,7 +117,8 @@ void rlColor4f(float x, float y, float z, float w); // Define one vertex (color)
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
void rlEnableTexture(unsigned int id); // Enable texture usage void rlEnableTexture(unsigned int id); // Enable texture usage
void rlDisableTexture(); // Disable texture usage void rlDisableTexture(); // Disable texture usage
void rlDeleteTextures(unsigned int id); // Delete OpenGL texture void rlDeleteTextures(unsigned int id); // Delete OpenGL texture from GPU
void rlDeleteVertexArrays(unsigned int id); // Unload vertex data from GPU memory
void rlClearColor(byte r, byte g, byte b, byte a); // Clear color buffer with color void rlClearColor(byte r, byte g, byte b, byte a); // Clear color buffer with color
void rlClearScreenBuffers(); // Clear used screen buffers (color and depth) void rlClearScreenBuffers(); // Clear used screen buffers (color and depth)
@ -96,13 +126,15 @@ void rlClearScreenBuffers(); // Clear used screen buffers (color
// Functions Declaration - rlgl functionality // Functions Declaration - rlgl functionality
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
#ifdef USE_OPENGL_33 #ifdef USE_OPENGL_33
void rlglInit(); // Initialize rlgl (shaders, VAO, VBO...) void rlglInit(); // Initialize rlgl (shaders, VAO, VBO...)
void rlglClose(); // De-init rlgl void rlglClose(); // De-init rlgl
void rlglDraw(); // Draw VAOs void rlglDraw(); // Draw VAOs
unsigned int rlglLoadModel(VertexData data);
#endif #endif
void rlglDrawModel(Model model, bool wires); // Draw model
void rlglInitGraphicsDevice(int fbWidth, int fbHeight); // Initialize Graphics Device (OpenGL stuff) void rlglInitGraphicsDevice(int fbWidth, int fbHeight); // Initialize Graphics Device (OpenGL stuff)
unsigned int rlglTexture(int width, int height, unsigned char *pixels); // Create OpenGL texture unsigned int rlglLoadTexture(int width, int height, unsigned char *pixels); // Load in GPU OpenGL texture
byte *rlglReadScreenPixels(int width, int height); // Read screen pixel data (color buffer) byte *rlglReadScreenPixels(int width, int height); // Read screen pixel data (color buffer)
#ifdef USE_OPENGL_33 #ifdef USE_OPENGL_33

View File

@ -62,7 +62,7 @@ void DrawPixel(int posX, int posY, Color color)
rlColor4ub(color.r, color.g, color.b, color.a); rlColor4ub(color.r, color.g, color.b, color.a);
rlVertex2i(posX, posY); rlVertex2i(posX, posY);
rlVertex2i(posX + 1, posY + 1); rlVertex2i(posX + 1, posY + 1);
rlEnd(); rlEnd();
} }
// Draw a pixel (Vector version) // Draw a pixel (Vector version)
@ -96,7 +96,6 @@ void DrawLineV(Vector2 startPos, Vector2 endPos, Color color)
} }
// Draw a color-filled circle // Draw a color-filled circle
// TODO: Review, on some GPUs is drawn with a weird transparency (GL_POLYGON_SMOOTH issue?)
void DrawCircle(int centerX, int centerY, float radius, Color color) void DrawCircle(int centerX, int centerY, float radius, Color color)
{ {
DrawPoly((Vector2){centerX, centerY}, 360, radius, 0, color); DrawPoly((Vector2){centerX, centerY}, 360, radius, 0, color);
@ -107,13 +106,13 @@ void DrawCircle(int centerX, int centerY, float radius, Color color)
void DrawCircleGradient(int centerX, int centerY, float radius, Color color1, Color color2) void DrawCircleGradient(int centerX, int centerY, float radius, Color color1, Color color2)
{ {
rlBegin(RL_TRIANGLES); rlBegin(RL_TRIANGLES);
for (int i=0; i <= 360; i += 2) //i++ --> Step = 1.0 pixels for (int i=0; i < 360; i += 2)
{ {
rlColor4ub(color1.r, color1.g, color1.b, color1.a); rlColor4ub(color1.r, color1.g, color1.b, color1.a);
rlVertex2i(centerX, centerY); rlVertex2i(centerX, centerY);
rlColor4ub(color2.r, color2.g, color2.b, color2.a); rlColor4ub(color2.r, color2.g, color2.b, color2.a);
rlVertex2f(centerX + sin(DEG2RAD*i) * radius, centerY + cos(DEG2RAD*i) * radius); rlVertex2f(centerX + sin(DEG2RAD*i) * radius, centerY + cos(DEG2RAD*i) * radius);
rlVertex2f(centerX + sin(DEG2RAD*(i+1)) * radius, centerY + cos(DEG2RAD*(i+1)) * radius); rlVertex2f(centerX + sin(DEG2RAD*(i+2)) * radius, centerY + cos(DEG2RAD*(i+2)) * radius);
} }
rlEnd(); rlEnd();
} }
@ -127,7 +126,7 @@ void DrawCircleV(Vector2 center, float radius, Color color)
rlColor4ub(color.r, color.g, color.b, color.a); rlColor4ub(color.r, color.g, color.b, color.a);
rlVertex2i(center.x, center.y); rlVertex2i(center.x, center.y);
rlVertex2f(center.x + sin(DEG2RAD*i) * radius, center.y + cos(DEG2RAD*i) * radius); rlVertex2f(center.x + sin(DEG2RAD*i) * radius, center.y + cos(DEG2RAD*i) * radius);
rlVertex2f(center.x + sin(DEG2RAD*(i+1)) * radius, center.y + cos(DEG2RAD*(i+1)) * radius); rlVertex2f(center.x + sin(DEG2RAD*(i+2)) * radius, center.y + cos(DEG2RAD*(i+2)) * radius);
} }
rlEnd(); rlEnd();
} }
@ -135,9 +134,6 @@ void DrawCircleV(Vector2 center, float radius, Color color)
// Draw circle outline // Draw circle outline
void DrawCircleLines(int centerX, int centerY, float radius, Color color) void DrawCircleLines(int centerX, int centerY, float radius, Color color)
{ {
//glEnable(GL_LINE_SMOOTH); // Smoothies circle outline (anti-aliasing applied)
//glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); // Best quality for line smooth (anti-aliasing best algorithm)
rlBegin(RL_LINES); rlBegin(RL_LINES);
rlColor4ub(color.r, color.g, color.b, color.a); rlColor4ub(color.r, color.g, color.b, color.a);
@ -148,10 +144,6 @@ void DrawCircleLines(int centerX, int centerY, float radius, Color color)
rlVertex2f(centerX + sin(DEG2RAD*(i+1)) * radius, centerY + cos(DEG2RAD*(i+1)) * radius); rlVertex2f(centerX + sin(DEG2RAD*(i+1)) * radius, centerY + cos(DEG2RAD*(i+1)) * radius);
} }
rlEnd(); rlEnd();
//glDisable(GL_LINE_SMOOTH);
// TODO: Draw all lines with line smooth??? --> Do it before drawing lines VAO
} }
// Draw a color-filled rectangle // Draw a color-filled rectangle
@ -255,16 +247,17 @@ void DrawPoly(Vector2 center, int sides, float radius, float rotation, Color col
if (sides < 3) sides = 3; if (sides < 3) sides = 3;
rlPushMatrix(); rlPushMatrix();
rlTranslatef(center.x, center.y, 0.0);
rlRotatef(rotation, 0, 0, 1); // TODO: compute vertex rotation manually! rlRotatef(rotation, 0, 0, 1);
rlBegin(RL_TRIANGLES); rlBegin(RL_TRIANGLES);
for (int i=0; i < 360; i += 2) for (int i=0; i < 360; i += 360/sides)
{ {
rlColor4ub(color.r, color.g, color.b, color.a); rlColor4ub(color.r, color.g, color.b, color.a);
rlVertex2i(center.x, center.y);
rlVertex2f(center.x + sin(DEG2RAD*i) * radius, center.y + cos(DEG2RAD*i) * radius); rlVertex2i(0, 0);
rlVertex2f(center.x + sin(DEG2RAD*(i+1)) * radius, center.y + cos(DEG2RAD*(i+1)) * radius); rlVertex2f(sin(DEG2RAD*i) * radius, cos(DEG2RAD*i) * radius);
rlVertex2f(sin(DEG2RAD*(i+360/sides)) * radius, cos(DEG2RAD*(i+360/sides)) * radius);
} }
rlEnd(); rlEnd();
rlPopMatrix(); rlPopMatrix();

View File

@ -313,7 +313,6 @@ void DrawTextEx(SpriteFont spriteFont, const char* text, Vector2 position, int f
rlEnableTexture(spriteFont.texture.glId); rlEnableTexture(spriteFont.texture.glId);
// Optimized to use one draw call per string
rlBegin(RL_QUADS); rlBegin(RL_QUADS);
for(int i = 0; i < length; i++) for(int i = 0; i < length; i++)
{ {

View File

@ -273,31 +273,30 @@ void DrawTextureEx(Texture2D texture, Vector2 position, float rotation, float sc
{ {
rlEnableTexture(texture.glId); rlEnableTexture(texture.glId);
// TODO: Apply rotation to vertex! --> rotate from origin CW (0, 0)
// TODO: Compute vertex scaling!
// NOTE: Rotation is applied before translation and scaling, even being called in inverse order... // NOTE: Rotation is applied before translation and scaling, even being called in inverse order...
// NOTE: Rotation point is upper-left corner // NOTE: Rotation point is upper-left corner
//rlTranslatef(position.x, position.y, 0); rlPushMatrix();
//rlScalef(scale, scale, 1.0f); rlTranslatef(position.x, position.y, 0.0);
//rlRotatef(rotation, 0, 0, 1); rlScalef(scale, scale, 1.0f);
rlRotatef(rotation, 0, 0, 1);
rlBegin(RL_QUADS);
rlColor4ub(tint.r, tint.g, tint.b, tint.a); rlBegin(RL_QUADS);
rlNormal3f(0.0f, 0.0f, 1.0f); // Normal vector pointing towards viewer rlColor4ub(tint.r, tint.g, tint.b, tint.a);
rlNormal3f(0.0f, 0.0f, 1.0f); // Normal vector pointing towards viewer
rlTexCoord2f(0.0f, 0.0f);
rlVertex2f(position.x, position.y); // Bottom-left corner for texture and quad rlTexCoord2f(0.0f, 0.0f);
rlVertex2f(position.x, position.y); // Bottom-left corner for texture and quad
rlTexCoord2f(0.0f, 1.0f);
rlVertex2f(position.x, position.y + texture.height); // Bottom-right corner for texture and quad rlTexCoord2f(0.0f, 1.0f);
rlVertex2f(position.x, position.y + texture.height); // Bottom-right corner for texture and quad
rlTexCoord2f(1.0f, 1.0f);
rlVertex2f(position.x + texture.width, position.y + texture.height); // Top-right corner for texture and quad rlTexCoord2f(1.0f, 1.0f);
rlVertex2f(position.x + texture.width, position.y + texture.height); // Top-right corner for texture and quad
rlTexCoord2f(1.0f, 0.0f);
rlVertex2f(position.x + texture.width, position.y); // Top-left corner for texture and quad rlTexCoord2f(1.0f, 0.0f);
rlEnd(); rlVertex2f(position.x + texture.width, position.y); // Top-left corner for texture and quad
rlEnd();
rlPopMatrix();
rlDisableTexture(); rlDisableTexture();
} }
@ -337,32 +336,33 @@ void DrawTexturePro(Texture2D texture, Rectangle sourceRec, Rectangle destRec, V
{ {
rlEnableTexture(texture.glId); rlEnableTexture(texture.glId);
// TODO: Apply translation, rotation and scaling of vertex manually! // NOTE: First we translate texture to origin to apply rotation and translation from there
rlPushMatrix();
//rlTranslatef(-origin.x, -origin.y, 0); rlTranslatef(-origin.x, -origin.y, 0);
//rlRotatef(rotation, 0, 0, 1); rlRotatef(rotation, 0, 0, 1);
//rlTranslatef(destRec.x + origin.x, destRec.y + origin.y, 0); rlTranslatef(destRec.x + origin.x, destRec.y + origin.y, 0);
rlBegin(RL_QUADS); rlBegin(RL_QUADS);
rlColor4ub(tint.r, tint.g, tint.b, tint.a); rlColor4ub(tint.r, tint.g, tint.b, tint.a);
rlNormal3f(0.0f, 0.0f, 1.0f); // Normal vector pointing towards viewer rlNormal3f(0.0f, 0.0f, 1.0f); // Normal vector pointing towards viewer
// Bottom-left corner for texture and quad // Bottom-left corner for texture and quad
rlTexCoord2f((float)sourceRec.x / texture.width, (float)sourceRec.y / texture.height); rlTexCoord2f((float)sourceRec.x / texture.width, (float)sourceRec.y / texture.height);
rlVertex2f(0.0f, 0.0f); rlVertex2f(0.0f, 0.0f);
// Bottom-right corner for texture and quad // Bottom-right corner for texture and quad
rlTexCoord2f((float)(sourceRec.x + sourceRec.width) / texture.width, (float)sourceRec.y / texture.height); rlTexCoord2f((float)(sourceRec.x + sourceRec.width) / texture.width, (float)sourceRec.y / texture.height);
rlVertex2f(destRec.width, 0.0f); rlVertex2f(destRec.width, 0.0f);
// Top-right corner for texture and quad // Top-right corner for texture and quad
rlTexCoord2f((float)(sourceRec.x + sourceRec.width) / texture.width, (float)(sourceRec.y + sourceRec.height) / texture.height); rlTexCoord2f((float)(sourceRec.x + sourceRec.width) / texture.width, (float)(sourceRec.y + sourceRec.height) / texture.height);
rlVertex2f(destRec.width, destRec.height); rlVertex2f(destRec.width, destRec.height);
// Top-left corner for texture and quad // Top-left corner for texture and quad
rlTexCoord2f((float)sourceRec.x / texture.width, (float)(sourceRec.y + sourceRec.height) / texture.height); rlTexCoord2f((float)sourceRec.x / texture.width, (float)(sourceRec.y + sourceRec.height) / texture.height);
rlVertex2f(0.0f, destRec.height); rlVertex2f(0.0f, destRec.height);
rlEnd(); rlEnd();
rlPopMatrix();
rlDisableTexture(); rlDisableTexture();
} }
@ -385,7 +385,7 @@ Texture2D CreateTexture(Image image)
j++; j++;
} }
texture.glId = rlglTexture(image.width, image.height, img); texture.glId = rlglLoadTexture(image.width, image.height, img);
texture.width = image.width; texture.width = image.width;
texture.height = image.height; texture.height = image.height;