opengl没有绘制任何东西

时间:2013-10-16 01:19:19

标签: c++ opengl

试图用opengl版本4.3的glut and glew绘制一个点

我的代码

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

1 个答案:

答案 0 :(得分:2)

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

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

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