2015-12-21 117 views
-1

我正在嘗試使用GLSL編寫着色器,但每當我嘗試編譯此代碼時,在Visual Studio中引發異常,我不知道如何處理它或究竟是如何導致此問題的我會很感激一些幫助。例外情況如下:visual studio中的GLSL錯誤

Open GL.exe中0x00220645的第一次機會異常:0xC0000005:訪問衝突讀取地址0x00D54000。

我真的不知道該如何去做。當我檢查我的輸出我認爲這是它應該有兩條線的例外發生的一切:

'Open GL.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ig7icd32.dll'. Cannot find or open the PDB file. 
'Open GL.exe' (Win32): Loaded 'C:\Windows\SysWOW64\igdusc32.dll'. Cannot find or open the PDB file. 

這裏是我的代碼:

Main.cpp的

#include <iostream> 
#include "Display.h" 
#include "Shader.h" 

int main(int argc, char *argv[]) 
{ 
    Display display(800, 600, "test"); 
    Shader shader("basicshader"); 

    while (!display.isClosed()) 
    { 
     display.Clear(1.0f, 0.0f, 0.0f, 1.0f); 
     shader.Bind(); 
     display.Update(); 
    } 

    return 0; 
} 

顯示.cpp

#pragma once 
#include "Display.h" 

Display::Display(int width, int height, const char *title) 
{ 
    if (SDL_Init(SDL_INIT_EVERYTHING) == 0) 
    { 
     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); 

     m_window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL); 
     m_context = SDL_GL_CreateContext(m_window); 

     GLenum status = glewInit(); 
     if (status != GLEW_OK){ 
      std::cerr << "this is where the problem is" << std::endl; 
     } 
     m_isClosed = false; 
    } 
} 


Display::~Display() 
{ 
     std::cerr << "destructor ran" << std::endl; 
     SDL_GL_DeleteContext(m_context); 
     SDL_DestroyWindow(m_window); 
     SDL_Quit(); 
} 

void Display::Update() 
{ 
    SDL_Event event; 
    SDL_GL_SwapWindow(m_window); 

    while (SDL_PollEvent(&event)) 
    { 
     switch (event.type) 
     { 
     case SDL_QUIT: 
      m_isClosed = true; 
     } 
    } 
} 

void Display::Clear(float r, float g, float b, float a) 
{ 
    glClearColor(r, g, b, a); 
    glClear(GL_COLOR_BUFFER_BIT); 
} 

bool Display::isClosed(){ 
    return m_isClosed; 
} 

Display.h

#pragma once 
#include <SDL.h> 
#include <glew.h> 
#include <iostream> 

class Display 
{ 
public: 
    void Update(); 
    void Clear(float r, float g, float b, float a); 
    bool isClosed(); 
    Display(int width, int height, const char *title); 
    virtual ~Display(); 
private: 
    bool m_isClosed; 
    SDL_Window* m_window; 
    SDL_GLContext m_context; 
}; 

Shader.cpp

#pragma once 
#include "Shader.h" 

std::string Shader::LoadShader(const std::string& filename) 
{ 

    std::ifstream file; 
    std::string output; 
    std::string line; 
    file.open(filename.c_str()); 
    if (file.is_open()) 
    { 
     while (file) 
     { 
      std::getline(file, line); 
      output.append(line + "\n"); 
     } 
     file.close(); 
     return output; 
    } 
} 

GLuint Shader::CreateShader(const std::string& text, GLenum shadertype) 
{ 
    GLuint shader = glCreateShader(shadertype); 
    const GLchar* shadersourcestrings[1]; 
    GLint shadersourcelengths[1]; 

    shadersourcestrings[0] = text.c_str(); 
    shadersourcelengths[0] = text.length(); 

    glShaderSource(shader, 1, shadersourcestrings, shadersourcelengths); 
    glCompileShader(shader); 
    return shader; 
} 

void Shader::Bind() 
{ 
    glUseProgram(m_program); 
} 


Shader::Shader(const std::string& filename) 
{ 
    m_program = glCreateProgram(); 
    m_shaders[0] = CreateShader(LoadShader(filename + ".shade_v"), GL_VERTEX_SHADER); 
    m_shaders[1] = CreateShader(LoadShader(filename + ".shade_f"), GL_FRAGMENT_SHADER); 

    for (unsigned int i = 0; NUMOFSHADERS; i++) 
    { 
     glAttachShader(m_program, m_shaders[i]); <~(breaks here) 
    }; 

    glBindAttribLocation(m_program, 0, "position"); 
    glLinkProgram(m_program); 
} 

Shader::~Shader() 
{ 
    for (unsigned int i = 0; NUMOFSHADERS; i++) 
    { 
     glDetachShader(m_program, m_shaders[i]); 
     glDeleteShader(m_shaders[i]); 
    } 

    glDeleteProgram(m_program); 
} 

Shader.h

#pragma once 
#include <iostream> 
#include <fstream> 
#include <string> 
#include <glew.h> 


class Shader 
{ 
public: 
    const static unsigned int NUMOFSHADERS = 2; 

    GLuint m_program; 
    GLuint m_shaders[NUMOFSHADERS]; 

    void Bind(); 
    static std::string LoadShader(const std::string& filename); 
    static GLuint CreateShader(const std::string& text, GLenum shadertype); 
    Shader(const std::string& filename); 
    virtual ~Shader(); 
}; 

basicshader.shade_v

#version 120 

void main() 
{ 
    gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); 
} 

basicshader.shade_f

#version 120 

attribute vec3 position; 

void main() 
{ 
    gl_position = vec4(postion, 1.0); 
} 
+0

爲了上帝的愛,請* [停止逐行讀取文件行!(http://stackoverflow.com/a/2602258/734069)*人爲什麼這樣做? –

+0

這會幫我找到我的解決方案嗎?還是隻是用我的語法表達個人牛肉?我不介意閱讀,但我還是一個初學者,這是一個有點難以總結我周圍的一些事情提到的頭部,所以如果它不完全「相關的」我不想浪費我的時間就可以了。如果這是我需要你解釋一點(如果這不是問題),否則我仍然會被卡住。 –

回答

2

的一個問題是,如果LoadShader不能打開它會返回垃圾文件,因爲它脫落的末尾而不返回任何東西(這將導致未定義行爲) 。這可能會導致您遇到的問題。

如果你刷爆警告編譯(/ W4),你會得到一個有關警告(「並非所有的控制路徑返回一個值」,或類似的東西)。

+0

似乎是這個問題。謝謝,現在我只想弄清楚如何解決它。 –