Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Problem with glShaderSource crashing

  1. #1
    Newbie Newbie
    Join Date
    Sep 2014
    Posts
    2

    Question Problem with glShaderSource crashing

    Hello, I'm new to the forums and am also fairly new to using OpenGL and appreciate in advance any help I get.

    I am using SDL to create my OpenGL context and window and glew to load the extensions, and using the MinGW GCC compiler on windows 7.
    My code below crashes at the first glShaderSource called (I know from commenting out) and am not sure why. Below is my C source code and GLSL shader source code.
    My goal at the moment is to simply draw a triangle in a window.

    Code :
    #include<stdio.h>
    #include<GL/glew.h>
    #include<SDL/SDL.h>
     
    unsigned int getFileLength(FILE* file);
    int readFile(FILE* file,char* output);
    void writeFile(FILE* file,char* input);
     
    int main(int argc,char* argv[]){
    	//Init SDL
    	if(SDL_Init(SDL_INIT_VIDEO))
    		fprintf(stderr,"Error: SDL failed to initialize: %s",SDL_GetError());
     
    	//OpenGL 3.3
    	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION,3);
    	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION,3);
     
    	//Create a window and OpenGL context
    	SDL_Window* window = SDL_CreateWindow("The Window",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,640,480,SDL_WINDOW_OPENGL);
    	SDL_GLContext context = SDL_GL_CreateContext(window);
     
    	//Init GLEW
    	GLenum err = glewInit();
    	if(err != GLEW_OK)
    		fprintf(stderr,"Error: GLEW failed to initialize: %s",glewGetErrorString(err));
     
    	//Create a triangle (This will be drawn on screen later)
    	float triangle[] = {
    		0.0f,0.5f,
    		0.5f,-0.5f,
    		-0.5f,-0.5f
    	};
     
    	//Upload triangle data
    	GLuint buffer;
    	glGenBuffers(1,&buffer);
    	glBindBuffer(GL_ARRAY_BUFFER,buffer);
    	glBufferData(GL_ARRAY_BUFFER,sizeof(triangle),&triangle,GL_STATIC_DRAW);
     
    	//Create, load and compile shaders
    	GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    	GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    	FILE* vertexShaderFile = fopen("shaders/vertex.shader","r");
    	FILE* fragmentShaderFile = fopen("shaders/fragment.shader","r");
    	if(vertexShaderFile == NULL || fragmentShaderFile == NULL)
    		fprintf(stderr,"Error: Shader sources could not be found");
    	GLchar vertexShaderSource[getFileLength(vertexShaderFile) + 1],fragmentShaderSource[getFileLength(fragmentShaderFile) + 1];
    	readFile(vertexShaderFile,vertexShaderSource);
    	readFile(fragmentShaderFile,fragmentShaderSource);
    	glShaderSource(vertexShader,1,(const GLchar**) &vertexShaderSource,NULL);
    	glShaderSource(fragmentShader,1,(const GLchar**) &fragmentShaderSource,NULL);
    	glCompileShader(vertexShader);
    	glCompileShader(fragmentShader);
    	GLchar vertexShaderLog[1000],fragmentShaderLog[1000];
    	glGetShaderInfoLog(vertexShader,sizeof(vertexShaderLog),NULL,vertexShaderLog);
    	glGetShaderInfoLog(fragmentShader,sizeof(fragmentShaderLog),NULL,fragmentShaderLog);
    	FILE* vertexShaderLogFile = fopen("shaders/vertexShader.log","w");
    	FILE* fragmentShaderLogFile = fopen("shaders/fragmentShader.log","w");
    	writeFile(vertexShaderLogFile,vertexShaderLog);
    	writeFile(fragmentShaderLogFile,fragmentShaderLog);
     
    	SDL_Quit();
    	return 0;
    }
     
     
    //Shader loading functions
    unsigned int getFileLength(FILE* file){
    	rewind(file);
    	int c,count = 0;
    	while((c = fgetc(file)) != EOF)
    		count++;
    	return count;
    }
     
    int readFile(FILE* file,char* output){
    	rewind(file);
    	int c,i;
    	for(i = 0;(c = fgetc(file)) != EOF;i++)
    		output[i] = c;
    	output[i] = '\0';
    	return i;
    }
     
    void writeFile(FILE* file,char* input){
    	int i;
    	for(i = 0;i < sizeof(input);i++)
    		fputc(input[i],file);
    }

    and the shaders... (I copied these from an example on some tutorial site)

    Vertex Shader:
    Code :
    void main(){
    	vec4 a = gl_Vertex;
    	a.x = a.x * 0.5;
    	a.y = a.y * 0.5;
     
    	gl_Position = gl_ModelViewProjectionMatrix * a;
    }

    Fragment Shader:
    Code :
    void main(){
    	gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
    }

  2. #2
    Intern Newbie
    Join Date
    Nov 2013
    Posts
    48
    I believe you need to specify a version for the shaders, may be this is it...

  3. #3
    Newbie Newbie
    Join Date
    Sep 2014
    Posts
    2
    Thanks for the tip, and I actually did try to add the version shortly after posting this topic but sadly it did not work. I'll google search an example and compare it with mine to see... probably should of done that before making this topic... ha.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •