gpt4 book ai didi

c++ - 我的带有 SDL2 代码的 OpenGL 有什么问题?

转载 作者:太空狗 更新时间:2023-10-29 23:13:50 24 4
gpt4 key购买 nike

我无法绘制到我用下面编写的代码创建的窗口。

我所知道的是正确的:

  • sdl2 初始化
  • glew 初始化
  • 创建窗口
  • 上下文创建

我认为可能是问题所在:

  • glsl 着色器,但它们编译成功。
  • glVertexAttribPointer()
  • 缺少函数调用?

我通过错误检查排除的内容:

  • sdl初始化
  • glew 初始化
  • 创建窗口
  • 上下文创建
  • 顶点着色器编译
  • 片段着色器编译
  • 程序链接

注意:我已经内联编写了我的着色器 (glsl.h)。

主要.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;
}

最佳答案

深度测试搞砸了。这是通过 注释掉代码直到它工作 的历史悠久的方法发现的。当然,当您调试黑屏错误时,这并不总是有效。

为什么会乱码?因为使用 glClear(GL_COLOR_BUFFER_BIT | GL_COLOR_BUFFER_BIT); 似乎你粘贴错误并且没有清除深度缓冲区,因此后续三角形未通过默认深度测试(你启用深度测试很奇怪没有设置深度函数)。

关于c++ - 我的带有 SDL2 代码的 OpenGL 有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37038335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com