sábado, 26 de outubro de 2013

Windows/Linux: GLUT em DevC++ e CodeBlock

Windows



> DevC++


Passo 1: Baixe o arquivo winglut.zip , descompacte-o e coloque os arquivos nas seguintes pastas:
glut.h              ->    …\Dev-Cpp\MinGW32\include\GL
libglut32win.a      ->    …\Dev-Cpp\MinGW32\lib
 
Windows (32bits):
glut32.dll          ->    C:\Windows\System32
Windows (64bits):
glut32.dll          ->    C:\Windows\SysWOW64
 * Caso não funcione coloque a glut32.dll na mesma pasta do seu projeto ou executável.


Passo 2: 
Crie um novo projeto em “New Project” do tipo “Console Application” em “C++”. 
Em “Project” >> “Project Options” na aba “Parameters” adicione as seguintes linhas no “Linker”:



Ou coloque o caminho inteiro das libs:

…\Dev-Cpp\MinGW32\lib\libopengl32.a
…\Dev-Cpp\MinGW32\lib\libglu32.a
…\Dev-Cpp\MinGW32\lib\libglut32win32.a





Pronto! Agora é só rodar o código abaixo e ver se funciona!


Segue o código do GLUT para testarmos:


/*
 * Autor:   Gabriel de Araújo
 * Data:    26/10/2013
 *
 */
#include <iostream>
#include <gl/gl.h>
#include <gl/glut.h>
#include <stdlib.h>
#include <stdio.h>

void resize(int w, int h), keyboard(unsigned char c, int x, int y), display(void);

int main(int argc, char **argv){
    glutInit(&argc, argv);
    glutInitWindowSize(400, 400);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Translade");
    glutDisplayFunc(display);
    glutReshapeFunc(resize);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
    return 0;
}

void display(void){

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);

    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


   glRotatef(40, 0.1, 0.2, 0.0);
   //Desenha as linhas que representarão os eixos X, Y e Z.
   glBegin(GL_LINES);
      glColor3f(1, 1, 1);
      glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 0.0, 5.0);
      glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 5.0, 0.0);
      glVertex3f(0.0, 0.0, 0.0); glVertex3f(5.0, 0.0, 0.0);
   glEnd();
 
   //Quadrado Azul
   glBegin(GL_QUADS);
         glColor3f(0.0, 0.0, 1.0);
         glVertex3f(1.0, 1.0, 0.0);
         glVertex3f(1.0, 3.0, 0.0);
         glVertex3f(3.0, 3.0, 0.0);
         glVertex3f(3.0, 1.0, 0.0);
   glEnd();

    glFlush();
    glutSwapBuffers();
}

void keyboard(unsigned char c, int x, int y){
    switch (c) {
        case 27:
            exit(0);
            break;
        default:
            break;
    }
}

void resize(int w, int h){
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-4.0, 8.0, -4.0, 8.0, -4.0, 8.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}


Veja como ficou a imagem:





Linux



> CodeBlocks


Pressupondo que você já instalou o “Code::Blocks IDE” em “Administração” >> “Gerenciador de Aplicativos”.

Iremos utilizar a “freeglut”, pois de acordo com [1] a versão da GLUT por “Mark Kilgard não continua sendo desenvolvida (sem manutenção e sem novas funcionalidades) e sua licença não permite redistribuição ou versões modificadas da biblioteca. Isto aumentou a necessidade de uma versão livre da biblioteca, cuja primeira foi freeglut, que pretendia ser um clone exato da versão original e acrescentou poucas novas funções. “

Para tanto basta baixarmos a “freeglut-dev”:




Em seguida iremos criar no CodeBlocks um novo projeto do tipo “Console Application” em C++:




Em "Project Build Options" adicionar as seguintes linhas em "Link Libraries":





Observação1: “GL” e “GLU” devem ser maiúsculas e “glut” minúscula!

Observação2: Ao invés de incluirmos a “glut.h” devemos incluir a “freeglut.h” e nos atentarmos que o GL é maiúsculo e não minúsculo (erro comum), veja abaixo:
#include <GL/freeglut.h>



/*
 * Autor:   Gabriel de Araújo
 * Data:    26/10/2013
 *
 */
#include <GL/freeglut.h>
#include <stdlib.h>
#include <stdio.h>

void resize(int w, int h), keyboard(unsigned char c, int x, int y), display(void);

int main(int argc, char **argv){
    glutInit(&argc, argv);
    glutInitWindowSize(400, 400);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Translade");
    glutDisplayFunc(display);
    glutReshapeFunc(resize);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
    return 0;
}

void display(void){

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);

    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


   //Desenha as linhas que representarão os eixos X, Y e Z.
   glBegin(GL_LINES);
      glColor3f(1, 1, 1);
      glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 0.0, 5.0);
      glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 5.0, 0.0);
      glVertex3f(0.0, 0.0, 0.0); glVertex3f(5.0, 0.0, 0.0);
   glEnd();

   //Desenha as letras X, Y e Z dos eixos.
   glColor3f(0, 1, 0);
   glRasterPos3f(0.0, 0.0, 5.0);
   glutBitmapString(GLUT_BITMAP_8_BY_13,  (const unsigned char*)"Z");
   glRasterPos3f(0.0, 5.0, 0.0);
   glutBitmapString(GLUT_BITMAP_8_BY_13, (const unsigned char*)"Y");
   glRasterPos3f(5.0, 0.0, 0.0);
   glutBitmapString(GLUT_BITMAP_8_BY_13, (const unsigned char*)"X");

   //Quadrado Azul
   glBegin(GL_QUADS);
         glColor3f(0.0, 0.0, 1.0);
         glVertex3f(1.0, 1.0, 0.0);
         glVertex3f(1.0, 3.0, 0.0);
         glVertex3f(3.0, 3.0, 0.0);
         glVertex3f(3.0, 1.0, 0.0);
   glEnd();

   glRotatef(40, 0.1, 0.2,0.0);
  

    glFlush();
    glutSwapBuffers();
}

void keyboard(unsigned char c, int x, int y){
    switch (c) {
        case 27:
            exit(0);
            break;
        default:
            break;
    }
}

void resize(int w, int h){
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-4.0, 8.0, -4.0, 8.0, -4.0, 8.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}


O resultado é esse:






Referências:

http://web.cs.wpi.edu/~gogo/courses/mingw/
http://chortle.ccsu.edu/Bloodshed/howToGL.html

http://aass.oru.se/~mbl/datorgrafik/devpp.shtml 
[1] http://pt.wikipedia.org/wiki/GLUT

Nenhum comentário:

Postar um comentário