【问题标题】:OpenGL Shader language initializing error cannot conver from a GLChar to a constOpenGL Shader 语言初始化错误无法从 GLChar 转换为 const
【发布时间】:2016-03-16 10:21:17
【问题描述】:

我在将参数传递给我的函数 Void ShaderCode() 时遇到问题,更具体地说,我收到此错误:

错误 C2440:“正在初始化”:无法从“GLchar”转换为“const” GLchar *'
1> 整数类型到指针的转换 type 需要 reinterpret_cast、C-style cast 或 function-style cast

为了更深入了解这里是我的头文件:

typedef unsigned long Molong;
void CompileFunction();
GLchar ShaderCodeStorage(const char* FilePath);
void ShaderCode(const GLuint &ShaderHandler,  char* FilePath);
void CompileShader(const GLuint &ShaderHandler, const char* Response);
Molong getFileLength(std::ifstream& file);

这是我的 CPP

#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <fstream>
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtx/transform2.hpp"
#include "glm/gtc/type_precision.hpp"
#include "glew/glew.h"
#include <gl/GL.h>
#include <gl/GLU.h>
#include "glut.h"
#include "Storage.h"

using namespace std;

void CompileFunction()
{

    GLuint MyFirstVertShader = glCreateShader(GL_VERTEX_SHADER);

    if (0 == MyFirstVertShader)
    {
        std::cout << "There was an error making the shader" << std::endl;
        exit(1);
    }


     ShaderCode (MyFirstVertShader, "BasicVert.shader");

//  glShaderSource(MyFirstVertShader, 1, CodeArray, NULL);


        glCompileShader(MyFirstVertShader);

}

void ShaderCode(const GLuint &ShaderHandler,  char* FilePath)
{
    const GLchar *ShaderArray = ShaderCodeStorage(FilePath);
    glShaderSource(ShaderHandler, 1, &ShaderArray, NULL);

}


GLchar ShaderCodeStorage(const char* FilePath)
{
    ifstream infile;
    infile.open(FilePath, ios::in);
    unsigned long Filelength = getFileLength(infile);
    GLchar *ShaderCode = new char[Filelength + 1];
    ShaderCode[Filelength] = 0;
}


Molong getFileLength(ifstream& file) {
    if (!file.good()) return 0;
    file.seekg(0, ios::end);
    unsigned long Filelength = file.tellg();
    file.seekg(0, ios::beg);
    return Filelength;
}

【问题讨论】:

    标签: c++ opengl glsl


    【解决方案1】:

    以下函数是罪魁祸首:(至少有2个问题)

    GLchar *ShaderCodeStorage(const char* FilePath)
    // 1 Return type should be GLChar *
    {
        ...
        ShaderCode[Filelength] = 0;
        return ShaderCode; // 2. add return
    }
    

    还有你在哪里读取着色器文件的内容?确保填充文件内容。

    【讨论】:

    • 我还没有走到那一步,我正在研究我拥有的一些资源。但是,您刚刚解决了我的问题!当接受冷却到期时,我会批准答案。非常感谢!
    • @Mosesflava 我很高兴这个答案对你有帮助:) 还要确保delete 你使用new分配的内存
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多