/* Copyright (c) 2017,2018 Regents of the University of Minnesota. All Rights Reserved. See corresponding header file for details. */ #include "default_shader.h" #include "platform.h" namespace mingfx { DefaultShader::DefaultShader(bool addDefaultLight) { for (int i = 0; i < MAX_LIGHTS; i++) { lightPositions_[3 * i + 0] = 0.0f; lightPositions_[3 * i + 1] = 0.0f; lightPositions_[3 * i + 2] = 0.0f; lightIas_[4 * i + 0] = 0.0f; lightIas_[4 * i + 1] = 0.0f; lightIas_[4 * i + 2] = 0.0f; lightIas_[4 * i + 3] = 0.0f; lightIds_[4 * i + 0] = 0.0f; lightIds_[4 * i + 1] = 0.0f; lightIds_[4 * i + 2] = 0.0f; lightIds_[4 * i + 3] = 0.0f; lightIss_[4 * i + 0] = 0.0f; lightIss_[4 * i + 1] = 0.0f; lightIss_[4 * i + 2] = 0.0f; lightIss_[4 * i + 3] = 0.0f; } if (addDefaultLight) { AddLight(LightProperties()); } } DefaultShader::~DefaultShader() { } void DefaultShader::AddLight(LightProperties light) { lights_.push_back(light); update_light_arrays(); } void DefaultShader::SetLight(int i, LightProperties light) { lights_[i] = light; update_light_arrays(); } void DefaultShader::update_light_arrays() { DefaultShader::LightProperties defaultlight; for (int i=0; iposition[0]; lightPositions_[3*i + 1] = light->position[1]; lightPositions_[3*i + 2] = light->position[2]; lightIas_[4*i + 0] = light->ambient_intensity[0]; lightIas_[4*i + 1] = light->ambient_intensity[1]; lightIas_[4*i + 2] = light->ambient_intensity[2]; lightIas_[4*i + 3] = light->ambient_intensity[3]; lightIds_[4*i + 0] = light->diffuse_intensity[0]; lightIds_[4*i + 1] = light->diffuse_intensity[1]; lightIds_[4*i + 2] = light->diffuse_intensity[2]; lightIds_[4*i + 3] = light->diffuse_intensity[3]; lightIss_[4*i + 0] = light->specular_intensity[0]; lightIss_[4*i + 1] = light->specular_intensity[1]; lightIss_[4*i + 2] = light->specular_intensity[2]; lightIss_[4*i + 3] = light->specular_intensity[3]; } } int DefaultShader::num_lights() { return (int)lights_.size(); } DefaultShader::LightProperties DefaultShader::light(int i) { return lights_[i]; } void DefaultShader::Init() { phongShader_.AddVertexShaderFromFile(Platform::FindMinGfxShaderFile("default.vert")); phongShader_.AddFragmentShaderFromFile(Platform::FindMinGfxShaderFile("default.frag")); phongShader_.LinkProgram(); } void DefaultShader::Draw(const Matrix4 &model, const Matrix4 &view, const Matrix4 &projection, Mesh *mesh, const MaterialProperties &material) { UseProgram(model, view, projection, material); // Draw the mesh using the shader program mesh->Draw(); StopProgram(); } void DefaultShader::UseProgram(const Matrix4 &model, const Matrix4 &view, const Matrix4 &projection, const MaterialProperties &material) { if (!phongShader_.initialized()) { Init(); } Matrix4 normalMatrix = (view*model).Inverse().Transpose(); for (int i=0; i