在 OpenGL 中绘制一个实体立方体

Draw a solid cube in OpenGL

本文关键字:一个 实体 立方体 OpenGL 绘制      更新时间:2023-10-16

我正在尝试在打开的GL中绘制一个实体立方体作为界面按钮的背景。问题是,它根本没有被绘制在屏幕上。我有一个处理绘图的类 OGWindow 和一个主类/方法。我在主类中调用了 OGWindow 的所有必要方法。我在这里做错了什么?

这是我的主要课程:

OGWindow    theWindow;
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);   
glutInitWindowSize( 1000, 600 );
glutInitWindowPosition(0, 0);
glutCreateWindow("OpenGL Demo");
glEnable(GL_DEPTH_TEST);    // enable the depth buffer test
glutDisplayFunc(DrawGLScene);
glutReshapeFunc(ReSizeGLScene);
glutMouseFunc(mouseClick);
glutMotionFunc(mouseMotion);
glutPassiveMotionFunc(mousePassiveMotion);
glutIdleFunc(Idle);
theWindow.initGL(); 
glutMainLoop();
}
void DrawGLScene(void) {
   theWindow.myDrawGLScene();
}

这是我的 OGWindow 类:

void OGWindow::initGL(void) {

glClearColor(1.0, 1.0, 1.0, 1.0);
squareX = 1.0;
squareY = 1.0;
squareWidth = 40.0;
squareHeight = 40.0;
squareColour = RED;
squareDraggingP = false;
}

void OGWindow::MyReSizeGLScene(int fwidth, int fheight) 
{
// Store window size  in class variables so it can be accessed in myDrawGLScene() if necessary
wWidth = fwidth;
wHeight = fheight;
// Calculate aspect ration of the OpenGL window
aspect_ratio = (float) fwidth / fheight;
// Set camera so it can see a square area of space running from 0 to 10 
// in both X and Y directions, plus a bit of space around it.
Ymin = 0;
Xmin = 0;
Ymax = 600;
// Choose Xmax so that the aspect ration of the projection
// = the aspect ratio of the viewport
//Xmax = (aspect_ratio * (Ymax -Ymin)) + Xmin;
Xmax = 1000;
glMatrixMode(GL_PROJECTION);        // Select The Projection Stack
glLoadIdentity();
glOrtho(Xmin, Xmax, Ymin, Ymax, -1.0, 1.0);
glViewport(0, 0, wWidth, wHeight);      // Viewport fills the window
}

void OGWindow::myDrawGLScene(GLvoid)        // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the drawing area
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
OGWindow::myDrawModel();
glColor3f(0.0, 0.0, 0.0);
drawToolbar();
drawCreateButton();
glutSwapBuffers(); // Needed if we're running an animation
glFlush();
}

这是假设在场景中绘制实体立方体的方法:

void OGWindow::drawCreateButton(GLvoid){
glPushMatrix();
glColor3f(0.0, 1.0, 0.0);
    glTranslatef(10.0,30.0,0.0);
    glutSolidCube(4);
glPopMatrix();
 }
抱歉,

如果我没有回答您的问题,但您似乎没有使用现代 OpenGL,在现代应用程序中应避免使用glMatrixMode(GL_PROJECTION);等函数以及代码中的更多功能。

您应该使用 VBO 和着色器,而不是glVertex()glColor()

这是一个易于遵循的现代OpenGL教程:面向初学者的OpenGL教程