2016-12-28 1 views
-1

Ce code lève l'exception nvoglv32.dll. Je pense qu'il ya une erreur dans glShaderSource somewherer, mais je ne peux pas trouverOpenGL glShaderSource lève une exception

ifstream ifs("vertexShader.txt"); 
string vertexShadersSource((istreambuf_iterator<char>(ifs)), 
    (std::istreambuf_iterator<char>())); 

ifs.close(); 
ifs.open("fragmentShader.txt"); 
string fragmentShadersSource((istreambuf_iterator<char>(ifs)), 
    (std::istreambuf_iterator<char>())); 

cout << fragmentShadersSource.c_str() << endl; 
cout << vertexShadersSource.c_str() << endl; 

GLuint shaderProgram; 
GLuint fragmentShader, vertexShader; 
vertexShader = glCreateShader(GL_VERTEX_SHADER); 
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); 

const char *data = vertexShadersSource.c_str(); 
glShaderSource(vertexShader, 1, &data, (GLint*)vertexShadersSource.size()); 
data = fragmentShadersSource.c_str(); 
glShaderSource(fragmentShader, 1, &data, (GLint*)fragmentShadersSource.size()); 

EDIT:

Bien que je pense que le shader est correct, ici vous pouvez voir le code shader VertexShader:

#version 150 
// in_Position was bound to attribute index 0 and in_Color was bound to  attribute index 1 
//in vec2 in_Position; 
//in vec3 in_Color; 

// We output the ex_Color variable to the next shader in the chain 
out vec3 ex_Color; 
void main(void) { 
// Since we are using flat lines, our input only had two points: x and y. 
// Set the Z coordinate to 0 and W coordinate to 1 

//gl_Position = vec4(in_Position.x, in_Position.y, 0.0, 1.0); 

// GLSL allows shorthand use of vectors too, the following is also valid: 
// gl_Position = vec4(in_Position, 0.0, 1.0); 
// We're simply passing the color through unmodified 

ex_Color = vec3(1.0, 1.0, 0.0); 
} 

FragmentShader:

#version 150 
// It was expressed that some drivers required this next line to function properly 
precision highp float; 

in vec3 ex_Color; 
out vec4 gl_FragColor; 

void main(void) { 
// Pass through our original color with full opacity. 
gl_FragColor = vec4(ex_Color,1.0); 
} 
+0

Comment créer un contexte OpenGL? Quel est le contenu de 'vertexShader.txt' et' fragmentShader.txt'? – jimvonmoon

+1

Modifier dans un [mcve]. – genpfault

+0

J'utilise glfw pour créer ma fenêtre et j'utilise glfwMakeContextCurrent (window) pour créer le contexte. – Max

Répondre

2

Votre appel est erroné:

glShaderSource(vertexShader, 1, &data, (GLint*)vertexShadersSource.size()); 

et la distribution de type d'un size_t à un type de pointeur aurait dû soulever quelques drapeaux rouges alors que vous l'avez écrit.

glShaderSource() attend un pointeur à un tableau de chaîne Longueurs - un élément par chaîne séparée. Puisque vous n'utilisez qu'une seule chaîne, elle va essayer d'accéder à lenght[0]. Cela signifie qu'il traite votre taille de chaîne comme une adresse, et cette adresse est très susceptible de ne pas appartenir à votre processus.

Étant donné que vous utilisez déjà des C-Strings à 0-terminaison, vous pouvez simplement utiliser NULL comme paramètre length. Ou, si vous voulez absolument l'utiliser, vous devez simplement passer un pointeur sur un GLint:

GLint len=vertexShadersSource.size(); 
glShaderSource(..., 1, ..., &len);