I'm trying to read a whole file as characters and then print those characters into the screen, but at the end , I'm getting some extra weird characters. Take a look at this output:
Code:
#version 330 core
layout(location = 0) in vec3 aPos;
void main()
{
gl_Position = vec4(aPos, 1.0);
}═══════²²²²
The input file is:
Code:
#version 330 core
layout(location = 0) in vec3 aPos;
void main()
{
gl_Position = vec4(aPos, 1.0);
}
And my source code:
Code:
#pragma once
#include <iostream>
class Shader
{
private:
unsigned int m_id;
public:
Shader(std::string vertex_filename, std::string fragment_filename);
~Shader();
void Bind();
};
Code:
#include "shader.h"
#include "glcall.h"
#include "engine_error.h"
#include <fstream>
#include <string>
struct ShaderSource
{
std::string vertex_src;
std::string fragment_src;
};
static void ReadSources(std::string filename, bool is_vertex, struct ShaderSource *src)
{
//Create a file object.
std::ifstream file;
//Open the file.
file.open(filename, std::ios::in);
//If the file opened successfully read it.
if (file.is_open())
{
//Size of the file.
file.seekg(0, std::ios::end);
std::streampos size = file.tellg();
file.seekg(0, std::ios::beg);
//Allocate memory to store the data.
char *data = (char *)malloc(sizeof(char) * size);
//Read the data from the file.
file.read(data, size);
//Close the file.
file.close();
//This was the vertex file.
if (is_vertex)
src->vertex_src = (const char *)data;
//This was the fragment file.
else
src->fragment_src = (const char *)data;
//Release the memory for the data since I coppied them into the ShaderSource structure.
free(data);
}
//Problem opening the file.
else
throw EngineError("There was a problem opening the file: " + filename);
}
Shader::Shader(std::string vertex_filename, std::string fragment_filename)
{
//Testing if ReadSources works//
ShaderSource source;
ReadSources(vertex_filename, true, &source);
std::cout << source.vertex_src;
//Testing if ReadSources works//
}
Shader::~Shader()
{
}
void Shader::Bind()
{
GLCall(glUseProgram(m_id));
}