**关闭。**此题需要debugging details。目前不接受答复。
编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答这个问题。
昨天关门了。
Improve this question
最近我创建了一个新的OpenGL项目。我写了一个简单的应用程序,使用SDL 2,GLAD,应该画一个三角形,但没有工作。下面是我的代码:
/* Library headers */
#include "glad/glad.h"
#include "SDL2/SDL.h"
/* Standard headers */
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
/* User-defined headers*/
#include "defs.h"
/* Globals */
SDL_Window* g_window = NULL;
SDL_GLContext g_context = NULL;
bool g_quit = false;
/* OpenGL Objects */
GLuint g_vao, g_vbo, g_ebo;
const GLfloat vertices[] = {
0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f
};
const GLuint indices[] = {
0, 1, 2
};
bool init_sdl(void)
{
/* Init SDL2 */
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "SDL2 couldn't initialize: %s\n", SDL_GetError());
return false;
}
/* Set the attributes */
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, MAJOR_VERSION);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, MINOR_VERSION);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, OPENGL_PROFILE);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, HAS_DOUBLEBUFFER);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, DEPTH_SIZE);
/* Create the window */
g_window = SDL_CreateWindow("SDL Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
if (g_window == NULL) {
fprintf(stderr, "SDL_Window couldn't be created: %s\n", SDL_GetError());
return false;
}
g_context = SDL_GL_CreateContext(g_window);
if (g_context == NULL)
{
fprintf(stderr, "SDL_GLContext couldn't be created: %s\n", SDL_GetError());
return false;
}
if (!gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress)) {
fprintf(stderr, "GLAD couldn't initialize!\n");
return false;
}
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
glClearColor(0.0f, 0.4f, 0.4f, 0.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
return true;
}
void handle_events(void)
{
SDL_Event e;
while (SDL_PollEvent(&e) != 0) {
switch (e.type) {
case SDL_QUIT:
g_quit = true;
break;
case SDL_KEYDOWN:
if (e.key.keysym.sym == SDLK_ESCAPE)
g_quit = true;
default:
break;
}
}
}
void pre_render(void)
{
glGenVertexArrays(1, &g_vao);
glBindVertexArray(g_vao);
glGenBuffers(1, &g_vbo);
glBindBuffer(GL_ARRAY_BUFFER, g_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glGenBuffers(1, &g_ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (void*)0);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void render(void)
{
glBindVertexArray(g_vao);
glDrawElements(GL_TRIANGLES, sizeof(indices)/sizeof(indices[0]), GL_UNSIGNED_INT, NULL);
}
void cleanup(void)
{
glDeleteVertexArrays(1, &g_vao);
glDeleteBuffers(1, &g_vbo);
glDeleteBuffers(1, &g_ebo);
SDL_GL_DeleteContext(g_context);
SDL_DestroyWindow(g_window);
g_context = NULL;
g_window = NULL;
SDL_Quit();
}
int main(int argc, char* argv[])
{
if (!init_sdl())
return EXIT_FAILURE;
pre_render();
while (!g_quit) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
render();
SDL_GL_SwapWindow(g_window);
handle_events();
}
cleanup();
return EXIT_SUCCESS;
}
我试图找到一些相关的主题,但我没有找到任何东西。如果你能帮我我会很感激的!
我的另一个项目用同样的代码工作得很好。(我有默认的着色器,所以一切都应该工作没有任何外部着色器)
1条答案
按热度按时间z0qdvdin1#
我找到答案了!不知何故,默认着色器这次不起作用。当我做着色器加载和错误处理时,我传递了GL_COMPILE_STATUS而不是GL_LINK_STATUS(在glGetProgramiv中)。所以我把它修好了,现在一切都正常了。谢谢大家的帮助!