mirror of
https://github.com/raysan5/raylib.git
synced 2025-09-20 10:18:14 +00:00
[rlgl] Implement vertex normals for RLGL immediate drawing mode (#3866)
* Fix several non-functional `target_compile_definitions` * Avoid hardcoding the default vertex attribute locations * Implement functional `rlNormal3f` * Add normal definitions for `DrawCube` * Update the basic lighting example to use `DrawCube` and `DrawPlane`
This commit is contained in:
@@ -269,6 +269,7 @@ void DrawCube(Vector3 position, float width, float height, float length, Color c
|
||||
rlColor4ub(color.r, color.g, color.b, color.a);
|
||||
|
||||
// Front face
|
||||
rlNormal3f(0.0f, 0.0f, 1.0f);
|
||||
rlVertex3f(x - width/2, y - height/2, z + length/2); // Bottom Left
|
||||
rlVertex3f(x + width/2, y - height/2, z + length/2); // Bottom Right
|
||||
rlVertex3f(x - width/2, y + height/2, z + length/2); // Top Left
|
||||
@@ -278,6 +279,7 @@ void DrawCube(Vector3 position, float width, float height, float length, Color c
|
||||
rlVertex3f(x + width/2, y - height/2, z + length/2); // Bottom Right
|
||||
|
||||
// Back face
|
||||
rlNormal3f(0.0f, 0.0f, -1.0f);
|
||||
rlVertex3f(x - width/2, y - height/2, z - length/2); // Bottom Left
|
||||
rlVertex3f(x - width/2, y + height/2, z - length/2); // Top Left
|
||||
rlVertex3f(x + width/2, y - height/2, z - length/2); // Bottom Right
|
||||
@@ -287,6 +289,7 @@ void DrawCube(Vector3 position, float width, float height, float length, Color c
|
||||
rlVertex3f(x - width/2, y + height/2, z - length/2); // Top Left
|
||||
|
||||
// Top face
|
||||
rlNormal3f(0.0f, 1.0f, 0.0f);
|
||||
rlVertex3f(x - width/2, y + height/2, z - length/2); // Top Left
|
||||
rlVertex3f(x - width/2, y + height/2, z + length/2); // Bottom Left
|
||||
rlVertex3f(x + width/2, y + height/2, z + length/2); // Bottom Right
|
||||
@@ -296,6 +299,7 @@ void DrawCube(Vector3 position, float width, float height, float length, Color c
|
||||
rlVertex3f(x + width/2, y + height/2, z + length/2); // Bottom Right
|
||||
|
||||
// Bottom face
|
||||
rlNormal3f(0.0f, -1.0f, 0.0f);
|
||||
rlVertex3f(x - width/2, y - height/2, z - length/2); // Top Left
|
||||
rlVertex3f(x + width/2, y - height/2, z + length/2); // Bottom Right
|
||||
rlVertex3f(x - width/2, y - height/2, z + length/2); // Bottom Left
|
||||
@@ -305,6 +309,7 @@ void DrawCube(Vector3 position, float width, float height, float length, Color c
|
||||
rlVertex3f(x - width/2, y - height/2, z - length/2); // Top Left
|
||||
|
||||
// Right face
|
||||
rlNormal3f(1.0f, 0.0f, 0.0f);
|
||||
rlVertex3f(x + width/2, y - height/2, z - length/2); // Bottom Right
|
||||
rlVertex3f(x + width/2, y + height/2, z - length/2); // Top Right
|
||||
rlVertex3f(x + width/2, y + height/2, z + length/2); // Top Left
|
||||
@@ -314,6 +319,7 @@ void DrawCube(Vector3 position, float width, float height, float length, Color c
|
||||
rlVertex3f(x + width/2, y + height/2, z + length/2); // Top Left
|
||||
|
||||
// Left face
|
||||
rlNormal3f(-1.0f, 0.0f, 0.0f);
|
||||
rlVertex3f(x - width/2, y - height/2, z - length/2); // Bottom Right
|
||||
rlVertex3f(x - width/2, y + height/2, z + length/2); // Top Left
|
||||
rlVertex3f(x - width/2, y + height/2, z - length/2); // Top Right
|
||||
@@ -1208,13 +1214,13 @@ void UploadMesh(Mesh *mesh, bool dynamic)
|
||||
// Enable vertex attributes: position (shader-location = 0)
|
||||
void *vertices = (mesh->animVertices != NULL)? mesh->animVertices : mesh->vertices;
|
||||
mesh->vboId[0] = rlLoadVertexBuffer(vertices, mesh->vertexCount*3*sizeof(float), dynamic);
|
||||
rlSetVertexAttribute(0, 3, RL_FLOAT, 0, 0, 0);
|
||||
rlEnableVertexAttribute(0);
|
||||
rlSetVertexAttribute(RL_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION, 3, RL_FLOAT, 0, 0, 0);
|
||||
rlEnableVertexAttribute(RL_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION);
|
||||
|
||||
// Enable vertex attributes: texcoords (shader-location = 1)
|
||||
mesh->vboId[1] = rlLoadVertexBuffer(mesh->texcoords, mesh->vertexCount*2*sizeof(float), dynamic);
|
||||
rlSetVertexAttribute(1, 2, RL_FLOAT, 0, 0, 0);
|
||||
rlEnableVertexAttribute(1);
|
||||
rlSetVertexAttribute(RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD, 2, RL_FLOAT, 0, 0, 0);
|
||||
rlEnableVertexAttribute(RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD);
|
||||
|
||||
// WARNING: When setting default vertex attribute values, the values for each generic vertex attribute
|
||||
// is part of current state, and it is maintained even if a different program object is used
|
||||
@@ -1224,64 +1230,64 @@ void UploadMesh(Mesh *mesh, bool dynamic)
|
||||
// Enable vertex attributes: normals (shader-location = 2)
|
||||
void *normals = (mesh->animNormals != NULL)? mesh->animNormals : mesh->normals;
|
||||
mesh->vboId[2] = rlLoadVertexBuffer(normals, mesh->vertexCount*3*sizeof(float), dynamic);
|
||||
rlSetVertexAttribute(2, 3, RL_FLOAT, 0, 0, 0);
|
||||
rlEnableVertexAttribute(2);
|
||||
rlSetVertexAttribute(RL_DEFAULT_SHADER_ATTRIB_LOCATION_NORMAL, 3, RL_FLOAT, 0, 0, 0);
|
||||
rlEnableVertexAttribute(RL_DEFAULT_SHADER_ATTRIB_LOCATION_NORMAL);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Default vertex attribute: normal
|
||||
// WARNING: Default value provided to shader if location available
|
||||
float value[3] = { 1.0f, 1.0f, 1.0f };
|
||||
rlSetVertexAttributeDefault(2, value, SHADER_ATTRIB_VEC3, 3);
|
||||
rlDisableVertexAttribute(2);
|
||||
rlSetVertexAttributeDefault(RL_DEFAULT_SHADER_ATTRIB_LOCATION_NORMAL, value, SHADER_ATTRIB_VEC3, 3);
|
||||
rlDisableVertexAttribute(RL_DEFAULT_SHADER_ATTRIB_LOCATION_NORMAL);
|
||||
}
|
||||
|
||||
if (mesh->colors != NULL)
|
||||
{
|
||||
// Enable vertex attribute: color (shader-location = 3)
|
||||
mesh->vboId[3] = rlLoadVertexBuffer(mesh->colors, mesh->vertexCount*4*sizeof(unsigned char), dynamic);
|
||||
rlSetVertexAttribute(3, 4, RL_UNSIGNED_BYTE, 1, 0, 0);
|
||||
rlEnableVertexAttribute(3);
|
||||
rlSetVertexAttribute(RL_DEFAULT_SHADER_ATTRIB_LOCATION_COLOR, 4, RL_UNSIGNED_BYTE, 1, 0, 0);
|
||||
rlEnableVertexAttribute(RL_DEFAULT_SHADER_ATTRIB_LOCATION_COLOR);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Default vertex attribute: color
|
||||
// WARNING: Default value provided to shader if location available
|
||||
float value[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; // WHITE
|
||||
rlSetVertexAttributeDefault(3, value, SHADER_ATTRIB_VEC4, 4);
|
||||
rlDisableVertexAttribute(3);
|
||||
rlSetVertexAttributeDefault(RL_DEFAULT_SHADER_ATTRIB_LOCATION_COLOR, value, SHADER_ATTRIB_VEC4, 4);
|
||||
rlDisableVertexAttribute(RL_DEFAULT_SHADER_ATTRIB_LOCATION_COLOR);
|
||||
}
|
||||
|
||||
if (mesh->tangents != NULL)
|
||||
{
|
||||
// Enable vertex attribute: tangent (shader-location = 4)
|
||||
mesh->vboId[4] = rlLoadVertexBuffer(mesh->tangents, mesh->vertexCount*4*sizeof(float), dynamic);
|
||||
rlSetVertexAttribute(4, 4, RL_FLOAT, 0, 0, 0);
|
||||
rlEnableVertexAttribute(4);
|
||||
rlSetVertexAttribute(RL_DEFAULT_SHADER_ATTRIB_LOCATION_TANGENT, 4, RL_FLOAT, 0, 0, 0);
|
||||
rlEnableVertexAttribute(RL_DEFAULT_SHADER_ATTRIB_LOCATION_TANGENT);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Default vertex attribute: tangent
|
||||
// WARNING: Default value provided to shader if location available
|
||||
float value[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
|
||||
rlSetVertexAttributeDefault(4, value, SHADER_ATTRIB_VEC4, 4);
|
||||
rlDisableVertexAttribute(4);
|
||||
rlSetVertexAttributeDefault(RL_DEFAULT_SHADER_ATTRIB_LOCATION_TANGENT, value, SHADER_ATTRIB_VEC4, 4);
|
||||
rlDisableVertexAttribute(RL_DEFAULT_SHADER_ATTRIB_LOCATION_TANGENT);
|
||||
}
|
||||
|
||||
if (mesh->texcoords2 != NULL)
|
||||
{
|
||||
// Enable vertex attribute: texcoord2 (shader-location = 5)
|
||||
mesh->vboId[5] = rlLoadVertexBuffer(mesh->texcoords2, mesh->vertexCount*2*sizeof(float), dynamic);
|
||||
rlSetVertexAttribute(5, 2, RL_FLOAT, 0, 0, 0);
|
||||
rlEnableVertexAttribute(5);
|
||||
rlSetVertexAttribute(RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD2, 2, RL_FLOAT, 0, 0, 0);
|
||||
rlEnableVertexAttribute(RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD2);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Default vertex attribute: texcoord2
|
||||
// WARNING: Default value provided to shader if location available
|
||||
float value[2] = { 0.0f, 0.0f };
|
||||
rlSetVertexAttributeDefault(5, value, SHADER_ATTRIB_VEC2, 2);
|
||||
rlDisableVertexAttribute(5);
|
||||
rlSetVertexAttributeDefault(RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD2, value, SHADER_ATTRIB_VEC2, 2);
|
||||
rlDisableVertexAttribute(RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD2);
|
||||
}
|
||||
|
||||
if (mesh->indices != NULL)
|
||||
@@ -3504,8 +3510,8 @@ void GenMeshTangents(Mesh *mesh)
|
||||
}
|
||||
|
||||
rlEnableVertexArray(mesh->vaoId);
|
||||
rlSetVertexAttribute(4, 4, RL_FLOAT, 0, 0, 0);
|
||||
rlEnableVertexAttribute(4);
|
||||
rlSetVertexAttribute(RL_DEFAULT_SHADER_ATTRIB_LOCATION_TANGENT, 4, RL_FLOAT, 0, 0, 0);
|
||||
rlEnableVertexAttribute(RL_DEFAULT_SHADER_ATTRIB_LOCATION_TANGENT);
|
||||
rlDisableVertexArray();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user