ここでうまくいきます:
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <vector>
#include <iostream>
// header-only, http://glm.g-truc.net/
#include <glm/glm.hpp>
using namespace glm;
// GLSL shader program loader
struct Program
{
static GLuint Load( const char* vert, const char* geom, const char* frag )
{
GLuint prog = glCreateProgram();
if( vert ) AttachShader( prog, GL_VERTEX_SHADER, vert );
if( geom ) AttachShader( prog, GL_GEOMETRY_SHADER, geom );
if( frag ) AttachShader( prog, GL_FRAGMENT_SHADER, frag );
glLinkProgram( prog );
CheckStatus( prog );
return prog;
}
private:
static void CheckStatus( GLuint obj )
{
GLint status = GL_FALSE, len = 10;
if( glIsShader(obj) ) glGetShaderiv( obj, GL_COMPILE_STATUS, &status );
if( glIsProgram(obj) ) glGetProgramiv( obj, GL_LINK_STATUS, &status );
if( status == GL_TRUE ) return;
if( glIsShader(obj) ) glGetShaderiv( obj, GL_INFO_LOG_LENGTH, &len );
if( glIsProgram(obj) ) glGetProgramiv( obj, GL_INFO_LOG_LENGTH, &len );
std::vector< char > log( len, 'X' );
if( glIsShader(obj) ) glGetShaderInfoLog( obj, len, NULL, &log[0] );
if( glIsProgram(obj) ) glGetProgramInfoLog( obj, len, NULL, &log[0] );
std::cerr << &log[0] << std::endl;
exit( -1 );
}
static void AttachShader( GLuint program, GLenum type, const char* src )
{
GLuint shader = glCreateShader( type );
glShaderSource( shader, 1, &src, NULL );
glCompileShader( shader );
CheckStatus( shader );
glAttachShader( program, shader );
glDeleteShader( shader );
}
};
#define GLSL( version, shader ) "#version " #version "\n" #shader
const GLchar* vert = GLSL
(
150,
in vec4 vPosition;
void main()
{
gl_Position = vPosition;
}
);
const GLchar* frag = GLSL
(
150,
void main (void)
{
gl_FragColor = vec4( 1.0, 0.0, 0.0, 0.0 );
}
);
//----------------------------------------------------------------------------
const int NumPoints = 5000;
void init( void )
{
vec2 points[NumPoints];
// Specifiy the vertices for a triangle
vec2 vertices[3] =
{
vec2( -1.0, -1.0 ), vec2( 0.0, 1.0 ), vec2( 1.0, -1.0 )
};
// Select an arbitrary initial point inside of the triangle
points[0] = vec2( 0.25, 0.50 );
// compute and store N-1 new points
for ( int i = 1; i < NumPoints; ++i )
{
int j = rand() % 3; // pick a vertex at random
// Compute the point halfway between the selected vertex
// and the previous point
points[i] = ( points[i - 1] + vertices[j] ) / 2.0f;
}
// Create a vertex array object
GLuint vao;
glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );
// Create and initialize a buffer object
GLuint buffer;
glGenBuffers( 1, &buffer );
glBindBuffer( GL_ARRAY_BUFFER, buffer );
glBufferData( GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW );
// Load shaders and use the resulting shader program
GLuint program = Program::Load( vert, NULL, frag );
glUseProgram( program );
// Initialize the vertex position attribute from the vertex shader
GLuint loc = glGetAttribLocation( program, "vPosition" );
glEnableVertexAttribArray( loc );
glVertexAttribPointer( loc, 2, GL_FLOAT, GL_FALSE, 0, (void*)(0) );
glClearColor( 1.0, 1.0, 1.0, 1.0 ); // white background
}
//----------------------------------------------------------------------------
void display( void )
{
glClear( GL_COLOR_BUFFER_BIT ); // clear the window
glDrawArrays( GL_POINTS, 0, NumPoints ); // draw the points
glutSwapBuffers();
}
//----------------------------------------------------------------------------
void keyboard( unsigned char key, int x, int y )
{
switch ( key )
{
case 033:
exit( EXIT_SUCCESS );
break;
}
}
//----------------------------------------------------------------------------
int main( int argc, char **argv )
{
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutInitWindowSize( 512, 512 );
// bumped to 3.2 (from 3.1) because your shaders are #version 150
glutInitContextVersion( 3, 2 );
glutInitContextProfile( GLUT_CORE_PROFILE );
glutCreateWindow( "Sierpinski Gasket" );
glewInit();
std::cout << "OpenGL version: " << glGetString( GL_VERSION ) << std::endl;
std::cout << "GLSL version: " << glGetString( GL_SHADING_LANGUAGE_VERSION ) << std::endl;
init();
glutDisplayFunc( display );
glutKeyboardFunc( keyboard );
glutMainLoop();
return 0;
}
OpenGL Extensions Viewerなどを使用して、GL 実装のバージョンが実際に >= 3.2 であることを確認してください。
また、ソースから最新の FreeGLUT (現時点では 2.8.1) と GLEW (1.9.0) をビルドすることをお勧めします。