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: 18.980 ]
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(int x,int y,SDL_Rect a){ // É verificado primeiro no eixo X if(x >= a.x && x <= a.x + a.w){ // Depois no eixo Y if(y >= a.y && y <= a.y + a.h){ // Se aconteceu a colisão irá retornar 1 return 1; } } // Se passou pelos if's e não retornou significa que nao 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){ glClear(GL_COLOR_BUFFER_BIT); 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){ 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; inicializaOpenGL(); int r = 255; int g = 0; int b = 0; int posX = 10; int posY = 10; while(estaRodando){ while(SDL_PollEvent(&evento)){ switch(evento.type){ case SDL_QUIT: estaRodando = 0; break; case SDL_MOUSEBUTTONDOWN: if(evento.button.button == SDL_BUTTON_LEFT){ // Se apertar no botão esquerdo e o mouse estiver em cima do retangulo ... if(colisao(evento.button.x,evento.button.y,retangulo)){ // 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; } }else if(evento.button.button == SDL_BUTTON_RIGHT){ // Se apertar com o botão direito , o retangulo assume a posição do mouse posX = evento.button.x; posY = evento.button.y; } break; default: break; } } // Propriedades do Retangulo // Posição X retangulo.x = posX; // Posição Y retangulo.y = posY; // Aqui a coisa começa a ficar interessante ... // Largura do Retangulo retangulo.w = 50; // Altura do Retangulo retangulo.h = 50; GL_FillRect(retangulo,r,g,b); SDL_Delay(30); SDL_GL_SwapBuffers(); } SDL_Quit(); return 0; }
Se a posição X do mouse for maior ou igual ao lado superior esquerdo do retângulo e for menor ou igual ao lado superior direito (a.x + a.w), colidiu. Agora, só nos resta detectar a colisão do eixo Y , que segue a mesma logica (até aqui).Agora um desafio: elabore um programa que tenha 3 (ou mais) retângulos e que se o usuário clicar no retângulo certo, ele muda de cor. Senão, o programa continuará sendo executado até que acerte.
#include <SDL/SDL.h> #include <SDL/SDL_opengl.h> #include <time.h> #define LARGURA 400 #define ALTURA 400 int colisao(int x,int y,SDL_Rect a) { // É verificado primeiro no eixo X if(x >= a.x && x <= a.x + a.w) { // Depois no eixo Y if(y >= a.y && y <= a.y + a.h) { // Se aconteceu a colisão irá retornar 1 return 1; } } // Se passou pelos if's e não retornou significa que nao 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); } void definicoesRetangulo(int posX ,int posY,SDL_Rect * a) { glClear(GL_COLOR_BUFFER_BIT); // Propriedades do Retangulo // Posição X a->x = posX; // Posição Y a->y = posY; // Aqui a coisa começa a ficar interessante ... // Largura do Retangulo a->w = 50; // Altura do Retangulo a->h = 50; } // 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 r1,r2,r3; inicializaOpenGL(); int r = 255; int g = 0; int b = 0; while(estaRodando) { while(SDL_PollEvent(&evento)) { switch(evento.type) { case SDL_QUIT: estaRodando = 0; break; case SDL_MOUSEBUTTONDOWN: if(evento.button.button == SDL_BUTTON_LEFT) { // Se apertar no botão esquerdo e o mouse estiver em cima do retangulo (2) ... if(colisao(evento.button.x,evento.button.y,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; } } break; default: break; } } definicoesRetangulo(50,50,&r1); definicoesRetangulo(150,50,&r2); definicoesRetangulo(250,50,&r3); GL_FillRect(r2,r,g,b); GL_FillRect(r1,255,0,255); GL_FillRect(r3,0,255,0); SDL_Delay(30); SDL_GL_SwapBuffers(); } SDL_Quit(); return 0; }
Ubuntu/Debian/Kali Linux e outros no Android
LivreNFE - O emissor Nfe open source para Linux
Criando programas com suporte a arquivos de configuração com a libConfuse
Utilizando a biblioteca NCURSES - Parte III
Aprenda a Gerenciar Permissões de Arquivos no Linux
Como transformar um áudio em vídeo com efeito de forma de onda (wave form)
Como aprovar Pull Requests em seu repositório Github via linha de comando
Quebra de linha na data e hora no Linux Mint
Organizando seus PDF com o Zotero
Problema com Conexão Outlook via Firewall (OpenSUSE) com Internet Fibr... (4)
Sempre que vou baixar algum pacote acontece o erro dpkg (7)
como instalo panfrost-dri e o driver panfrost fork , ou panfrost (12)