只有黑色 window 绘制三角形,在 MacOS 上使用 openGL 和 GLUT Xcode

Only black window drawing Triangle, using openGL and GLUT with Xcode on MacOS

我的 window 上什么也没有显示,只有黑色纯色,构建继续进行,但没有其他任何事情发生..

我也在 Windows 上尝试了相同的代码,仍然没有。

这是我的代码:

#define _USE_MATH_DEFINES
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <vector>

#if defined(__APPLE__)
#include <GLUT/GLUT.h>
#include <OpenGL/gl3.h>
#include <OpenGL/glu.h>
#else
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
#include <windows.h>
#endif
#include <GL/glew.h>        // must be downloaded
#include <GL/freeglut.h>    // must be downloaded unless you have an Apple
#endif

using namespace std;

void changeViewPort(int w, int h)
{
    glViewport(0, 0, w, h);
}

void render()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBegin(GL_TRIANGLES);
    glVertex2f(0.5, 0.5);
    glVertex2f(-0.5, -0.5);
    glVertex2f(1.5, 1.5);
    glEnd();
    glutSwapBuffers();
}

int main(int argc, char* argv[]) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
    glutInitWindowSize(800, 600);
    glutCreateWindow("Hello, GL");
    glutReshapeFunc(changeViewPort);
    glutDisplayFunc(render);
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(0,400,0,500);
    glutMainLoop();
    return 0;
}

那是一个zero-area三角形,所有的顶点都在一条直线上。您可以 double-check 通过使用 GL_LINE_LOOP 而不是 GL_TRIANGLES 或使用 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE).

Zero-area 三角形在光栅化过程中一般不会产生任何碎片。没有碎片,没有画。

修复:

总计:

#include <GL/glut.h>

void render()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBegin(GL_TRIANGLES);
    glVertex2f( -0.5, -0.5 );
    glVertex2f(  0.5, -0.5 );
    glVertex2f(  0.0,  0.5 );
    glEnd();
    glutSwapBuffers();
}

int main(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
    glutInitWindowSize(800, 600);
    glutCreateWindow("Hello, GL");
    glutDisplayFunc(render);
    glutMainLoop();
    return 0;
}