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: 19.774 ]
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
Ponteiros void na linguagem C (parte 2)
Apreendendo a utilizar o GNU Debugger (parte 2)
Como aprender a programar e produzir aplicativos usando Euphoria
Túnel do Tempo: a função itoa()
Como extrair chaves TOTP 2FA a partir de QRCODE (Google Authenticator)
Linux em 2025: Segurança prática para o usuário
Desktop Linux em alta: novos apps, distros e privacidade marcam o sábado
IA chega ao desktop e impulsiona produtividade no mundo Linux
Novos apps de produtividade, avanços em IA e distros em ebulição agitam o universo Linux
Como instalar o repositório do DBeaver no Ubuntu
Como instalar o Plex Media Server no Ubuntu
Digitando underscore com "shift" + "barra de espaços"
Como ativar a lixeira e recuperar aquivos deletados em um servidor Linux
Como mudar o nome de dispositivos Bluetooth via linha de comando
O programa assinador digital (0)
dpkg: erro: gatilho de arquivo duplicado chamado pelo arquivo de nome (6)
Instalação não está resolvendo as dependencias (2)
Captação de áudio no zorin linux começa a diminuir com o tempo (5)









