该程序每帧绘制一个点,直到屏幕充满点。
#include <SDL2/SDL.h>
#include <GL/glew.h>
int SCREEN_WIDTH = 500,
SCREEN_HEIGHT = 500;
int main( int argc, char** argv )
{
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 3 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
SDL_GL_SetAttribute( SDL_GL_ACCELERATED_VISUAL, 1 );
SDL_Window* gWindow = SDL_CreateWindow( "Title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL);
SDL_GLContext gContext = SDL_GL_CreateContext( gWindow );
glewExperimental = GL_TRUE;
glewInit();
const char
*vertex_shader_text=
"#version 330 core\n"
"uniform vec2 p;"
"void main(){"
"gl_Position = vec4(vec2(p),0,1);}",
*fragment_shader_text=
"#version 330 core\n"
"out vec4 col;"
"void main()"
"{col =vec4(1,1,1,0);}";
GLuint
vs = glCreateShader(GL_VERTEX_SHADER),
fs = glCreateShader(GL_FRAGMENT_SHADER),
program = glCreateProgram(),
p_loc;
glShaderSource(vs, 1, &vertex_shader_text, NULL);
glShaderSource(fs, 1, &fragment_shader_text, NULL);
glCompileShader(vs);
glCompileShader(fs);
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glUseProgram(program);
float x= -1.f, y= -1.f;
p_loc = glGetUniformLocation(program, "p");
glPointSize(3);
glViewport(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
SDL_GL_SetSwapInterval(1);
int quit=0;
SDL_Event event;
while( !quit )
{
while(SDL_PollEvent( &event ))
{if( event.type == SDL_QUIT )
quit = 1;}
x+=.01f;
if(x > 1) {
x-=2;
y+=.01f; }
glUniform2f(p_loc, x, y);
glDrawArrays(GL_POINTS,0,1);
SDL_GL_SwapWindow( gWindow );
}
SDL_DestroyWindow(gWindow);
return 0;
}
它工作正常,但当我最小化窗口时,所有先前绘制的点都被删除。有什么方法可以阻止这种情况吗?我在Windows操作系统上运行此程序,但尚未在Linux上测试它并使用Mingw编译它。
你已经很幸运了,在缓冲区交换后,操作系统不是默认帧缓冲区中的giving you garbage。不要依赖那种行为。
或者: