bool Shader::Init(char* vsFile, char* fragFile) { GLint vSrc = SetShaderSource(vsFile, GL_VERTEX_SHADER), fSrc = SetShaderSource(fragFile, GL_FRAGMENT_SHADER); if (vSrc < 0 || fSrc < 0) { // Log that shit happened return false; } program = glCreateProgram(); glCompileShader(vSrc); PrintLog("Vertex compile log: ", vSrc); glAttachShader(program, vSrc); glCompileShader(fSrc); PrintLog("Fragment compile log: ", fSrc); glAttachShader(program, fSrc); glLinkProgram(program); BindArrayObjects(); BindUniforms(); BindBufferObjects(); return true; }
//protected: virtual void Render() const override { //Debug::Log("Rendere %s", m_gameObject->name().c_str()); auto meshFilter = m_gameObject->GetComponent<MeshFilter>(); if (meshFilter == nullptr) { Debug::LogWarning("This GameObject has no MeshFilter"); return; } auto model = transform()->localToWorldMatrix(); auto camera = Scene::mainCamera(); auto view = camera->worldToCameraMatrix(); auto proj = camera->projectionMatrix(); auto mv = view * model; auto mvp = proj * mv; ShaderUniforms uniforms; //std::map<std::string, Matrix4x4> matrices; uniforms.mat4s["_Object2World"] = model; uniforms.mat4s["MATRIX_V"] = view; uniforms.mat4s["MATRIX_P"] = proj; uniforms.mat4s["MATRIX_VP"] = proj * view; uniforms.mat4s["MATRIX_MVP"] = mvp; uniforms.mat4s["MATRIX_MV"] = mv; uniforms.mat4s["MATRIX_IT_M"] = glm::inverse(glm::transpose(model)); uniforms.mat4s["MATRIX_IT_MV"] = glm::inverse(glm::transpose(mv)); //auto camera = Scene::getMainCamera(); uniforms.vec3s["_WorldSpaceCameraPos"] = camera->transform()->position(); for (auto& m : m_materials) { auto shader = m->shader(); shader->Use(); shader->PreRender(); shader->BindUniforms(uniforms); m->Update(); shader->CheckStatus(); meshFilter->mesh()->Render(); shader->PostRender(); } }
bool Init() { srand(0); LOG::Initialize(); if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { LOG::Message("Unable to initialize SDL."); LOG::Message(SDL_GetError()); return false; } SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); SDL_WM_SetCaption("FractalDemo", "FractalDemo"); Reshape(screen_width, screen_height); glewInit(); if (!GLEW_VERSION_3_0) { LOG::Message("Requires OpenGL 3.0 or later."); return false; } LOG::Message("GL_VENDOR", (char*)glGetString(GL_VENDOR)); LOG::Message("GL_RENDERER", (char*)glGetString(GL_RENDERER)); LOG::Message("GL_VERSION", (char*)glGetString(GL_VERSION)); LOG::Message("GL_SHADING_LANGUAGE_VERSION", (char*)glGetString(GL_SHADING_LANGUAGE_VERSION)); const char* shader_error = ""; if (!gluInitializeCompiler()) { LOG::Message("Unable to initialize GLSL compiler."); return false; } if (!BuildShader(&ShaderProgram, vert_filepath, frag_filepath, &shader_error)) { LOG::Message(shader_error); return false; } if (!BindUniforms(ShaderProgram, uniform_locations, num_of_uniforms)) { LOG::Message("Could not bind uniforms."); return false; } if (!BindAttributes(ShaderProgram, attribute_locations, num_of_attributes)) { LOG::Message("Could not bind attributes."); return false; } glGenBuffers(1, &array_buffer); glBindBuffer(GL_ARRAY_BUFFER, array_buffer); glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(float), plane_data, GL_STATIC_DRAW); glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0); glEnableVertexAttribArray(0); reset(); return true; }