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: 20.458 ]
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
Reprodução de arquivos WAV com SDL_mixer e linguagem C
Como aprender a programar e produzir aplicativos usando Euphoria
Utilizando a biblioteca NCURSES - Parte II
Criando programas com suporte a arquivos de configuração com a libConfuse
Gentoo binário em 2026: UEFI, LUKS, Btrfs e Systemd
Trabalhando Nativamente com Logs no Linux
Jogando Daikatana (Steam) com Patch 1.3 via Luxtorpeda no Linux
LazyDocker – Interface de Usuário em Tempo Real para o Docker
Por que sua empresa precisa de uma PKI (e como automatizar EMISSÕES de certificados via Web API)
Instalando NoMachine no Gentoo com Systemd (acesso Remoto em LAN)
Gentoo: Trocando wpa_supplicant pelo iwd no NetworkManager (Systemd)
OCS Inventory NG: Instalação de Agentes Linux e Windows + Autenticação HTTP
O Free Download Manager não abre no Fedora 43 KDE Plasma (1)
O que houve com slackware ??? (9)
Permissão acesso as pastas servidor Ubuntu Server 24.04 (5)









