- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个 txt 文件,其中的数据是顶部旋转(3D 中)质心位置的时间函数。所以我想用一个圆锥体来表示这个顶部,我拥有的数据应该代表圆锥体基础的中心,及时围绕顶点旋转。
我有一个代表圆锥体的代码,我设法弄清楚了如何围绕底部旋转它。但是我没有设法围绕它的 Vertex 旋转它。我也不知道如何用我的数据移动圆锥体。
#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 旋转的圆锥体来表现会更漂亮
最佳答案
如果你想围绕它的顶部旋转圆锥体,那么你必须以这种方式平移圆锥体,圆锥体的顶部在原点:
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();
}
根据评论扩展:
Could you add a short code snipped to help me understanding what I should do to rotate the cone with the datas stored in the array ?
查看如何逐行读取文件 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];
关于c++ - 使用 txt 文件中的数据根据时间围绕其顶点旋转圆锥体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56207206/
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我正在尝试绘制动态雷达,但无法正确绘制圆锥形状。我可以让这两条线正常工作,但我无法让它们之间的阴影正常工作。 我的目标: 我得到了什么: 这是我的代码: paint.setColor(Color.WH
我是一名优秀的程序员,十分优秀!