我写了一个程序来创建一个当前包含三角形的房间。我想制作更多像这个三角形的对象,但我还想添加一些功能,允许我通过点击它们并拖动它们来移动它们。我认为最好的方法是创建某种模板/原型等等,它具有通过构造函数传递的几何数据,但具有通过鼠标移动它的功能,但我不确定我是否正确,而且我并不完全确定我真正需要做些什么才能让它像这样工作。这是我当前的代码,它允许使用方向键旋转场景,但我不确定如何制作它所以我可以点击一个对象并在此处拖动它(我有一些我试图为此注释掉的东西的开头因为我认为它不会起作用,但是如果它会让我知道如何):
#include <stdio.h>
#include <stdarg.h>
#include <math.h>
#include <stdlib.h>
#include <GL/glut.h>
void display();
void viewButtons();
//const double ZoomSTEP = 0.2;
//const double zoomFactor = 1.03;
double rotationY = 0;
double rotationX = 0;
// Mouse positions, normalized to [0,1].
//double xMouse = 0.5;
//double yMouse = 0.5;
//GLdouble startView = 2.0;
void display()
{
//Clear the screen and clear z-buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset transformations
glLoadIdentity();
// Rotate when user changes rotate_x and rotate_y
glRotatef(rotationX, 1.0, 0.0, 0.0);
glRotatef(rotationY, 0.0, 1.0, 0.0);
glBegin(GL_POLYGON);
//Vertices and colors
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.5, -0.5, 0.5); // P1 is red
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.5, 0.5, 0.5); // P2 is green
glColor3f(0.0, 0.0, 1.0);
glVertex3f(-0.5, 0.5, 0.5); // P3 is blue
glColor3f(1.0, 0.0, 1.0);
glVertex3f(-0.5, -0.5, 0.5); // P4 is purple
glEnd();
/*// White side - BACK
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(0.5, -0.5, 0.5);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, -0.5, 0.5);
glEnd();*/
// Purple side - RIGHT
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 1.0);
glVertex3f(0.5, -0.5, -0.5);
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(0.5, -0.5, 0.5);
glEnd();
// Green side - LEFT
glBegin(GL_POLYGON);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(-0.5, -0.5, 0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, -0.5, -0.5);
glEnd();
// Blue side - TOP
glBegin(GL_POLYGON);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, 0.5);
glEnd();
// Red side - BOTTOM
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.5, -0.5, -0.5);
glVertex3f(0.5, -0.5, 0.5);
glVertex3f(-0.5, -0.5, 0.5);
glVertex3f(-0.5, -0.5, -0.5);
glEnd();
glBegin(GL_POLYGON);
glColor3f(0.5, 0.6, 0.3);
glVertex3f(0.3, 0.1, 0.5);
glVertex3f(0.4, 0.1, 0.5);
glVertex3f(0.2, -0.2, 0.1);
glEnd();
glFlush();
glutSwapBuffers();
}
void viewButtons(int button, int x, int y)
{
if (button == GLUT_KEY_RIGHT)
rotationY += 5;
else if (button == GLUT_KEY_LEFT)
rotationY -= 5;
else if (button == GLUT_KEY_UP)
rotationX += 5;
else if (button == GLUT_KEY_DOWN)
rotationX -= 5;
//update display
glutPostRedisplay();
}
int main(int argc, char **argv) {
// init GLUT and create window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(720, 640);
glutCreateWindow("Room with objects");
// register callbacks
glutDisplayFunc(display);
glutSpecialFunc(viewButtons);
glEnable(GL_DEPTH_TEST);
// enter GLUT event processing cycle
glutMainLoop();
return 1;
}
答案 0 :(得分:2)
如果你想转置/旋转/缩放任意对象,那么模板是这样的:
对场景中的每个可绘制对象重复此过程。
操作对象时,您可以为每个对象定义一个矩阵,以便您可以独立操作它们。然后,您将自己计算每个对象的矩阵(查看glm - 一个用于完成此类操作的简洁框架),然后使用glMultMatrix并设置自己的矩阵。
此外,您正在使用传统的openGL代码,因此我猜您现在正在学习如何解决问题。我的建议是不要学习旧的东西:看看glsl着色器编程,vertexbufferobjects以及超出opengl 1.4的所有好东西,有一些非常好的东西http://www.opengl-tutorial.org/