在 OpenGL 上单击鼠标时无法旋转和更改对象的颜色
Posted
技术标签:
【中文标题】在 OpenGL 上单击鼠标时无法旋转和更改对象的颜色【英文标题】:Can't rotate and change color of object on mouseclick on OpenGL 【发布时间】:2019-10-29 15:10:29 【问题描述】:当我单击鼠标左键时,我试图围绕他自己的中心旋转一个对象,但它似乎不起作用......另外,当我右键单击鼠标时,我应该更改对象的颜色,但我不明白为什么它似乎不起作用,它只是停止了对象的旋转.....这是我的代码可以帮助你,我添加了所有函数以查看我正在尝试做什么:
#include <stdlib.h>
#include <math.h>
#include "dependente\freeglut\freeglut.h"
#include "dependente\glfw\glfw3.h"
#include <stdio.h> //incluziuni librarii
float ORG[3] = 0,0,0 ;
static GLfloat spin = 0.0;
GLfloat viewangle = 0, tippangle = 0, traj[120][3]; //variabila pentru unghi camera
GLfloat d[3] = 0.1, 0.1, 0.1 ; //vector directie
GLfloat xAngle = 0.0, yAngle = 0.0, zAngle = 0.0;
bool draw_triangle = false; //variabila desenat figuri
bool draw_square = false;
bool draw_decagon = false;
// Use arrow keys to rotate entire scene !!!
void Special_Keys(int key, int x, int y) //functie ptr taste sus jos stanga dreapta
switch (key)
case GLUT_KEY_LEFT: viewangle -= 5; break;
case GLUT_KEY_RIGHT: viewangle += 5; break;
case GLUT_KEY_UP: tippangle -= 5; break;
case GLUT_KEY_DOWN: tippangle += 5; break;
default: printf("Special key %c == %d", key, key);
glutPostRedisplay();
void Triangle(void) //draw the triangle shape
glBegin(GL_TRIANGLE_FAN);//triangles have a common vertex, which is the central vertex
glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(0.0f, 1.0f, 0.0f); //V0(red)
glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); //V1(green)
glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(1.0f, -1.0f, 1.0f); //V2(blue)
glEnd();
void Square(void)
glBegin(GL_QUADS);
glVertex2f(-1.0f, 1.0f); // top left
glVertex2f(1.0f, 1.0f); // top right
glVertex2f(1.0f, -1.0f); // bottom right
glVertex2f(-1.0f, -1.0f); // bottom left
glEnd();
void Decagon(void) //draw the decagon shape
glBegin(GL_POLYGON);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.72f,0.8f, 0.0f); //a1
glVertex3f(0.52f, 0.8f,0.0f); //z
glVertex3f(0.35f, 0.64f, 0.0f); //b1
glVertex3f(0.3f, 0.48f, 0.0f); //d1
glVertex3f(0.35f, 0.3f, 0.0f); //e1
glVertex3f(0.52f, 0.16f, 0.0f); //l1
glVertex3f(0.72f, 0.16f, 0.0f); //m1
glVertex3f(0.9f, 0.3f, 0.0f); //o1
glVertex3f(0.95f, 0.48f, 0.0f); //p1
glVertex3f(0.9f, 0.64f, 0.0f); //c1
glScalef(10, 10, 10);
glTranslatef(1, 2, 3);
glEnd();
void Keyboard(unsigned char key, int x, int y) //press a key to perform actions
switch (key)
case 'd': d[0] += 0.1; break; //camera right
case 'a': d[0] -= 0.1; break; //camera left
case 'w': d[1] += 0.1; break; //camera up
case 's': d[1] -= 0.1; break; //camera down
case 'm': d[2] += 0.1; break; //magnify
case 'n': d[2] -= 0.1; break; //minify
case 't': draw_triangle = true; draw_decagon = false; break; //draw pyramid when key is pressed
case 'q': draw_square = true; draw_decagon = false; draw_triangle = false; break; //draw cube when key is pressed
case 'l': draw_decagon = true; draw_triangle = false; break; //draw prism when key is pressed
case 'x': xAngle += 5; break; //modify x axis angle
case 'y': yAngle += 5; break; //modify y axis angle
case 'z': zAngle += 5; break; //modify z axis angle
default: printf(" Keyboard %c == %d", key, key); //see what key it's pressed
glutPostRedisplay();
void spinDisplay()
spin = spin + 0.1;
if (spin > 360.0)
spin = 0.0;
glutPostRedisplay();
void mouse(int buton, int state, int x, int y)
switch (buton)
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(spinDisplay);
break;
case GLUT_RIGHT_BUTTON: //here I don't know how to change the color of the shape
glutIdleFunc(NULL);
default:glutIdleFunc(NULL);
break;
void redraw(void)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glLoadIdentity();
glTranslatef(0, 0, -3);
glRotatef(tippangle, 1, 0, 0); // Up and down arrow keys 'tip' view.
glRotatef(viewangle, 0, 1, 0); // Right/left arrow keys 'turn' view.
glDisable(GL_LIGHTING);
glPushMatrix();
glTranslatef(d[0], d[1], d[2]); // Move box down X axis.
glScalef(0.3f, 0.3f, 0.3f); //increase the object size
glRotatef(zAngle, 0, 0, 1);
glRotatef(yAngle, 0, 1, 0);
glRotatef(xAngle, 1, 0, 0);
glRotatef(spin, 0.0, 0.0, 1.0);
if (draw_triangle)
Triangle();
if (draw_decagon)
Decagon();
if (draw_square)
Square();
glPopMatrix();
glutSwapBuffers();
int main(int argc, char** argv)
glutInit(&argc, argv);
glutInitWindowSize(900, 600);
glutInitWindowPosition(300, 300);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE);
glutMouseFunc(mouse);
glutCreateWindow("Figure Rotation");
glutDisplayFunc(redraw);
glutKeyboardFunc(Keyboard);
glutSpecialFunc(Special_Keys);
glClearColor(0.1, 0.0, 0.1, 1.0);
glMatrixMode(GL_PROJECTION);//specify which matrix is the current matrix, matrix that represents your camera's lens (aperture, far-field, near-field, etc).
gluPerspective(60, 1.5, 1, 10); //set up a perspective projection matrix
glMatrixMode(GL_MODELVIEW); //specify which matrix is the current matrix,matrix that represents your camera (position, pointing, and up vector).
glutMainLoop();
return 1;
【问题讨论】:
代码中没有调用函数void mouse()
。
尽量不要多标签。这是 C 还是 C++?
是的,你可能只是忘了打电话给glutMouseFunc
或者不管它的名字是什么。
谢谢,我太笨了..但是现在如果我右键单击它不会改变颜色,它只会停止旋转
如果我右键单击它不会改变颜色,它只会停止旋转这就是我所期望的:case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN) glutIdleFunc(spinDisplay); break; case GLUT_RIGHT_BUTTON: glutIdleFunc(NULL);
【参考方案1】:
您必须声明一个表示形状颜色的变量。就像你改变绘制三角形或十边形一样。
声明全局变量:
bool colorChanged = false;
在鼠标函数中更改此变量,并添加 glutPostRedisplay 调用(否则将不会调用显示函数)。
colorChanged = !colorChanged;
glutPostRedisplay();
渲染时考虑这个变量。
if (colorChanged)
glColor3f(0,0,0);
else
glColor3f(1,1,1);
小心这个:
glutMouseFunc(mouse); glutCreateWindow("Figure Rotation");
您在创建窗口之前注册了鼠标功能。一些实现会忽略此调用。
【讨论】:
以上是关于在 OpenGL 上单击鼠标时无法旋转和更改对象的颜色的主要内容,如果未能解决你的问题,请参考以下文章