【OpenGL】void glutInitWindowPosition(int x, int y)

void glutInitWindowPosition(int x, int y);//设置初始窗口的位置(窗口左上角相对于桌面坐标(x,y))

若为(0,0),则窗口左上角位于桌面左上角。

x和y的单位均为像素。

 

工作环境为#include "stdAfx.h" #include "circle.h" /*以下常量定义鼠标状态,目前之后两个状态,分别是鼠标左键被按下, 和默认正常状态,没有任何按键被按下 */ #define MOUSE_LEFT_BUTTION_DOWN 0X0001 //定义鼠标状 #define MOUSE_NORMAL 0x0000 //定义鼠标状态 int mouse_state = MOUSE_NORMAL; //定义2维空间内的点(x,y) typedef struct Point2D { int x; int y; }Point2D; //定义鼠标开始位置和结束位置 Point2D mouseStartPos,mouseEndPos; //定义显示函数 void display() { glClearColor(1.0f,1.0f,1.0f,0.0f); glClear(GL_COLOR_BUFFER_BIT); printf("%d\n",mouse_state); if(mouse_state == MOUSE_LEFT_BUTTION_DOWN) { float r = sqrt((mouseEndPos.x-mouseStartPos.x)*(mouseEndPos.x-mouseStartPos.x) + (mouseEndPos.y-mouseStartPos.y)*(mouseEndPos.y-mouseStartPos.y)); drawCircleBresenham(mouseStartPos.x,mouseStartPos.y,r); } glFlush(); printf("call display\n"); } //鼠标处理函数 void mouseHandler(int button,int state,int x,int y) { switch(button) { case GLUT_LEFT_BUTTON: switch(state) { case GLUT_UP: mouse_state = MOUSE_NORMAL; break; case GLUT_DOWN: printf("mosue down\n"); mouseStartPos.x = x; //记录鼠标开始位置 mouseStartPos.y = y; mouse_state = MOUSE_LEFT_BUTTION_DOWN; break; default: break; } break; case GLUT_RIGHT_BUTTON: break; case GLUT_MIDDLE_BUTTON: break; default: break; } } //鼠标按下并且移动时候,调用此函数 void mouseMotionHandler(int x,int y) { //记录鼠标结束位置 mouseEndPos.x = x; mouseEndPos.y = y; if(mouse_state == MOUSE_LEFT_BUTTION_DOWN) { float r = sqrt((mouseEndPos.x-mouseStartPos.x)*(mouseEndPos.x-mouseStartPos.x) + (mouseEndPos.y-mouseStartPos.y)*(mouseEndPos.y-mouseStartPos.y)); drawCircleBresenham(mouseStartPos.x,mouseStartPos.y,r); } } void main () { glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE); glutInitWindowPosition(600,400); glutInitWindowSize(400,400); glutCreateWindow("OpenGL"); glutDisplayFunc(display); //设置显示函数 glutMouseFunc(mouseHandler); //设置鼠标处理函数 glutMotionFunc(mouseMotionHandler);//当鼠标按下并移动时候,会调用此函数 gluOrtho2D(0.0f,400.0f,400.0f,0.0f); glutMainLoop(); }
最新发布
06-09
#include <glut.h> #include <stdio.h> #include <stdlib.h> #define LEFT_EDGE 1 #define RIGHT_EDGE 2 #define BOTTOM_EDGE 4 #define TOP_EDGE 8 struct Rectangle { float xmin, xmax, ymin, ymax; }; Rectangle rect; int x0, y0, x1, y1; void LineGL(int x0, int y0, int x1, int y1) { glBegin(GL_LINES); glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(x0, y0); glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(x1, y1); glEnd(); } //求出坐标点的Cohen-SutherLand编码 int CompCode(int x, int y, Rectangle rect) { int code = 0000; if (y < rect.ymin) code = code | 4; else if (y > rect.ymax) code = code | 8; else if (x < rect.xmin) code = code | 1; else if (x < rect.xmax) code = code | 2; return code; } int cohenSutherland(Rectangle rect, int &x0, int & y0, int &x1, int &y1) { if (CompCode(x,y,rect) & LEFT_EDGE) { y = y0 + (y1 - y0) * (rect.xmin - x0) / (x1 - x0); x = (float)rect.xmin; } return 0; } void Display() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(0.5f, 0.0f, 0.0f); glRectf(rect.xmin, rect.ymin, rect.xmax, rect.ymax); LineGL(x0, y0, x1, y1); glFlush(); } void Init() { glClearColor(0.0, 0.0, 0.0, 0.0); glShadeModel(GL_FLAT); //设定要裁剪的直线和用于裁剪的矩形 rect.xmin = 100; rect.xmax = 500; rect.ymin = 100; rect.ymax = 400; x0 = 0, y0 = 0, x1 = 600, y1 = 300; printf("Press key 'c' to Clip!\nPress key 'r' to Restore!\n"); } void Reshape(int w, int h) { glViewport(0, 0, (GLsizei)w, (GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h); } void keyboard(unsigned char key, int x, int y) { switch (key) { case 'c': cohenSutherland(rect, x0, y0, x1, y1); glutPostRedisplay(); break; case 'r': Init(); glutPostRedisplay(); break; case 'x': exit(0); break; default: break; } } int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutInitWindowPosition(100, 100); glutInitWindowSize(640, 480); glutCreateWindow("CohenSutherland algorithm"); Init(); glutDisplayFunc(Display); glutReshapeFunc(Reshape); glutKeyboardFunc(keyboard); glutMainLoop(); return 0; }掌握Cohen-Sutherland裁剪算法的原理及算法,通过示范程序学习,利用OpenGL实现算法。 二、实验内容 (1)根据所给的示范程序,在计算机上编译运行,输出正确结果。 (2)根据给出的示范程序,补全程序其他分区的实现。
05-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值