0% encontró este documento útil (0 votos)
68 vistas3 páginas

OPENGL

Este documento contiene el código C++ para dibujar diferentes figuras geométricas 2D utilizando OpenGL. Define funciones para dibujar líneas rectas usando los algoritmos DDA y punto medio, círculos usando el algoritmo punto medio, e ingresa valores iniciales para dibujar una línea. La función principal inicializa OpenGL y llama a funciones para limpiar, dibujar y redibujar la ventana a medida que cambia de tamaño.
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como TXT, PDF, TXT o lee en línea desde Scribd
0% encontró este documento útil (0 votos)
68 vistas3 páginas

OPENGL

Este documento contiene el código C++ para dibujar diferentes figuras geométricas 2D utilizando OpenGL. Define funciones para dibujar líneas rectas usando los algoritmos DDA y punto medio, círculos usando el algoritmo punto medio, e ingresa valores iniciales para dibujar una línea. La función principal inicializa OpenGL y llama a funciones para limpiar, dibujar y redibujar la ventana a medida que cambia de tamaño.
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como TXT, PDF, TXT o lee en línea desde Scribd

//#include <iostream.

h>
#include <math.h>
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
void circunferencia_punto_medio(int,int,int);
void init(void);
void display(void);
void reshape(int, int);
void abasico(int, int, int, int);
void ingresoDatos(void);
void recta_dda(int, int, int, int);
void recta_punto_medio(int, int, int, int);
int px0,py0,px1,py1;
int main(int argc, char** argv)
{
ingresoDatos();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(200, 200);
glutInitWindowPosition(100,100);
glutCreateWindow("Algoritmo Basico");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
void init(void)
{
glClearColor(1.0,1.0,1.0,0.0); //parametros: rojo, amarillo y azul, el cuarto es el

glShadeModel(GL_FLAT);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix(); // salva el estado actual de la matriz
glColor3f(0,0,1); // Azul
glPointSize(2); // Fije el grosor de pixel = 2
abasico(px0, py0, px1, py1);
glColor3f(1,0,0);
recta_dda(px0, py0+5, px1, py1+5);
glColor3f(0,0,0);
recta_punto_medio(px0, py0+10, px1, py1+10);
circunferencia_punto_medio(0,0,45);
glColor3f(0,0,1);
circunferencia_punto_medio(10,20,25);
glColor3f(1,0,0);
circunferencia_punto_medio(-10,-20,25);
glPopMatrix(); // recupera el estado del matriz
glFlush();
}
void reshape(int w, int h)
{
glViewport(0,0,(GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0, 50.0, -50.0, 50, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void abasico(int x0,int y0,int x1,int y1)
{
int x;
float dx, dy, m, b, y;
dx = x1 - x0;
dy = y1 - y0;
m = dy/dx;
b = y0 - m*x0;
y = y0;
glBegin(GL_POINTS);
for(x=x0; x<=x1; x++)
{
y = m*x + b;
//cout << x <<" "<< y <<" "<< (int)ceil(y-0.5) <<"\n";
glVertex2f(x, (int)ceil(y-0.5));
}
glEnd();
}

void circunferencia_punto_medio(int xc,int yc,int R)


{
// discretizacion valida en el II octante
int x=0;
int y=R,d=1-R;
glBegin(GL_POINTS);
glVertex2f(x+xc,y+yc);
while (x<y){
if (d<0)
d+=2*x+3;
else{
d+=2*(x-y)+5;
y--;
}
x++;
glVertex2f(x+xc,y+yc);
glVertex2f(-x+xc,y+yc);
glVertex2f(-y+xc,x+yc);
glVertex2f(-y+xc,-x+yc);
glVertex2f(-x+xc,-y+yc);
glVertex2f(x+xc,-y+yc);
glVertex2f(y+xc,-x+yc);
glVertex2f(y+xc,x+yc);
}
glEnd();
}

void recta_punto_medio(int x0, int y0, int x1, int y1)


{
int dx, dy, dE, dNE, d, x, y;
dx = x1 - x0;
dy = y1- y0;
d = 2*dy - dx;
dE = 2*dy;
dNE = 2*(dy - dx);
x = x0;
y = y0;
glBegin(GL_POINTS);
glVertex2f(x,y);
while (x<x1)
{
if(d<=0){
d += dE;
x++;
}
else{
d += dNE;
x++;
y++;
}
glVertex2f(x,y);
}
glEnd();
}

void recta_dda(int x0, int y0, int x1, int y1)


{
int x;
float dx, dy, m,y;
dx = x1-x0;
dy = y1-y0;
m = dy/dx;
y = y0;
glBegin(GL_POINTS);
for(x=x0; x<=x1; x++)
{
glVertex2f(x, (int)ceil(y-0.5));
y += m;
}
glEnd();
}

// opcional
void ingresoDatos(void)
{
// lo que Ud desee
px0=-40;
py0=-40;
px1=40;
py1=0;
}

También podría gustarte