資源簡介
在VC++環境中調用opengl函數,在opengl窗口中捕捉鼠標位置,并將坐標位置顯示在窗口左下角

代碼片段和文件信息
#pragma?once
#include?
#include?
#include“glut.h“
//定義全局變量記錄當前鼠標的位置信息
static?int?initWidth?initHeight;
static?int?mousex?mousey;
//鼠標移動回調函數
void?mousemove(int?x?int?y)
{
mousex?=?x;
mousey?=?glutGet(GLUT_WINDOW_HEIGHT)?-?y;
glutPostRedisplay();
}
/*-------------------------------------------------------------------*//*!
*?\摘要? 在窗口中指定位置指定大小的矩形內顯示鼠標的當前位置
*?\參數?? x:???????????指定矩形的左小角的x坐標
????????????y:???????????指定矩形的左小角的y坐標
width:???????指定矩形的寬度
height:??????指定矩形的高度
win_width:???窗口的寬度
win_height:??窗口的高度
*?\返回值???
*?\標注???? 當前光標的坐標為:mousex?mousey
*//*-------------------------------------------------------------------*/
void?put_coordinate(float?x?float?y?float?width?float?height?float?win_width?float?win_height)
{
GLfloat?w_ratio?=?win_width?/?330;?
GLfloat?h_ratio?=?win_height?/?220;?
//正投影
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-win_width/2?win_width/2?-win_height/2?win_height/2?1.0?-1.0);
glViewport(00widthheight);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslatef(x-win_width/2?y-win_height/2+110*h_ratio?0);???????????//將要顯示的文字沿y軸上移110個單位(像素)
glScalef(w_ratio?h_ratio?1);????????????????????????????????????????//將要顯示的文字的xy坐標大小縮放至原來的0.1倍
//生成文字的固有大小為(330220)
glutStrokeCharacter(GLUT_STROKE_ROMAN?88);???????????????????//輸入要顯示文字“X”的ASC碼值
glutStrokeCharacter(GLUT_STROKE_ROMAN?58);???????????????????//輸入要顯示文字“:”的ASC碼值
glutStrokeCharacter(GLUT_STROKE_ROMAN?48+(mousex/100));??????//計算當前位置的x坐標的百位數字
glutStrokeCharacter(GLUT_STROKE_ROMAN?48+(mousex%100)/10);???//計算當前位置的x坐標的十位數字
glutStrokeCharacter(GLUT_STROKE_ROMAN?48+(mousex%10));???????//計算當前位置的x坐標的個位數字
glLoadIdentity();
glTranslatef(x-win_width/2?y-win_height/2?0);
glScalef(w_ratio?h_ratio?1);????????????????????????????????????????//將要顯示的文字的xy坐標大小縮放至原來的0.1倍
glutStrokeCharacter(GLUT_STROKE_ROMAN?89);???????????????????//輸入要顯示文字“Y”的ASC碼值
glutStrokeCharacter(GLUT_STROKE_ROMAN?58);???????????????????//輸入要顯示文字“:”的ASC碼值
glutStrokeCharacter(GLUT_STROKE_ROMAN?48+(mousey/100));??????//計算當前位置的y坐標的百位數字
glutStrokeCharacter(GLUT_STROKE_ROMAN?48+(mousey%100)/10);???//計算當前位置的y坐標的十位數字
glutStrokeCharacter(GLUT_STROKE_ROMAN?48+(mousey%10));???????//計算當前位置的y坐標的個位數字
glPopMatrix();
}
//將用戶坐標映射到窗口坐標;當窗口的大小改變時,該函數會被調用
void?ChangeSize(GLsizei?w?GLsizei?h)
{
glViewport(00wh);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//正投影
GLfloat?aspectRatio?=?(GLfloat)w?/?(GLfloat)(h+1)?;???????????//h+1防止除數為零
if(w<=h)
{
int?windowWidth?=?100;
int?windowHeight?=?100/aspectRatio;
glOrtho(-100.0?100.0?-windowHeight?windowHeight?1.0?-1.0);
}
else
{
int?windowWidth?=?100*aspectRatio;
int?windowHeight?=?100;
glOrtho(-windowWidth?windowWidth?-100.0?100.0?1.0?-1.0);
}
}
void?RenderScene(?void?)
{
//設置背景顏色
glClearColor(?0.0f?0.5f?0.0f?1.0f?);
//用當前清除顏色清除
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????40960??2007-12-11?18:25??zuobiao\debug\zuobiao.exe
?????文件?????169984??2007-06-19?13:02??zuobiao\glut.dll
?????文件??????21440??2007-12-10?11:56??zuobiao\glut.h
?????文件??????79654??2007-06-19?13:02??zuobiao\glut.lib
?????文件?????169984??2007-06-19?12:58??zuobiao\glut32.dll
?????文件??????79898??2007-06-19?12:58??zuobiao\glut32.lib
?????文件???????4099??2007-12-11?18:25??zuobiao\Simple.cpp
?????文件????????346??2007-12-11?16:03??zuobiao\zuobiao.sln
????..A..H.?????12288??2007-12-11?18:34??zuobiao\zuobiao.suo
?????文件???????3970??2007-12-11?17:55??zuobiao\zuobiao.vcproj
?????文件???????1413??2007-12-11?18:25??zuobiao\zuobiao.vcproj.ACTIONS.yyuan.user
?????目錄??????????0??2009-04-22?12:32??zuobiao\debug
?????目錄??????????0??2009-04-22?12:32??zuobiao
-----------?---------??----------?-----??----
???????????????584036????????????????????13
評論
共有 條評論