简体   繁体   English

应用 glTranslatef 和 glRotatef(OpenGL,C++)后顶点的新坐标

[英]New Coordinates of the vertices after appliying glTranslatef and glRotatef (OpenGL, C++)

First of all, let me tell i am quite new using OpenGL and C++.首先,让我告诉我我是使用 OpenGL 和 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.所以让我解释一下我的情况,我一直在寻找如何在应用glTrasnlatefglRotatefglTrasnlatef对象的新坐标。 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++.但是,我没有找到查找信息,实际上我找到了一些关于 Java 的信息,但我没有得到它,因为我告诉过你我正在使用 C++。

I read there is something to deal with the glPushMatrix();我读到有一些东西可以处理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.我知道在应用一些 trnaslation 和旋转之后,我正在对实际矩阵进行更改。

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

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. glPushMatrix()是一种旧方法,尝试使用 MVP 技术来帮助您解决问题。 So, you have to write your Vertex Shader and pass the Matrices through what called uniform in OpenGL.所以,你必须编写你的顶点着色器并通过 OpenGL 中所谓的uniform传递矩阵。 Obviously, you have to use new Routines.显然,您必须使用新的例程。 glPopMatrix() is also an old routine. glPopMatrix()也是一个老例程。

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!使用顶点着色器而不是使用 CPU 的简单演算!

you can change glTranslatef() and glRotatef() by lookAt so you can change the scale, rotation and translation.您可以通过lookAt更改glTranslatef()glRotatef()以便您可以更改比例、旋转和平移。

Use Vertex Shader instead of using or stressing the CPU by graphic calculus.使用顶点着色器,而不是通过图形演算来使用或强调 CPU。 Even if you have an Intel integrated GPU即使您拥有 Intel 集成 GPU

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

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