我使用OpenGL,SDL 2和GLEW编写了一个简单的程序。它可以在Linux上运行,在Wine下运行,在一个Windows 7系统上运行它。但是,在另一个Windows系统上,它一旦到达glGenBuffers就会崩溃,即使它说OpenGL 2.1可用。这是代码:
#define GLEW_STATIC
#include <stdio.h>
#include <GL/glew.h>
#include <SDL2/SDL.h>
SDL_Window *window;
SDL_GLContext context;
GLuint vbo;
void Init() {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_LoadLibrary(NULL);
int min, max;
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION,&max);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION,&min);
printf("Default OpenGL version %d.%d\n",max,min);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION,2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION,1);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION,&max);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION,&min);
printf("OpenGL version %d.%d\n",max,min);
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL,1);
window=SDL_CreateWindow("", 0, 0, 500, 500, SDL_WINDOW_OPENGL);
context = SDL_GL_CreateContext(window);
glewExperimental=1;
GLenum err=glewInit();
if(GLEW_OK!=err)
{
printf("Error: %s\n", glewGetErrorString(err));
return(1);
}
if(window==NULL) {
printf("Could not create window: %s\n", SDL_GetError());
return(1);
}
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glShadeModel(GL_FLAT);
glEnableClientState(GL_VERTEX_ARRAY);
float data[][2] = {{50,50},{100,50},{75,100}};
glGenBuffers(1,&vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
}
void Render() {
glViewport(0,0, (GLsizei) 500, (GLsizei) 500);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0f, (GLdouble) 500, 0.0f, (GLdouble) 500);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f,0.0f,0.0f);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexPointer(2, GL_FLOAT, 2*sizeof(float), 0);
glDrawArrays(GL_TRIANGLES,0,3);
SDL_GL_SwapWindow(window);
}
int main(int argc, char **argv){
Init();
Render();
SDL_Delay(2000);
glDeleteBuffers(1,&vbo);
return(0);
}
以下是自最初发布此问题以来我添加的更多信息: