Visual Studio,GLUT 错误 - 以下是对 GLUT 3.0 的新检查;更新您的代码窗口 1 需要重新显示,但没有显示回调

问题描述

代码正在成功构建,但是当我运行代码时出现以下错误 - 以下是对 gluT 3.0 的新检查;更新您的代码gluT:D:\6th Sem\4 中的致命错误。 CG UCS505\EasyAlgo\Project1\Debug\Project1.exe:窗口1需要重新显示,但没有显示回调。

我已经尝试了所有方法,但无法弄清楚,我是 open gl 和 vs 的新手。感谢您的帮助。

这里是.cpp文件代码 -
source.cpp

#include <GL/glut.h>
#include <debug.hpp>
#include <config.hpp>
#include <blobs.hpp>
#include <fonts.hpp>
#include <draw.hpp>
#include <highlight.hpp>
#include <animation.hpp>
#include <unistd.h>

#include <iostream>
using namespace std;

unsigned short mainWindow,subWindow1,subWindow2;

void Init(float,float,float);
void Init(float,int,int);
void mainInit();
void display();
void onClick(int,int);
void keyPress(unsigned char,int);

int main(int argc,char** argv)
{
#ifdef DEBUG
    mode = "InsertionSort";
    b1.bv.resize(7,blobs());
    int rad[7] = { 28,32,20,28,36,45,31 };
    for (int p = 0; p < 7; p++)
        b1.bv[p].radius = (float)rad[p];
    b1.min = 20;
    b1.max = 45;
#endif

    glutinit(&argc,argv);
    glutinitdisplayMode(gluT_SINGLE | gluT_RGB | gluT_DEPTH);
    glutinitwindowSize(WindowWidth,WindowHeight);
    std::string title = "AlgoLucidator " + AL_VERSION;
    mainWindow = glutCreateWindow(title.c_str());
    glutPositionWindow(WIN_POS_X,WIN_POS_Y);
    mainInit();
    glutMouseFunc(onClick);
    glutKeyboardFunc(keyPress);

    subWindow1 = glutCreateSubWindow(mainWindow,WindowWidth / 4,WindowHeight);
    Init(0.80f,0.80f,0.60f);
    glutKeyboardFunc(keyPress);
    glutdisplayFunc(display);

    subWindow2 = glutCreateSubWindow(mainWindow,3 * WindowWidth / 4,WindowHeight);
    Init(0.65f,0.75f,0.70f);
    std::string mainInstruct = "Press 1. InsertionSort   2. BubbleSort   3. Dijkstra";
    printText(0,-(5 * mainInstruct.size()),540,mainInstruct);
    glutMouseFunc(onClick);
    glutKeyboardFunc(keyPress);
    glutdisplayFunc(display);
    glutMainLoop();

    return 0;
}

void Init(float r,float g,float b)
{
    int CurrWindowWidth = glutGet(gluT_WINDOW_WIDTH);
    int CurrWindowHeight = glutGet(gluT_WINDOW_HEIGHT);
    glClearColor(r,g,b,1);
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(-CurrWindowWidth,CurrWindowWidth,-CurrWindowHeight,CurrWindowHeight);
    drawTitle(CurrWindowWidth,CurrWindowHeight);
}

void Init(float r,float b,int width,int height)
{
    glClearColor(r,1);
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(-width,width,-height,height);
    drawTitle(width,height);
}

void mainInit()
{
    int CurrWindowWidth = glutGet(gluT_WINDOW_WIDTH);
    int CurrWindowHeight = glutGet(gluT_WINDOW_HEIGHT);
    glLoadIdentity();
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(0,CurrWindowHeight);
    glClearColor(0,0.80f);
    glClear(GL_COLOR_BUFFER_BIT);
}

void display()
{
    glFlush();
}

void onClick(int button,int state,int oldx,int oldy)
{}

void keyPress(unsigned char key,int x,int y)
{
    int activeWindow = glutGetwindow();
    switch (key)
    {
    case 13:  if (b1.pushreq)
    {
        b1.bv.push_back(blobs((float)base_radius));
    }
           if (activeWindow != subWindow2)
               glutSetwindow(subWindow2);
           if (mode == "InsertionSort")
           {
               drawInsertionSort();
           }
           else if (mode == "BubbleSort")
           {
               drawBubbleSort();
           }
           break;

    case 27:  glutDestroyWindow(mainWindow);  //Esc
        break;

    case 49:  mode = "InsertionSort";
        b1.reset();
        glutSetwindow(subWindow2);
        glutMouseFunc(onClick2);
        Init2(0.65f,0.70f);
        drawTitle3(960,640);
        break;

    case 50:  mode = "BubbleSort";
        glutSetwindow(subWindow2);
        glutMouseFunc(onClick3);
        glutIdleFunc(display);
        Init3(0.65f,0.70f);
        drawTitle4(960,640);
        break;

    default:;
    }
}

解决方法

错误消息几乎准确地说明出了什么问题以及如何修复它 (QFT):

以下是对 GLUT 3.0 的新检查;更新您的代码。 GLUT:(…) Project1.exe 中的致命错误:窗口 1 需要重新显示,但没有显示回调。

对于您创建的第一个窗口,第一个窗口,使用 glutCreateWindow 创建的窗口您没有指定显示回调:

mainWindow = glutCreateWindow(title.c_str());
glutPositionWindow(WIN_POS_X,WIN_POS_Y);
mainInit();
glutMouseFunc(onClick);
glutKeyboardFunc(keyPress);
/// <<<<<<<< something is missing here    
subWindow1 = glutCreateSubWindow(mainWindow,WindowWidth / 4,WindowHeight);

给你创建的第一个窗口一个显示回调,错误就会消失。

再说明一下:…Init 函数中发生的一切都不是初始化。 OpenGL 是一个状态机,在 …Init 中所做的一切都是应该在渲染帧开始时进行的状态准备。