简体   繁体   English

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

[英]Draw a solid cube in OpenGL

I am trying to draw a solid cube in open GL to serve as a background of an interface button.我试图在打开的 GL 中绘制一个实心立方体作为界面按钮的背景。 The problem is, it is not being drawn on the screen at all.问题是,它根本没有被绘制在屏幕上。 I have a class OGWindow that handles the drawing and a main class/method.我有一个处理绘图的类 OGWindow 和一个主类/方法。 I invoke all of the necessary methods from OGWindow in the main class.我从主类中的 OGWindow 调用了所有必要的方法。 What am I doing wrong here?我在这里做错了什么?

This is my main class:这是我的主要课程:

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();
}

This is my OGWindow class:这是我的 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();
}

And this is the method that is suppose to draw a solid cube in the scene:这是假设在场景中绘制实心立方体的方法:

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

Sorry if i'm not answering your question, but it seems you're not using modern OpenGL, functions like glMatrixMode(GL_PROJECTION);抱歉,如果我没有回答您的问题,但您似乎没有使用现代 OpenGL,像glMatrixMode(GL_PROJECTION);这样的函数glMatrixMode(GL_PROJECTION); and a lot more in your code should be avoided in modern applications.在现代应用程序中应该避免代码中的更多内容。

Instead of glVertex() and glColor() you should use a VBO and a shader.您应该使用 VBO 和着色器,而不是glVertex()glColor()

Here's a modern OpenGL tutorial wich is easy to follow: OpenGL tutorial for beginners这是一个很容易遵循的现代 OpenGL 教程:初学者的 OpenGL 教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM