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

时间:2016-03-06 16:44:20

标签: c++ opengl vector-graphics

我正在尝试在open 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();
 }

1 个答案:

答案 0 :(得分:0)

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

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

这是一个易于理解的现代OpenGL教程:OpenGL tutorial for beginners