c++ - opengl不画任何东西

标签 c++ opengl

只是尝试使用 glut 和 glew 为 opengl 4.3 版画一个点

我的代码

void Renderer(void)
{ 
    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_POINTS);
    glPointSize(100);
    glColor3f(255, 0, 0);
    glVertex3d(10, 10, 0);
    glEnd();

    glFlush();
    glutSwapBuffers();
}

没有渲染任何东西,有人能告诉我我错过了什么吗?这是完整的代码:

#include <iostream>
using namespace std;

#include "vgl.h"
#include "LoadShaders.h"

enum VAO_IDs { Triangles, NumVAOS };

#define WIDTH 1024
#define HEIGHT 768
#define REFRESH_DELAY     10 //ms

//general
long frameCount = 0;

// mouse controls
int mouse_old_x, mouse_old_y;
int mouse_buttons = 0;
float rotate_x = 0.0, rotate_y = 0.0;
float translate_z = -3.0;

/////////////////////////////////////////////////////////////////////
//! Prototypes
/////////////////////////////////////////////////////////////////////
void keyboard(unsigned char key, int x, int y);
void mouse(int button, int state, int x, int y);
void motion(int x, int y);

void timerEvent(int value)
{
    glutPostRedisplay();
    glutTimerFunc(REFRESH_DELAY, timerEvent, frameCount++);
}

void init (void)
{
    // default initialization
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glDisable(GL_DEPTH_TEST);

    // viewport
    glViewport(0, 0, WIDTH, HEIGHT);

    // projection
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60.0, (GLfloat)WIDTH / (GLfloat)HEIGHT, 1, 10000.0);
}

void Renderer(void)
{ 
    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_POINTS);
    glPointSize(100);
    glColor3f(255, 0, 0);
    glVertex3d(10, 10, 0);
    glEnd();

    glFlush();
    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA);
    glutInitWindowSize(WIDTH, HEIGHT);
    glutInitContextVersion(4, 3);
    glutInitContextProfile(GLUT_CORE_PROFILE);
    glutCreateWindow("The Abyss");
    glutKeyboardFunc(keyboard);
    glutMotionFunc(motion);
    glutMouseFunc(mouse);

    glutTimerFunc(REFRESH_DELAY, timerEvent, frameCount);

    if (glewInit()) //i guess this is true on failure
    {
        cerr << "Error initializing glew, Program aborted." << endl;
        exit(EXIT_FAILURE);
    }


    //Init First
    init();

    //Init callback for Rendering
    glutDisplayFunc(Renderer);

    //Main Loop
    glutMainLoop();

    exit(EXIT_SUCCESS);
}

////////////////////////////////////////////////////////////////////////
//!Mouse and keyboard functionality
////////////////////////////////////////////////////////////////////////
void keyboard(unsigned char key, int /*x*/, int /*y*/)
{
    switch (key)
    {
        case (27) :
            exit(EXIT_SUCCESS);
            break;
    }
}

void mouse(int button, int state, int x, int y)
{
    if (state == GLUT_DOWN)
    {
        mouse_buttons |= 1<<button;
    }
    else if (state == GLUT_UP)
    {
        mouse_buttons = 0;
    }

    mouse_old_x = x;
    mouse_old_y = y;
}

void motion(int x, int y)
{
    float dx, dy;
    dx = (float)(x - mouse_old_x);
    dy = (float)(y - mouse_old_y);

    if (mouse_buttons & 1)
    {
        rotate_x += dy * 0.2f;
        rotate_y += dx * 0.2f;
    }
    else if (mouse_buttons & 4)
    {
        translate_z += dy * 0.01f;
    }

    mouse_old_x = x;
    mouse_old_y = y;
}

最佳答案

glutInitContextVersion(4, 3);
glutInitContextProfile(GLUT_CORE_PROFILE);

您已请求核心上下文。你需要:

  1. 指定顶点和片段着色器。 Core 中没有免费赠品。
  2. 停止使用已弃用的功能,如 glBegin()glMatrixMode()
  3. 开始使用 VBO 提交您的几何图形。
  4. 开始使用 glDrawArrays() 和 friend 来绘制您的几何图形。

关于c++ - opengl不画任何东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19393731/

相关文章:

iphone - 在 iPhone 上混合两个 FBO 会产生奇怪的结果?

c++ - 在 cmd 中运行和构建项目 C++

c++ - C++ 中的位图支持

c++ - 我怎样才能声明一个常量数据成员但稍后才初始化它?

c++ - 为什么在 C++ 中声明枚举时使用 typedef?

c++ - glm 数据类型如何通过 OpenGL 缓冲区直接传递给 GPU?

c++ - OpenCV 尝试分配 10 艾字节

c++ - 土壤:C++ 和 OpenGL 中的 'Unable to open file' 与 Xcode

opengl - 交错顶点提交对性能有何帮助?

c++ - 使用 Wavefront Obj 了解法线指标