OpenGL 2009. 3. 17. 23:24

#include <glut.h>    
#include <gl.h>      
#include <glu.h>
#include <stdlib.h>
#include <stdio.h>

struct vertex
{
 GLint x,y,z;
};

int vertexcnt=0;

struct vertex vertice[10000];
GLint TopLeftX, TopLeftY, BottomRightX, BottomRightY;
int type = 0;
 
void MyDisplay( ){  
 int cnt=0;
    glViewport(0, 0, 300, 300);
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.5, 0.5, 0.5);

 // 여기에서 드로잉
 if(type == 0)
 {
  glPointSize(3.0f);
  for(cnt=0; cnt<vertexcnt; cnt++)
   {    
    glBegin(GL_POINTS);
    glVertex3f((GLfloat)vertice[cnt].x/300,(GLfloat)vertice[cnt].y/300,0.0);    
    glEnd();  
   }  
 }

 if(type == 1)
 {
  glLineWidth(1.0f);
  if(vertexcnt >1)
  for(cnt=0; cnt<vertexcnt-1; cnt++)
   {    
    glBegin(GL_LINES);  
    glVertex2f((GLfloat)vertice[cnt].x/300,(GLfloat)vertice[cnt].y/300);    
    glVertex2f((GLfloat)vertice[cnt+1].x/300,(GLfloat)vertice[cnt+1].y/300);
    glEnd();  
   }  
 }

 if(type == 2)
 {
   if(vertexcnt>2)
   for(cnt=0; cnt<vertexcnt; cnt+=3)
   {    
    glBegin(GL_TRIANGLES);
    if(cnt+2 < vertexcnt)
    {
     glVertex3f((GLfloat)vertice[cnt].x/300,(GLfloat)vertice[cnt].y/300,0.0);    
     glVertex3f((GLfloat)vertice[cnt+1].x/300,(GLfloat)vertice[cnt+1].y/300,0.0);    
     glVertex3f((GLfloat)vertice[cnt+2].x/300,(GLfloat)vertice[cnt+2].y/300,0.0);    
    }
    glEnd();    
   }  
 }
 if(type == 3)
 {  
   glBegin(GL_POLYGON);
   for(cnt=0; cnt<vertexcnt; cnt++)
   {    
    glVertex3f((GLfloat)vertice[cnt].x/300,(GLfloat)vertice[cnt].y/300,0.0);      
   }
   glEnd();
 }
    glFlush( );  
}

// 마우스 클릭이벤트
void MyMouseClick(GLint Button, GLint State, GLint X, GLint Y){
    int cnt=0;
 if(Button==GLUT_LEFT_BUTTON && State==GLUT_DOWN){
  vertice[vertexcnt].x=X;
  vertice[vertexcnt].y=Y;
  vertice[vertexcnt++].z=0;
  printf("[%d] (%d, %d %d)\n",vertexcnt-1,X,Y,0);
    }
 glutPostRedisplay();
}

void MyMainMenu(int entryID){    
    if(entryID == 1)
 {
        vertexcnt=0;   // 초기화
  system("cls");
 }
 if(entryID == 2)
        exit(0);    //프로그램 종료

    glutPostRedisplay();
}  

void MySubMenu(int entryID){    
    if(entryID == 1)
        type = 0;   // 점그리기
    else if (entryID == 2)
  type = 1;   // 선그리기
 else if (entryID == 3)
  type = 2;   // 삼각형
 else if (entryID == 4)
  type = 3;   // 폴리곤
    glutPostRedisplay();
}  
void setup()
{
 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
 glColor3f(0.0f, 1.0f, 0.0f);
}
int main(int argc, char** argv) {
    glutInit(&argc,argv);  
    glutInitDisplayMode(GLUT_RGB);
    glutInitWindowSize(300, 300);     
    glutInitWindowPosition(0, 0);
    glutCreateWindow("OpenGL Mj's Report");
    glClearColor (1.0, 1.0, 1.0, 1.0);  
    glMatrixMode(GL_PROJECTION);   
    glLoadIdentity( );   
 glOrtho(0.0, 1.0, 1.0, 0.0, -1.0, 1.0); 
 // 서브메뉴등록
    GLint MySubMenuID = glutCreateMenu(MySubMenu);
    glutAddMenuEntry("Points", 1);
    glutAddMenuEntry("Lines", 2);
 glutAddMenuEntry("Triangle", 3);
 glutAddMenuEntry("Polygones", 4);

 // 메뉴등록
    GLint MyMainMenuID = glutCreateMenu(MyMainMenu);    
    glutAddSubMenu("Drawing", MySubMenuID);  
 glutAddMenuEntry("Clear", 1);
    glutAddMenuEntry("Exit", 2);
 // 버튼에대한 메뉴등록
    glutAttachMenu(GLUT_RIGHT_BUTTON);

 // 드로잉 펑션 등록
    glutDisplayFunc(MyDisplay);   
 setup();

 // 마우스 클릭이벤트 콜백함수 등록
 glutMouseFunc(MyMouseClick);  
    glutMainLoop( );  
    return 0;  
}

posted by Sense.J
: