Skip to content

Commit d2cd2a0

Browse files
authored
[rlgl][rmodels] Add instranceTransform shader location index #4538 (#4579)
1 parent 93a6741 commit d2cd2a0

7 files changed

Lines changed: 18 additions & 8 deletions

File tree

examples/shaders/resources/shaders/glsl330/lighting_instancing.vs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void main()
2525
// Send vertex attributes to fragment shader
2626
fragPosition = vec3(instanceTransform*vec4(vertexPosition, 1.0));
2727
fragTexCoord = vertexTexCoord;
28-
//fragColor = vertexColor;
28+
fragColor = vec4(1.0);
2929
fragNormal = normalize(vec3(matNormal*vec4(vertexNormal, 1.0)));
3030

3131
// Calculate final vertex position, note that we multiply mvp by instanceTransform

examples/shaders/shaders_mesh_instancing.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ int main(void)
6161
{
6262
Matrix translation = MatrixTranslate((float)GetRandomValue(-50, 50), (float)GetRandomValue(-50, 50), (float)GetRandomValue(-50, 50));
6363
Vector3 axis = Vector3Normalize((Vector3){ (float)GetRandomValue(0, 360), (float)GetRandomValue(0, 360), (float)GetRandomValue(0, 360) });
64-
float angle = (float)GetRandomValue(0, 10)*DEG2RAD;
64+
float angle = (float)GetRandomValue(0, 180)*DEG2RAD;
6565
Matrix rotation = MatrixRotate(axis, angle);
6666

6767
transforms[i] = MatrixMultiply(rotation, translation);
@@ -73,7 +73,6 @@ int main(void)
7373
// Get shader locations
7474
shader.locs[SHADER_LOC_MATRIX_MVP] = GetShaderLocation(shader, "mvp");
7575
shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos");
76-
shader.locs[SHADER_LOC_MATRIX_MODEL] = GetShaderLocationAttrib(shader, "instanceTransform");
7776

7877
// Set shader value: ambient light level
7978
int ambientLoc = GetShaderLocation(shader, "ambient");

src/config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@
151151
#define RL_DEFAULT_SHADER_ATTRIB_LOCATION_BONEIDS 7
152152
#define RL_DEFAULT_SHADER_ATTRIB_LOCATION_BONEWEIGHTS 8
153153
#endif
154+
#define RL_DEFAULT_SHADER_ATTRIB_LOCATION_INSTANCE_TX 9
155+
154156

155157
// Default shader vertex attribute names to set location points
156158
// NOTE: When a new shader is loaded, the following locations are tried to be set for convenience

src/raylib.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,8 @@ typedef enum {
801801
SHADER_LOC_MAP_BRDF, // Shader location: sampler2d texture: brdf
802802
SHADER_LOC_VERTEX_BONEIDS, // Shader location: vertex attribute: boneIds
803803
SHADER_LOC_VERTEX_BONEWEIGHTS, // Shader location: vertex attribute: boneWeights
804-
SHADER_LOC_BONE_MATRICES // Shader location: array of matrices uniform: boneMatrices
804+
SHADER_LOC_BONE_MATRICES, // Shader location: array of matrices uniform: boneMatrices
805+
SHADER_LOC_VERTEX_INSTANCE_TX // Shader location: vertex attribute: instanceTransform
805806
} ShaderLocationIndex;
806807

807808
#define SHADER_LOC_MAP_DIFFUSE SHADER_LOC_MAP_ALBEDO

src/rcore.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,7 @@ Shader LoadShaderFromMemory(const char *vsCode, const char *fsCode)
13461346
shader.locs[SHADER_LOC_VERTEX_COLOR] = rlGetLocationAttrib(shader.id, RL_DEFAULT_SHADER_ATTRIB_NAME_COLOR);
13471347
shader.locs[SHADER_LOC_VERTEX_BONEIDS] = rlGetLocationAttrib(shader.id, RL_DEFAULT_SHADER_ATTRIB_NAME_BONEIDS);
13481348
shader.locs[SHADER_LOC_VERTEX_BONEWEIGHTS] = rlGetLocationAttrib(shader.id, RL_DEFAULT_SHADER_ATTRIB_NAME_BONEWEIGHTS);
1349+
shader.locs[SHADER_LOC_VERTEX_INSTANCE_TX] = rlGetLocationAttrib(shader.id, RL_DEFAULT_SHADER_ATTRIB_NAME_INSTANCE_TX);
13491350

13501351
// Get handles to GLSL uniform locations (vertex shader)
13511352
shader.locs[SHADER_LOC_MATRIX_MVP] = rlGetLocationUniform(shader.id, RL_DEFAULT_SHADER_UNIFORM_NAME_MVP);

src/rlgl.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,9 @@
355355
#define RL_DEFAULT_SHADER_ATTRIB_LOCATION_BONEWEIGHTS 8
356356
#endif
357357
#endif
358+
#ifndef RL_DEFAULT_SHADER_ATTRIB_LOCATION_INSTANCE_TX
359+
#define RL_DEFAULT_SHADER_ATTRIB_LOCATION_INSTANCE_TX 9
360+
#endif
358361

359362
//----------------------------------------------------------------------------------
360363
// Types and Structures Definition
@@ -998,6 +1001,9 @@ RLAPI void rlLoadDrawQuad(void); // Load and draw a quad
9981001
#ifndef RL_DEFAULT_SHADER_ATTRIB_NAME_BONEWEIGHTS
9991002
#define RL_DEFAULT_SHADER_ATTRIB_NAME_BONEWEIGHTS "vertexBoneWeights" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_NAME_BONEWEIGHTS
10001003
#endif
1004+
#ifndef RL_DEFAULT_SHADER_ATTRIB_NAME_INSTANCE_TX
1005+
#define RL_DEFAULT_SHADER_ATTRIB_NAME_INSTANCE_TX "instanceTransform" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_NAME_INSTANCE_TX
1006+
#endif
10011007

10021008
#ifndef RL_DEFAULT_SHADER_UNIFORM_NAME_MVP
10031009
#define RL_DEFAULT_SHADER_UNIFORM_NAME_MVP "mvp" // model-view-projection matrix
@@ -4216,6 +4222,7 @@ unsigned int rlLoadShaderProgram(unsigned int vShaderId, unsigned int fShaderId)
42164222
glBindAttribLocation(program, RL_DEFAULT_SHADER_ATTRIB_LOCATION_COLOR, RL_DEFAULT_SHADER_ATTRIB_NAME_COLOR);
42174223
glBindAttribLocation(program, RL_DEFAULT_SHADER_ATTRIB_LOCATION_TANGENT, RL_DEFAULT_SHADER_ATTRIB_NAME_TANGENT);
42184224
glBindAttribLocation(program, RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD2, RL_DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD2);
4225+
glBindAttribLocation(program, RL_DEFAULT_SHADER_ATTRIB_LOCATION_INSTANCE_TX, RL_DEFAULT_SHADER_ATTRIB_NAME_INSTANCE_TX);
42194226

42204227
#ifdef RL_SUPPORT_MESH_GPU_SKINNING
42214228
glBindAttribLocation(program, RL_DEFAULT_SHADER_ATTRIB_LOCATION_BONEIDS, RL_DEFAULT_SHADER_ATTRIB_NAME_BONEIDS);

src/rmodels.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,12 +1734,12 @@ void DrawMeshInstanced(Mesh mesh, Material material, const Matrix *transforms, i
17341734
// no faster, since we're transferring all the transform matrices anyway
17351735
instancesVboId = rlLoadVertexBuffer(instanceTransforms, instances*sizeof(float16), false);
17361736

1737-
// Instances transformation matrices are send to shader attribute location: SHADER_LOC_MATRIX_MODEL
1737+
// Instances transformation matrices are sent to shader attribute location: SHADER_LOC_VERTEX_INSTANCE_TX
17381738
for (unsigned int i = 0; i < 4; i++)
17391739
{
1740-
rlEnableVertexAttribute(material.shader.locs[SHADER_LOC_MATRIX_MODEL] + i);
1741-
rlSetVertexAttribute(material.shader.locs[SHADER_LOC_MATRIX_MODEL] + i, 4, RL_FLOAT, 0, sizeof(Matrix), i*sizeof(Vector4));
1742-
rlSetVertexAttributeDivisor(material.shader.locs[SHADER_LOC_MATRIX_MODEL] + i, 1);
1740+
rlEnableVertexAttribute(material.shader.locs[SHADER_LOC_VERTEX_INSTANCE_TX] + i);
1741+
rlSetVertexAttribute(material.shader.locs[SHADER_LOC_VERTEX_INSTANCE_TX] + i, 4, RL_FLOAT, 0, sizeof(Matrix), i*sizeof(Vector4));
1742+
rlSetVertexAttributeDivisor(material.shader.locs[SHADER_LOC_VERTEX_INSTANCE_TX] + i, 1);
17431743
}
17441744

17451745
rlDisableVertexBuffer();

0 commit comments

Comments
 (0)