I am using Visual 2019 with Visual C++. I was following the tutorial by The Cherno. I am stuck on the end of the Eight Episode. What is causing this?
main.cpp:
#include <gl/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <fstream>
#include <sstream>
struct ShaderProgramSources
{
std::string VertexSource;
std::string FragmentSource;
};
static ShaderProgramSources ParseShader(const std::string& filepath)
{
std::ifstream stream(filepath);
enum class shaderType {
NONE = -1, VERTEX = 0, FRAGMENT = 1
};
std::string line;
std::stringstream ss[1];;
shaderType type = shaderType::NONE;
while (getline(stream, line))
{
if (line.find("#shader") != std::string::npos)
{
if (line.find("vertex") != std::string::npos)
shaderType type = shaderType::VERTEX;
else if (line.find("fragment") != std::string::npos)
shaderType type = shaderType::FRAGMENT;
}
else
{
ss[(int)type] << line << "\n";
}
}
return { ss[0].str(), ss[1].str() };
}
static unsigned int CompileShader(unsigned int type, const std::string& source)
{
unsigned int id = glCreateShader(type);
const char* src = source.c_str();
glShaderSource(id, 1, &src, nullptr);
glCompileShader(id);
int result;
glGetShaderiv(id, GL_COMPILE_STATUS, &result);
if (result = GL_FALSE)
{
int legnth;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &legnth);
char* message = (char*)alloca(legnth * sizeof(char));
glGetShaderInfoLog(id, legnth, &legnth, message);
std::cout << "Failed to Compile" << (type == GL_VERTEX_SHADER ? "Vertex: Shader:\n" : "Fragment Shader:\n");
glDeleteShader(id);
return 0;
}
return id;
}
static unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader)
{
unsigned int program = glCreateProgram();
unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glValidateProgram(program);
return program;
}
int main(void)
{
GLFWwindow* window;
if (!glewInit())
return -1;
/* Initialize the library */
if (!glfwInit())
return -1;
window = glfwCreateWindow(640, 480, "Cooked Pixel", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
if (glewInit() != GLEW_OK)
{
std::cout << "Error: GLEW is not initialized \n";
}
std::cout << "GL Version: " << glGetString(GL_VERSION) << "\n";
float positions[6] = {
-0.5, -0.5,
0.0, -0.5,
0.5, 0.5
};
unsigned int buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), positions, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
std::string vertexShader = R"glsl(
#version 330 core
layout(location = 0) in vec4 position;
void main()
{
gl_Position = position;
}
)glsl";
std::string fragmentShader = R"glsl(
#version 330 core
layout(location = 0) out vec4 color;
void main()
{
color = vec4(1.0, 0.0, 0.0, 1.0);
}
)glsl";
ShaderProgramSources source = ParseShader("res/shaders/basic.shader");
std::cout << source.FragmentSource << "\n";
std::cout << source.VertexSource << "\n";
unsigned int shader = CreateShader(vertexShader, fragmentShader);
glUseProgram(shader);
glfwMaximizeWindow(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
I get the error on x.string line 433 as:
Unhandled exception at 0x00CEA25D in CookedPixel.exe: 0xC0000005: Access violation reading location 0xCCCCCCD0. occurred