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.556 ]
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
Como aprender a programar e produzir aplicativos usando Euphoria
Substituindo a biblioteca conio.h no Linux usando ncurses curses.h
Utilizando a biblioteca NCURSES - Parte I
Melhorando o tempo de boot do Fedora e outras distribuições
Como instalar as extensões Dash To Dock e Hide Top Bar no Gnome 45/46
E a guerra contra bots continua
Tradução do artigo do filósofo Gottfried Wilhelm Leibniz sobre o sistema binário
Conheça o firewall OpenGFW, uma implementação do (Great Firewall of China).
Instalando o FreeOffice no LMDE 6
Anki: Remover Tags de Estilo HTML de Todas as Cartas
Colocando uma opção de redimensionamento de imagem no menu de contexto do KDE
chamados para entidades GLPI 10 (0)
Pi Network.... alguém minera? (3)
Como adicionar módulo de saúde da bateria dos notebooks Acer ao kernel... (17)
[Shell Script] Script para desinstalar pacotes desnecessários no OpenSuse
[Shell Script] Script para criar certificados de forma automatizada no OpenVpn
[Shell Script] Conversor de vídeo com opção de legenda
[C/C++] BRT - Bulk Renaming Tool
[Shell Script] Criação de Usuarios , Grupo e instalação do servidor de arquivos samba