|
|
@ -78,151 +78,244 @@ Material* MaterialLoader::load(const std::string& filename) |
|
|
|
return nullptr; |
|
|
|
} |
|
|
|
|
|
|
|
// Parse lines
|
|
|
|
std::string line; |
|
|
|
std::size_t lineNumber = 0; |
|
|
|
const std::string whitespace = " \t"; |
|
|
|
|
|
|
|
// Parse lines
|
|
|
|
while (file.good() && std::getline(file, line)) |
|
|
|
{ |
|
|
|
const std::string whitespace = " \t"; |
|
|
|
// Increment current line number
|
|
|
|
++lineNumber; |
|
|
|
|
|
|
|
// Skip empty lines
|
|
|
|
if (line.empty()) |
|
|
|
{ |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
// Find position of first character in variable name
|
|
|
|
std::size_t variableNamePosition = line.find_first_not_of(whitespace, 0); |
|
|
|
if (variableNamePosition == std::string::npos) |
|
|
|
{ |
|
|
|
// Skip whitespace-only lines
|
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
// Find position of equals sign
|
|
|
|
std::size_t equalsSignPosition = line.find_first_of("=", variableNamePosition); |
|
|
|
if (equalsSignPosition == std::string::npos) |
|
|
|
// Find position of first character in the command
|
|
|
|
std::size_t commandPosition = line.find_first_not_of(whitespace, 0); |
|
|
|
if (commandPosition == std::string::npos) |
|
|
|
{ |
|
|
|
// Skip lines with no equals sign
|
|
|
|
// Skip whitespace-only lines
|
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
// Find position of first character in variable type
|
|
|
|
std::size_t variableTypePosition = line.find_first_not_of(whitespace, equalsSignPosition + 1); |
|
|
|
if (variableTypePosition == std::string::npos) |
|
|
|
{ |
|
|
|
// Skip lines with no variable type definition
|
|
|
|
continue; |
|
|
|
} |
|
|
|
// Determine command type
|
|
|
|
std::string command = line.substr(commandPosition, line.find_first_of(" \t=", commandPosition) - commandPosition); |
|
|
|
|
|
|
|
// Count parentheses
|
|
|
|
std::size_t leftParenthesisCount = std::count(line.begin() + variableNamePosition, line.end(), '('); |
|
|
|
std::size_t rightParenthesisCount = std::count(line.begin() + variableNamePosition, line.end(), ')'); |
|
|
|
if (leftParenthesisCount != rightParenthesisCount || leftParenthesisCount == 0) |
|
|
|
// Parse command
|
|
|
|
if (command == "shader" || command == "flags") |
|
|
|
{ |
|
|
|
// Skip lines with invalid number of parentheses
|
|
|
|
continue; |
|
|
|
// Find position of equals sign
|
|
|
|
std::size_t equalsSignPosition = line.find_first_of("=", commandPosition); |
|
|
|
if (equalsSignPosition == std::string::npos) |
|
|
|
{ |
|
|
|
// Skip lines with no equals sign
|
|
|
|
std::cerr << "MaterialLoader::load(): Invalid line " << lineNumber << " in \"" << filename << "\"" << std::endl; |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
// Find position of first character in the value string
|
|
|
|
std::size_t valueStartPosition = line.find_first_not_of(whitespace, equalsSignPosition + 1); |
|
|
|
if (valueStartPosition == std::string::npos) |
|
|
|
{ |
|
|
|
// Skip lines with no value
|
|
|
|
std::cerr << "MaterialLoader::load(): Invalid line " << lineNumber << " in \"" << filename << "\"" << std::endl; |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
// Find position the end of the value string
|
|
|
|
std::size_t valueEndPosition = line.find_first_of(" \t;", valueStartPosition); |
|
|
|
|
|
|
|
// Determine value string
|
|
|
|
std::string valueString; |
|
|
|
if (valueEndPosition == std::string::npos) |
|
|
|
{ |
|
|
|
valueString = line.substr(valueStartPosition); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
valueString = line.substr(valueStartPosition, valueEndPosition - valueStartPosition); |
|
|
|
} |
|
|
|
|
|
|
|
// Execute command
|
|
|
|
if (command == "shader") |
|
|
|
{ |
|
|
|
// Load shader
|
|
|
|
Shader* shader = loadShader(valueString); |
|
|
|
if (!shader) |
|
|
|
{ |
|
|
|
std::cerr << "MaterialLoader::load(): Failed to load shader \"" << valueString << "\" on line " << lineNumber << " in \"" << filename << "\"" << std::endl; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
material->setShader(shader); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
// Parse flags
|
|
|
|
std::uint64_t flags; |
|
|
|
std::stringstream stream; |
|
|
|
stream << valueString; |
|
|
|
stream >> flags; |
|
|
|
|
|
|
|
material->setFlags(flags); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
std::string variableName = line.substr(variableNamePosition, line.find_first_of(" \t=", variableNamePosition) - variableNamePosition); |
|
|
|
std::string variableType = line.substr(variableTypePosition, line.find_first_of(" \t[(", variableTypePosition) - variableTypePosition); |
|
|
|
std::size_t elementCount = leftParenthesisCount; |
|
|
|
|
|
|
|
std::size_t currentPosition = variableTypePosition; |
|
|
|
std::vector<std::vector<std::string>> elements; |
|
|
|
bool invalid = false; |
|
|
|
for (std::size_t i = 0; i < elementCount; ++i) |
|
|
|
else if (command == "var") |
|
|
|
{ |
|
|
|
std::size_t leftParenthesisPosition = line.find_first_of("(", currentPosition); |
|
|
|
std::size_t rightParenthesisPosition = line.find_first_of(")", leftParenthesisPosition + 1); |
|
|
|
// Find position of first character in variable name
|
|
|
|
std::size_t variableNamePosition = line.find_first_not_of(whitespace, commandPosition + command.length()); |
|
|
|
if (variableNamePosition == std::string::npos) |
|
|
|
{ |
|
|
|
// Skip lines with no variable name
|
|
|
|
std::cerr << "MaterialLoader::load(): Invalid variable on line " << lineNumber << " in \"" << filename << "\"" << std::endl; |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
// Find position of equals sign
|
|
|
|
std::size_t equalsSignPosition = line.find_first_of("=", variableNamePosition); |
|
|
|
if (equalsSignPosition == std::string::npos) |
|
|
|
{ |
|
|
|
// Skip lines with no equals sign
|
|
|
|
std::cerr << "MaterialLoader::load(): Invalid variable on line " << lineNumber << " in \"" << filename << "\"" << std::endl; |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
if (leftParenthesisPosition == std::string::npos || rightParenthesisPosition == std::string::npos) |
|
|
|
// Find position of first character in variable type
|
|
|
|
std::size_t variableTypePosition = line.find_first_not_of(whitespace, equalsSignPosition + 1); |
|
|
|
if (variableTypePosition == std::string::npos) |
|
|
|
{ |
|
|
|
invalid = true; |
|
|
|
break; |
|
|
|
// Skip lines with no variable type definition
|
|
|
|
std::cerr << "MaterialLoader::load(): Invalid variable on line " << lineNumber << " in \"" << filename << "\"" << std::endl; |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
currentPosition = leftParenthesisPosition + 1; |
|
|
|
std::size_t argumentCount = std::count(line.begin() + leftParenthesisPosition + 1, line.begin() + rightParenthesisPosition, ',') + 1; |
|
|
|
std::vector<std::string> arguments; |
|
|
|
for (std::size_t j = 0; j < argumentCount; ++j) |
|
|
|
// Count parentheses
|
|
|
|
std::size_t leftParenthesisCount = std::count(line.begin() + variableNamePosition, line.end(), '('); |
|
|
|
std::size_t rightParenthesisCount = std::count(line.begin() + variableNamePosition, line.end(), ')'); |
|
|
|
if (leftParenthesisCount != rightParenthesisCount || leftParenthesisCount == 0) |
|
|
|
{ |
|
|
|
std::size_t argumentStart = line.find_first_not_of(whitespace, currentPosition); |
|
|
|
std::size_t argumentEnd = line.find_first_of(" \t,)", argumentStart + 1); |
|
|
|
// Skip lines with invalid number of parentheses
|
|
|
|
std::cerr << "MaterialLoader::load(): Invalid variable on line " << lineNumber << " in \"" << filename << "\"" << std::endl; |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
std::string variableName = line.substr(variableNamePosition, line.find_first_of(" \t=", variableNamePosition) - variableNamePosition); |
|
|
|
std::string variableType = line.substr(variableTypePosition, line.find_first_of(" \t[(", variableTypePosition) - variableTypePosition); |
|
|
|
std::size_t elementCount = leftParenthesisCount; |
|
|
|
|
|
|
|
std::size_t currentPosition = variableTypePosition; |
|
|
|
std::vector<std::vector<std::string>> elements; |
|
|
|
bool invalid = false; |
|
|
|
for (std::size_t i = 0; i < elementCount; ++i) |
|
|
|
{ |
|
|
|
std::size_t leftParenthesisPosition = line.find_first_of("(", currentPosition); |
|
|
|
std::size_t rightParenthesisPosition = line.find_first_of(")", leftParenthesisPosition + 1); |
|
|
|
|
|
|
|
if (argumentStart == std::string::npos || argumentEnd == std::string::npos) |
|
|
|
if (leftParenthesisPosition == std::string::npos || rightParenthesisPosition == std::string::npos) |
|
|
|
{ |
|
|
|
// Unable to parse argument
|
|
|
|
invalid = true; |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
std::string argument = line.substr(argumentStart, argumentEnd - argumentStart); |
|
|
|
arguments.push_back(argument); |
|
|
|
currentPosition = leftParenthesisPosition + 1; |
|
|
|
std::size_t argumentCount = std::count(line.begin() + leftParenthesisPosition + 1, line.begin() + rightParenthesisPosition, ',') + 1; |
|
|
|
std::vector<std::string> arguments; |
|
|
|
for (std::size_t j = 0; j < argumentCount; ++j) |
|
|
|
{ |
|
|
|
std::size_t argumentStart = line.find_first_not_of(whitespace, currentPosition); |
|
|
|
std::size_t argumentEnd = line.find_first_of(" \t,)", argumentStart + 1); |
|
|
|
|
|
|
|
if (argumentStart == std::string::npos || argumentEnd == std::string::npos) |
|
|
|
{ |
|
|
|
// Unable to parse argument
|
|
|
|
invalid = true; |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
std::string argument = line.substr(argumentStart, argumentEnd - argumentStart); |
|
|
|
arguments.push_back(argument); |
|
|
|
|
|
|
|
currentPosition = argumentEnd + 1; |
|
|
|
} |
|
|
|
|
|
|
|
currentPosition = argumentEnd + 1; |
|
|
|
if (invalid) |
|
|
|
{ |
|
|
|
// Unable to parse element
|
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
elements.push_back(arguments); |
|
|
|
|
|
|
|
currentPosition = rightParenthesisPosition + 1; |
|
|
|
} |
|
|
|
|
|
|
|
if (invalid) |
|
|
|
{ |
|
|
|
// Unable to parse element
|
|
|
|
break; |
|
|
|
// Unable to parse line
|
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
elements.push_back(arguments); |
|
|
|
|
|
|
|
currentPosition = rightParenthesisPosition + 1; |
|
|
|
} |
|
|
|
|
|
|
|
if (invalid) |
|
|
|
{ |
|
|
|
// Unable to parse line
|
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
if (variableType == "int") |
|
|
|
{ |
|
|
|
ShaderInt* variable = material->addVariable<int>(variableName, elements.size()); |
|
|
|
loadShaderInt(variable, elements); |
|
|
|
} |
|
|
|
else if (variableType == "float") |
|
|
|
{ |
|
|
|
ShaderFloat* variable = material->addVariable<float>(variableName, elements.size()); |
|
|
|
loadShaderFloat(variable, elements); |
|
|
|
} |
|
|
|
else if (variableType == "vec2") |
|
|
|
{ |
|
|
|
ShaderVector2* variable = material->addVariable<Vector2>(variableName, elements.size()); |
|
|
|
loadShaderVector2(variable, elements); |
|
|
|
} |
|
|
|
else if (variableType == "vec3") |
|
|
|
{ |
|
|
|
ShaderVector3* variable = material->addVariable<Vector3>(variableName, elements.size()); |
|
|
|
loadShaderVector3(variable, elements); |
|
|
|
} |
|
|
|
else if (variableType == "vec4") |
|
|
|
{ |
|
|
|
ShaderVector4* variable = material->addVariable<Vector4>(variableName, elements.size()); |
|
|
|
loadShaderVector4(variable, elements); |
|
|
|
} |
|
|
|
else if (variableType == "mat3") |
|
|
|
{ |
|
|
|
ShaderMatrix3* variable = material->addVariable<Matrix3>(variableName, elements.size()); |
|
|
|
loadShaderMatrix3(variable, elements); |
|
|
|
} |
|
|
|
else if (variableType == "mat4") |
|
|
|
{ |
|
|
|
ShaderMatrix4* variable = material->addVariable<Matrix4>(variableName, elements.size()); |
|
|
|
loadShaderMatrix4(variable, elements); |
|
|
|
} |
|
|
|
else if (variableType == "texture") |
|
|
|
{ |
|
|
|
ShaderTexture2D* variable = material->addVariable<const Texture2D*>(variableName, elements.size()); |
|
|
|
loadShaderTexture2D(variable, elements); |
|
|
|
if (variableType == "int") |
|
|
|
{ |
|
|
|
ShaderInt* variable = material->addVariable<int>(variableName, elements.size()); |
|
|
|
loadShaderInt(variable, elements); |
|
|
|
} |
|
|
|
else if (variableType == "float") |
|
|
|
{ |
|
|
|
ShaderFloat* variable = material->addVariable<float>(variableName, elements.size()); |
|
|
|
loadShaderFloat(variable, elements); |
|
|
|
} |
|
|
|
else if (variableType == "vec2") |
|
|
|
{ |
|
|
|
ShaderVector2* variable = material->addVariable<Vector2>(variableName, elements.size()); |
|
|
|
loadShaderVector2(variable, elements); |
|
|
|
} |
|
|
|
else if (variableType == "vec3") |
|
|
|
{ |
|
|
|
ShaderVector3* variable = material->addVariable<Vector3>(variableName, elements.size()); |
|
|
|
loadShaderVector3(variable, elements); |
|
|
|
} |
|
|
|
else if (variableType == "vec4") |
|
|
|
{ |
|
|
|
ShaderVector4* variable = material->addVariable<Vector4>(variableName, elements.size()); |
|
|
|
loadShaderVector4(variable, elements); |
|
|
|
} |
|
|
|
else if (variableType == "mat3") |
|
|
|
{ |
|
|
|
ShaderMatrix3* variable = material->addVariable<Matrix3>(variableName, elements.size()); |
|
|
|
loadShaderMatrix3(variable, elements); |
|
|
|
} |
|
|
|
else if (variableType == "mat4") |
|
|
|
{ |
|
|
|
ShaderMatrix4* variable = material->addVariable<Matrix4>(variableName, elements.size()); |
|
|
|
loadShaderMatrix4(variable, elements); |
|
|
|
} |
|
|
|
else if (variableType == "texture") |
|
|
|
{ |
|
|
|
ShaderTexture2D* variable = material->addVariable<const Texture2D*>(variableName, elements.size()); |
|
|
|
loadShaderTexture2D(variable, elements); |
|
|
|
} |
|
|
|
else if (variableType == "textureCube") |
|
|
|
{ |
|
|
|
ShaderTextureCube* variable = material->addVariable<const TextureCube*>(variableName, elements.size()); |
|
|
|
loadShaderTextureCube(variable, elements); |
|
|
|
} |
|
|
|
} |
|
|
|
else if (variableType == "textureCube") |
|
|
|
else |
|
|
|
{ |
|
|
|
ShaderTextureCube* variable = material->addVariable<const TextureCube*>(variableName, elements.size()); |
|
|
|
loadShaderTextureCube(variable, elements); |
|
|
|
if (command[0] == '#') |
|
|
|
{ |
|
|
|
// Skip comments
|
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
// Invalid command
|
|
|
|
std::cerr << "MaterialLoader::load(): Invalid command \"" << command << "\" on line " << lineNumber << " in \"" << filename << "\"" << std::endl; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -235,6 +328,30 @@ Material* MaterialLoader::load(const std::string& filename) |
|
|
|
return material; |
|
|
|
} |
|
|
|
|
|
|
|
Shader* MaterialLoader::loadShader(const std::string& filename) |
|
|
|
{ |
|
|
|
auto it = shaderCache.find(filename); |
|
|
|
if (it != shaderCache.end()) |
|
|
|
{ |
|
|
|
return it->second; |
|
|
|
} |
|
|
|
|
|
|
|
std::string fullFilename = std::string("data/shaders/") + filename; |
|
|
|
|
|
|
|
// Load shader
|
|
|
|
Shader* shader = new Shader(); |
|
|
|
if (!shader->loadSource(fullFilename)) |
|
|
|
{ |
|
|
|
delete shader; |
|
|
|
return nullptr; |
|
|
|
} |
|
|
|
|
|
|
|
// Add shader to cache
|
|
|
|
shaderCache[filename] = shader; |
|
|
|
|
|
|
|
return shader; |
|
|
|
} |
|
|
|
|
|
|
|
Texture2D* MaterialLoader::loadTexture2D(const std::string& filename) |
|
|
|
{ |
|
|
|
// Check if texture exists in cache
|
|
|
|