Categories
Mastering Development

Random error in Opengl for some reason in my include ssstream i am getting this error when i run: Unhandled exception at 0x005BA1BD

I believe this is some kind of random error thing I am getting only when I run it. I am including my shader files and main in the code below. I am getting a strange error in my include sstream and this it:Unhandled exception at 0x005BA1BD in jippy.cpp.exe: 0xC0000005: Access violation reading location 0xCCCCCCD0. occurred.

// GLEW
#define GLEW_STATIC
#include <GL/glew.h>



struct Shaderprogramsource {


std::string VertexSouce;

std::string FragmentSource;



};


static Shaderprogramsource Parseshader(const std::string& filepath) {

std::ifstream stream(filepath);


enum class Shadertype {

    NONE = -1, VERTEX = 0, FRAGMENT = 1

};


std::string line;
std::stringstream ss[2];

Shadertype type = Shadertype::NONE;


while (getline(stream, line)) {

    if (line.find("#shader") != std::string::npos) {



        if (line.find("vertex") != std::string::npos)
            type = Shadertype::VERTEX;

        else if (line.find("fragment") != std::string::npos)
            type = Shadertype::FRAGMENT;

    }
    else {

        ss[(int)type] << line << "\n";






    }
}


return { ss[0].str(), ss[1].str() };

}








static 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 length;
    glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);

    char* message = (char*)alloca(length * sizeof(char));
    glGetShaderInfoLog(id, length, &length, message);

    std::cout << message ;

    return 0;
}







return id;
}











static unsigned int CreateShader(const std::string& Vertexshader, const std::string& Fragmentshader) 
{


unsigned int program = glCreateProgram();
unsigned int vertex = CompileShader(GL_VERTEX_SHADER, Vertexshader);
unsigned int fragment = CompileShader(GL_FRAGMENT_SHADER, Fragmentshader);

glAttachShader(program, vertex);
glAttachShader(program, fragment);

glLinkProgram(program);
glValidateProgram(program);

return program;



}



int main(void)
{
GLFWwindow* window;

/* Initialize the library */
if (!glfwInit())
    return -1;

/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
    glfwTerminate();
    return -1;
}

/* Make the window's context current */
glfwMakeContextCurrent(window);

if (GLEW_OK == glewInit())
{


}


float vertices[6] = {
    -0.5, -0.5,
    0.0, 0.5,
    0.5, 0.5



};







unsigned int buffer1;

glGenBuffers(1, &buffer1);
glBindBuffer(GL_ARRAY_BUFFER, buffer1);
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), vertices, GL_STATIC_DRAW);


glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);

glEnableVertexAttribArray(0);



Shaderprogramsource source = Parseshader("res/shaders/Basic.Shader");

unsigned int shader = CreateShader(source.VertexSouce, source.FragmentSource);
glUseProgram(shader);




/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
    /* Render here */
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glUseProgram(shader);
    glDrawArrays(GL_TRIANGLES, 0, 3);







    /* Swap front and back buffers */
    glfwSwapBuffers(window);

    /* Poll for and process events */
    glfwPollEvents();
}

glDeleteProgram(shader);

glfwTerminate();
return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *