gpt4 book ai didi

c++ - OpenGL中的旋转立方体

转载 作者:行者123 更新时间:2023-12-04 07:53:43 24 4
gpt4 key购买 nike

我想要一个围绕中心旋转的立方体。这是我可以用这个转换做的事情:

model = glm::rotate(identity, glm::radians(-100.0f * time), glm::vec3(0.0, 1.0, 0.0)); //rotate around y-axis
model = glm::translate(model, glm::vec3(8.0, 0.0, 0.0));
model = glm::rotate(model, glm::radians(-100.0f * time), glm::vec3(1.0, 0.0, 0.0)); //self-rotation
现在我想要一个立方体,当它围绕 y 轴旋转时,它会围绕第一个立方体旋转。
想象第一个立方体是地球,第二个立方体是月球。
我试过这个,但它没有用。
test = glm::rotate(test, glm::radians(-100.0f * time), glm::vec3(0.0, 1.0, 0.0));
test = glm::translate(test, glm::vec3(3.0, 0.0, 0.0));
test = glm::translate(test, glm::vec3(8.0, 0.0, 0.0));
test = glm::rotate(test, glm::radians(-100.0f * time), glm::vec3(0.0, 1.0, 0.0));
test = glm::translate(test, glm::vec3(-8.0, 0.0, 0.0));
test = glm::rotate(test, glm::radians(-100.0f * time), glm::vec3(1.0, 0.0, 0.0)); // self-rotation
任何想法我做错了什么?
这是旋转的图片:
Sun - Earth - Moon Rotations

最佳答案

您的评论建议您有以下操作顺序:

mvp = m_projection*m_view*m_model
vertex' = mvp*vertex
含义 m_view是 View 和 m_model 的逆矩阵是实际网格的直接矩阵。
所以你应该设置 m_projectionm_view一次然后只更新 m_model .
我不使用 GLM(有我自己的数学库)但是 IIRC 他们模仿旧的固定管道矩阵数学。所以当我这样做时(C++/VCL/OpenGL/GLSL ......我知道这些点应该是 VBO/VAO 我只是想快速测试):
//---------------------------------------------------------------------------
// ang ,ang speed,body r,orbit r
// [deg] ,[deg/s] [unit],[unit]
float rs=1.0; // star
float a0=0.0,da0= 50.0,r0=0.5,R0= 7.0; // planet
float a1=0.0,da1=200.0,r1=0.2,R1= 1.0; // moon
float a2=0.0,da2=250.0,r2=0.2,R2= 1.5; // moon
float a3=0.0,da3= 20.0,r3=0.5,R3=10.0; // planet
float a4=0.0,da4=150.0,r4=0.2,R4= 1.0; // moon
float a5=0.0,da5=180.0,r5=0.2,R5= 1.5; // moon
float b =0.0,db =50.0; // common self rotation
//---------------------------------------------------------------------------
void gl_draw()
{
GLint ix;
GLfloat mp[16],mv[16],mm[16],m0[16];
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

float aspect=float(xs)/float(ys);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0/aspect,aspect,0.1,100.0);
glGetFloatv(GL_PROJECTION_MATRIX,mp);
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0,0.0,-25.0);
glGetFloatv(GL_MODELVIEW_MATRIX,mv);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glGetFloatv(GL_MODELVIEW_MATRIX,mm);

glDisable(GL_DEPTH_TEST);
glDisable(GL_TEXTURE_2D);
glDisable(GL_CULL_FACE);
// glEnable(GL_CULL_FACE);

// GLSL sphere shader
glUseProgram(prog_id);
ix=glGetUniformLocation(prog_id,"m_projection"); glUniformMatrix4fv(ix,1,false,mp);
ix=glGetUniformLocation(prog_id,"m_view"); glUniformMatrix4fv(ix,1,false,mv);
ix=glGetUniformLocation(prog_id,"m_model");

// sun
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(b,0.0,0.0,1.0);
glGetFloatv(GL_MODELVIEW_MATRIX,mm); glUniformMatrix4fv(ix,1,false,mm);
glBegin(GL_POINTS); glColor3f(1.0,1.0,0.0); glVertex4f(0.0,0.0,0.0,rs); glEnd();
// planet
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(a0,0.0,0.0,1.0);
glTranslatef(R0,0.0,0.0);
glGetFloatv(GL_MODELVIEW_MATRIX,m0);
glRotatef(b,0.0,0.0,1.0);
glGetFloatv(GL_MODELVIEW_MATRIX,mm); glUniformMatrix4fv(ix,1,false,mm);
glBegin(GL_POINTS); glColor3f(0.0,0.7,1.0); glVertex4f(0.0,0.0,0.0,r0); glEnd();
// moon
glLoadMatrixf(m0);
glRotatef(a1,0.0,0.0,1.0);
glTranslatef(R1,0.0,0.0);
glRotatef(b,0.0,0.0,1.0);
glGetFloatv(GL_MODELVIEW_MATRIX,mm); glUniformMatrix4fv(ix,1,false,mm);
glBegin(GL_POINTS); glColor3f(0.4,0.4,0.4); glVertex4f(0.0,0.0,0.0,r1); glEnd();
// moon
glLoadMatrixf(m0);
glRotatef(a2,0.0,0.0,1.0);
glTranslatef(R2,0.0,0.0);
glRotatef(b,0.0,0.0,1.0);
glGetFloatv(GL_MODELVIEW_MATRIX,mm); glUniformMatrix4fv(ix,1,false,mm);
glBegin(GL_POINTS); glColor3f(0.4,0.4,0.4); glVertex4f(0.0,0.0,0.0,r2); glEnd();
// planet
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(a3,0.0,0.0,1.0);
glTranslatef(R3,0.0,0.0);
glGetFloatv(GL_MODELVIEW_MATRIX,m0);
glRotatef(b,0.0,0.0,1.0);
glGetFloatv(GL_MODELVIEW_MATRIX,mm); glUniformMatrix4fv(ix,1,false,mm);
glBegin(GL_POINTS); glColor3f(0.0,0.7,1.0); glVertex4f(0.0,0.0,0.0,r3); glEnd();
// moon
glLoadMatrixf(m0);
glRotatef(a4,0.0,0.0,1.0);
glTranslatef(R4,0.0,0.0);
glRotatef(b,0.0,0.0,1.0);
glGetFloatv(GL_MODELVIEW_MATRIX,mm); glUniformMatrix4fv(ix,1,false,mm);
glBegin(GL_POINTS); glColor3f(0.4,0.4,0.4); glVertex4f(0.0,0.0,0.0,r4); glEnd();
// moon
glLoadMatrixf(m0);
glRotatef(a5,0.0,0.0,1.0);
glTranslatef(R5,0.0,0.0);
glRotatef(b,0.0,0.0,1.0);
glGetFloatv(GL_MODELVIEW_MATRIX,mm); glUniformMatrix4fv(ix,1,false,mm);
glBegin(GL_POINTS); glColor3f(0.4,0.4,0.4); glVertex4f(0.0,0.0,0.0,r5); glEnd();

glUseProgram(0);

glFlush();
SwapBuffers(hdc);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
// this is periodicaly called by timer
gl_draw();
float dt=0.001*float(Timer1->Interval); // timer period in seconds
a0=fmod(a0+da0*dt,360.0);
a1=fmod(a1+da1*dt,360.0);
a3=fmod(a3+da3*dt,360.0);
a4=fmod(a4+da4*dt,360.0);
a5=fmod(a5+da5*dt,360.0);
b =fmod(b +db *dt,360.0);
}
//---------------------------------------------------------------------------
我得到了这个输出(使用我的球体着色器):
preview
着色器只需点 x,y,z,r作为球体 3D 中心和半径,发射 BBOX 四边形并使用法线着色渲染内 catch 体。它还使用颜色和您的 3 个矩阵。
因此,如果我认为正确,您应该执行以下操作:
model = identity; 
model = glm::rotate(model, glm::radians(b), glm::vec3(0.0, 1.0, 0.0));
// render star
model = identity;
model = glm::rotate(model, glm::radians(a0), glm::vec3(0.0, 1.0, 0.0));
model = glm::translate(model, glm::vec3(R0, 0.0, 0.0));
model0= model;
model = glm::rotate(model, glm::radians(b), glm::vec3(0.0, 1.0, 0.0));
// render planet
model = model0;
model = glm::rotate(model, glm::radians(a1), glm::vec3(0.0, 1.0, 0.0));
model = glm::translate(model, glm::vec3(R1, 0.0, 0.0));
model = glm::rotate(model, glm::radians(b), glm::vec3(0.0, 1.0, 0.0));
// render moon
model = model0;
model = glm::rotate(model, glm::radians(a2), glm::vec3(0.0, 1.0, 0.0));
model = glm::translate(model, glm::vec3(R2, 0.0, 0.0));
model = glm::rotate(model, glm::radians(b), glm::vec3(0.0, 1.0, 0.0));
// render moon
model = identity;
model = glm::rotate(model, glm::radians(a3), glm::vec3(0.0, 1.0, 0.0));
model = glm::translate(model, glm::vec3(R3, 0.0, 0.0));
model0= model;
model = glm::rotate(model, glm::radians(b), glm::vec3(0.0, 1.0, 0.0));
// render planet
model = model0;
model = glm::rotate(model, glm::radians(a4), glm::vec3(0.0, 1.0, 0.0));
model = glm::translate(model, glm::vec3(R4, 0.0, 0.0));
model = glm::rotate(model, glm::radians(b), glm::vec3(0.0, 1.0, 0.0));
// render moon
model = model0;
model = glm::rotate(model, glm::radians(a5), glm::vec3(0.0, 1.0, 0.0));
model = glm::translate(model, glm::vec3(R5, 0.0, 0.0));
model = glm::rotate(model, glm::radians(b), glm::vec3(0.0, 1.0, 0.0));
// render moon
如果这不起作用,那么您在矩阵顺序和使用的数学之间会出现其他不匹配,或者 GLM 的行为与我预期的不同。我使用普通角度进行自旋转,所以你只需为你的 body 添加索引......
还要注意旧的 GL 旋转使用 [deg]所以如果 GLM 想要 [rad]您需要转换角度和角速度常数...
如果您想要更精确/与现实世界相关的东西或更好地直观地看到这个:
  • Is it possible to make realistic n-body solar system simulation in matter of size and mass?
  • 关于c++ - OpenGL中的旋转立方体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66812765/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com