我无法使用下面编写的代码绘制到我创建的窗口。
我所知道的是正确的:
我认为可能是问题:
我通过错误检查排除了什么:
注意:我已将内容编写器内联(glsl.h)。
的main.cpp
#include <GL\glew.h>
#include <SDL.h>
#include <glm\glm.hpp>
#include <iostream>
#include "glsl.h"
#define SDL_INIT_FAIL -1
#define IMAGE_INIT_FAIL 0
#define glsl(version, glsl) "#version " #version "\n" #glsl
namespace glsl { namespace vs {
//VERTEX SHADERS
//=========================
// simple VS
//=========================
constexpr GLchar * const simple = glsl(450 core,
layout(location = 0) in vec3 position;
void main() {
gl_Position = vec4(position, 1.0f);
}
);
} namespace fs {
//FRAGMENT SHADERS
//=========================
// simple FS
//=========================
constexpr GLchar * const simple = glsl(450 core,
out vec4 color;
void main() {
color = vec4(1.0f, 0.0f, 0.0f, 1.0f);
}
);
} }
constexpr GLint image_Flags{ IMG_INIT_PNG | IMG_INIT_JPG };
constexpr GLuint init_Flags{ SDL_INIT_VIDEO | SDL_INIT_EVENTS },window_Flags{ SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE};
constexpr GLchar * window_Title{ "Sprite Game with OpenGL" };
constexpr GLint window_Width{ 1200 }, window_Height{ 740 }, window_PositionX{ SDL_WINDOWPOS_CENTERED },
window_PositionY{ SDL_WINDOWPOS_CENTERED };
int main(int argc, char * argv[]) {
std::cout << glsl::vs::simple << std::endl << std::endl;
std::cout << glsl::fs::simple << std::endl << std::endl;
system("pause");
if (SDL_Init(init_Flags) == SDL_INIT_FAIL) {
std::cerr << "error init sdl" << std::endl;
return 1;
}
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 23);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_Window * window = nullptr;
window = SDL_CreateWindow(window_Title, window_PositionX, window_PositionY, window_Width, window_Height, window_Flags);
if (window == nullptr) {
std::cerr << "error window is nullptr" << std::endl;
SDL_Quit();
return 4;
}
SDL_GLContext context = nullptr;
context = SDL_GL_CreateContext(window);
if (context == nullptr) {
std::cerr << "error context is nullptr" << std::endl;
SDL_DestroyWindow(window);
window = nullptr;
SDL_Quit();
return 5;
}
glewExperimental = true;
GLenum glewError = glewInit();
if (glewError != GLEW_OK) {
std::cerr << "error glew init: " << glewGetErrorString(glewError) << std::endl;
SDL_Quit();
return 3;
}
std::cout << "OpenGL Version " << glGetString(GL_VERSION) << std::endl;
struct Mouse
{
Uint32 mouseState = 0;
GLint mx{ 0 }, my{ 0 };
};
struct Input {
Mouse mouse;
SDL_Event e;
const Uint8 * keyState = nullptr;
} input;
struct Shader {
GLuint program;
GLuint vertexShader;
GLuint fragmentShader;
} shader;
shader.vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(shader.vertexShader, 1, &glsl::vs::simple, NULL);
glCompileShader(shader.vertexShader);
GLint vertexCompilei;
GLchar vsiInfo[512];
glGetShaderiv(shader.vertexShader, GL_COMPILE_STATUS, &vertexCompilei);
if (vertexCompilei == GL_FALSE) {
glGetShaderInfoLog(shader.vertexShader, sizeof vsiInfo, NULL, vsiInfo);
std::cerr << vsiInfo << std::endl;
}
shader.fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(shader.fragmentShader, 1, &glsl::fs::simple, NULL);
glCompileShader(shader.fragmentShader);
GLint fragmentCompilei;
GLchar fsiInfo[512];
glGetShaderiv(shader.fragmentShader, GL_COMPILE_STATUS, &fragmentCompilei);
if (fragmentCompilei == GL_FALSE) {
glGetShaderInfoLog(shader.fragmentShader, sizeof fsiInfo, NULL, fsiInfo);
std::cerr << fsiInfo << std::endl;
}
shader.program = glCreateProgram();
glAttachShader(shader.program, shader.vertexShader);
glAttachShader(shader.program, shader.fragmentShader);
glLinkProgram(shader.program);
GLint programLinki;
GLchar programInfo[512];
glGetProgramiv(shader.program, GL_LINK_STATUS, &programLinki);
if (programLinki == GL_FALSE) {
glGetProgramInfoLog(shader.program, sizeof programInfo, NULL, programInfo);
std::cerr << programInfo << std::endl;
}
struct Object {
GLuint VAO;
GLuint VBO;
} object;
GLfloat vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
glGenVertexArrays(1, &object.VAO);
glGenBuffers(1, &object.VBO);
glBindVertexArray(object.VAO);
glBindBuffer(GL_ARRAY_BUFFER, object.VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof GLfloat * 3, (GLvoid*)0);
glBindVertexArray(0);
glEnable(GL_DEPTH_TEST);
do {
glViewport(0, 0, window_Width, window_Height);
glClearColor(0.3f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glUseProgram(shader.program);
glBindVertexArray(object.VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
glUseProgram(0);
SDL_GL_SwapWindow(window);
SDL_PollEvent(&input.e);
input.keyState = SDL_GetKeyboardState(0);
input.mouse.mouseState = SDL_GetMouseState(&input.mouse.mx, &input.mouse.my);
} while (!input.keyState[SDL_SCANCODE_ESCAPE] && input.e.type != SDL_QUIT);
glDeleteShader(shader.vertexShader);
glDeleteShader(shader.fragmentShader);
glDeleteProgram(shader.program);
SDL_GL_DeleteContext(context);
context = nullptr;
SDL_DestroyWindow(window);
window = nullptr;
SDL_Quit();
return 0;
}
答案 0 :(得分:0)
深度测试搞砸了。这是通过评论代码直到它工作的历史悠久的方法发现的。当然,当您调试空白屏幕错误时,这并不总是有效。
为什么搞乱了?因为使用glClear(GL_COLOR_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
,您似乎错误地粘贴并且没有清除深度缓冲区,因此后续三角形无法通过默认深度测试(在没有设置深度函数的情况下启用深度测试很奇怪)。 / p>