Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在openglc+中绘制一个球和两个圆锥体+;_C++_Eclipse_Opengl_Graphics_Glut - Fatal编程技术网

C++ 在openglc+中绘制一个球和两个圆锥体+;

C++ 在openglc+中绘制一个球和两个圆锥体+;,c++,eclipse,opengl,graphics,glut,C++,Eclipse,Opengl,Graphics,Glut,我有一个代码,我想一次画一个碗和两个圆锥体 但是,它只显示了那些圆锥体,没有显示球 #include <GL/glut.h> #include <stdlib.h> #include <Math.h> // Needed for sin, cos #define PI 3.14159265f GLfloat ballRadius = 0.5f; // Radius of the bouncing ball GLfloat ballX = 0.0

我有一个代码,我想一次画一个碗和两个圆锥体

但是,它只显示了那些圆锥体,没有显示球

#include <GL/glut.h>
#include <stdlib.h>
#include <Math.h>     // Needed for sin, cos

#define PI 3.14159265f

GLfloat ballRadius = 0.5f;   // Radius of the bouncing ball
GLfloat ballX = 0.0f;        // Ball's center (x, y) position
GLfloat ballY = 0.0f;
GLfloat ballXMax, ballXMin, ballYMax, ballYMin; // Ball's center (x, y) bounds
GLfloat xSpeed = 0.02f;      // Ball's speed in x and y directions
GLfloat ySpeed = 0.007f;
int refreshMillis = 30;      // Refresh period in milliseconds



static void resize(int width, int height)
{
    const float ar = (float) width / (float) height;

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);


}

static void display(void)
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

你看不到这个圆的原因是它紧靠着近平面。使用
glvrustum(-ar,ar,-1.0,1.0,2.0,100.0)指定近平面位于z=-2,远平面位于z=-100。超出这些值的任何内容都将被剪裁。但是通过使用
glVertex2
,圆顶点的z值为0,因此所有顶点都被剪裁。您可以通过调用
glTranslatef(ballX,ballY,-10.0f)来修复它取而代之

还有几点建议:

  • 始终将矩阵模式重置为GL_MODELVIEW(例如,在
    resize()
    函数中)。你不必这么做,但这是一个很好的惯例
  • 在修改矩阵堆栈之前(例如,在平移圆时),始终
    glPush/PopMatrix()
  • glColor3f(1.0f,0.0f,0.0f);//蓝色?;)
    glColor3f(1.0f,0.0f,0.0f);//蓝色?;)它被设置为蓝色,然后我更改了,但忘记更改注释。无论如何,谢谢你的建议。我要试试。我知道,这让我笑了。:)@乌梅尔很高兴我能帮忙。顺便说一句,你看到有一个SolidSphere
  • 函数了吗?不,我的@andrea中没有。我可以问你想说什么吗。@Umair如果你想画一个保龄球,渲染一个3D球体而不是2D光盘可能会给你带来更好的效果,只要你添加灯光。
        ***glTranslatef(ballX, ballY, 0.0f);  // Translate to (xPos, yPos)
         // Use triangular segments to form a circle
         glBegin(GL_TRIANGLE_FAN);
            glColor3f(1.0f, 0.0f, 0.0f);  // Blue
            glVertex2f(0.0f, 0.0f);       // Center of circle
            int numSegments = 100;
            GLfloat angle;
            for (int i = 0; i <= numSegments; i++) { // Last vertex same as first vertex
               angle = i * 2.0f * PI / numSegments;  // 360 deg for all segments
               glVertex2f(cos(angle) * ballRadius, sin(angle) * ballRadius);
            }
         glEnd();***
    
         glColor3d(0,1,0);
        glPushMatrix();
            glTranslated(-1.0,0.5,-6);
            glRotated(65, -1.0, 0.0, 0.0);
            glutSolidCone(1, 2, 70, 50);
        glPopMatrix();
    
        glPushMatrix();
            glTranslated(0.0,-1.5,-6);
            glRotated(65, -1.0, 3.0, 0.0);
            glutWireCone(1,2, 16, 16);
        glPopMatrix();
    
        glutSwapBuffers();
    }
    
    
    /* Program entry point */
    
    int main(int argc, char *argv[])
    {
        glutInit(&argc, argv);
        glutInitWindowSize(740,580);
        glutInitWindowPosition(10,10);
        glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    
        glutCreateWindow("Programming Techniques - 3D Cones");
    
        glutReshapeFunc(resize);
        glutDisplayFunc(display);
    
        glClearColor(1,1,1,1);
     glutMainLoop();
    
        return EXIT_SUCCESS;
    }