그림판 만드는 중인데요. 초반이라
지금 소스는 간단해요 Drawmode함수를 통해서 그리고자하는 도형을 선택했고, 그걸 MyDisplay함수에서
if문을 통해 도형을 불러와요.
근데 지금은 한번그리면 계속 그린것이 유지가 안되고 리셋됬다가 다시그리고 , 이런식인데
그린것들을 계속 유지하려면 어떻게 해야하나요?
대충 찾아본바로는 glPushMatrix()랑 glPopMatrix() 이용하는거같은데 잘안되네요 ㅜㅜ
저 소스에 어느곳에 넣어야되는지도 모르겠고.. 도와주셔요
#include <GL\glut.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int DrawMode1, DrawMode2, DrawMode3, DrawMode4, DrawMode5;
GLint TopLeftX, TopLeftY, BottomRightX, BottomRightY;
void DrawMode(unsigned char KeyPressed, int X, int Y) {
switch (KeyPressed){
case 'w':{ //선 선택
DrawMode1=1;
DrawMode2=0;
DrawMode3=0;
DrawMode4=0;
DrawMode5=0;
}
break;
case 'e':{ //사각형 선택
DrawMode1=0;
DrawMode2=1;
DrawMode3=0;
DrawMode4=0;
DrawMode5=0;
}
break;
case 'r':{ //이등변삼각형 선택
DrawMode1=0;
DrawMode2=0;
DrawMode3=1;
DrawMode4=0;
DrawMode5=0;
}
break;
case 't':{ //직각삼각형 선택
DrawMode1=0;
DrawMode2=0;
DrawMode3=0;
DrawMode4=1;
DrawMode5=0;
}
break;
case 'y':{ //원 선택
DrawMode1=0;
DrawMode2=0;
DrawMode3=0;
DrawMode4=0;
DrawMode5=1;
}
break;
case 'q':
glClear(GL_COLOR_BUFFER_BIT);
break;
}
}
void MyDisplay( ){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.5, 0.5, 0.5);
glViewport(0, 0, 500, 500);
if(DrawMode1==1){
glBegin(GL_LINES); //선
glVertex3f(TopLeftX/500.0,(500.0-TopLeftY)/500.0, 0.0);
glVertex3f(BottomRightX/500.0, (500.0-BottomRightY)/500.0, 0.0);
glEnd( );
glFlush( );
}
else if(DrawMode2==1){
glBegin(GL_POLYGON); //사각형
glVertex3f(TopLeftX/500.0,(500.0-TopLeftY)/500.0, 0.0);
glVertex3f(TopLeftX/500.0,(500.0-BottomRightY)/500.0 , 0.0);
glVertex3f(BottomRightX/500.0, (500.0-BottomRightY)/500.0, 0.0);
glVertex3f(BottomRightX/500.0, (500.0-TopLeftY)/500.0, 0.0);
glEnd();
glFlush();
}
else if(DrawMode3==1){
glBegin(GL_POLYGON); //이등변삼각형
glVertex3f(TopLeftX/500.0,(500.0-TopLeftY)/500.0, 0.0);
glVertex3f(BottomRightX/500.0, (500.0-BottomRightY)/500.0, 0.0);
glVertex3f(TopLeftX/500.0-((BottomRightX-TopLeftX)/500.0),(500.0-BottomRightY)/500.0, 0.0);
glEnd();
glFlush();
}
else if(DrawMode4==1){
glBegin(GL_TRIANGLES); //직각삼각형
glVertex3f(TopLeftX/500.0,(500.0-TopLeftY)/500.0, 0.0);
glVertex3f(TopLeftX/500.0,(500.0-BottomRightY)/500.0 , 0.0);
glVertex3f(BottomRightX/500.0, (500.0-BottomRightY)/500.0, 0.0);
glVertex3f(BottomRightX/500.0, (500.0-TopLeftY)/500.0, 0.0);
glEnd( );
glFlush( );
}
/*
else if(DrawMode5==1){
glutWireSphere(TopLeftX/500*2.0,(500.0-BottomRightY)/500,0.0); //원glutWireSphere(size/2.0, 15, 15);
glEnd();
glFlush();
}
*/
}
void MyMouseClick(GLint Button, GLint State, GLint X, GLint Y){
if(Button==GLUT_LEFT_BUTTON && State==GLUT_DOWN){
TopLeftX = X;
TopLeftY = Y;
}
}
void MyMouseMove(GLint X, GLint Y) {
BottomRightX = X;
BottomRightY = Y;
glutPostRedisplay( );
}
int main(int argc, char** argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("OpenGL Drawing Example");
glClearColor (1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
glutDisplayFunc(MyDisplay);
glutKeyboardFunc(DrawMode);
glutMouseFunc(MyMouseClick);
glutMotionFunc(MyMouseMove);
glutMainLoop( );
return 0;
}