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.203 ]
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
Criação e uso de um interpretador de script BrainFuck em C++
Criando programas com suporte a arquivos de configuração com a libConfuse
Túnel do Tempo: a função itoa()
Cirurgia para acelerar o openSUSE em HD externo via USB
Void Server como Domain Control
Modo Simples de Baixar e Usar o bash-completion
Monitorando o Preço do Bitcoin ou sua Cripto Favorita em Tempo Real com um Widget Flutuante
Como fazer a conversão binária e aplicar as restrições no Linux
Como quebrar a senha de um servidor Linux Debian
Como bloquear pendrive em uma rede Linux
Um autoinstall.yaml para Ubuntu com foco em quem vai fazer máquina virtual
Instalar GRUB sem archinstall no Arch Linux em UEFI Problemático









