0

First of all, let me tell i am quite new using OpenGL and C++. However, i want to get involved with this two topics.

So let me explain my case, ive been searching how to get the new coordinates of an object after glTrasnlatef and glRotatef were applied. However, i did not find the find info, actually i found some info about java but i am not getting it, as i told you i am working with C++.

I read there is something to deal with the glPushMatrix(); function but idont know how to handle it.

I know that after applying some trnaslation and rotation i am doing changes into the actual matrix.

Finally, the main purpose of this is because ill use those vertices from the rombohedrom and do a lot of translations and rotations, those are going to be needed as well.

So far this is my code (BTW i am working with lines and the vertices of course because i only need those).

i will really appreciate if someone can address me through the right path.

Thanks in advance Alberto

#include <GL/glut.h>
#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

// Global variables
double rotate_y=0; 
double rotate_x=0;

int width = 640;
int height = 640;

#define PI 3.14159265

float theta = 60;
float edgeLength = 1;
float sinThetaOverHypotenuse = (sin((theta*PI)/180))/edgeLength;

vector<vector<float>> coordinates{{0.0, 0.0, 0.0},
{1.0, 0.0, 0.0},

{1.0, 0.0, 0.0},
{1.5, sinThetaOverHypotenuse, 0.0},

{1.5, sinThetaOverHypotenuse, 0.0},
{0.5, sinThetaOverHypotenuse, 0},

{0.5, sinThetaOverHypotenuse, 0},
{0.0, 0.0, 0.0}};

void rhombohedrom()
{
    vector<vector<float>> rotated {};
//    glClearColor(1,1,0,0)
    //  Clear screen and Z-buffer
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    double w = glutGet( GLUT_WINDOW_WIDTH ) / 300.0;
    double h = glutGet( GLUT_WINDOW_HEIGHT ) / 300.0;
    glOrtho( -1 * w, 1 * w, -1 * h, 1 * h, 10, -10);

glMatrixMode( GL_MODELVIEW );

    // Reset transformations
    glLoadIdentity();

    // Rotate when user changes rotate_x and rotate_y
    glRotatef( rotate_x, 1.0, 0.0, 0.0 );
    glRotatef( rotate_y, 0.0, 1.0, 0.0 );
/*
    FACE 0
    FACE 0
    FACE 0
    FACE 0
*/
    // random color side - front
    glBegin(GL_LINE_LOOP);
        glColor3f( 0.7, 0.3, 0.8 );
        for (int i = 0; i < 8; ++i)
        {
            glVertex3f(coordinates[i][0], coordinates[i][1], coordinates[i][2]);
        }
    glEnd();
/*
    FACE 1
    FACE 1
    FACE 1
    FACE 1
*/
    glPushMatrix();
    glTranslatef(0.0,0.0,0.0);
    glRotatef(90.0, 1.0, 0.0, 0.0);
    glBegin(GL_LINE_LOOP);
        glColor3f( 1.0, 1.0, 1.0 );
        for (int i = 0; i < 8; ++i)
        {
            glVertex3f(coordinates[i][0], coordinates[i][1], coordinates[i][2]);
        }        
    glEnd();
    glPopMatrix();

/*
    FACE 2
    FACE 2
    FACE 2
    FACE 2
*/

    glPushMatrix();
    glTranslatef(0.5,0.0,sinThetaOverHypotenuse);
    glBegin(GL_LINE_LOOP);
        glColor3f( 0.5, 0.5, 0.0 );
        for (int i = 0; i < 8; ++i)
        {
            glVertex3f(coordinates[i][0], coordinates[i][1], coordinates[i][2]);
        }        
    glEnd();
    glPopMatrix();

/*
    FACE 3
    FACE 3
    FACE 3
    FACE 3
*/

    glPushMatrix();
    glTranslatef(0.5,sinThetaOverHypotenuse,0.0);
    glRotatef(90.0, 1.0, 0.0, 0.0);
    glBegin(GL_LINE_LOOP);
        glColor3f( 0.5, 0.0, 0.0 );
        for (int i = 0; i < 8; ++i)
        {
            glVertex3f(coordinates[i][0], coordinates[i][1], coordinates[i][2]);
        }        
    glEnd();
    glPopMatrix();

    glFlush();
    glutSwapBuffers();
}

void specialKeys(int key, int x, int y)

{
    //  Right arrow - increase rotation by 5 degree
    if (key == GLUT_KEY_RIGHT)
        rotate_y += 5;
 
    //  Left arrow - decrease rotation by 5 degree
    else if (key == GLUT_KEY_LEFT)
        rotate_y -= 5;
 
    else if (key == GLUT_KEY_UP)
        rotate_x += 5;
 
    else if (key == GLUT_KEY_DOWN)
        rotate_x -= 5;
 
    //  Request display update
    glutPostRedisplay();
}

int main(int argc, char *argv[])
{

    //  Initialize GLUT and process user parameters
    glutInit(&argc,argv);

    glutInitWindowSize(width,height);
    // Position of the window
    glutInitWindowPosition(10,10);

    //  Request double buffered true color window with Z-buffer
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

    // Create window
    glutCreateWindow("rhombohedrom");

    //  Enable Z-buffer depth test
    glEnable(GL_DEPTH_TEST);

    // Callback functions
    glutDisplayFunc(rhombohedrom);
    glutSpecialFunc(specialKeys);

    //
    glutMainLoop();
    
    return 0;
} 
baezacaljo
  • 29
  • 4
  • 2
    [glRotatef()](https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glRotate.xml) modifies the top matrix of the currently active matrix stack. The effect of the matrix multiplication doesn't get "visible" on the CPU side. The vertices are multiplied with the matrix in the OpenGL rendering (i.e. on "GPU side" - ignoring the option of an S/W renderer). Please, note that this is all subject of the [Fixed Function Pipeline](https://www.khronos.org/opengl/wiki/Fixed_Function_Pipeline) (which is deprecated for many years now). – Scheff's Cat Nov 03 '21 at 08:58
  • 3
    To see how this can be done on CPU side (i.e. modify vertices in C++), have a look at [SO: How to normalize the vertice of a 3d mesh into a unit cube, centered in the origin?](https://stackoverflow.com/a/69808766/7478597) which is based on C++ only. – Scheff's Cat Nov 03 '21 at 09:00
  • 1
    FYI: [SO: What is the point of the matrix stack in OpenGL?](https://stackoverflow.com/q/3755998/7478597), [OpenGL matrix stacks and current matrix](http://makble.com/opengl-matrix-stacks-and-current-matrix), [Manipulating the Matrix Stacks (Viewing) (OpenGL Programming)](http://what-when-how.com/opengl-programming-guide/manipulating-the-matrix-stacks-viewing-opengl-programming/) – Scheff's Cat Nov 03 '21 at 09:03
  • @Scheff'sCat, First of all thank you very much for your answer. Ive been checking the info. However, i have to read a bit more, look in the code you provided, as far as i can see is composed by "two" parts the first one linear algebra, on which you generate the matrix 4X4, (standard base matrix, xyzw). if i am correct i should not touch such info, right?. The "second part" is on which the scaling happens. Followed by this you make the multiplication of a vector by the matrix in order to obtain the new coordinates, right?. there is on which i have a doubt – baezacaljo Nov 03 '21 at 17:06
  • 1
    The upper code and the lower code of [my other answer](https://stackoverflow.com/a/69808766/7478597) are the same. The upper is only the `main()` function, the lower is the complete source code (with the identical `main()` function). – Scheff's Cat Nov 03 '21 at 17:12
  • @Scheff'sCat i know that such code will work for rotation as well, but how apply it?. i got the info for scaling, i am sure the code does the same stuff but i can not figure it out how to use it for the angle rotation. I know those are silly question, but really i want to learn C++ and OpenGL. – baezacaljo Nov 03 '21 at 17:13
  • There is an older but more complete version in [NoGL3dDemo](https://github.com/scheff173/NoGL3dDemo) where I covered rotation and other things as well. Though, in this version, the matrices are stored row-wise while the new sample stores them column-wise. The latter is like in [**glm**](https://github.com/g-truc/glm) which I would warmly recommend. – Scheff's Cat Nov 04 '21 at 08:02

1 Answers1

1

First, I want to address some remarks such as:

glPushMatrix() is an old way, try to use the MVP technique which will help you solving your problem. So, you have to write your Vertex Shader and pass the Matrices through what called uniform in OpenGL. Obviously, you have to use new Routines. glPopMatrix() is also an old routine.

I'm happy to answer your further questions if you want to know more about those remarks.

Use Vertex Shader instead of simple calculus using the CPU!

you can change glTranslatef() and glRotatef() by lookAt so you can change the scale, rotation and translation.

Use Vertex Shader instead of using or stressing the CPU by graphic calculus. Even if you have an Intel integrated GPU

ic_Engineer
  • 313
  • 3
  • 12
  • Thanks for your answer, look i am quite new in C++, OpenGL. i really want to learn. Therefore, i was searching for the info you already provided before, and i don't see any clues, probably i am searching into the wrong path, but i don't get it.i know this might sound silly, but as i told you i am a beginner in programming and i want to learn more, i found that programming makes the life easier – baezacaljo Nov 03 '21 at 17:17
  • no problem, I'm happy to help! first thing that I want to pinpoint is every programmer rely on this document: https://docs.gl/ that I advise you to use it. Next, I see that you're using the glut library, it's fine, but I would suggest the GLFW library: https://www.glfw.org/ you will find everything there to help you starting up. I also advise you to use this library for rendering 3D content: http://glew.sourceforge.net/ . I would also suggest some tutorials to get the essence of OpenGL and C++ like this one: The Cherno on youtube. you can still ask questions later to fix what you've learned – ic_Engineer Nov 04 '21 at 08:29