how read a shader from another file c++
C++
#include "GLShader.hpp"
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
std::string readFile(const char *filePath) {
std::string content;
std::ifstream fileStream(filePath, std::ios::in);
if(!fileStream.is_open()) {
std::cerr << "Could not read file " << filePath << ". File does not exist." << std::endl;
return "";
}
std::string line = "";
while(!fileStream.eof()) {
std::getline(fileStream, line);
content.append(line + "\n");
}
fileStream.close();
return content;
}
GLuint LoadShader(const char *vertex_path, const char *fragment_path) {
GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);
GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);
// Read shaders
std::string vertShaderStr = readFile(vertex_path);
std::string fragShaderStr = readFile(fragment_path);
const char *vertShaderSrc = vertShaderStr.c_str();
const char *fragShaderSrc = fragShaderStr.c_str();
GLint result = GL_FALSE;
int logLength;
// Compile vertex shader
std::cout << "Compiling vertex shader." << std::endl;
glShaderSource(vertShader, 1, &vertShaderSrc, NULL);
glCompileShader(vertShader);
// Check vertex shader
glGetShaderiv(vertShader, GL_COMPILE_STATUS, &result);
glGetShaderiv(vertShader, GL_INFO_LOG_LENGTH, &logLength);
std::vector vertShaderError((logLength > 1) ? logLength : 1);
glGetShaderInfoLog(vertShader, logLength, NULL, &vertShaderError[0]);
std::cout << &vertShaderError[0] << std::endl;
// Compile fragment shader
std::cout << "Compiling fragment shader." << std::endl;
glShaderSource(fragShader, 1, &fragShaderSrc, NULL);
glCompileShader(fragShader);
// Check fragment shader
glGetShaderiv(fragShader, GL_COMPILE_STATUS, &result);
glGetShaderiv(fragShader, GL_INFO_LOG_LENGTH, &logLength);
std::vector fragShaderError((logLength > 1) ? logLength : 1);
glGetShaderInfoLog(fragShader, logLength, NULL, &fragShaderError[0]);
std::cout << &fragShaderError[0] << std::endl;
std::cout << "Linking program" << std::endl;
GLuint program = glCreateProgram();
glAttachShader(program, vertShader);
glAttachShader(program, fragShader);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &result);
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength);
std::vector<char> programError( (logLength > 1) ? logLength : 1 );
glGetProgramInfoLog(program, logLength, NULL, &programError[0]);
std::cout << &programError[0] << std::endl;
glDeleteShader(vertShader);
glDeleteShader(fragShader);
return program;
}
Also in C++:
- Title
- min heap priority queue c++
- Category
- C++
- Title
- c++ sql
- Category
- C++
- Title
- how to find the index of an element in a vector c++
- Category
- C++
- Title
- c++ allocate dynamic with initial values
- Category
- C++
- Title
- min heap declaration in c++ stl
- Category
- C++
- Title
- system("pause") note working c++
- Category
- C++
- Title
- 2d vector
- Category
- C++
- Title
- RLE Encoding/Compression c++
- Category
- C++
- Title
- c++ formatting
- Category
- C++
- Title
- traverse a map
- Category
- C++
- Title
- if vector contains value c++
- Category
- C++
- Title
- check for bst
- Category
- C++
- Title
- iterate const vector
- Category
- C++
- Title
- insert at position in vector c++
- Category
- C++
- Title
- C++ cin cout
- Category
- C++
- Title
- repeating character in c++
- Category
- C++
- Title
- gta san andreas
- Category
- C++
- Title
- object reference not set to an instance of an object c#
- Category
- C++
- Title
- single line if c++
- Category
- C++
- Title
- string substr c++
- Category
- C++
- Title
- generate random double c++
- Category
- C++
- Title
- convert int to string c++
- Category
- C++
- Title
- two sum problem in c++
- Category
- C++
- Title
- how to sort an array c++
- Category
- C++
- Title
- decimal to hex cpp
- Category
- C++
- Title
- c++ switch case statement
- Category
- C++
- Title
- c++ cli convert string to string^
- Category
- C++
- Title
- substr in c++
- Category
- C++
- Title
- user input c++
- Category
- C++
- Title
- file format not recognized treating as linker script c++
- Category
- C++
- Title
- pyqt connect
- Category
- C++
- Title
- c++ get length of array
- Category
- C++
- Title
- what is difference between ciel and floor
- Category
- C++
- Title
- is TLE means my code is correct but taking more time to computr
- Category
- C++
- Title
- how to add a number after each number in an array with a for loop in C++
- Category
- C++
- Title
- check if key exists in map c++
- Category
- C++
- Title
- advanced c++ topics
- Category
- C++
- Title
- c++ try
- Category
- C++
- Title
- string input
- Category
- C++
- Title
- c++ vector iterator
- Category
- C++
- Title
- how to delete something in an array c++
- Category
- C++
- Title
- initialize map c++
- Category
- C++
- Title
- convert decimal to binary in c++
- Category
- C++
- Title
- can you use a return to print a string when referencing an integer c++
- Category
- C++
- Title
- how to use max_element in c++ with vector
- Category
- C++
- Title
- multiset c++
- Category
- C++
- Title
- what does map.count() return in c++
- Category
- C++
- Title
- residuo en lenguaje c
- Category
- C++
- Title
- how to modulo 10^9+7
- Category
- C++
- Title
- remove from unordered_set c++
- Category
- C++
- Title
- c++ initialization list
- Category
- C++
- Title
- new class * [] c++
- Category
- C++
- Title
- c++ do while loop
- Category
- C++
- Title
- c++ code for polynomial addition
- Category
- C++
- Title
- mkdir boost filesystem
- Category
- C++
- Title
- Check if a Number is Odd or Even using Bitwise Operators
- Category
- C++
- Title
- string to vector c++
- Category
- C++
- Title
- range of long long in c++
- Category
- C++
- Title
- initialize int c++
- Category
- C++
- Title
- list conda environments
- Category
- C++
- Title
- how to avoid tle in c++
- Category
- C++
- Title
- c++ push multiple elements to vector
- Category
- C++
- Title
- double to int c++
- Category
- C++
- Title
- : error: ‘cont’ cannot be used as a function return (cont(cont-1))/2;
- Category
- C++
- Title
- c++ do you not inherit constructor
- Category
- C++
- Title
- fmod c++
- Category
- C++
- Title
- change int to string cpp
- Category
- C++
- Title
- split string at index c++
- Category
- C++
- Title
- c++ clear console
- Category
- C++
- Title
- std::iomanip c++
- Category
- C++
- Title
- c++ string to stream
- Category
- C++
- Title
- how to initialize a vector in c++
- Category
- C++
- Title
- c++ remove item from list
- Category
- C++
- Title
- c++ append to list
- Category
- C++
- Title
- how to check sqrt of number is integer c++
- Category
- C++
- Title
- Create a program that finds the minimum value in these numbers
- Category
- C++
- Title
- new c++
- Category
- C++
- Title
- cin.ignore
- Category
- C++
- Title
- set of vectors c++
- Category
- C++
- Title
- how to hide ui elements unity
- Category
- C++
- Title
- cin.fail()
- Category
- C++
- Title
- linear search in c++
- Category
- C++
- Title
- c++ stream string into fiel
- Category
- C++
- Title
- maximum subarray sum equal with K in c++
- Category
- C++
- Title
- check an stack is empty c++
- Category
- C++
- Title
- c++ declare variable
- Category
- C++
- Title
- reference function in c++
- Category
- C++
- Title
- Convert binary tree to a doubly linked list
- Category
- C++
- Title
- sorting of array in c++
- Category
- C++
- Title
- using namespace std in c++
- Category
- C++
- Title
- c++ iterate over vector
- Category
- C++
- Title
- tokenize string c++
- Category
- C++
- Title
- substr c++
- Category
- C++
- Title
- C++ sfinae
- Category
- C++
- Title
- initialize 3d vector c++
- Category
- C++
- Title
- c++ isalphanum
- Category
- C++
- Title
- c++ replace substrings
- Category
- C++
- Title
- Html tabulation
- Category
- C++
- Title
- sfml basic program
- Category
- C++
- Title
- namespaces c++
- Category
- C++