welcome

Selasa, 09 April 2013

praktikum ke 4 grafika komputer

membuat objek garis dengan DDA dan Bresenham


#include < windows.h >
#include < stdio.h >
#include < stdlib.h >
#include < string.h >
#include < stdarg.h >
#include < glut.h >
#include < math.h >

void display(void)
{
//set display-window background color to white
glClearColor(1.0,1.0,1.0,0.0);
//set projection parameters
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 300.0, 0.0, 300.0);
}
void setPixel(GLint xCoordinate, GLint yCoordinate)
{
glBegin(GL_POINTS);
glVertex2i(xCoordinate, yCoordinate);
glEnd();
glFlush();
}
//Procedure Bresenham line-drawing untuk |m| < 1.0
void lineBres(GLint x0, GLint y0, GLint xEnd, GLint yEnd)
{
GLint dx = (float)fabs((float) xEnd - x0);
GLint dy = (float)fabs((float) yEnd - y0);
GLint p = 2 * dy - dx;
GLint twoDy = 2 * dy;
GLint twoDyMinusDx = 2 * (dy - dx);
GLint x,y;
//determine which endpoint to use as start position
if (x0 > xEnd){
x = xEnd;
y = yEnd;
xEnd = x;
} else {
  x = x0;
  y = y0;
  }
  setPixel(x,y);
  while (x<xEnd){
  x++;
  if (p<0)
  p += twoDy;
  else {
  y++;
  p += twoDyMinusDx;
  }
  setPixel(x,y);
  }
  }
/*void drawMyLine(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glPointSize(4.0);
GLint x0 = 100;
GLint y0 = 150;
GLint xEnd = 200;
GLint yEnd = 250;
lineBres(x0,y0,xEnd, yEnd);
}*/

void drawMyLine(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.1,0.0);
glPointSize(4.0);
GLint x0 = 50;
GLint y0 = 50;
GLint xEnd = 200;
GLint yEnd = 200;
lineBres(x0,y0,xEnd, yEnd);

glColor3f(0.0,1.0,0.0);
glPointSize(4.0);
GLint x1 = 100;
GLint y1 = 50;
GLint x2 = 200;
GLint y2 = 200;
lineBres(x1,y1,x2, y2);

glColor3f(0.0,0.0,1.0);
glPointSize(4.0);
GLint x3 = 150;
GLint y3 = 50;
GLint x4 = 200;
GLint y4 = 200;
lineBres(x3,y3,x4, y4);
}
int main(int argc, char** argv)
{
//initialize GLUT
glutInit(&argc, argv);
//initialize display mode
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
//set display-window width & height
glutInitWindowSize(400,400);
//set display-window upper-left position
glutInitWindowPosition(0,0);
//create display-window with a title
glutCreateWindow("Digital Differential Analiyzer Algorithm");
//initialize OpenGL
display();
//call graphics to be diplayes on the window
glutDisplayFunc(drawMyLine);
//glutDisplayFunc(drawMyLine1);
//display everything and wait
glutMainLoop();
return 0;
}




     //main
int main (int argc, char **argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(400,400);
glutInitWindowPosition(100,100);
glutCreateWindow("circle.cpp");
glClearColor(0.0,0.0,0.0,0.0);
glutDisplayFunc(disp);
glutKeyboardFunc(keyb);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}


// disp
void disp(void){
double angle;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE_LOOP);
for (int i=0; i<num_lines1; i++){
angle = i*2*3.14/num_lines1;
glVertex2f(cos(angle),sin(angle));
}
glEnd();

glBegin(GL_LINE_LOOP);
    for(int i =0;i<num_lines;i++){
            angle = i*2*3.14/num_lines;
        glVertex2f(cos(angle)/5,sin(angle));
    }
  glEnd();

  glBegin(GL_LINE_LOOP);
    for(int i =0;i<num_lines;i++){
            angle = i*2*3.14/num_lines;
        glVertex2f(cos(angle)/5,sin(angle)/5);
    }
  glEnd();

glutSwapBuffers();
}

// keyb
void keyb (unchar k, int x, int y){
switch (k){
case 'q' :
exit (0);
break;
case '+':
if(num_lines < 99){
num_lines++;
cout << "Circle consist of " << num_lines << "lines" << endl;
glutPostRedisplay();
}
break;
case '-':
if(num_lines > 3) {
num_lines --;
cout << "Circle consist of " << num_lines << "lines" << endl;
glutPostRedisplay();
}
break;
}
}

// reshape
void reshape (int x, int y){
if (x<y)
glViewport(0, (y-x) / 2, x, x);
Else
glViewport((x-y) / 2, 0, y, y);
}