每当我 运行 我的 OpenGL 代码时,我都会出现白屏和崩溃
Whenever I run my OpenGL Code I get a white screen and a crash
每当我 运行 我的 OpenGL 代码时,我创建的 window 会弹出几秒钟的空白,window 然后立即崩溃。我收到错误代码:(process 15692) exited with code -1073741819.
我不确定是什么问题。我使用了一些我制作的 类,例如 Shader,它只创建一个给定两个着色器的程序。这是下面的代码:
#pragma once
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <vector>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#ifndef STBI_INCLUDE_STB_IMAGE_H
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#endif
#include "Shader.h"
void processInput(GLFWwindow* window, Shader Program) {
static glm::mat4 BasicMatrix;
static glm::vec3 BasicVector;
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, true);
}
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
BasicMatrix = glm::mat4(1.0f);
BasicMatrix = translate(BasicMatrix, glm::vec3(0.05f, 0.0f, 0.0f));
glUniformMatrix4fv(glGetUniformLocation(Program.ID, "Matrices"), 1, GL_FALSE, glm::value_ptr(BasicMatrix));
}
}
void runTest() {
if (!glfwInit()) {
throw(-1);
std::cout << "Error: GLFW Init Failed" << std::endl;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(1920, 1080, "HAHAH BRRR", NULL, NULL);
if (window == NULL) {
std::cout << "Failed to create GLFW Window \n";
glfwTerminate();
throw (-1);
}
glfwMakeContextCurrent(window);
glewExperimental = true;
if (glewInit() != GLEW_OK) {
std::cout << "GLEW INIT FAILED\n";
exit(1);
}
//The first two parameters set the position of the lower left corner
glViewport(0, 0, 1920, 1080);
float vertex[] = {
-0.15f, -0.15f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
-0.15f, 0.15f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
0.15f, 0.15f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
-0.15f, 0.15f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f
};
unsigned int vertices[] = {
0, 1, 3,
3, 1, 2
};
const char* vertexTexShader =
"#version 330 core\n"
"\n"
"layout (location = 0) in vec3 aPos;\n"
"layout (location = 1) in vec3 aColor;\n"
"layout (location = 2) in vec2 TexCoords;\n"
"\n"
"out vec3 Color;\n"
"out vec2 TexCoord;\n"
"\n"
"uniform mat4 Matrices;\n"
"\n"
"void main(){\n"
"\n"
"gl_Position = Matrices * vec4(aPos, 1.0f);\n"
"Color = aColor;\n"
"TexCoord = TexCoords;\n"
"\n"
"}\n";
const char* fragmentTexShader =
"#version 330 core\n"
"\n"
"in vec3 Color;\n"
"in vec2 TexCoord;\n"
"out vec4 FragColor;\n"
"uniform sampler2D texture1;\n"
"\n"
"void main(){\n"
"\n"
"\n"
"FragColor = texture(texture1, TexCoord) * Color;\n"
"\n"
"\n"
"}\n";
Shader GameProgram(vertexTexShader, fragmentTexShader, 1);
unsigned int VAO4, VBO4, EBO4;
glGenVertexArrays(1, &VAO4);
glBindVertexArray(VAO4);
glGenBuffers(1, &VBO4);
glBindBuffer(GL_ARRAY_BUFFER, VBO4);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex), vertex, GL_STATIC_DRAW);
glGenBuffers(GL_ELEMENT_ARRAY_BUFFER, &EBO4);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO4);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(0));
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
unsigned int Texture3;
glGenTextures(1, &Texture3);
glBindTexture(GL_TEXTURE_2D, Texture3);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
int width, heigth, nrChannles;
unsigned char* data = stbi_load("src/Images/awesomeface.png", &width, &heigth, &nrChannles, 4);
if (data) {
glTexImage2D(GL_TEXTURE_2D, 1, GL_RGB, width, heigth, 0, GL_RGBA, GL_UNSIGNED_BYTE, &data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else {
std::cout << "ERROR: Could Not Generate Texture\n REASON:";
std::cout << stbi_failure_reason();
}
GameProgram.use();
glUniform1i(glGetUniformLocation(GameProgram.ID, "texture1"), 0);
while (glfwWindowShouldClose(window)) {
processInput(window, GameProgram);
glClearColor(0.5f, 0.2f, 0.6f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, Texture3);
glBindVertexArray(VAO4);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
}
应用程序中的所有错误都可以在调试程序时轻松找到。因此我投票结束你的问题。
无论如何,这里是您的错误的简要列表:
shader程序编译失败,因为变量Color
的类型是vec3
:
"FragColor = texture(texture1, TexCoord) * Color;
FragColor = texture(texture1, TexCoord) * vec4(Color, 1.0);
还有一个错别字:
glGenBuffers(GL_ELEMENT_ARRAY_BUFFER, &EBO4);
glGenBuffers(1, &EBO4);
glTexImage2D
的第二个参数必须是0:
glTexImage2D(GL_TEXTURE_2D, 1, GL_RGB, width, heigth, 0, GL_RGBA, GL_UNSIGNED_BYTE, &data);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, heigth, 0, GL_RGBA, GL_UNSIGNED_BYTE, &data);
您的应用程序循环立即终止:
while (glfwWindowShouldClose(window)) {
while (!glfwWindowShouldClose(window)) {
每当我 运行 我的 OpenGL 代码时,我创建的 window 会弹出几秒钟的空白,window 然后立即崩溃。我收到错误代码:(process 15692) exited with code -1073741819.
我不确定是什么问题。我使用了一些我制作的 类,例如 Shader,它只创建一个给定两个着色器的程序。这是下面的代码:
#pragma once
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <vector>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#ifndef STBI_INCLUDE_STB_IMAGE_H
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#endif
#include "Shader.h"
void processInput(GLFWwindow* window, Shader Program) {
static glm::mat4 BasicMatrix;
static glm::vec3 BasicVector;
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, true);
}
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
BasicMatrix = glm::mat4(1.0f);
BasicMatrix = translate(BasicMatrix, glm::vec3(0.05f, 0.0f, 0.0f));
glUniformMatrix4fv(glGetUniformLocation(Program.ID, "Matrices"), 1, GL_FALSE, glm::value_ptr(BasicMatrix));
}
}
void runTest() {
if (!glfwInit()) {
throw(-1);
std::cout << "Error: GLFW Init Failed" << std::endl;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(1920, 1080, "HAHAH BRRR", NULL, NULL);
if (window == NULL) {
std::cout << "Failed to create GLFW Window \n";
glfwTerminate();
throw (-1);
}
glfwMakeContextCurrent(window);
glewExperimental = true;
if (glewInit() != GLEW_OK) {
std::cout << "GLEW INIT FAILED\n";
exit(1);
}
//The first two parameters set the position of the lower left corner
glViewport(0, 0, 1920, 1080);
float vertex[] = {
-0.15f, -0.15f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
-0.15f, 0.15f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
0.15f, 0.15f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
-0.15f, 0.15f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f
};
unsigned int vertices[] = {
0, 1, 3,
3, 1, 2
};
const char* vertexTexShader =
"#version 330 core\n"
"\n"
"layout (location = 0) in vec3 aPos;\n"
"layout (location = 1) in vec3 aColor;\n"
"layout (location = 2) in vec2 TexCoords;\n"
"\n"
"out vec3 Color;\n"
"out vec2 TexCoord;\n"
"\n"
"uniform mat4 Matrices;\n"
"\n"
"void main(){\n"
"\n"
"gl_Position = Matrices * vec4(aPos, 1.0f);\n"
"Color = aColor;\n"
"TexCoord = TexCoords;\n"
"\n"
"}\n";
const char* fragmentTexShader =
"#version 330 core\n"
"\n"
"in vec3 Color;\n"
"in vec2 TexCoord;\n"
"out vec4 FragColor;\n"
"uniform sampler2D texture1;\n"
"\n"
"void main(){\n"
"\n"
"\n"
"FragColor = texture(texture1, TexCoord) * Color;\n"
"\n"
"\n"
"}\n";
Shader GameProgram(vertexTexShader, fragmentTexShader, 1);
unsigned int VAO4, VBO4, EBO4;
glGenVertexArrays(1, &VAO4);
glBindVertexArray(VAO4);
glGenBuffers(1, &VBO4);
glBindBuffer(GL_ARRAY_BUFFER, VBO4);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex), vertex, GL_STATIC_DRAW);
glGenBuffers(GL_ELEMENT_ARRAY_BUFFER, &EBO4);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO4);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(0));
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
unsigned int Texture3;
glGenTextures(1, &Texture3);
glBindTexture(GL_TEXTURE_2D, Texture3);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
int width, heigth, nrChannles;
unsigned char* data = stbi_load("src/Images/awesomeface.png", &width, &heigth, &nrChannles, 4);
if (data) {
glTexImage2D(GL_TEXTURE_2D, 1, GL_RGB, width, heigth, 0, GL_RGBA, GL_UNSIGNED_BYTE, &data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else {
std::cout << "ERROR: Could Not Generate Texture\n REASON:";
std::cout << stbi_failure_reason();
}
GameProgram.use();
glUniform1i(glGetUniformLocation(GameProgram.ID, "texture1"), 0);
while (glfwWindowShouldClose(window)) {
processInput(window, GameProgram);
glClearColor(0.5f, 0.2f, 0.6f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, Texture3);
glBindVertexArray(VAO4);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
}
应用程序中的所有错误都可以在调试程序时轻松找到。因此我投票结束你的问题。
无论如何,这里是您的错误的简要列表:
shader程序编译失败,因为变量
Color
的类型是vec3
:"FragColor = texture(texture1, TexCoord) * Color;
FragColor = texture(texture1, TexCoord) * vec4(Color, 1.0);
还有一个错别字:
glGenBuffers(GL_ELEMENT_ARRAY_BUFFER, &EBO4);
glGenBuffers(1, &EBO4);
glTexImage2D
的第二个参数必须是0:glTexImage2D(GL_TEXTURE_2D, 1, GL_RGB, width, heigth, 0, GL_RGBA, GL_UNSIGNED_BYTE, &data);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, heigth, 0, GL_RGBA, GL_UNSIGNED_BYTE, &data);
您的应用程序循环立即终止:
while (glfwWindowShouldClose(window)) {
while (!glfwWindowShouldClose(window)) {