加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 10:25:55 所属栏目:百科 来源:网络整理
导读:我有一个非常非常基本的OpenGL程序,使用glfw3作为窗口的东西. 这是我的主要内容: //Headers#include GLglew.h#include GLFW/glfw3.h#include "Utils.h"//Function Prototypesvoid setupEvents(GLFWwindow* window);//Main functionint main(void){ GLFWwin
我有一个非常非常基本的OpenGL程序,使用glfw3作为窗口的东西.

这是我的主要内容:

//Headers
#include <GLglew.h>
#include <GLFW/glfw3.h>
#include "Utils.h"



//Function Prototypes
void setupEvents(GLFWwindow* window);


//Main function
int 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 <GLglew.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 : %sn",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,"%sn",&VertexShaderErrorMessage[0]);

    // Compile Fragment Shader
    printf("Compiling shader : %sn",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 programn");
    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: 1282
Glew 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 current
glfwMakeContextCurrent(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);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读