求用opengl在指定点(鼠标点击确定)做正n边形的程序
2个回答
展开全部
鼠标的实现可以用glut的glutMouseFunc或者用WinProc里的鼠标消息来达到,至于绘画部分建议网上找下基本教程,glBegin和glVertex什么的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <gl/glut.h>
GLdouble pt[3]={0};
int st=0;
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glPointSize(2);
if(st){
int d = 10;
glBegin(GL_LINE_LOOP);
glVertex2d(pt[0]+d,pt[1]+d);
glVertex2d(pt[0]+d,pt[1]-d);
glVertex2d(pt[0]-d,pt[1]-d);
glVertex2d(pt[0]-d,pt[1]+d);
glEnd();
glBegin(GL_POINTS);
glVertex2dv(pt);
glEnd();
}
glPopMatrix();
glutSwapBuffers();
}
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.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void toWorldCoords(int x, int y, GLdouble w[3])
{
GLint viewport[4];
GLdouble mv[16],proj[16];
GLint realy;
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, mv);
glGetDoublev(GL_PROJECTION_MATRIX, proj);
realy = viewport[3] - (GLint) y - 1;
gluUnProject(x,realy,0,mv,proj,viewport, w,w+1,w+2);
}
void mouse(int button, int state, int x, int y)
{
switch (button) {
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
{
toWorldCoords(x,y,pt);
glutPostRedisplay();
st=1;
}
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
GLdouble pt[3]={0};
int st=0;
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glPointSize(2);
if(st){
int d = 10;
glBegin(GL_LINE_LOOP);
glVertex2d(pt[0]+d,pt[1]+d);
glVertex2d(pt[0]+d,pt[1]-d);
glVertex2d(pt[0]-d,pt[1]-d);
glVertex2d(pt[0]-d,pt[1]+d);
glEnd();
glBegin(GL_POINTS);
glVertex2dv(pt);
glEnd();
}
glPopMatrix();
glutSwapBuffers();
}
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.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void toWorldCoords(int x, int y, GLdouble w[3])
{
GLint viewport[4];
GLdouble mv[16],proj[16];
GLint realy;
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, mv);
glGetDoublev(GL_PROJECTION_MATRIX, proj);
realy = viewport[3] - (GLint) y - 1;
gluUnProject(x,realy,0,mv,proj,viewport, w,w+1,w+2);
}
void mouse(int button, int state, int x, int y)
{
switch (button) {
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
{
toWorldCoords(x,y,pt);
glutPostRedisplay();
st=1;
}
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询