【发布时间】:2021-12-17 15:42:57
【问题描述】:
首先,让我告诉我,我是使用 OpenGL 和 C++ 的新手。但是,我想参与这两个主题。
所以让我解释一下我的情况,在应用glTrasnlatef 和glRotatef 之后,我一直在寻找如何获取对象的新坐标。但是,我没有找到查找信息,实际上我找到了一些关于 java 的信息,但我没有得到它,因为我告诉过你我正在使用 C++。
我读到有一些东西要处理 glPushMatrix(); 函数,但不知道如何处理它。
我知道在应用了一些平移和旋转之后,我正在对实际矩阵进行更改。
最后,这样做的主要目的是因为错误地使用了 rombohedrom 中的那些顶点并进行了大量的平移和旋转,这些也是需要的。
到目前为止,这是我的代码(顺便说一句,我当然正在使用线条和顶点,因为我只需要这些)。
如果有人能通过正确的方式与我联系,我将不胜感激。
提前致谢 阿尔贝托
#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;
}
【问题讨论】:
-
glRotatef() 修改当前活动矩阵堆栈的顶部矩阵。矩阵乘法的效果在 CPU 端不会“可见”。顶点与 OpenGL 渲染中的矩阵相乘(即在“GPU 端” - 忽略 S/W 渲染器的选项)。请注意,这是Fixed Function Pipeline 的所有主题(现已弃用多年)。
-
要了解如何在 CPU 端完成此操作(即修改 C++ 中的顶点),请查看仅基于 C++ 的 SO: How to normalize the vertice of a 3d mesh into a unit cube, centered in the origin?。
-
@Scheff'sCat,首先非常感谢您的回答。我一直在查资料。但是,我必须阅读更多内容,查看您提供的代码,据我所知,它由“两个”部分组成,第一个线性代数在其上生成矩阵 4X4,(标准基本矩阵,xyzw )。如果我是正确的,我不应该接触这些信息,对吧? “第二部分”是缩放发生的地方。接下来,您将向量乘以矩阵以获得新坐标,对吗?我有疑问
-
my other answer的上码和下码是一样的。上面只是
main()函数,下面是完整源代码(与main()函数相同)。