2014-01-30 28 views
1

問題: 我試圖寫一個類來繪製可點擊的矩形問題是,因爲我開始在程序中使用新的OpenGL版本(4.0)它不呈現並在位置0x00000000上讀取時給我提供運行時錯誤錯誤。 我將包括類的代碼以及底部的主循環。OpenGL應用程序與GLFW和GLEW崩潰

GUI::GUI(std::string name, int top, int left,int heigh, int width, bool isvisible, void(_cdecl * func)()) 
{ 
    Name=name; 
    Top=top; 
    Left=left; 
    Heigh=heigh; 
    Width=width; 
    BackColor = struct_RGB(); 
    glClearColor(0.0f, 0.0f, 0.4f, 1.0f); 
    BackDrawFunc=func; 
    Visibility=isvisible; 
    GuiObj = std::vector<GUIOBJ *>(); 

    vertices = std::vector<float>(); 
    vertices.clear(); 

    for(unsigned int i=0; i<GuiObj.size(); i++) 
    { 
     GuiObj[i]->addVertices(); 
    }; 

    glGenVertexArrays(1, &VertexArrayID); 
    glBindVertexArray(VertexArrayID); 

    // Generate 1 buffer, put the resulting identifier in vertexbuffer 
    glGenBuffers(1, &VBO); 

    // The following commands will talk about our 'vertexbuffer' buffer 
    glBindBuffer(GL_ARRAY_BUFFER, VBO); 

    glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW); 

    program = LoadShaders("res/VertexShader.glsl", "res/FragmentShader.glsl"); 
} 


void GUI::Draw() 
{ 
    glClear(GL_COLOR_BUFFER_BIT); 
       // Use our shader 
       glUseProgram(program); 
       // 1rst attribute buffer : vertices 
       glBindBuffer(GL_ARRAY_BUFFER, VBO); 
       glEnableVertexAttribArray(0); 
       glVertexAttribPointer(
         0,     // attribute 0. No particular reason for 0, but must match the layout in the shader. 
         vertices.size(),     // size 
         GL_FLOAT,   // type 
         GL_FALSE,   // normalized? 
         0,     // stride 
         (void*)0   // array buffer offset 
       ); 
       // Draw the triangle ! 
       glDrawArrays(GL_TRIANGLES, 0, vertices.size()); // 3 indices starting at 0 -> 1 triangle 
       printf("drawing\n"); 
       glDisableVertexAttribArray(0); 

} 




void main() 
{ 
    init(); 

    //Main Loop 
     while(!glfwWindowShouldClose(Window)) 
     { 
      if (glfwGetKey(Window, GLFW_KEY_ESCAPE) == GLFW_PRESS) 
       glfwSetWindowShouldClose(Window, GL_TRUE); 

      Test->Draw(); 
      glfwSwapBuffers(Window); 
      glfwPollEvents(); 
     } 
    //Termination 
     glfwTerminate(); 
} 

編輯: 我在同一個文件複製所有的代碼錯誤解決,但它仍然沒有呈現

#include "extincludes.h" 
#include <vector> 
#include <fstream> 
#include <sstream> 

GLFWwindow* Window; 

typedef unsigned int GLuint; 

GLuint VertexArrayID; 
GLuint VBO; 
GLuint program; 

GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path){ 

    // Create the shaders 
    GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER); 
    GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER); 

    // Read the Vertex Shader code from the file 
    std::string VertexShaderCode; 
    std::ifstream VertexShaderStream = std::ifstream(vertex_file_path, std::ios::in); 
    if (!VertexShaderStream.is_open()) VertexShaderStream.open(vertex_file_path, std::ios::in); 
    if(VertexShaderStream.is_open()) 
    { 
     std::string Line = ""; 
     while(getline(VertexShaderStream, Line)) 
      VertexShaderCode += "\n" + Line; 
     VertexShaderStream.close(); 
    } else 
     printf("Couldn't open VertexShader"); 

    // Read the Fragment Shader code from the file 
    std::string FragmentShaderCode; 
    std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in); 
    if(FragmentShaderStream.is_open()){ 
     std::string Line = ""; 
     while(getline(FragmentShaderStream, Line)) 
      FragmentShaderCode += "\n" + Line; 
     FragmentShaderStream.close(); 
    } else 
     printf("Couldn't open FragmentShader"); 

    GLint Result = GL_FALSE; 
    int InfoLogLength; 

    // Compile Vertex Shader 
    printf("Compiling shader : %s\n", vertex_file_path); 
    char const * VertexSourcePointer = VertexShaderCode.c_str(); 
    glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL); 
    glCompileShader(VertexShaderID); 

    // Check Vertex Shader 
    glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result); 
    glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); 
    std::vector<char> VertexShaderErrorMessage(InfoLogLength); 
    glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]); 
    fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]); 

    // Compile Fragment Shader 
    printf("Compiling shader : %s\n", fragment_file_path); 
    char const * FragmentSourcePointer = FragmentShaderCode.c_str(); 
    glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL); 
    glCompileShader(FragmentShaderID); 

    // Check Fragment Shader 
    glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result); 
    glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); 
    std::vector<char> FragmentShaderErrorMessage(InfoLogLength); 
    glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]); 
    fprintf(stdout, "%s\n", &FragmentShaderErrorMessage[0]); 

    // Link the program 
    fprintf(stdout, "Linking program\n"); 
    GLuint ProgramID = glCreateProgram(); 
    glAttachShader(ProgramID, VertexShaderID); 
    glAttachShader(ProgramID, FragmentShaderID); 
    glLinkProgram(ProgramID); 

    // Check the program 
    glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result); 
    glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength); 
    std::vector<char> ProgramErrorMessage(glm::max(InfoLogLength, int(1))); 
    glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]); 
    fprintf(stdout, "%s\n", &ProgramErrorMessage[0]); 

    glDeleteShader(VertexShaderID); 
    glDeleteShader(FragmentShaderID); 

    return ProgramID; 
} 

void main() 
{ 

    //GLFW 
    printf("started init of GLFW \n"); 
    if (!glfwInit()) 
     printf("startup of GLFW errored \n"); 
    printf("started GLFW \n"); 
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); 
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); 
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //excluding old openGL functionality 

    Window = glfwCreateWindow(640, 768, "Simple example", NULL, NULL); 
    printf("attempt to create window \n"); 
    if (!Window) 
    { 
     glfwTerminate(); 
     exit(EXIT_FAILURE); 
    } 
    glfwMakeContextCurrent(Window); 
    printf("Init of GLFW done \n"); 
    //GLEW 
     printf("start init of GLEW \n"); 
    glewExperimental = true; // Needed for core profile 
    printf("using experimental version of GLEW\n"); 
    if (glewInit() != GLEW_OK) { 
       printf("Failed to initialize GLEW\n"); 

     } 
    printf("done with GLEW\n"); 

    if (glfwGetCurrentContext()!=Window) 
     printf("context error"); 

    glClearColor(1.0f, 0.0f, 0.0f, 1.0f); 


    glGenVertexArrays(1, &VertexArrayID); 
    glBindVertexArray(VertexArrayID); 
    program = LoadShaders("res/VertexShader.glsl", "res/FragmentShader.glsl"); 

    std::vector<float> vertices = std::vector<float>(); 
    vertices.clear(); 

    //triangle 1 
    vertices.push_back(1); 
    vertices.push_back(1); 

    vertices.push_back(0); 
    vertices.push_back(1); 

    vertices.push_back(0); 
    vertices.push_back(3); 
    //triangle 2 
    vertices.push_back(1); 
    vertices.push_back(1); 

    vertices.push_back(0); 
    vertices.push_back(1); 

    vertices.push_back(0); 
    vertices.push_back(3); 

    // Generate 1 buffer, put the resulting identifier in vertexbuffer 
    glGenBuffers(1, &VBO); 

    // The following commands will talk about our 'vertexbuffer' buffer 
    glBindBuffer(GL_ARRAY_BUFFER, VBO); 

    glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), &vertices[0], GL_STATIC_DRAW); 

    glEnableVertexAttribArray(0); 


    glVertexAttribPointer(
     0,     // attribute 0. No particular reason for 0, but must match the layout in the shader. 
     3,     // size 
     GL_FLOAT,   // type 
     GL_FALSE,   // normalized? 
     0,     // stride 
     (void*)0   // array buffer offset 
    ); 



    //Main Loop 
     while(!glfwWindowShouldClose(Window)) 
     { 
      if (glfwGetKey(Window, GLFW_KEY_ESCAPE) == GLFW_PRESS) 
       glfwSetWindowShouldClose(Window, GL_TRUE); 

      glClear(GL_COLOR_BUFFER_BIT); 
       // Use our shader 
       glUseProgram(program); 
       // 1rst attribute buffer : vertices 
       glBindBuffer(GL_ARRAY_BUFFER, VBO); 
       glBindVertexArray(VertexArrayID); 
       // Draw the triangle ! 
       printf("everything setup we can draw \n"); 
       glDrawArrays(GL_TRIANGLES, 0, vertices.size()); // 3 indices starting at 0 -> 1 triangle 
       printf("drawing\n"); 

      glfwSwapBuffers(Window); 
      glfwPollEvents(); 
     } 
    //Termination 
glDisableVertexAttribArray(0); 
glfwDestroyWindow(Window); 
     glfwTerminate(); 
} 
+0

使用跟蹤陳述或者調試器,試圖隔離*,其中*發生了故障。除此之外,在調用'glewInit()'之前嘗試調用'glewExperimental = true;'' –

+0

你調用了'glewInit()'嗎? – concept3d

+0

是的,我沒有添加初始化階段,但glew設置正確。另外碰撞發生在glDrawArrays我忘了提及它 – Deathlymad

回答

0

我想通了崩潰的錯誤了,這只是你需要三個維度而不是一個維度。這導致了錯誤的繪製函數被讀出的範圍....

+0

它仍然不呈現四方形:( – Deathlymad