我正在尝试使用 SDL 在 OpenGL 中显示简单的白色立方体。我已经像这样设置了我的 VBO 和 IBO:
GLfloat vertexData[] =
{
-0.5f, -0.5f, -0.5f, // bot, left, back
-0.5f, 0.5f, -0.5f, // top, left, back
-0.5f, -0.5f, 0.5f, // bot, left, front
-0.5f, 0.5f, 0.5f, // top, left, front
0.5f, -0.5f, -0.5f, // bot, right, back
0.5f, -0.5f, 0.5f, // bot, right, front
0.5f, 0.5f, -0.5f, // top, right, back
0.5f, 0.5f, 0.5f // top, right, front
};
GLint indexData[] =
{
//back
0, 1, 6,
0, 5, 6,
//left
0, 2, 3,
0, 1, 3,
//right
4, 5, 7,
4, 6, 7,
//bot
0, 4, 5,
0, 2, 5,
//top
1, 3, 7,
1, 6, 7,
//front
2, 3, 7,
2, 5, 7
};
但是,它看起来像这样:
我究竟做错了什么?
编辑:我的初始化函数
bool App::OpenGLInit()
{
programID = glCreateProgram();
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
std::string vertexString = getShaderFromFile("src/shader.vert");
const GLchar * vertexShaderSource = vertexString.c_str();
glShaderSource(vertexShader, 1, (const GLchar **)&vertexShaderSource, NULL);
glCompileShader(vertexShader);
glAttachShader(programID, vertexShader);
GLuint fragmentShader = glCreateShader( GL_FRAGMENT_SHADER );
//Get fragment source
std::string fragmentString = getShaderFromFile("src/shader.frag");
const GLchar* fragmentShaderSource = fragmentString.c_str();
//Set fragment source
glShaderSource( fragmentShader, 1, (const GLchar **)&fragmentShaderSource, NULL );
//Compile fragment source
glCompileShader( fragmentShader );
glAttachShader(programID, fragmentShader );
glLinkProgram(programID);
return true;
}
bool App::OnInit()
{
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 2 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE );
if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
return false;
}
if((screen = SDL_CreateWindow("Color Wars", 100, 100, 1024, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN)) == NULL) {
return false;
}
if ((gl_context = SDL_GL_CreateContext(screen)) == NULL)
{
return false;
}
if((renderer = SDL_CreateRenderer(screen, -1, 0)) == NULL)
{
return false;
}
if (SDL_GL_SetSwapInterval(1) < 0)
return false;
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
return false;
}
if (!OpenGLInit())
return false;
game_screen = new GameScreen();
game_screen->Init(programID);
return true;
}
我的渲染功能:
void App::OnRender()
{
glClear( GL_COLOR_BUFFER_BIT );
glUseProgram( programID );
game_screen->Render(renderer);
glUseProgram( NULL );
SDL_GL_SwapWindow(screen);
}
void GameScreen::Render(SDL_Renderer *renderer)
{
//Enable vertex position
glEnableVertexAttribArray(vertex2DPosition);
//Set vertex data
glBindBuffer( GL_ARRAY_BUFFER, VBO );
glVertexAttribPointer(vertex2DPosition, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), NULL );
//Set index data and render
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, IBO );
glDrawElements(GL_TRIANGLE_FAN, 12, GL_UNSIGNED_INT, NULL );
//Disable vertex position
glDisableVertexAttribArray( vertex2DPosition );
}
虚拟文本:asdasdasdasdasdasd