Problem bei Visual C++ mit openGL: ACCESS VIOLATION (noch offen)



  • Hallo!

    In den letzten Tagen habe ich angefangen ein bisschen Programmieren (insbesondere mit openGL) zu lernen.

    Bei der Installierung von openGL mit GLEW und GLFW habe ich das Tutorial mit dem folgenen Link verwendet:
    http://www.learnopengl.com/#!Getting-started/Creating-a-window
    GLEW und GLFW sind mMn richtig initialisiert.

    Da hat auch alles gut geklappt bis jetzt.
    In dem Kapitel, in dem man in Shader eingeführt wurde habe ich lange gesessen, da ich immer eine ACCESS VIOLATION-Meldung bekam, egal wieviel ich an meinem Code mache.

    Hier ist mein Code:

    #include "stdafx.h"
    #include <iostream>
    // GLEW
    #include <glew.h>
    // GLFW
    #include <glfw3.h>
    #include <fstream>
    using namespace std;
    
    int main()
    {
    
    	cout << "Startet" << endl;
    	GLuint vertexShader;
    	cout << "OpenGL-Objekt erstellt: VertexShader" << endl;
    	vertexShader = glCreateShader(GL_VERTEX_SHADER);
    	cout << "Objekt als Shader deklariert, noch nicht ausgelesen: VertexShader" << endl;
    
    	const char* vertex_shader = "#version 330 core"
                                  "layout (location = 0) in vec3 position;"
                                  "void main()"
    							  "{"
                                  "  gl_Position = vec4(position.x, position.y, position.z, 1.0);"
                                  "}";
    
    	cout << "VertexShader eingelesen" << endl;
    
    	glShaderSource(vertexShader, 1, reinterpret_cast<const GLchar *const *>(vertex_shader), NULL); 
    	cout << "ShaderCode ausgelesen: VertexShader" << endl;
    	glCompileShader(vertexShader);
    	cout << "Shader kompiliert: VertexSHader" << endl;
    
    	GLint success;
    	GLchar infoLog[512];
    	glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
    
    	if(!success)
    	{
    		glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
    		std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
    	}
    
    	cout << "VertexShader installiert" << endl;
    	cout << "Main läuft" << endl;
    	system("pause");
    	return(0);
    
    }
    

    Stoppt mein Programm direkt vor der Zeile "vertexShader = glCreateShader(GL_VERTEX_SHADER);" (ziemlich weit am Anfang)

    Das ACCESS-VIOLATION-Fenster unterbricht mein Programm dort, und zeigt das folgende: Unbehandelte Ausnahme bei 0x00000000 in test.exe: 0xC0000005: Access violation.

    Ich hab schon mehrere Problemlösungen aus dem Internet ausprobiert:
    Visual C++ 2010 Express neu zustarten, und es als Administrator zu starten. Ich hab auch mein Visual C++ neu installiert, und openGL mit GLFW und GLEW eingefügt (wie es mein Tutorial gezeigt hat).

    Ich kann mir das einfach nicht erklären, und wollte nach circa 12 Stunden Nachdenkens mal nachfragen.
    Ich würde mich über Problem-Lösungen freuen!

    PS: Ich bin noch nicht so bewandert in dem Bereich, also bitte alles etwas einfacher und schrittweise erklären 😉



  • Lies doch mal im nächsten Abschnitt, wie man GLFW (und GLEW) initialisiert.
    http://www.learnopengl.com/#!Getting-started/Hello-Window



  • Okay!

    Ich habe nochmal einen neuen Code geschrieben mit Initialisierung und hab den Context mit reingepackt.

    #include "stdafx.h"
    #include <iostream>
    
    #define GLEW_STATIC
    #include <glew.h>
    
    #include <glfw3.h>
    
    // Function prototypes
    void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
    
    // Window dimensions
    const GLuint WIDTH = 800, HEIGHT = 600;
    
    int main()
    {
    
    	std::cout << "Starting GLFW context, OpenGL 3.3" << std::endl;
        // Init GLFW
        glfwInit();
        // Set all the required options for GLFW
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
        glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
    
    	// Create a GLFWwindow object that we can use for GLFW's functions
        GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", nullptr, nullptr);    
        if (window == nullptr)
        {
            std::cout << "Failed to create GLFW window" << std::endl;
            glfwTerminate();
            return -1;
        }
        glfwMakeContextCurrent(window);
        // Set the required callback functions
        glfwSetKeyCallback(window, key_callback);
    
    	std::cout << "GLFW init" << std::endl;
    	system("pause");
    
    	glewExperimental = GL_TRUE;
        // Initialize GLEW to setup the OpenGL Function pointers
        if (glewInit() != GLEW_OK)
        {
            std::cout << "Failed to initialize GLEW" << glewGetErrorString(glewInit())  << std::endl;
    		system("pause");
            return -1;
        } 
    
    	std::cout << "GLEW init" << std::endl;
    	system("pause");
    
    	GLchar* vertexShaderSource = "#version 330 core\n"
                                  "layout (location = 0) in vec3 position;"
                                  "void main()"
    							  "{"
                                  "  gl_Position = vec4(position.x, position.y, position.z, 1.0);"
                                  "}";
    
    	std::cout << "ShaderCode read" << std::endl;
    	system("pause");
    
    	GLfloat vertices[] = {
    		-0.5f, -0.5f, 0.0f,
    		0.5f, -0.5f, 0.0f,
    		0.0f,  0.5f, 0.0f
    	};  
    
    	std::cout << "Vertizes read" << std::endl;
    	system("pause");
    
    	GLuint VBO;
    	glGenBuffers(1, &VBO); 
    	glBindBuffer(GL_ARRAY_BUFFER, VBO); 
    	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    
    	std::cout << "GL Buffer initialised" << std::endl;
    	system("pause");
    
    	GLuint vertexShader;
    	vertexShader = glCreateShader(GL_VERTEX_SHADER);
    
    	std::cout << "Shaderobj." << std::endl;
    	system("pause");
    
    	glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    	glCompileShader(vertexShader);
    
    	std::cout << "Shader´compiled" << std::endl;
    	system("pause");
    
    	GLint success;
    	GLchar infoLog[512];
    	glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
    
    	if(!success)
    	{
    		glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
    		std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
    	}
    
    	std::cout << "Shader: success" << std::endl;
    	system("pause");
    	return 0;
    }
    
    // Is called whenever a key is pressed/released via GLFW
    void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
    {
        std::cout << key << std::endl;
        if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
            glfwSetWindowShouldClose(window, GL_TRUE);
    }
    

    😃 😃 😃 😃 😃


Anmelden zum Antworten