1

I have a txt file with datas in function of time of the position of the center of mass of a top rotating ( in 3D ). So I would want to represent this top with a cone and the datas i have should represent the center of the basis of cone in time rotating around the vertex.

I have a code representing a cone and I managed working out how to rotate it around the base. However I didn't manage to rotate it around its Vertex . I also have no idea how to move the cone with my datas.

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

I already managed to represent the center of mass moving in 3D with Gnuplot but it would be much more beautiful to do it with a cone rotating with OpenGL

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Jatos
  • 333
  • 2
  • 13

1 Answers1

0

If you want to rotate the cone around its top, then you've to translate the cone in this way, that the top of the cone is in the origin:

glTranslatef(0.0, 0.0, -height);

This has to be done before the rotation and scaling is applied. Since glTranslate (and other matrix transformations) multiply the current matrix by a new transformation matrix, the glTranslate instruction has to be done after the other model transformations like glRotate and 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();
}

Extension according to the comment:

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 ?

See how to read a file line by line Read file line by line using ifstream in C++. In the following example, store the data to a std::vector, where each element consists of an std::array of 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 } );
    }
}

Read the data and access the 3 angles by an index (e.g. j):

std::vector<std::array<GLfloat, 3>> data;
ReadData( "mydata.txt", data );
xRotated = data[j][0];
yRotated = data[j][1];
zRotated = data[j][2];
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Thank you !! Any ideas on how to move with angles taken from a txt file. For example a txt with 3 columns of data representing the rotation about the X, Y and Z axis each second. Do you also know the real time that OpenGL takes to do a single rotation of the body and if it is possible to change that time ? – Jatos May 19 '19 at 17:00
  • I know how to create an input stream in c++, is it enough? To make my question more clear let's suppose you have the file i said and each line represent the rotation about the 3 principal axis in a real time of 0.01 seconds . How should I do to represent a real time rotation? From what you said I think I should make 60 rotations each second representing the 100 rotations that I would have in this example – Jatos May 19 '19 at 17:32
  • @ Rabbid76 Yes because in my example each line represents a rotation made in a real time of 0.01 seconds – Jatos May 19 '19 at 17:36
  • @ Rabbid76 And what about using the txt file for the rotations ? – Jatos May 19 '19 at 17:42
  • @ Rabbid76 I know myself how to open a stream but I don't have any idea what to do in this case in OpenGl . Should I open theinput stream and use it in the function idleCone? However I would be glad of a code snippet – Jatos May 19 '19 at 17:47
  • Okay that's fine . 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 ? – Jatos May 19 '19 at 17:57
  • It is a simple txt file . Each line has 3 numbers representing the angle of rotation about X, Y and Z axis – Jatos May 19 '19 at 18:09