Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "error.h"
- #include "glslprogram.h"
- #include "BenGine.h"
- #include <vector>
- #include <fstream>
- #include <iostream>
- #include <string>
- namespace BenGine {
- glslprogram::glslprogram() : _numAttributes(0), _programID(0), _vertexShaderID(0), _fragmentShaderID(0)
- {
- }
- glslprogram::~glslprogram()
- {
- }
- void glslprogram::compileShaders(const std::string& vertexShaderFilePath, const std::string& fragmentShaderFilePath) {
- _programID = glCreateProgram();
- _vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
- if (_vertexShaderID == 0) {
- fetalError("vertex shader failed to created");
- }
- _fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
- if (_fragmentShaderID == 0) {
- fetalError("fragment shader failed to created");
- }
- compileShader(vertexShaderFilePath, _vertexShaderID);
- compileShader(fragmentShaderFilePath, _fragmentShaderID);
- }
- void glslprogram::linkshaders(){
- glAttachShader(_programID, _vertexShaderID);
- glAttachShader(_programID, _fragmentShaderID);
- glLinkProgram(_programID);
- GLuint isLinked = 0;
- glGetProgramiv(_programID, GL_LINK_STATUS, (int *)&isLinked);
- if (isLinked == GL_FALSE)
- {
- GLint maxLength = 0;
- glGetProgramiv(_programID, GL_INFO_LOG_LENGTH, &maxLength);
- std::vector<char> errorLog(maxLength);
- glGetProgramInfoLog(_programID, maxLength, &maxLength, &errorLog[0]);
- glDeleteProgram(_programID);
- glDeleteShader(_vertexShaderID);
- glDeleteShader(_fragmentShaderID);
- std::printf("%sn", &(errorLog[0]));
- fetalError("shader failed to link");
- }
- glDetachShader(_programID,_vertexShaderID);
- glDetachShader(_programID,_fragmentShaderID);
- glDeleteShader(_vertexShaderID);
- glDeleteShader(_fragmentShaderID);
- }
- void glslprogram::addAttribute(const std::string& attributeName) {
- glBindAttribLocation(_programID, _numAttributes++, attributeName.c_str());
- }
- GLint glslprogram::getUniformLocation(const std::string& uniformName){
- GLint location = glGetUniformLocation(_programID, uniformName.c_str());
- if (location == GL_INVALID_INDEX) {
- fetalError("uniform" + uniformName +"not found in shader");
- }
- return location;
- }
- void glslprogram::use(){
- glUseProgram(_programID);
- for (int i = 0; i < _numAttributes; i++){
- glEnableVertexAttribArray(i);
- }
- }
- void glslprogram::unuse(){
- glUseProgram(0);
- for (int i = 0; i < _numAttributes; i++){
- glDisableVertexAttribArray(i);
- }
- }
- void glslprogram::compileShader(const std::string& filePath, GLuint id) {
- //Open the file
- std::ifstream vertexFile(filePath);
- if (vertexFile.fail()) {
- perror(filePath.c_str());
- fetalError("Failed to open " + filePath);
- }
- //File contents stores all the text in the file
- std::string fileContents = "";
- //line is used to grab each line of the file
- std::string line;
- //Get all the lines in the file and add it to the contents
- while (std::getline(vertexFile, line)) {
- fileContents += line + "n";
- }
- vertexFile.close();
- //get a pointer to our file contents c string;
- const char* contentsPtr = fileContents.c_str();
- //tell opengl that we want to use fileContents as the contents of the shader file
- glShaderSource(id, 1, &contentsPtr, nullptr);
- //compile the shader
- glCompileShader(id);
- //check for errors
- GLint success = 0;
- glGetShaderiv(id, GL_COMPILE_STATUS, &success);
- if (success == GL_FALSE)
- {
- GLint maxLength = 0;
- glGetShaderiv(id, GL_INFO_LOG_LENGTH, &maxLength);
- //The maxLength includes the NULL character
- std::vector<char> errorLog(maxLength);
- glGetShaderInfoLog(id, maxLength, &maxLength, &errorLog[0]);
- //Provide the infolog in whatever manor you deem best.
- //Exit with failure.
- glDeleteShader(id); //Don't leak the shader.
- //Print error log and quit
- std::printf("%sn", &(errorLog[0]));
- fetalError("Shader " + filePath + " failed to compile");
- }
- }
- }
- #pragma once
- #include "BenGine.h"
- #include <string>
- #include <fstream>
- #include <iostream>
- #include <string>
- namespace BenGine {
- extern void fetalError(std::string errorString);
- }
- if (_vertexShaderID == 0) {>
- fetalError("vertex shader failed to created");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement