解决方案 »
- 有用vc2012的吗?智能提示强了不少,就是会卡死
- CListViewCtrl里如何得到选中项的序号,没有GetFirstSelectedItem
- 休息三天散分,周一结贴
- 我想在dialog里加一个图片,想随时让这个图片可以隐藏或显示出来.如何做?
- 如何从PE文件格式分析得到类名和标题,急!急!在线等!.................100分奉送
- 请问如何剪切CDC中选进的位图?
- 怎么得到鼠标点击处的坐标值?用什么函数?
- 串口异步通信问题
- 动态链接库调用数据库
- 请问如何将一串int或float型的数字转化为CString?
- 怎样改变菜单栏的位置
- 如何读取afxFrameOrView42控件中的内容
在void menu(int item) 里面加上
switch(item)
{
case 1:
a=1;
break;
case 2:
a=2;
break;
}
void keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case 'q' | 'Q' :
exit(0);
break;
case VK_ESCAPE :
exit(0);
break;
case 'z':
case 'Z':
viewer[0] -= 0.1;
break;
case 'c':
case 'C':
viewer[0] += 0.1;
break;
case 's':
case 'S':
viewer[1] -= 0.1;
break;
case 'x':
case 'X':
viewer[1] += 0.1;
break;
case 'a':
case 'A':
viewer[2] -= 0.1;
break;
case 'd':
case 'D':
viewer[2] += 0.1;
break;
case 1:
a=1;//这里改了,原来用的指针
break;
case 2:
a=2;//这里改了,原来用的指针
break;
default:
break;
}
glutPostRedisplay();
}
这样不行,然后又把这里找你说的这样改了,也是不行
void menu(int item)
{
switch(item)
{
case 1:
a=1;
break;
case 2:
a=2;
break;
}
keyboard((unsigned char)item,0,0);
}
用keyboard((unsigned char)item,0,0); 做键盘处理就不用了吧,难道你选中菜单用键盘控制?
还有glutMotionFunc是在按下鼠标后移送鼠标才用的,你这我看用glutMouseFunc就可以了还有上面的代码有点问题,应该是这样,呵呵
void menu(int item)
{
switch(item)
{
case 0:
a=1;
break;
case 1:
a=2;
break;
} 以上是我的建议,如果还不行,我也没办法了
if (a=1)
{
if(w <=h)
......
}
else if (a=2)
{
if(w <=h)
......
}
if 语句 中 你是 要赋值 还是 判断?
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>
#include <winUser.h>
#include <math.h> static float viewer[3]; int *pointer,a=1;
int g_w,g_h;
static bool mouseLeftDown;
static float point[2][2];
static float theta;
void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(viewer[0], viewer[1], viewer[2], 0, 0, 0, 0, 1, 0);
glRotatef(theta,0,1,0);
GLfloat vertices[8][3] = {{-1,-1,1},{-1,1,1},{1,1,1},{1,-1,1},{-1,-1,-1},{-1,1,-1},{1,1,-1},{1,-1,-1}};
GLfloat colors[8][3] = {{0,0,1},{0,1,1},{1,1,1},{1,0,1},{0,0,0},{0,1,0},{1,1,0},{1,0,0}};
glBegin(GL_QUADS);
glColor3fv(colors[0]); glVertex3fv(vertices[0]);
glColor3fv(colors[3]); glVertex3fv(vertices[3]);
glColor3fv(colors[2]); glVertex3fv(vertices[2]);
glColor3fv(colors[1]); glVertex3fv(vertices[1]);
glColor3fv(colors[2]); glVertex3fv(vertices[2]);
glColor3fv(colors[3]); glVertex3fv(vertices[3]);
glColor3fv(colors[7]); glVertex3fv(vertices[7]);
glColor3fv(colors[6]); glVertex3fv(vertices[6]);
glColor3fv(colors[3]); glVertex3fv(vertices[3]);
glColor3fv(colors[0]); glVertex3fv(vertices[0]);
glColor3fv(colors[4]); glVertex3fv(vertices[4]);
glColor3fv(colors[7]); glVertex3fv(vertices[7]);
glColor3fv(colors[1]); glVertex3fv(vertices[1]);
glColor3fv(colors[2]); glVertex3fv(vertices[2]);
glColor3fv(colors[6]); glVertex3fv(vertices[6]);
glColor3fv(colors[5]); glVertex3fv(vertices[5]);
glColor3fv(colors[4]); glVertex3fv(vertices[4]);
glColor3fv(colors[5]); glVertex3fv(vertices[5]);
glColor3fv(colors[6]); glVertex3fv(vertices[6]);
glColor3fv(colors[7]); glVertex3fv(vertices[7]);
glColor3fv(colors[5]); glVertex3fv(vertices[5]);
glColor3fv(colors[4]); glVertex3fv(vertices[4]);
glColor3fv(colors[0]); glVertex3fv(vertices[0]);
glColor3fv(colors[1]); glVertex3fv(vertices[1]);
glEnd();
glutSwapBuffers();
}
void mouseMotion(int x, int y)
{
if(mouseLeftDown)
{
double viewport[4];
glGetDoublev(GL_VIEWPORT,viewport);
point[1][0] = x / (float)viewport[2] * 500;
point[1][1] = (viewport[3] - y ) / (float)viewport[3]*500;
}
glutPostRedisplay();
} void timer(int value)
{
theta += 2.0;
if(theta >= 360.0)
theta -= 360.0;
glutPostRedisplay();
glutTimerFunc(1000/30,timer,1);
} void init(void)
{
viewer[0] = 2.5;
viewer[1] = 2.5;
viewer[2] = 2.5;
}
void keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case 'q' | 'Q' :
exit(0);
break;
case VK_ESCAPE :
exit(0);
break;
case 'z':
case 'Z':
viewer[0] -= 0.1;
break;
case 'c':
case 'C':
viewer[0] += 0.1;
break;
case 's':
case 'S':
viewer[1] -= 0.1;
break;
case 'x':
case 'X':
viewer[1] += 0.1;
break;
case 'a':
case 'A':
viewer[2] -= 0.1;
break;
case 'd':
case 'D':
viewer[2] += 0.1;
break;
default:
break;
}
glutPostRedisplay();
}
void SetupRC(void)
{
glClearColor(1.0f,1.0f,1.0f,1.0f );
glEnable(GL_DEPTH_TEST);
} void ChangeSize(int w, int h)
{
if(h==0)
h=1;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); g_w=w;g_h=h; if(w <=h)
glOrtho(-2.0 , 2.0, -2.0*(float)h/(float)w, 2.0*(float)h/(float)w, 1.0, 10.0 );
else
glOrtho(-2.0*(float)w/(float)h, 2.0*(float)w/(float)h, -2.0, 2.0, 1.0, 10.0 );
} void menu(int item)
{
switch(item)
{
case 1:
glViewport (0, 0, (GLsizei)(g_w), (GLsizei)(g_w)); // 设置窗口可见区为窗口大小
glMatrixMode (GL_PROJECTION); // 设置投影矩阵
glLoadIdentity (); // 重置它
if(g_w <=g_h)
glOrtho(-2.0 , 2.0, -2.0*(float)g_h/(float)g_w, 2.0*(float)g_h/(float)g_w, 1.0, 10.0 );
else
glOrtho(-2.0*(float)g_w/(float)g_h, 2.0*(float)g_w/(float)g_h, -2.0, 2.0, 1.0, 10.0 );
glMatrixMode (GL_MODELVIEW); // 设置为模型变换矩阵
glLoadIdentity ();
break;
case 2:
glViewport (0, 0, (GLsizei)(g_w), (GLsizei)(g_w));
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
if(g_w <=g_h)
glFrustum(-2.0 , 2.0, -2.0*(float)g_h/(float)g_w, 2.0*(float)g_h/(float)g_w, 1.0, 10.0 );
else
glFrustum(-2.0*(float)g_w/(float)g_h, 2.0*(float)g_w/(float)g_h, -2.0, 2.0, 1.0, 10.0 );
glMatrixMode (GL_MODELVIEW); // 设置为模型变换矩阵
glLoadIdentity ();
break;
}
glutPostRedisplay();
} void main(int argc, char* argv[])
{
pointer = &a; glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(600,600);
glutCreateWindow("Simple"); init();
SetupRC(); glutDisplayFunc(RenderScene);
glutMotionFunc(mouseMotion);
glutKeyboardFunc(keyboard);
glutCreateMenu(menu);
glutAddMenuEntry("Ortho",1);
glutAddMenuEntry("Frustum",2);
glutAttachMenu(GLUT_RIGHT_BUTTON); glutReshapeFunc(ChangeSize);
glutMainLoop();
}
以及两个变量的定义g_w,g_h,a赋了初值=1;
但是楼上的朋友,我还是有一点疑问,void keyboard(unsigned char key, int x, int y) 函数是只接受键盘的指令吗?如果是只接受键盘的指令的话。那么将他改成下面这样。用鼠标点击菜单时候依然能退出。
void keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case 'q' | 'Q' :
exit(0);
break;
case VK_ESCAPE :
exit(0);
break;case 'z':
case 'Z':
viewer[0] -= 0.1;
break;
case 'c':
case 'C':
viewer[0] += 0.1;
break;
case 's':
case 'S':
viewer[1] -= 0.1;
break;
case 'x':
case 'X':
viewer[1] += 0.1;
break;
case 'a':
case 'A':
viewer[2] -= 0.1;
break;
case 'd':
case 'D':
viewer[2] += 0.1;
break;
case 1:
exit(0);//这里改了
break;
case 2:
exit(0);//这里改了
break;
default:
break;
}
glutPostRedisplay();
}