Redesign to use Material type -IN PROGRESS-

Requires Shader access functions review
This commit is contained in:
raysan5 2016-03-06 02:05:16 +01:00
parent 893facdf6d
commit c9d22c7a14
4 changed files with 89 additions and 233 deletions

View file

@ -551,10 +551,7 @@ Model LoadModel(const char *fileName)
// NOTE: At this point we have all vertex, texcoord, normal data for the model in mesh struct // NOTE: At this point we have all vertex, texcoord, normal data for the model in mesh struct
if (mesh.vertexCount == 0) if (mesh.vertexCount == 0) TraceLog(WARNING, "Model could not be loaded");
{
TraceLog(WARNING, "Model could not be loaded");
}
else else
{ {
// NOTE: model properties (transform, texture, shader) are initialized inside rlglLoadModel() // NOTE: model properties (transform, texture, shader) are initialized inside rlglLoadModel()
@ -627,17 +624,8 @@ void UnloadModel(Model model)
// Link a texture to a model // Link a texture to a model
void SetModelTexture(Model *model, Texture2D texture) void SetModelTexture(Model *model, Texture2D texture)
{ {
if (texture.id <= 0) if (texture.id <= 0) model->material.texDiffuse.id = whiteTexture; // Use default white texture
{ else model->material.texDiffuse = texture;
// Use default white texture (use mesh color)
model->texture.id = whiteTexture; // OpenGL 1.1
model->shader.texDiffuseId = whiteTexture; // OpenGL 3.3 / ES 2.0
}
else
{
model->texture = texture;
model->shader.texDiffuseId = texture.id;
}
} }
static Mesh GenMeshHeightmap(Image heightmap, Vector3 size) static Mesh GenMeshHeightmap(Image heightmap, Vector3 size)

View file

@ -349,11 +349,6 @@ typedef struct Mesh {
typedef struct Shader { typedef struct Shader {
unsigned int id; // Shader program id unsigned int id; // Shader program id
// TODO: This should be Texture2D objects
unsigned int texDiffuseId; // Diffuse texture id
unsigned int texNormalId; // Normal texture id
unsigned int texSpecularId; // Specular texture id
// Variable attributes // Variable attributes
int vertexLoc; // Vertex attribute location point (vertex shader) int vertexLoc; // Vertex attribute location point (vertex shader)
int texcoordLoc; // Texcoord attribute location point (vertex shader) int texcoordLoc; // Texcoord attribute location point (vertex shader)
@ -370,20 +365,19 @@ typedef struct Shader {
} Shader; } Shader;
// Material type // Material type
// TODO: Redesign material-shaders-textures system
typedef struct Material { typedef struct Material {
//Shader shader; Shader shader; // Standard shader (supports 3 map types: diffuse, normal, specular)
//Texture2D texDiffuse; // Diffuse texture Texture2D texDiffuse; // Diffuse texture
//Texture2D texNormal; // Normal texture Texture2D texNormal; // Normal texture
//Texture2D texSpecular; // Specular texture Texture2D texSpecular; // Specular texture
Color colDiffuse; Color colDiffuse; // Diffuse color
Color colAmbient; Color colAmbient; // Ambient color
Color colSpecular; Color colSpecular; // Specular color
float glossiness; float glossiness; // Glossiness level
float normalDepth; float normalDepth; // Normal map depth
} Material; } Material;
// 3d Model type // 3d Model type
@ -391,9 +385,7 @@ typedef struct Material {
typedef struct Model { typedef struct Model {
Mesh mesh; Mesh mesh;
Matrix transform; Matrix transform;
Texture2D texture; // Only for OpenGL 1.1, on newer versions this should be in the shader Material material;
Shader shader;
//Material material;
} Model; } Model;
// Ray type (useful for raycast) // Ray type (useful for raycast)
@ -806,10 +798,10 @@ int GetShaderLocation(Shader shader, const char *uniformName);
void SetShaderValue(Shader shader, int uniformLoc, float *value, int size); // Set shader uniform value (float) void SetShaderValue(Shader shader, int uniformLoc, float *value, int size); // Set shader uniform value (float)
void SetShaderValuei(Shader shader, int uniformLoc, int *value, int size); // Set shader uniform value (int) void SetShaderValuei(Shader shader, int uniformLoc, int *value, int size); // Set shader uniform value (int)
void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix mat); // Set shader uniform value (matrix 4x4) void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix mat); // Set shader uniform value (matrix 4x4)
void SetShaderMapDiffuse(Shader *shader, Texture2D texture); // Default diffuse shader map texture assignment //void SetShaderMapDiffuse(Shader *shader, Texture2D texture); // Default diffuse shader map texture assignment
void SetShaderMapNormal(Shader *shader, const char *uniformName, Texture2D texture); // Normal map texture shader assignment //void SetShaderMapNormal(Shader *shader, const char *uniformName, Texture2D texture); // Normal map texture shader assignment
void SetShaderMapSpecular(Shader *shader, const char *uniformName, Texture2D texture); // Specular map texture shader assignment //void SetShaderMapSpecular(Shader *shader, const char *uniformName, Texture2D texture); // Specular map texture shader assignment
void SetShaderMap(Shader *shader, int mapLocation, Texture2D texture, int textureUnit); // TODO: Generic shader map assignment //void SetShaderMap(Shader *shader, int mapLocation, Texture2D texture, int textureUnit); // TODO: Generic shader map assignment
void SetBlendMode(int mode); // Set blending mode (alpha, additive, multiplied) void SetBlendMode(int mode); // Set blending mode (alpha, additive, multiplied)

View file

@ -1421,7 +1421,7 @@ void rlglDrawModel(Model model, Vector3 position, Vector3 rotationAxis, float ro
#if defined(GRAPHICS_API_OPENGL_11) #if defined(GRAPHICS_API_OPENGL_11)
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, model.texture.id); glBindTexture(GL_TEXTURE_2D, model.material.texDiffuse.id);
// NOTE: On OpenGL 1.1 we use Vertex Arrays to draw model // NOTE: On OpenGL 1.1 we use Vertex Arrays to draw model
glEnableClientState(GL_VERTEX_ARRAY); // Enable vertex array glEnableClientState(GL_VERTEX_ARRAY); // Enable vertex array
@ -1452,7 +1452,7 @@ void rlglDrawModel(Model model, Vector3 position, Vector3 rotationAxis, float ro
#endif #endif
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
glUseProgram(model.shader.id); glUseProgram(model.material.shader.id);
// At this point the modelview matrix just contains the view matrix (camera) // At this point the modelview matrix just contains the view matrix (camera)
// That's because Begin3dMode() sets it an no model-drawing function modifies it, all use rlPushMatrix() and rlPopMatrix() // That's because Begin3dMode() sets it an no model-drawing function modifies it, all use rlPushMatrix() and rlPopMatrix()
@ -1476,28 +1476,30 @@ void rlglDrawModel(Model model, Vector3 position, Vector3 rotationAxis, float ro
Matrix matMVP = MatrixMultiply(matModelView, matProjection); // Transform to screen-space coordinates Matrix matMVP = MatrixMultiply(matModelView, matProjection); // Transform to screen-space coordinates
// Send combined model-view-projection matrix to shader // Send combined model-view-projection matrix to shader
glUniformMatrix4fv(model.shader.mvpLoc, 1, false, MatrixToFloat(matMVP)); glUniformMatrix4fv(model.material.shader.mvpLoc, 1, false, MatrixToFloat(matMVP));
// Apply color tinting to model // Apply color tinting to model
// NOTE: Just update one uniform on fragment shader // NOTE: Just update one uniform on fragment shader
float vColor[4] = { (float)color.r/255, (float)color.g/255, (float)color.b/255, (float)color.a/255 }; float vColor[4] = { (float)color.r/255, (float)color.g/255, (float)color.b/255, (float)color.a/255 };
glUniform4fv(model.shader.tintColorLoc, 1, vColor); glUniform4fv(model.material.shader.tintColorLoc, 1, vColor);
// Set shader textures (diffuse, normal, specular) // Set shader textures (diffuse, normal, specular)
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, model.shader.texDiffuseId); glBindTexture(GL_TEXTURE_2D, model.material.texDiffuse.id);
glUniform1i(model.shader.mapDiffuseLoc, 0); glUniform1i(model.material.shader.mapDiffuseLoc, 0); // Texture fits in active texture unit 0
if (model.shader.texNormalId != 0) if (model.material.texNormal.id != 0)
{ {
glActiveTexture(GL_TEXTURE1); glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, model.shader.texNormalId); glBindTexture(GL_TEXTURE_2D, model.material.texNormal.id);
glUniform1i(model.material.shader.mapNormalLoc, 1); // Texture fits in active texture unit 1
} }
if (model.shader.texSpecularId != 0) if (model.material.texSpecular.id != 0)
{ {
glActiveTexture(GL_TEXTURE2); glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, model.shader.texSpecularId); glBindTexture(GL_TEXTURE_2D, model.material.texSpecular.id);
glUniform1i(model.material.shader.mapSpecularLoc, 2); // Texture fits in active texture unit 2
} }
if (vaoSupported) if (vaoSupported)
@ -1508,19 +1510,19 @@ void rlglDrawModel(Model model, Vector3 position, Vector3 rotationAxis, float ro
{ {
// Bind model VBOs data // Bind model VBOs data
glBindBuffer(GL_ARRAY_BUFFER, model.mesh.vboId[0]); glBindBuffer(GL_ARRAY_BUFFER, model.mesh.vboId[0]);
glVertexAttribPointer(model.shader.vertexLoc, 3, GL_FLOAT, 0, 0, 0); glVertexAttribPointer(model.material.shader.vertexLoc, 3, GL_FLOAT, 0, 0, 0);
glEnableVertexAttribArray(model.shader.vertexLoc); glEnableVertexAttribArray(model.material.shader.vertexLoc);
glBindBuffer(GL_ARRAY_BUFFER, model.mesh.vboId[1]); glBindBuffer(GL_ARRAY_BUFFER, model.mesh.vboId[1]);
glVertexAttribPointer(model.shader.texcoordLoc, 2, GL_FLOAT, 0, 0, 0); glVertexAttribPointer(model.material.shader.texcoordLoc, 2, GL_FLOAT, 0, 0, 0);
glEnableVertexAttribArray(model.shader.texcoordLoc); glEnableVertexAttribArray(model.material.shader.texcoordLoc);
// Add normals support // Add normals support
if (model.shader.normalLoc != -1) if (model.material.shader.normalLoc != -1)
{ {
glBindBuffer(GL_ARRAY_BUFFER, model.mesh.vboId[2]); glBindBuffer(GL_ARRAY_BUFFER, model.mesh.vboId[2]);
glVertexAttribPointer(model.shader.normalLoc, 3, GL_FLOAT, 0, 0, 0); glVertexAttribPointer(model.material.shader.normalLoc, 3, GL_FLOAT, 0, 0, 0);
glEnableVertexAttribArray(model.shader.normalLoc); glEnableVertexAttribArray(model.material.shader.normalLoc);
} }
} }
@ -1531,13 +1533,13 @@ void rlglDrawModel(Model model, Vector3 position, Vector3 rotationAxis, float ro
//glDisableVertexAttribArray(model.shader.texcoordLoc); //glDisableVertexAttribArray(model.shader.texcoordLoc);
//if (model.shader.normalLoc != -1) glDisableVertexAttribArray(model.shader.normalLoc); //if (model.shader.normalLoc != -1) glDisableVertexAttribArray(model.shader.normalLoc);
if (model.shader.texNormalId != 0) if (model.material.texNormal.id != 0)
{ {
glActiveTexture(GL_TEXTURE1); glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
} }
if (model.shader.texSpecularId != 0) if (model.material.texSpecular.id != 0)
{ {
glActiveTexture(GL_TEXTURE2); glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
@ -1907,22 +1909,29 @@ Model rlglLoadModel(Mesh mesh)
Model model; Model model;
model.mesh = mesh; model.mesh = mesh;
model.transform = MatrixIdentity();
model.mesh.vaoId = 0; // Vertex Array Object model.mesh.vaoId = 0; // Vertex Array Object
model.mesh.vboId[0] = 0; // Vertex position VBO model.mesh.vboId[0] = 0; // Vertex positions VBO
model.mesh.vboId[1] = 0; // Texcoords VBO model.mesh.vboId[1] = 0; // Vertex texcoords VBO
model.mesh.vboId[2] = 0; // Normals VBO model.mesh.vboId[2] = 0; // Vertex normals VBO
model.transform = MatrixIdentity();
#if defined(GRAPHICS_API_OPENGL_11) #if defined(GRAPHICS_API_OPENGL_11)
model.texture.id = 0; // No texture required model.material.texDiffuse.id = 0; // No texture required
model.shader.id = 0; // No shader used model.material.shader.id = 0; // No shader used
#elif defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) #elif defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
model.texture.id = whiteTexture; // Default whiteTexture model.material.shader = simpleShader; // Default model shader
model.texture.width = 1; // Default whiteTexture width
model.texture.height = 1; // Default whiteTexture height model.material.texDiffuse.id = whiteTexture; // Default whiteTexture
model.texture.format = UNCOMPRESSED_R8G8B8A8; // Default whiteTexture format model.material.texDiffuse.width = 1; // Default whiteTexture width
model.shader = simpleShader; // Default model shader model.material.texDiffuse.height = 1; // Default whiteTexture height
model.material.texDiffuse.format = UNCOMPRESSED_R8G8B8A8; // Default whiteTexture format
model.material.texNormal.id = 0; // By default, no normal texture
model.material.texSpecular.id = 0; // By default, no specular texture
// TODO: Fill default material properties (color, glossiness...)
GLuint vaoModel = 0; // Vertex Array Objects (VAO) GLuint vaoModel = 0; // Vertex Array Objects (VAO)
GLuint vertexBuffer[3]; // Vertex Buffer Objects (VBO) GLuint vertexBuffer[3]; // Vertex Buffer Objects (VBO)
@ -1940,20 +1949,20 @@ Model rlglLoadModel(Mesh mesh)
// Enable vertex attributes: position // Enable vertex attributes: position
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[0]); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh.vertexCount, mesh.vertices, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh.vertexCount, mesh.vertices, GL_STATIC_DRAW);
glVertexAttribPointer(model.shader.vertexLoc, 3, GL_FLOAT, 0, 0, 0); glVertexAttribPointer(model.material.shader.vertexLoc, 3, GL_FLOAT, 0, 0, 0);
glEnableVertexAttribArray(model.shader.vertexLoc); glEnableVertexAttribArray(model.material.shader.vertexLoc);
// Enable vertex attributes: texcoords // Enable vertex attributes: texcoords
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[1]); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*mesh.vertexCount, mesh.texcoords, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*mesh.vertexCount, mesh.texcoords, GL_STATIC_DRAW);
glVertexAttribPointer(model.shader.texcoordLoc, 2, GL_FLOAT, 0, 0, 0); glVertexAttribPointer(model.material.shader.texcoordLoc, 2, GL_FLOAT, 0, 0, 0);
glEnableVertexAttribArray(model.shader.texcoordLoc); glEnableVertexAttribArray(model.material.shader.texcoordLoc);
// Enable vertex attributes: normals // Enable vertex attributes: normals
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[2]); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh.vertexCount, mesh.normals, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh.vertexCount, mesh.normals, GL_STATIC_DRAW);
glVertexAttribPointer(model.shader.normalLoc, 3, GL_FLOAT, 0, 0, 0); glVertexAttribPointer(model.material.shader.normalLoc, 3, GL_FLOAT, 0, 0, 0);
glEnableVertexAttribArray(model.shader.normalLoc); glEnableVertexAttribArray(model.material.shader.normalLoc);
model.mesh.vboId[0] = vertexBuffer[0]; // Vertex position VBO model.mesh.vboId[0] = vertexBuffer[0]; // Vertex position VBO
model.mesh.vboId[1] = vertexBuffer[1]; // Texcoords VBO model.mesh.vboId[1] = vertexBuffer[1]; // Texcoords VBO
@ -2154,11 +2163,6 @@ Shader LoadShader(char *vsFileName, char *fsFileName)
{ {
TraceLog(INFO, "[SHDR ID %i] Custom shader loaded successfully", shader.id); TraceLog(INFO, "[SHDR ID %i] Custom shader loaded successfully", shader.id);
// Set shader textures ids (all 0 by default)
shader.texDiffuseId = 0;
shader.texNormalId = 0;
shader.texSpecularId = 0;
// Get handles to GLSL input attibute locations // Get handles to GLSL input attibute locations
//------------------------------------------------------------------- //-------------------------------------------------------------------
shader.vertexLoc = glGetAttribLocation(shader.id, "vertexPosition"); shader.vertexLoc = glGetAttribLocation(shader.id, "vertexPosition");
@ -2314,28 +2318,7 @@ void SetCustomShader(Shader shader)
if (currentShader.id != shader.id) if (currentShader.id != shader.id)
{ {
rlglDraw(); rlglDraw();
currentShader = shader; currentShader = shader;
/*
if (vaoSupported) glBindVertexArray(vaoQuads);
// Enable vertex attributes: position
glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[0]);
glEnableVertexAttribArray(currentShader.vertexLoc);
glVertexAttribPointer(currentShader.vertexLoc, 3, GL_FLOAT, 0, 0, 0);
// Enable vertex attributes: texcoords
glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[1]);
glEnableVertexAttribArray(currentShader.texcoordLoc);
glVertexAttribPointer(currentShader.texcoordLoc, 2, GL_FLOAT, 0, 0, 0);
// Enable vertex attributes: colors
glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[2]);
glEnableVertexAttribArray(currentShader.colorLoc);
glVertexAttribPointer(currentShader.colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0);
if (vaoSupported) glBindVertexArray(0); // Unbind VAO
*/
} }
#endif #endif
} }
@ -2358,7 +2341,7 @@ void SetPostproShader(Shader shader)
texture.width = screenWidth; texture.width = screenWidth;
texture.height = screenHeight; texture.height = screenHeight;
SetShaderMapDiffuse(&postproQuad.shader, texture); postproQuad.material.texDiffuse = texture;
//TraceLog(DEBUG, "Postproquad texture id: %i", postproQuad.texture.id); //TraceLog(DEBUG, "Postproquad texture id: %i", postproQuad.texture.id);
//TraceLog(DEBUG, "Postproquad shader diffuse map id: %i", postproQuad.shader.texDiffuseId); //TraceLog(DEBUG, "Postproquad shader diffuse map id: %i", postproQuad.shader.texDiffuseId);
@ -2386,7 +2369,7 @@ void SetDefaultShader(void)
void SetModelShader(Model *model, Shader shader) void SetModelShader(Model *model, Shader shader)
{ {
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
model->shader = shader; model->material.shader = shader;
if (vaoSupported) glBindVertexArray(model->mesh.vaoId); if (vaoSupported) glBindVertexArray(model->mesh.vaoId);
@ -2407,8 +2390,6 @@ void SetModelShader(Model *model, Shader shader)
if (vaoSupported) glBindVertexArray(0); // Unbind VAO if (vaoSupported) glBindVertexArray(0); // Unbind VAO
// NOTE: If SetModelTexture() is called previously, texture is not assigned to new shader
if (model->texture.id > 0) model->shader.texDiffuseId = model->texture.id;
#elif (GRAPHICS_API_OPENGL_11) #elif (GRAPHICS_API_OPENGL_11)
TraceLog(WARNING, "Shaders not supported on OpenGL 1.1"); TraceLog(WARNING, "Shaders not supported on OpenGL 1.1");
#endif #endif
@ -2480,104 +2461,6 @@ void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix mat)
#endif #endif
} }
// Default diffuse shader map texture assignment
void SetShaderMapDiffuse(Shader *shader, Texture2D texture)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
shader->texDiffuseId = texture.id;
glUseProgram(shader->id);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, shader->texDiffuseId);
glUniform1i(shader->mapDiffuseLoc, 0); // Texture fits in active texture unit 0
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE0);
glUseProgram(0);
#endif
}
// Normal map texture shader assignment
void SetShaderMapNormal(Shader *shader, const char *uniformName, Texture2D texture)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
shader->mapNormalLoc = glGetUniformLocation(shader->id, uniformName);
if (shader->mapNormalLoc == -1) TraceLog(WARNING, "[SHDR ID %i] Shader location for %s could not be found", shader->id, uniformName);
else
{
shader->texNormalId = texture.id;
glUseProgram(shader->id);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, shader->texNormalId);
glUniform1i(shader->mapNormalLoc, 1); // Texture fits in active texture unit 1
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE0);
glUseProgram(0);
}
#endif
}
// Specular map texture shader assignment
void SetShaderMapSpecular(Shader *shader, const char *uniformName, Texture2D texture)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
shader->mapSpecularLoc = glGetUniformLocation(shader->id, uniformName);
if (shader->mapSpecularLoc == -1) TraceLog(WARNING, "[SHDR ID %i] Shader location for %s could not be found", shader->id, uniformName);
else
{
shader->texSpecularId = texture.id;
glUseProgram(shader->id);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, shader->texSpecularId);
glUniform1i(shader->mapSpecularLoc, 2); // Texture fits in active texture unit 2
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE0);
glUseProgram(0);
}
#endif
}
// Generic shader maps assignment
// TODO: Trying to find a generic shader to allow any kind of map
// NOTE: mapLocation should be retrieved by user with GetShaderLocation()
// ISSUE: mapTextureId: Shader should contain a reference to map texture and corresponding textureUnit,
// so it can be automatically checked and used in rlglDrawModel()
void SetShaderMap(Shader *shader, int mapLocation, Texture2D texture, int textureUnit)
{
/*
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
if (mapLocation == -1) TraceLog(WARNING, "[SHDR ID %i] Map location could not be found", shader->id);
else
{
shader->mapTextureId = texture.id;
glUseProgram(shader->id);
glActiveTexture(GL_TEXTURE0 + textureUnit);
glBindTexture(GL_TEXTURE_2D, shader->mapTextureId);
glUniform1i(mapLocation, textureUnit); // Texture fits in active textureUnit
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE0);
glUseProgram(0);
}
#endif
*/
}
// Set blending mode (alpha, additive, multiplied) // Set blending mode (alpha, additive, multiplied)
// NOTE: Only 3 blending modes predefined // NOTE: Only 3 blending modes predefined
void SetBlendMode(int mode) void SetBlendMode(int mode)
@ -2652,15 +2535,11 @@ static void LoadCompressedTexture(unsigned char *data, int width, int height, in
} }
// Load Shader (Vertex and Fragment) // Load Shader (Vertex and Fragment)
// NOTE: This shader program is used only for batch buffers (lines, triangles, quads) // NOTE: This shader program is used for batch buffers (lines, triangles, quads)
static Shader LoadDefaultShader(void) static Shader LoadDefaultShader(void)
{ {
Shader shader; Shader shader;
// NOTE: Shaders are written using GLSL 110 (desktop), that is equivalent to GLSL 100 on ES2
// NOTE: Detected an error on ATI cards if defined #version 110 while OpenGL 3.3+
// Just defined #version 330 despite shader is #version 110
// Vertex shader directly defined, no external file required // Vertex shader directly defined, no external file required
#if defined(GRAPHICS_API_OPENGL_33) #if defined(GRAPHICS_API_OPENGL_33)
char vShaderStr[] = "#version 330 \n" char vShaderStr[] = "#version 330 \n"
@ -2724,10 +2603,6 @@ static Shader LoadDefaultShader(void)
shader.mapDiffuseLoc = glGetUniformLocation(shader.id, "texture0"); shader.mapDiffuseLoc = glGetUniformLocation(shader.id, "texture0");
shader.mapNormalLoc = -1; // It can be set later shader.mapNormalLoc = -1; // It can be set later
shader.mapSpecularLoc = -1; // It can be set later shader.mapSpecularLoc = -1; // It can be set later
shader.texDiffuseId = whiteTexture; // Default white texture
shader.texNormalId = 0;
shader.texSpecularId = 0;
//-------------------------------------------------------------------- //--------------------------------------------------------------------
return shader; return shader;
@ -2739,10 +2614,6 @@ static Shader LoadSimpleShader(void)
{ {
Shader shader; Shader shader;
// NOTE: Shaders are written using GLSL 110 (desktop), that is equivalent to GLSL 100 on ES2
// NOTE: Detected an error on ATI cards if defined #version 110 while OpenGL 3.3+
// Just defined #version 330 despite shader is #version 110
// Vertex shader directly defined, no external file required // Vertex shader directly defined, no external file required
#if defined(GRAPHICS_API_OPENGL_33) #if defined(GRAPHICS_API_OPENGL_33)
char vShaderStr[] = "#version 330 \n" char vShaderStr[] = "#version 330 \n"
@ -2802,10 +2673,6 @@ static Shader LoadSimpleShader(void)
shader.mapDiffuseLoc = glGetUniformLocation(shader.id, "texture0"); shader.mapDiffuseLoc = glGetUniformLocation(shader.id, "texture0");
shader.mapNormalLoc = -1; // It can be set later shader.mapNormalLoc = -1; // It can be set later
shader.mapSpecularLoc = -1; // It can be set later shader.mapSpecularLoc = -1; // It can be set later
shader.texDiffuseId = whiteTexture; // Default white texture
shader.texNormalId = 0;
shader.texSpecularId = 0;
//-------------------------------------------------------------------- //--------------------------------------------------------------------
return shader; return shader;
@ -2878,7 +2745,6 @@ static void InitializeBuffers(void)
quads.indices = (unsigned short *)malloc(sizeof(short)*6*MAX_QUADS_BATCH); // 6 int by quad (indices) quads.indices = (unsigned short *)malloc(sizeof(short)*6*MAX_QUADS_BATCH); // 6 int by quad (indices)
#endif #endif
for (int i = 0; i < (3*4*MAX_QUADS_BATCH); i++) quads.vertices[i] = 0.0f; for (int i = 0; i < (3*4*MAX_QUADS_BATCH); i++) quads.vertices[i] = 0.0f;
for (int i = 0; i < (2*4*MAX_QUADS_BATCH); i++) quads.texcoords[i] = 0.0f; for (int i = 0; i < (2*4*MAX_QUADS_BATCH); i++) quads.texcoords[i] = 0.0f;
for (int i = 0; i < (4*4*MAX_QUADS_BATCH); i++) quads.colors[i] = 0; for (int i = 0; i < (4*4*MAX_QUADS_BATCH); i++) quads.colors[i] = 0;

View file

@ -154,11 +154,6 @@ typedef enum { OPENGL_11 = 1, OPENGL_33, OPENGL_ES_20 } GlVersion;
typedef struct Shader { typedef struct Shader {
unsigned int id; // Shader program id unsigned int id; // Shader program id
// TODO: This should be Texture2D objects
unsigned int texDiffuseId; // Diffuse texture id
unsigned int texNormalId; // Normal texture id
unsigned int texSpecularId; // Specular texture id
// Variable attributes // Variable attributes
int vertexLoc; // Vertex attribute location point (vertex shader) int vertexLoc; // Vertex attribute location point (vertex shader)
int texcoordLoc; // Texcoord attribute location point (vertex shader) int texcoordLoc; // Texcoord attribute location point (vertex shader)
@ -184,12 +179,27 @@ typedef enum { OPENGL_11 = 1, OPENGL_33, OPENGL_ES_20 } GlVersion;
int format; // Data format (TextureFormat) int format; // Data format (TextureFormat)
} Texture2D; } Texture2D;
// Material type
typedef struct Material {
Shader shader;
Texture2D texDiffuse; // Diffuse texture
Texture2D texNormal; // Normal texture
Texture2D texSpecular; // Specular texture
Color colDiffuse;
Color colAmbient;
Color colSpecular;
float glossiness;
float normalDepth;
} Material;
// 3d Model type // 3d Model type
typedef struct Model { typedef struct Model {
Mesh mesh; Mesh mesh;
Matrix transform; Matrix transform;
Texture2D texture; Material material;
Shader shader;
} Model; } Model;
// Color blending modes (pre-defined) // Color blending modes (pre-defined)