資源簡介
一、實驗目的:
掌握利用OpenGL函數進行鼠標、鍵盤操作,創建菜單等
二、實驗內容:
1、用鼠標拖動畫直線,線段終點始終跟隨鼠標移動;畫線可以利用實驗1或實驗2中已經實現的畫線功能;
2、使用菜單界面修改直線的顏色;
3、利用鍵盤控制直線在屏幕上移動;
三、實現效果及步驟(或流程)
1. 主要是依靠函數glutMouseFunc(mouseclick);返回鼠標光標在窗口的位置坐標,以及glutMotionFunc(screenmotion);處理當鼠標鍵摁下時,鼠標拖動的事件。
主要方法代碼如下:
void mouseclick(int button, int state, int x, int y) {
switch (button) {
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN) {
old_x = x;
old_y = 500 - y;
mouse_type = 1;
}
else if (state == GLUT_UP) { //左鍵被松開
mouse_type = 0;
glutPostRedisplay(); //標記當前窗口需要重新繪制
}
break;
case GLUT_MIDDLE_BUTTON:
break;
}
}
void screenmotion(int x, int y) {
if (mouse_type == 1) {
new_x = x;
new_y = 500 - y;
glutPostRedisplay(); //標記當前窗口需要重新繪制
}
2.在main方法中創建菜單
glutCreateMenu(colorMenu);
glutAddMenuEntry("red", 1);
glutAddMenuEntry("green", 2);
glutAddMenuEntry("blue", 3);
glutAddMenuEntry("black", 4);
glutAttachMenu(GLUT_RIGHT_BUTTON);
并把指定鼠標右鍵來選擇菜單
菜單被選中時調用的修改直線顏色代碼如下:
void colorMenu(GLint colorOption) {
switch (colorOption)
{
case 1:
red = 1; green = 0; blue = 0;
break;
case 2:
red = 0; green = 1; blue = 0;
break;
case 3:
red = 0; green = 0; blue = 1;
break;
case 4:
red = 0; green = 0; blue = 0;
break;
}
glutPostRedisplay();
}
3.在main方法中用函數glutSpecialFunc(keyboard); 指定對于方向鍵按下時調用的函數keyboard,該方法具體實現代碼如下:
void keyboard(int key, int x, int y) {
switch (key)
{
case GLUT_KEY_DOWN:
old_y = old_y - 10;
new_y = new_y - 10;
glutPostRedisplay();
break;
case GLUT_KEY_LEFT:
old_x = old_x - 10;
new_x = new_x - 10;
glutPostRedisplay();
break;
case GLUT_KEY_RIGHT:
old_x = old_x 10;
new_x = new_x 10;
glutPostRedisplay();
break;
case GLUT_KEY_UP:
old_y = old_y 10;
new_y = new_y 10;
glutPostRedisplay();
break;
}
}
代碼片段和文件信息
//?ex3.cpp?:?Defines?the?entry?point?for?the?console?application.
//
#include?“stdafx.h“
#include?
#include?
#include?
#include?
#include?
#include?
using?namespace?std;
typedef?vector?list;
//vector?ls;
int?old_x?old_y?new_x?new_y;
int?mouse_type;
GLfloat?red?=?0?green?=?0?blue?=?0;
/*?breseman畫線算法?*/
void?line(int?x1?int?y1?int?x2?int?y2)?{
glBegin(GL_POINTS);
if?(x1?==?x2?&&?y1?==?y2)?{
glVertex2i(x1?y1);
return;
}
if?(x1?>?x2)?{
swap(x1?x2);
swap(y1?y2);
}
glVertex2d(x1?y1);
int?dy?=?abs(y2?-?y1);
int?dx?=?abs(x2?-?x1);
if?(dx?>=?dy)?{
int?incy?=?(dy?!=?0???dy?/?(y2?-?y1)?:?0);
int?p?=?2?*?dy?-?dx;
int?dy2?=?2?*?dy;
int?dd2?=?2?*?(dy?-?dx);
i
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
????..A..H.?????33280??2017-05-11?22:16??ex3\.vs\ex3\v14\.suo
?????文件??????77824??2017-04-20?00:25??ex3\Debug\ex3.exe
?????文件?????542016??2017-04-20?00:25??ex3\Debug\ex3.ilk
?????文件????2256896??2017-04-20?00:25??ex3\Debug\ex3.pdb
?????文件?????218624??2015-10-14?09:58??ex3\Debug\freeglut.dll
?????文件?????337408??2015-10-14?09:58??ex3\Debug\glew32.dll
?????文件?????350720??2015-10-14?09:58??ex3\Debug\glewinfo.exe
?????文件??????43520??2015-10-14?09:58??ex3\Debug\glfw3.dll
?????文件?????222720??2015-10-14?09:58??ex3\Debug\visualinfo.exe
?????文件????????187??2017-04-20?00:25??ex3\ex3\Debug\ex3.log
?????文件??????60199??2017-04-20?00:25??ex3\ex3\Debug\ex3.obj
?????文件????4128768??2017-04-19?23:30??ex3\ex3\Debug\ex3.pch
?????文件???????3510??2017-04-20?00:25??ex3\ex3\Debug\ex3.tlog\CL.command.1.tlog
?????文件??????27782??2017-04-20?00:25??ex3\ex3\Debug\ex3.tlog\CL.read.1.tlog
?????文件????????838??2017-04-20?00:25??ex3\ex3\Debug\ex3.tlog\CL.write.1.tlog
?????文件????????215??2017-04-20?00:25??ex3\ex3\Debug\ex3.tlog\ex3.lastbuildstate
?????文件???????1852??2017-04-20?00:25??ex3\ex3\Debug\ex3.tlog\li
?????文件???????3990??2017-04-20?00:25??ex3\ex3\Debug\ex3.tlog\li
?????文件????????444??2017-04-20?00:25??ex3\ex3\Debug\ex3.tlog\li
?????文件??????12248??2017-04-19?23:30??ex3\ex3\Debug\stdafx.obj
?????文件?????855040??2017-04-20?00:25??ex3\ex3\Debug\vc140.idb
?????文件?????561152??2017-04-20?00:25??ex3\ex3\Debug\vc140.pdb
?????文件???????3995??2017-04-20?00:25??ex3\ex3\ex3.cpp
?????文件????????219??2017-04-18?20:00??ex3\ex3\ex3.Shared\App.xaml
?????文件???????4238??2017-04-18?20:00??ex3\ex3\ex3.Shared\App.xaml.cpp
?????文件????????813??2017-04-18?20:00??ex3\ex3\ex3.Shared\App.xaml.h
?????文件???????1521??2017-04-18?20:00??ex3\ex3\ex3.Shared\ex3.Shared.vcxitems
?????文件????????545??2017-04-18?20:00??ex3\ex3\ex3.Shared\ex3.Shared.vcxitems.filters
?????文件?????????87??2017-04-18?20:00??ex3\ex3\ex3.Shared\pch.cpp
?????文件????????150??2017-04-18?20:00??ex3\ex3\ex3.Shared\pch.h
............此處省略134個文件信息
評論
共有 條評論