我在将参数传递到我的函数 Void ShaderCode() 之一时遇到问题,更具体地说,我收到此错误:
Error C2440: 'initializing' : cannot convert from 'GLchar' to 'const GLchar *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or 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;
}
最佳答案
以下函数是罪魁祸首:(至少有 2 个问题)
GLchar *ShaderCodeStorage(const char* FilePath)
// 1 Return type should be GLChar *
{
...
ShaderCode[Filelength] = 0;
return ShaderCode; // 2. add return
}
另外,您在哪里读取着色器文件的内容?确保填充文件内容。
关于c++ - OpenGL 着色器语言初始化错误无法从 GLChar 转换为常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36032806/