繁体   English   中英

使用glut,我如何制作它,以便可以通过单击并拖动来在场景中四处移动对象?

[英]Using glut, how do I make it so I can move an object around in a scene by clicking and dragging it around?

我编写了一个程序,该程序创建了一个当前包含三角形的房间。 我想制作更多类似该三角形的对象,但是我还想添加功能,使我可以通过单击并拖动它们来移动对象。 我认为最好的方法是创建某种模板/原型等,这些模板具有通过构造函数传递的几何数据,但是具有通过鼠标移动它的功能,但是我不确定是否正确,而且我不太确定要使它正常工作实际上需要做什么。 这是我当前的代码,它允许使用方向键旋转场景,但是我不确定如何制作它,因此我可以在此时单击并拖动它(我已经尝试过将其注释掉了因为我认为这行不通,但是如果可以的话,请告诉我):

#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;
}

如果要转置/旋转/缩放任意对象,则此模板如下所示:

  1. 保存当前的世界矩阵(可以使用glPushMatrix来完成)
  2. 将对象转换到所需位置(使用glRotate / glTranslate / glScale)
  3. 绘制对象(glBegin / glEnd部分)
  4. 检索旧矩阵(如果将其推入堆栈,请使用glPopMatrix)

对场景中的每个可绘制对象重复此过程。

操纵对象时,可以为每个对象定义一个矩阵,以便可以独立地操纵它们。 然后,您将自己计算每个对象的矩阵(看看glm-一个用于完成此类事情的简洁框架),然后代替步骤2。使用glMultMatrix并设置自己的矩阵。

另外,您使用的是旧版openGL代码,所以我想您现在正在学习如何工作。 我的建议是不要学习旧知识:研究glsl shader编程,vertexbufferobjects和opengl 1.4以外的所有好东西,这里有一些非常不错的教程:http://www.opengl-tutorial.org/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM