OpenGL:调试断言失败
OpenGL: Debug assertion failed
#include <stdio.h>
#include <GLglew.h>
#include <GLfreeglut.h>
#define BUFFER_OFFSET(i) ((char *)NULL+(i))
GLuint shaderProgramID;
GLuint vao = 0;
GLuint vbo;
GLuint positionID, colorID;
#pragma region SHADER_FUNCTIONS
static char* readFile(const char* fileName)
{
//Open the file
FILE* fp = fopen(fileName, "r");
//Move the file pointer to the end of the file and determining the length
fseek(fp, 0, SEEK_END);
long file_length = ftell(fp);
fseek(fp, 0, SEEK_SET);
char* contents = new char[file_length + 1];
//zero out memory
for (int i = 0; i < file_length + 1; i++)
{
contents[i] = 0;
}
//Here's the actual read
fread(contents, 1, file_length, fp);
contents[file_length + 1] = ' ';
fclose(fp);
return contents;
}
bool compiledStatus(GLint shaderID){
GLint compiled = 0;
glGetShaderiv(shaderID, GL_COMPILE_STATUS, &compiled);
if (compiled) {
return true;
}
else {
GLint logLength;
glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &logLength);
char* msgBuffer = new char[logLength];
glGetShaderInfoLog(shaderID, logLength, NULL, msgBuffer);
printf("%sn", msgBuffer);
delete (msgBuffer);
return false;
}
}
//Takes in source code as string
GLuint makeVertexShader(const char* shaderSource)
{
//Call GL to make a vertex shader and get the ID
GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
//Bind the provided source code to the shader ID
glShaderSource(vertexShaderID, 1, (const GLchar**)&shaderSource, NULL);
//Compile the vertex Shader
glCompileShader(vertexShaderID);
bool compiledCorrect = compiledStatus(vertexShaderID);
if (compiledCorrect)
{
return vertexShaderID;
}
return -1;
return vertexShaderID;
}
GLuint makeFragmentShader(const char* shaderSource)
{
//Call GL to make a fragment shader and get the ID
GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
//Bind the provided source code to the shader ID
glShaderSource(fragmentShaderID, 1, (const GLchar**)&shaderSource, NULL);
//Compile the fragment Shader
glCompileShader(fragmentShaderID);
bool compiledCorrect = compiledStatus(fragmentShaderID);
if (compiledCorrect)
{
return fragmentShaderID;
}
return -1;
return fragmentShaderID;
}
#pragma endregion SHADER_FUNCTIONS
void changeViewport(int w, int h)
{
glViewport(0, 0, w, h);
}
//This is the function we are using each time the window needs to be redrawn
void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glDrawElements(GL_TRIANGLES, 4, GL_UNSIGNED_INT, NULL);
glDrawArrays(GL_TRIANGLES, 0, 3);
glutSwapBuffers();
}
GLuint makeShaderProgram(GLuint vertextShaderID, GLuint fragmentShaderID)
{
GLuint shaderID = glCreateProgram();
//Attach the vertex shader to the shader program
glAttachShader(shaderID, vertextShaderID);
//Attatch the fragment shader to the shader program
glAttachShader(shaderID, fragmentShaderID);
//Link all the shaders together
glLinkProgram(shaderID);
return shaderID;
}
int main(int argc, char** argv)
{
//Standards
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(800, 600);
glutCreateWindow("Test");
glutReshapeFunc(changeViewport);
glutDisplayFunc(render);
glewInit();
//Verticies
GLfloat vertices_0[] = { -0.5f, -0.5f, 0.0f, //0 -> Bottom Left
0.5f, -0.5f, 0.0f, //2 -> Bottom Right
0.0f, 0.5f, 0.0f };//3 -> Top Right
//Colors
GLfloat colors_0[] = { 1.0f, 0.0, 0.0f, 1.0f,//0
0.0f, 1.0f, 0.0f, 1.0f,//1
0.0f, 0.0f, 1.0f, 1.0f };//3
//Indicies -> Triangle 1 -> 0 1 2, Traingle 2 -> 1 3 2
//GLuint indicies_0[] = { 0, 1, 2, 1, 3, 2 };
//Read the vertex shader
char* vertexShaderSourceCode = readFile("vertexShader.vsh");
//Read the fragment shader
char* fragmentShaderSourceCode = readFile("fragmentShader.fsh");
//Make Vertex Shader
GLuint vertexShaderID = makeVertexShader(vertexShaderSourceCode);
//Make Fragment Shader
GLuint fragmentShaderID = makeFragmentShader(fragmentShaderSourceCode);
//Make Shader Program
shaderProgramID = makeShaderProgram(vertexShaderID, fragmentShaderID);
printf("Vertex Shader ID is %dn", vertexShaderID);
printf("Fragment Shader ID is %dn", fragmentShaderID);
printf("Shader Program ID is %dn", shaderProgramID);
printf("s_vPosition's ID is %dn", positionID);
//Create vertex array object
glGenVertexArrays(1, &vao);
//Bind Vertex array object
glBindVertexArray(vao);
//Create vertex buffer object
glGenBuffers(1, &vbo);
//Bind vertex buffer object
glBindBuffer(GL_ARRAY_BUFFER, vbo);
//Create Buffer ->7 Values for 4 vertices
glBufferData(GL_ARRAY_BUFFER, 7 * 3 * sizeof(GLfloat), NULL, GL_STATIC_DRAW);
//Starting at the beggining of the buffer, place the position data (3 values for 4 verticies)
glBufferSubData(GL_ARRAY_BUFFER, 0, 3 * 3 * sizeof(GLfloat), vertices_0);
//Starting after the placement of position data, place the color data (4 values for 4 verticies)
glBufferSubData(GL_ARRAY_BUFFER, 3 * 3 * sizeof(GLfloat), 3 * 4 * sizeof(GLfloat), colors_0);
//Generate the index buffer
//glGenBuffers(1, &indexBufferID);
//Bind the buffer
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
//Place index buffer data for the 6 indicies
//glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(GLuint), indicies_0, GL_STATIC_DRAW);
//Get the position attribute from the shader
positionID = glGetAttribLocation(shaderProgramID, "s_vPosition");
//Get the color attribute from the shader
colorID = glGetAttribLocation(shaderProgramID, "s_vColor");
//Tell the variables where they can find its info in the buffer
glVertexAttribPointer(positionID, 3, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(colorID, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(3 * 3 * sizeof(GLfloat)));
//Tell the program to use the shader program
glUseProgram(shaderProgramID);
//Turn on the position variable in the shader
glEnableVertexAttribArray(positionID);
//Turn on the color variable in the shader
glEnableVertexAttribArray(colorID);
glutMainLoop();
return 0;
}
当我运行程序时,它总是显示以下内容:错误
和窗口显示
谁能帮我解决它??# 我的代码有什么问题?我已经将"_CRT_SECURE_NO_WARNINGS"添加到预处理器定义中。
断
点和断言已经告诉你大部分内容:不允许使用指向 null 的文件指针调用 fseek,因为这表示文件无效。
我猜路径是错误的,因此无法打开文件。在发出任何文件访问调用之前,您绝对应该添加错误处理并检查fopen
是否成功。
我不确定您为什么认为_CRT_SECURE_NO_WARNINGS
可能与该问题有关,因为此定义在编译过程中会抑制一些警告,但在运行时不会更改任何内容。
相关文章:
- 尝试使用 std::vector<std::thread时出现静态断言失败错误>
- uint_not_usable_without_attribute在业力规则中使用数字生成器时静态断言失败
- 从 exe 文件 (Visual Studio ) 启动时调试断言失败
- 断言"id < 0"在Qt ActiveX中失败
- 在 CppUnit 中测试中止断言失败
- 使用扫描的调试断言失败
- MS 本机单元测试 - 断言::线程失败不起作用
- 为什么我的Qt程序在断言失败后继续运行?
- 图片不显示,关闭时出错 --> 调试断言失败!表达式:is_block_type_valid(标头>_block_use)
- 访问提升:shared_ptr 主范围外崩溃,断言失败:px != 0.指针的正确用法是什么?
- C++ 调试断言失败 - 矢量下标超出视觉工作室的范围
- 错误:断言失败 (src.type() == CV_8UC1) 在阈值中
- Boost Beast 异步服务器失败,断言失败:(id_ != T::id) 在多个 aync 调用中
- 使用imwrite OpenCV时断言失败
- 调试断言失败的缓冲区!=nullptr
- 使用 ofstream 写入文本文件时断言失败
- OpenCV 错误:相机校准:断言在matrix_wrap.cpp失败
- 提升继续恢复断言失败
- 获取有关调试断言失败的错误:表达式:"(_Ptr_user &(_BIG_ALLOCATION_ALIGNMENT -1)) == 0" &&0
- 打破gdb中失败断言的正确方法是什么