Dibujado de circulos OpenGL

download Dibujado de circulos OpenGL

of 11

Transcript of Dibujado de circulos OpenGL

  • 7/25/2019 Dibujado de circulos OpenGL

    1/11

    CIRCULO

    1

    INSTITUTO TECNOLGICO

    DE CIUDAD JUREZ

    INGENIERA EN SISTEMAS

    COMPUTACIONALES

    GRAFICACIN

    TAREA 3

    UNIDAD I I

    CRCULO CON ALGORITMO DE PUNTO MEDIO Y

    ALGORTMICA

    LEYVA MONDRAGON FRANCISCO

    ROSALES MORALES NO RAMN

    JUEVES 17 DE SEPTIEMBRE DE 2015

  • 7/25/2019 Dibujado de circulos OpenGL

    2/11

    CIRCULO

    2

    INTRODUCCION

    Por medio de este breve ensayo se conocer acerca de las libreras que usa al

    programar lneas con openGL en c++. As como ejemplos de cmo crear un

    crculo con la ayuda del algoritmo de punto medio y el mtodo con los

    trigonomtricos seno y coseno.

  • 7/25/2019 Dibujado de circulos OpenGL

    3/11

    CIRCULO

    3

    INDICEPROGRAMACIN DE CRCULOS

    INTRODUCCIN..........Pgina 2

    PROGRAMA BASE..................................Pgina 4

    CIRCULO CON PUNTO MEDIO.....................................................................Pgina 5

    CIRCO CON ALGORITMICA..........................................................................Pgina 8

    CONCLUSIN.......Pgina 10

    BIBLIOGRAFA.....Pgina 11

  • 7/25/2019 Dibujado de circulos OpenGL

    4/11

    CIRCULO

    4

    PROGRAMA BASEEl programa de OpenGL est organizado en tres procedimientos. El primero es el colocar

    todas las inicializaciones y los parmetros de configuracin relacionados en el

    procedimiento init. El segundo ser referenciado por la funcin de GLUT glutDisplayfunc.

    Y el tercer procedimiento es el procedimiento main que contiene las funciones GLUT que

    configuran la ventana de visualizacin y muestran el segmento en pantalla.

    Enseguida se muestra el cdigo fuente de este ejemplo:

    #include "stdafx.h"#include "stdafx.h"#include #include #include

    void init (void){

    glClearColor ( 1.0, 1.0, 1.0, 0.0);glMatrixMode (GL_PROJECTION);gluOrtho2D ( 0.0, 200.0, 0.0, 150.0);}

    void main ( int argc, char *argv ){glutInit (&argc,&argv) ;glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);glutInitWindowPosition (50 , 100 );glutInitWindowSize (400 , 300 );glutCreateWindow( "Programa de ejemplo de OpenGL" ) ;

    init ( );

    glutDisplayFunc () ;glutMainLoop ( ) ;}

    void setPixel (int x,int y)

    {

    glBegin(GL_POINTS);

    glVertex2i (x, y); // Vertice1

    glEnd();

    }

    Con este cdigo de base podemos programar lo que queramos dibujar en nuestra

    pantalla. En esta ocasin dibujaremos crculos pero formados por puntos utilizando las

    dos mtodos.

    Enseguida se muestra el cdigo fuente de todo el programa, donde se utilizan los

    mtodos:

  • 7/25/2019 Dibujado de circulos OpenGL

    5/11

    CIRCULO

    5

    CIRCULO CON ALGORITMO DE PUNTO MEDIO

    Y aqu se muestra el cdigo de todo el algoritmo:

    #include "stdafx.h"

    #include #include #include

    class screenPt{private:

    GLint x, y;

    public:screenPt() {

    x = y = 0;}void setCoords(GLint xCoordValue, GLint yCoordValue){

    x = xCoordValue;y = yCoordValue;

    }

    GLint getx() const{return x;

    }

    GLint gety() const{return y;

    }void incrementx(){

    x++;

    }void decrementy(){y--;

    }};

    void init(void);void DibujaLinea(void);void setPixel(GLint xCoord, GLint yCoord);void circleMidpoint(GLint xc, GLint yc, GLint radius);void circlePlotPoints(GLint xc, GLint yc, screenPt circPt);

    void main(int argc, char** argv){

    glutInit(&argc, argv);glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);glutInitWindowPosition(50, 100); // POSICION INICIALglutInitWindowSize(400, 400); // TAMAO DE VENTANAglutCreateWindow("CIRCULO");init();glutDisplayFunc(DibujaLinea);glutMainLoop();

    }

  • 7/25/2019 Dibujado de circulos OpenGL

    6/11

    CIRCULO

    6

    void init(void){

    glClearColor(1.0, 1.0, 1.0, 0.0); //Estamos asignando un color a la ventana,se basa en RGB + 1 color alfa.

    glMatrixMode(GL_PROJECTION);gluOrtho2D(0.0, 400.0, 0.0, 400.0);

    }

    void DibujaLinea(void){

    glClear(GL_COLOR_BUFFER_BIT); // Visualizamos el color de la ventanaglColor3f(0.0, 0.0, 1.0);circleMidpoint(200, 200, 50);glFlush();

    }

    void setPixel(GLint xCoord, GLint yCoord){

    glBegin(GL_POINTS);glVertex2i(xCoord, yCoord);glEnd();

    }

    void circleMidpoint(GLint xc, GLint yc, GLint radius){

    screenPt circPt;

    GLint p = 1 - radius;

    circPt.setCoords(0, radius);

    void circlePlotPoints(GLint, GLint, screenPt);

    circlePlotPoints(xc, yc, circPt);

    while (circPt.getx() < circPt.gety()){

    circPt.incrementx();if (p < 0)

    p += 2 * circPt.getx() + 1;else{

    circPt.decrementy();p += 2 * (circPt.getx() - circPt.gety()) + 1;

    }

    circlePlotPoints(xc, yc, circPt);}

    }

    void circlePlotPoints(GLint xc, GLint yc, screenPt circPt){

    setPixel(xc + circPt.getx(), yc + circPt.gety());setPixel(xc - circPt.getx(), yc + circPt.gety());setPixel(xc + circPt.getx(), yc - circPt.gety());setPixel(xc - circPt.getx(), yc - circPt.gety());

  • 7/25/2019 Dibujado de circulos OpenGL

    7/11

    CIRCULO

    7

    setPixel(xc + circPt.gety(), yc + circPt.getx());setPixel(xc - circPt.gety(), yc + circPt.getx());setPixel(xc + circPt.gety(), yc - circPt.getx());setPixel(xc - circPt.gety(), yc - circPt.getx());

    }

    Aqu se muestra lo que se imprime en pantalla:

  • 7/25/2019 Dibujado de circulos OpenGL

    8/11

    CIRCULO

    8

    ALGORITMO O MTODO CON TRIGONOMTRICAS

    Y aqu se muestra el mtodo en el cual se puede observar el cdigo:

    #include "stdafx.h"#include #include #include

    void init(void);void DibujaLinea(void);void circulotrig(float xc, float yc, float rad);

    float xc, yc, rad, calx, caly;

    void main(int argc, char** argv){

    glutInit(&argc, argv);glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);glutInitWindowPosition(50, 100); // POSICION INICIALglutInitWindowSize(400, 300); // TAMAO DE VENTANAglutCreateWindow("CIRCULO");init();glutDisplayFunc(DibujaLinea);glutMainLoop();

    }

    void init(void){

    glClearColor(1.0, 1.0, 1.0, 0.0); //Estamos asignando un color a la ventana,

    se basa en RGB + 1 color alfa.glMatrixMode(GL_PROJECTION);gluOrtho2D(0.0, 400.0, 0.0, 300.0);

    }

    void DibujaLinea(void){

    glClear(GL_COLOR_BUFFER_BIT); // Visualizamos el color de la ventanacirculotrig(200, 150, 50);glFlush();

    }

    void circulotrig(float xc, float yc, float rad){

    glColor3f(1.0, 0.0, 0.0);glBegin(GL_POINTS);for (float i = 0; i < 360; i++){

    calx = rad*cos(i) + xc;caly = rad*sin(i) + yc;

    glVertex2f(calx, caly);}

  • 7/25/2019 Dibujado de circulos OpenGL

    9/11

    CIRCULO

    9

    glEnd();glFlush();

    }

    Aqu se muestra lo que se imprime en pantalla:

  • 7/25/2019 Dibujado de circulos OpenGL

    10/11

    CIRCULO

    10

    CONCLUSIN

    En conclusin se puede decidir por el mtodo de las trigonomtricas ya que se utiliza

    menos cdigo, es ms sencillo y es menos propenso a errores.

  • 7/25/2019 Dibujado de circulos OpenGL

    11/11

    CIRCULO

    11

    BibliografaHEARN, D., & BAKER, M. P. (2006). GRAFICOS POR COMPUTADORA CON OPENGL.MADRID:

    PEARSON.