Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this OpenGL Shader segmentation fault on calls to glCreateShader?

I'm trying to learn how to write OpenGL Shaders. Why does this code segmentation fault when run on my machine? (I'm using Ubuntu 10.04 and I called it shader.cpp.)

#include <GL/glut.h>
#include <iostream>
using namespace std;

int
main (int argc, char **argv)
{
  GLuint myVertexShader = glCreateShader(GL_VERTEX_SHADER);
  return 0;
}

I'm compiling it using the following Makefile:

CC=g++
CFLAGS=-c -Wall -DGL_GLEXT_PROTOTYPES
LDFLAGS= -lglut -lGLU -lGL -lXmu -lXext -lX11 -lm
SOURCES=shader.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=shader

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
    $(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
    $(CC) $(CFLAGS) $< -o $@

clean:
    rm -rf *o $(EXECUTABLE)

check-syntax:
    $(CC) -o nul -S ${CHK_SOURCES}

It seems to be segmentation faulting on the line which calls glCreateShader. I haven't had any luck finding out the source of this problem. I'm a beginner. Thanks!

Note: What this code represents is my first attempt to write a simple shader using OpenGL. If it's all wrong, please feel free to post some working code. What I really want is to get something I can compile and run.

like image 483
Steve Johnson Avatar asked Apr 20 '11 01:04

Steve Johnson


1 Answers

You cannot create a shader in a vacuum. It must be created with a valid RenderContext current. As a matter of fact, there is very little that can be done in OpenGL without a current RenderContext. (Creating a RenderContext is left as an exercise for the reader)

The other thing to consider is that the GL driver on your machine may not support shaders. Just because the function symbol resolves does not mean that it can be done. This is part of the reason why you need the RC -- so the GL runtime can determine what features are supported by the hardware the RC is fronting for.

http://www.opengl.org/wiki/Detecting_the_Shader_Model

like image 180
mcmcc Avatar answered Oct 03 '22 03:10

mcmcc