c – glewInit()失败和OpenGL错误1282

c – glewInit()失败和OpenGL错误1282,第1张

概述我有一个非常非常基本的OpenGL程序,使用glfw3作为窗口的东西. 这是我的主要内容: //Headers#include <GL\glew.h>#include <GLFW/glfw3.h>#include "Utils.h"//Function Prototypesvoid setupEvents(GLFWwindow* window);//Main function 我有一个非常非常基本的OpenGL程序,使用glfw3作为窗口的东西.

这是我的主要内容:

//headers#include <GL\glew.h>#include <GLFW/glfw3.h>#include "Utils.h"//Function PrototypesvoID setupEvents(GLFWwindow* window);//Main functionint main(voID){    GLFWwindow* window;    if (!glfwInit())    exit(EXIT_FAILURE);    window = glfwCreateWindow(640,480,"Simple example",NulL,NulL);    glewExperimental = GL_FALSE;    GLenum error = glGetError();    if (error != GL_NO_ERROR)    {        std::cout << "OpenGL Error: " << error << std::endl;    }    GLenum glewinit = glewInit();    if (glewinit != GLEW_OK) {        std::cout << "Glew not okay! " << glewinit;        exit(EXIT_FAILURE);    }    if (!window){   glfwTerminate();    exit(EXIT_FAILURE); } //Failed to create window    //Make our window current    glfwMakeContextCurrent(window);    setupEvents(window);    //Let's make our program    //gluint glProgram = LoadShaders("../Shaders/Section_1/Basic.vert","../Shaders/Section_1/Basic.frag");    gluint glProgram = LoadShaders("Basic.vert","Basic.frag");    while (!glfwwindowshouldClose(window))    {        glClear(GL_color_BUFFER_BIT);        //Swap buffers and call events.        glfwSwapBuffers(window);        glfwPollEvents();    }    //Destory our window and exit glfw.    glfwDestroyWindow(window);     glfwTerminate();    exit(EXIT_SUCCESS);}static voID key_callback(GLFWwindow* window,int key,int scancode,int action,int mods){    //The key callback    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)        glfwSetwindowshouldClose(window,GL_TRUE);}voID setupEvents(GLFWwindow* window) {    //Setup our events.    glfwSetKeyCallback(window,key_callback);}

utils的:

#include <GL\glew.h>#include <glm/glm.hpp>#include <fstream> #include <vector>#include <stdlib.h>#include <algorithm>#include <iostream>#include <string>using namespace std;gluint LoadShaders(const char * vertex_file_path,const char * fragment_file_path){    // Create the shaders    gluint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);    gluint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);    // Read the Vertex Shader code from the file    std::string Vertexshadercode;    std::ifstream VertexShaderStream(vertex_file_path,std::ios::in);    if (VertexShaderStream.is_open())    {        std::string line = "";        while (getline(VertexShaderStream,line))            Vertexshadercode += "\n" + line;        VertexShaderStream.close();    }    // Read the Fragment Shader code from the file    std::string Fragmentshadercode;    std::ifstream FragmentShaderStream(fragment_file_path,std::ios::in);    if (FragmentShaderStream.is_open()){        std::string line = "";        while (getline(FragmentShaderStream,line))            Fragmentshadercode += "\n" + line;        FragmentShaderStream.close();    }    Glint Result = GL_FALSE;    int InfologLength;    // Compile Vertex Shader    printf("Compiling shader : %s\n",vertex_file_path);    char const * VertexSourcePointer = Vertexshadercode.c_str();    glShaderSource(VertexShaderID,1,&VertexSourcePointer,NulL);    glCompileShader(VertexShaderID);    // Check Vertex Shader    glGetShaderiv(VertexShaderID,GL_COMPILE_STATUS,&Result);    glGetShaderiv(VertexShaderID,GL_INFO_LOG_LENGTH,&InfologLength);    std::vector<char> VertexShaderErrorMessage(InfologLength);    glGetShaderInfolog(VertexShaderID,InfologLength,&VertexShaderErrorMessage[0]);    fprintf(stdout,"%s\n",&VertexShaderErrorMessage[0]);    // Compile Fragment Shader    printf("Compiling shader : %s\n",fragment_file_path);    char const * FragmentSourcePointer = Fragmentshadercode.c_str();    glShaderSource(FragmentShaderID,&FragmentSourcePointer,NulL);    glCompileShader(FragmentShaderID);    // Check Fragment Shader    glGetShaderiv(FragmentShaderID,&Result);    glGetShaderiv(FragmentShaderID,&InfologLength);    std::vector<char> FragmentShaderErrorMessage(InfologLength);    glGetShaderInfolog(FragmentShaderID,&FragmentShaderErrorMessage[0]);    fprintf(stdout,&FragmentShaderErrorMessage[0]);    // link the program    fprintf(stdout,"linking program\n");    gluint ProgramID = glCreateProgram();    glAttachShader(ProgramID,VertexShaderID);    glAttachShader(ProgramID,FragmentShaderID);    gllinkProgram(ProgramID);    // Check the program    glGetProgramiv(ProgramID,GL_link_STATUS,&Result);    glGetProgramiv(ProgramID,&InfologLength);    std::vector<char> ProgramErrorMessage(max(InfologLength,int(1)));    glGetProgramInfolog(ProgramID,&ProgramErrorMessage[0]);    fprintf(stdout,&ProgramErrorMessage[0]);    glDeleteShader(VertexShaderID);    glDeleteShader(FragmentShaderID);    return ProgramID;}

输出:

OpenGL Error: 1282Glew not okay! 1

我真的不知道我做错了什么.

解决方法 您最近检查窗口创建失败,并且您尝试在没有活动GL上下文的情况下调用GL函数.两者都错了.它应该是(复制粘贴并重新订购您的代码):

GLFWwindow* window;if (!glfwInit())    exit(EXIT_FAILURE);window = glfwCreateWindow(640,NulL);if (!window){   glfwTerminate();    exit(EXIT_FAILURE); } //Failed to create window//Make our window currentglfwMakeContextCurrent(window);glewExperimental = GL_FALSE;GLenum error = glGetError();if (error != GL_NO_ERROR){    std::cout << "OpenGL Error: " << error << std::endl;}GLenum glewinit = glewInit();if (glewinit != GLEW_OK) {    std::cout << "Glew not okay! " << glewinit;    exit(EXIT_FAILURE);}
总结

以上是内存溢出为你收集整理的c – glewInit()失败和OpenGL错误1282全部内容,希望文章能够帮你解决c – glewInit()失败和OpenGL错误1282所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/1220091.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-05
下一篇2022-06-05

发表评论

登录后才能评论

评论列表(0条)

    保存