如何旋转在屏幕上绘制的单个三角形设法旋转屏幕上的所有内容。下面的代码旋转所有的图形,我只想旋转一个图形。
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
glRotatef(10,0.0,0.0,1.0);
if(key == GLUT_KEY_DOWN)
glRotatef(10,0.0,0.0,-1.0);
// Refresh the Window
glutPostRedisplay();
}发布于 2011-11-03 04:40:00
在输入处理程序中设置变量,在绘图函数中应用转换。
OpenGL不是场景图。这意味着您不会在OpenGL中构建某些场景表示,您可以在以后更改这些表示。OpenGL是一种绘图接口。它为您提供了在操作系统提供的画布上绘制铅笔、画笔、拼贴画的数字等价物。
这也意味着,当您更改场景中的某些内容时,您将从头开始重新绘制这些内容。
由于OpenGL的这种工作方式,所以在输入事件处理程序中进行矩阵操作或任何类型的OpenGL操作都是没有意义的。它根本不是这样工作的。
更新
最小GLUT用户交互代码示例(OpenGL-1.1):
#include <math.h>
#include <GL/gl.h>
#include <GL/glut.h>
struct {
float triangle_rotation;
} scene;
static const GLfloat triangle_vertices[3][3] = {
{-1., -1., 0.},
{ 1., -1., 0.},
{ 0., 0.732, 0.}
};
static void draw_triangle()
{
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 3*sizeof(GLfloat), &triangle_vertices[0][0]);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableClientState(GL_VERTEX_ARRAY);
}
static void display(void)
{
const int window_width = glutGet(GLUT_WINDOW_WIDTH);
const int window_height = glutGet(GLUT_WINDOW_HEIGHT);
if( !window_width || !window_height )
return;
const float window_aspect = (float)window_width / (float)window_height;
glDisable(GL_SCISSOR_TEST);
glClearColor(0.4, 0.3, 0.5, 1.0);
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, window_width, window_height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-window_aspect, window_aspect, -1, 1, 2, 10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0., 0., -5.);
glPushMatrix();
glRotatef(scene.triangle_rotation, 0., 1., 0.);
draw_triangle();
glPopMatrix();
glutSwapBuffers();
}
static void special_key(int key, int x, int y)
{
switch(key) {
case GLUT_KEY_LEFT:
scene.triangle_rotation = fmod(scene.triangle_rotation + 5, 360);
break;
case GLUT_KEY_RIGHT:
scene.triangle_rotation = fmod(scene.triangle_rotation - 5, 360);
break;
default:
break;
}
glutPostRedisplay();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("Simple input altering scene demo");
glutDisplayFunc(display);
glutSpecialFunc(special_key);
glutMainLoop();
}https://stackoverflow.com/questions/7986878
复制相似问题