我有一个txt文件,该文件的数据随顶部旋转的质心位置(以3D形式)的时间而变化。因此,我想用圆锥体表示该顶部,而我拥有的数据应表示圆锥体在围绕顶点旋转的时间的中心。
我有一个表示圆锥的代码,我设法确定了如何围绕底座旋转圆锥。但是我没有设法使其绕其顶点旋转。我也不知道如何用数据移动圆锥。
#include <GL\glut.h>
GLfloat xRotated, yRotated, zRotated;
// Cone
GLdouble base=1;
GLdouble height=1.5;
GLint slices =50;
GLint stacks =50;
void displayCone(void)
{
glMatrixMode(GL_MODELVIEW);
// clear the drawing buffer.
glClear(GL_COLOR_BUFFER_BIT);
// clear the identity matrix.
glLoadIdentity();
// traslate the draw by z = -4.0
// Note this when you decrease z like -8.0 the drawing will looks far , or smaller.
glTranslatef(0.0,0.0,-4.5);
// Red color used to draw.
glColor3f(0.8, 0.2, 0.1);
// changing in transformation matrix.
// rotation about X axis
glRotatef(xRotated,1.0,0.0,0.0);
// rotation about Y axis
glRotatef(yRotated,0.0,1.0,0.0);
// rotation about Z axis
glRotatef(zRotated,0.0,0.0,1.0);
// scaling transfomation
glScalef(1.0,1.0,1.0);
// built-in (glut library) function , draw you a Cone.
glutSolidCone(base,height,slices,stacks);
// Flush buffers to screen
glFlush();
// sawp buffers called because we are using double buffering
// glutSwapBuffers();
}
void idleCone(void)
{
xRotated += 0.1;
yRotated += 0.1;
zRotated += 0.1;
displayCone();
}
int main (int argc, char **argv)
{
//Initialize GLUT
glutInit(&argc, argv);
//double buffering used to avoid flickering problem in animation
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
// window size
glutInitWindowSize(400,350);
// create the window
glutCreateWindow("Cone Rotating Animation");
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
xRotated = yRotated = zRotated = 30.0;
xRotated=33;
yRotated=40;
glClearColor(0.0,0.0,0.0,0.0);
//Assign the function used in events
glutDisplayFunc(displayCone);
glutReshapeFunc(reshapeCone);
glutIdleFunc(idleCone);
//Let start glut loop
glutMainLoop();
return 0;
}
我已经设法用Gnuplot表示3D运动的质心,但是用OpenGL旋转的圆锥体做它会更漂亮
答案 0 :(得分:0)
如果要围绕圆锥的顶部旋转圆锥体,则必须以这种方式平移圆锥体,使圆锥体的顶部位于原点中:
glTranslatef(0.0, 0.0, -height);
这必须在应用旋转和缩放之前完成。由于glTranslate
(和其他矩阵转换)将当前矩阵乘以新的转换矩阵,因此glTranslate
指令必须在其他模型转换(如glRotate
和glScale
之后执行) :
void displayCone(void)
{
glMatrixMode(GL_MODELVIEW);
// clear the drawing buffer.
glClear(GL_COLOR_BUFFER_BIT);
// clear the identity matrix.
glLoadIdentity();
// traslate the draw by z = -4.0
// Note this when you decrease z like -8.0 the drawing will looks far , or smaller.
glTranslatef(0.0,0.0,-4.5);
// Red color used to draw.
glColor3f(0.8, 0.2, 0.1);
// changing in transformation matrix.
// rotation about X axis
glRotatef(xRotated,1.0,0.0,0.0);
// rotation about Y axis
glRotatef(yRotated,0.0,1.0,0.0);
// rotation about Z axis
glRotatef(zRotated,0.0,0.0,1.0);
// scaling transfomation
glScalef(1.0,1.0,1.0);
// built-in (glut library) function , draw you a Cone.
// move the peak of the cone to the origin
glTranslatef(0.0, 0.0, -height);
glutSolidCone(base,height,slices,stacks);
// Flush buffers to screen
glFlush();
// sawp buffers called because we are using double buffering
// glutSwapBuffers();
}
根据评论的扩展名:
您能否添加一个简短的代码以帮助我理解如何使用存储在数组中的数据旋转圆锥体?
查看如何逐行Read file line by line using ifstream in C++读取文件。
在下面的示例中,将数据存储到std::vector
中,其中每个元素都由
std::array
之3 GLfloat
中的一个:
#include <vector>
#include <array>
#include <fstream>
#include <string>
#include <sstream>
void ReadData( const char *fileName, std::vector<std::array<GLfloat, 3>> &data )
{
std::ifstream dataFile(fileName);
std::string line;
while (std::getline(dataFile, line))
{
std::istringstream insstr(line);
GLfloat x, y, z;
if (!(insstr >> x >> y >> z))
break; // reading error
data.push_back( { x, y, z } );
}
}
读取数据并通过索引访问3个角度(例如j
)
std::vector<std::array<GLfloat, 3>> data;
ReadData( "mydata.txt", data );
xRotated = data[j][0];
yRotated = data[j][1];
zRotated = data[j][2];