Tutorial OpenGL v2.0
Finalmente chegou! Após quase 1 ano depois de meu primeiro artigo sobre OpenGL, chegou a versão 2.0. Clique e fique mais Geek.
[ Hits: 17.605 ]
Por: Thiago Henrique Hüpner em 08/05/2015
#include <SDL/SDL.h> #include <SDL/SDL_opengl.h> #include <time.h> #define LARGURA 400 #define ALTURA 400 int colisao(SDL_Rect a,SDL_Rect b){ if(a.x <= b.x+b.w && a.x+a.w >= b.x){ if(a.y <= b.y+b.h && a.y+a.h >= b.y){ return 1; } } // Se passou pelos if's e não retornou significa que não houve colisao return 0; } void inicializaOpenGL(){ glClearColor(255,255,255,1); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0,LARGURA,ALTURA,0); glMatrixMode(GL_MODELVIEW); glClear(GL_COLOR_BUFFER_BIT); } // Simulando o SDL_FillRect void GL_FillRect(SDL_Rect a,int r,int g,int b){ glLoadIdentity(); glColor3ub(r,g,b); glBegin(GL_QUADS); // Lado Superior Esquerdo glVertex2f(a.x,a.y); // Lado Superior Direito glVertex2f(a.x+a.w,a.y); // Lado Inferior Direito glVertex2f(a.x+a.w,a.y+a.h); // Lado Inferior Esquerdo glVertex2f(a.x,a.y+a.h); glEnd(); } int main(int argc,char *argv[]){ if(SDL_Init(SDL_INIT_VIDEO) < 0){ // ... imprima a mensagem de erro e ... printf("Erro : %s ",SDL_GetError()); return -1; } // Para sempre ter valores pseudo-aleatorios srand((unsigned)time(NULL)); SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 ); SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 ); SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 ); SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 ); SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 2 ); SDL_Surface * tela = SDL_SetVideoMode(LARGURA,ALTURA,32,SDL_OPENGL); if(tela == NULL){ printf("Erro : %s ",SDL_GetError()); SDL_Quit(); return -1; } SDL_WM_SetCaption("Colisão nem é tão complexo assim",NULL); SDL_Event evento; int estaRodando = 1; SDL_Rect retangulo,r2; inicializaOpenGL(); int r = 255; int g = 0; int b = 0; int posX = 100; int posY = 100; int aux1 = 10; int aux2 = 10; while(estaRodando){ while(SDL_PollEvent(&evento)){ switch(evento.type){ case SDL_QUIT: estaRodando = 0; break; case SDL_MOUSEMOTION: posX = evento.motion.x; posY = evento.motion.y; if(colisao(retangulo,r2)){ // Muda a cor R,G e B e consequentemente a cor do retangulo muda // NOTA : Tem que ser 256 e não 256 , pois a chance de ser 255 é muito pequena e usando o 256 , a chance aumenta // de ser 255 r = rand() % 256; g = rand() % 256; b = rand() % 256; // Posicao aleatoria aux1 = rand() % (LARGURA - retangulo.w); aux2 = rand() % (ALTURA - retangulo.h); } break; default: break; } } retangulo.x = aux1; retangulo.y = aux2; retangulo.w = 50; retangulo.h = 50; r2.x = posX; r2.y = posY; r2.w = 50; r2.h = 50; // Desta vez limpo a tela aqui e não no GL_FillRect, pois pode haver conflito //de um retangulo aparecer e outro não glClear(GL_COLOR_BUFFER_BIT); GL_FillRect(retangulo,r,g,b); GL_FillRect(r2,255,255,0); SDL_Delay(30); SDL_GL_SwapBuffers(); } SDL_Quit(); return 0; }
Ubuntu/Debian/Kali Linux e outros no Android
Gerencie suas contas financeiras pessoais com Terminal Finances
Introdução à plataforma GNU de desenvolvimento
Aprendendo a utilizar o GNU Debugger (parte 1)
Como aprender a programar e produzir aplicativos usando Euphoria
Apreendendo a utilizar o GNU Debugger (parte 2)
Crie alias para as tarefas que possuam longas linhas de comando - bash e zsh
Criando um gateway de internet com o Debian
Configuração básica do Conky para mostrar informações sobre a sua máquina no Desktop
Aprenda a criar músicas com Inteligência Artificial usando Suno AI
Entendendo o que é URI, URL, URN e conhecendo as diferenças entre POST e GET
Instalando Discord no Fedora 40
Instalando Discord no Ubuntu 24.04 LTS
Como instalar o XFCE4-terminal na sua distribuição Linux e integrá-lo ao sistema
Como instalar o Tilix Terminal no Gnome e usá-lo como menu drop-down ao estilo Quake
Acento til, agudo e circunflexo nao funcionam com Debian (0)
Problemas na configuração da acentuação do meu teclado ABNT2 [RESOLVI... (12)
BUG ao atualizar pacotes libglib2.0-0 libglib2.0-bin libglib2.0-data [... (15)