gpt4 book ai didi

c++ - OpenGL真实坐标和glutTimerFunc()问题C++

转载 作者:太空宇宙 更新时间:2023-11-04 12:26:02 25 4
gpt4 key购买 nike

嗨,我开始学习 C++ 的 openGl。但在声明点我卡住了。我有 2 个问题是绘制某些对象的坐标?我的意思是 X、Y 和 Z 在哪里?

第二个我正在从一些网站制作教程。我正在尝试为我的三角形制作动画。在教程中它可以工作但在我的电脑上不行。我还下载了源代码但它没有移动。并且没有编译错误。

编辑:我解决了问题。随着重新启动计算机。我认为这是我的计算机的问题。

这里是示例代码。我以为问题是 glutTimerFunc()。

 #include <iostream>
#include <stdlib.h>

#ifdef __APPLE__
#include<OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <gl/glut.h>
#endif

using namespace std;

//Called when a key is pressed
void handleKeypress(unsigned char key, int x, int y) {
switch (key) {
case 27: //Escape key
exit(0);
}
}

//Initializes 3D rendering
void initRendering() {
glEnable(GL_DEPTH_TEST);
}

//Called when the window is resized
void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
}

float _angle = 30.0f;
float _cameraAngle = 0.0f;

//Draws the 3D scene
void drawScene() {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective
glRotatef(-_cameraAngle, 0.0f, 1.0f, 0.0f); //Rotate the camera
glTranslatef(0.0f, 0.0f, -5.0f); //Move forward 5 units

glPushMatrix(); //Save the transformations performed thus far
glTranslatef(0.0f, -1.0f, 0.0f); //Move to the center of the trapezoid
glRotatef(_angle, 0.0f, 0.0f, 1.0f); //Rotate about the z-axis

glBegin(GL_QUADS);

//Trapezoid
glVertex3f(-0.7f, -0.5f, 0.0f);
glVertex3f(0.7f, -0.5f, 0.0f);
glVertex3f(0.4f, 0.5f, 0.0f);
glVertex3f(-0.4f, 0.5f, 0.0f);

glEnd();

glPopMatrix(); //Undo the move to the center of the trapezoid
glPushMatrix(); //Save the current state of transformations
glTranslatef(1.0f, 1.0f, 0.0f); //Move to the center of the pentagon
glRotatef(_angle, 0.0f, 1.0f, 0.0f); //Rotate about the y-axis
glScalef(0.7f, 0.7f, 0.7f); //Scale by 0.7 in the x, y, and z directions

glBegin(GL_TRIANGLES);

//Pentagon
glVertex3f(-0.5f, -0.5f, 0.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
glVertex3f(-0.5f, 0.0f, 0.0f);

glVertex3f(-0.5f, 0.0f, 0.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
glVertex3f(0.5f, 0.0f, 0.0f);

glVertex3f(-0.5f, 0.0f, 0.0f);
glVertex3f(0.5f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.5f, 0.0f);

glEnd();

glPopMatrix(); //Undo the move to the center of the pentagon
glPushMatrix(); //Save the current state of transformations
glTranslatef(-1.0f, 1.0f, 0.0f); //Move to the center of the triangle
glRotatef(_angle, 1.0f, 2.0f, 3.0f); //Rotate about the the vector (1, 2, 3)

glBegin(GL_TRIANGLES);

//Triangle
glVertex3f(0.5f, -0.5f, 0.0f);
glVertex3f(0.0f, 0.5f, 0.0f);
glVertex3f(-0.5f, -0.5f, 0.0f);

glEnd();

glPopMatrix(); //Undo the move to the center of the triangle

glutSwapBuffers();
}

void update(int value) {
_angle += 2.0f;
if (_angle > 360) {
_angle -= 260;
}

glutPostRedisplay(); //Tell GLUT that the display has changed

//Tell GLUT to call update again in 25 milliseconds
glutTimerFunc(25, update, 0);
}

int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);

//Create the window
glutCreateWindow("Transformations and Timers - videotutorialsrock.com");
initRendering();

//Set handler functions
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);

glutTimerFunc(24, update, 0); //Add a timer
glutMainLoop();

return 0;
}

最佳答案

关于你的第一个问题,你可以使用gluProject()应用你的变换矩阵 (via GL_MODELVIEW_MATRIX and GL_PROJECTION_MATRIX)到任意顶点。

关于c++ - OpenGL真实坐标和glutTimerFunc()问题C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2488908/

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