我正在通过 learnopengl.com 学习 OpenGL。我正在上相机课。运行时显示空白屏幕。预计会让相机围绕立方体旋转。我已经调试了好几天了。这很可能是我犯的一些愚蠢的错误。
我正在使用:
C language
CGLM
OpenGL 4.1
Debian 12
SDL2
stb-image
抱歉,如果我的代码很乱,这是我的 main.c:
#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include <cglm/cglm.h>
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef float f32;
typedef double f64;
SDL_Window* window = NULL;
SDL_Event event;
bool keep_window_open = 1;
u32 VAO;
u32 VBO;
u32 EBO;
f32 vertices[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,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 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
};
u32 vertex_shader;
u32 fragment_shader;
u32 shader_program;
u32 height = 480;
u32 width = 640;
i32 tex_width;
i32 tex_height;
i32 nr_channels;
u8* data;
u32 texture;
int tick();
void framebuffer_size_callback(SDL_Window*, u32, u32);
char* read_whole_file(FILE*);
int main() {
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
window = SDL_CreateWindow(
"m o o d", SDL_WINDOWPOS_CENTERED_DISPLAY(0),
SDL_WINDOWPOS_CENTERED_DISPLAY(0), 640, 480,
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GLContext glctx = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, glctx);
SDL_GL_SetSwapInterval(1);
// Your viewport should be the same as your window dimensions.
glViewport(0, 0, width, height);
FILE *v = fopen("vert.glsl", "r");
FILE *f = fopen("frag.glsl", "r");
char* vsrc = read_whole_file(v);
char* fsrc = read_whole_file(f);
const char* glvsrc = vsrc;
const char* glfsrc = fsrc;
fclose(v);
fclose(f);
glEnable(GL_DEPTH_TEST);
stbi_set_flip_vertically_on_load(true);
data = stbi_load("texture.jpg", &tex_width, &tex_height, &nr_channels, 0);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex_width, tex_height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data);
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &glvsrc, NULL);
glCompileShader(vertex_shader);
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &glfsrc, NULL);
glCompileShader(fragment_shader);
shader_program = glCreateProgram();
glAttachShader(shader_program, vertex_shader);
glAttachShader(shader_program, fragment_shader);
glLinkProgram(shader_program);
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
free(vsrc);
free(fsrc);
// HELLO ABDULMOHSIN!
// README!!!
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, 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);
glUseProgram(shader_program);
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_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
mat4 projection;
glm_mat4_identity(projection);
u32 projection_location = glGetUniformLocation(shader_program, "projection");
glUniformMatrix4fv(projection_location, 1, GL_FALSE, projection[0]);
glm_perspective(0.98968f, 640.0f / 480.0f, 0.1f, 100.0f, projection);
while(keep_window_open) {
tick();
}
SDL_GL_DeleteContext(glctx);
return 0;
}
int tick() {
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_QUIT:
keep_window_open = 0;
break;
case SDL_WINDOWEVENT:
if(event.window.event == SDL_WINDOWEVENT_RESIZED) {
width = event.window.data1;
height = event.window.data2;
glViewport(0, 0, width, height);
}
}
}
glClearColor(0.5f, 0.2f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
vec3 axis;
axis[0] = 1.0f;
axis[1] = 1.0f;
axis[2] = 1.0f;
glm_vec3_normalize(axis);
vec3 translation_factor;
translation_factor[0] = 0.0f;
translation_factor[1] = 0.0f;
translation_factor[2] = -3.0f;
glm_vec3_normalize(translation_factor);
vec3 camera_position;
camera_position[0] = 0.0f;
camera_position[1] = 0.0f;
camera_position[2] = 3.0f;
glm_vec3_normalize(camera_position);
vec3 camera_target;
camera_target[0] = 0.0f;
camera_target[1] = 0.0f;
camera_target[2] = 0.0f;
glm_vec3_normalize(camera_target);
vec3 camera_direction;
vec3 difference;
glm_vec3_sub(camera_position, camera_target, difference);
glm_vec3_normalize(difference);
glm_vec3_normalize_to(difference, camera_direction);
vec3 up;
up[0] = 0.0f;
up[1] = 1.0f;
up[2] = 0.0f;
glm_vec3_normalize(up);
vec3 camera_right;
vec3 product;
glm_vec3_cross(up, camera_direction, product);
glm_vec3_normalize(product);
glm_vec3_normalize_to(product, camera_right);
vec3 camera_up;
glm_vec3_cross(camera_direction, camera_right, camera_up);
glm_vec3_normalize(camera_up);
vec3 cube_positions[10] = {
0.0f, 0.0f, 0.0f,
2.0f, 5.0f, -15.0f,
-1.5f, -2.2f, -2.5f,
-3.8f, -2.0f, -12.3f,
2.4f, -0.4f, -3.5f,
-1.7f, 3.0f, -7.5f,
1.3f, -2.0f, -2.5f,
1.5f, 2.0f, -2.5f,
1.5f, 0.2f, -1.5f,
-1.3f, 1.0f, -1.5f
};
f32 angle;
mat4 view;
glm_mat4_identity(view);
f32 radius = 10.0f;
f32 cam_x = (sin((SDL_GetTicks64() / 1000) * radius));
f32 cam_z = (cos((SDL_GetTicks64() / 1000) * radius));
vec3 a;
a[0] = cam_x;
a[1] = 0.0f;
a[2] = cam_z;
vec3 b;
b[0] = 0.0f;
b[1] = 0.0f;
b[2] = 0.0f;
vec3 c;
c[0] = 0.0f;
c[1] = 1.0f;
c[2] = 0.0f;
glm_look(a, b, c, view);
u32 view_location = glGetUniformLocation(shader_program, "view");
glUniformMatrix4fv(view_location, 1, GL_FALSE, view[0]);
glBindTexture(GL_TEXTURE_2D, texture);
glBindVertexArray(VAO);
for(int i = 0; i < 10; i++) {
mat4 model;
glm_mat4_identity(model);
glm_translate(model, cube_positions[i]);
glDrawArrays(GL_TRIANGLES, 0, 36);
angle = 0.349066f * i;
glm_spin(model, angle, axis);
u32 model_location = glGetUniformLocation(shader_program, "model");
glUniformMatrix4fv(model_location, 1, GL_FALSE, model[0]);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
SDL_GL_SwapWindow(window);
return 0;
}
void framebuffer_size_callback(SDL_Window* window, u32 window_width, u32 window_height) {
glViewport(0, 0, window_width, window_height);
}
char* read_whole_file(FILE *f) {
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
char* str = malloc(fsize + 1);
fread(str, fsize, 1, f);
str[fsize] = 0;
return str;
}
我以某种方式修复了它,不知道如何解决,但是对于遇到相同问题的任何人,这里是 src:
#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include <cglm/cglm.h>
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef float f32;
typedef double f64;
SDL_Window* window = NULL;
SDL_Event event;
bool keep_window_open = 1;
u32 VAO;
u32 VBO;
u32 EBO;
/* f32 vertices[] = {
// positions // colors // texture coords
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
}; */
f32 vertices[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, -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.0f, 0.0f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
};
u32 indices[] = {
0, 1, 3, // first triangle
1, 2, 3, // second triangle
2, 3, 5,
3, 4, 5,
4, 5, 7,
5, 6, 7,
6, 7, 9,
7, 8, 9,
8, 9, 11,
9, 10, 11,
10, 11, 13,
11, 12, 13
};
u32 vertex_shader;
u32 fragment_shader;
u32 shader_program;
u32 height = 480;
u32 width = 640;
i32 tex_width;
i32 tex_height;
i32 nr_channels;
u8* data;
u32 texture;
int tick();
void framebuffer_size_callback(SDL_Window*, u32, u32);
char* read_whole_file(FILE*);
int main() {
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
window = SDL_CreateWindow(
"m o o d", SDL_WINDOWPOS_CENTERED_DISPLAY(0),
SDL_WINDOWPOS_CENTERED_DISPLAY(0), 640, 480,
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GLContext glctx = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, glctx);
SDL_GL_SetSwapInterval(1);
// Your viewport should be the same as your window dimensions.
glViewport(0, 0, width, height);
FILE *v = fopen("vert.glsl", "r");
FILE *f = fopen("frag.glsl", "r");
char* vsrc = read_whole_file(v);
char* fsrc = read_whole_file(f);
const char* glvsrc = vsrc;
const char* glfsrc = fsrc;
fclose(v);
fclose(f);
glEnable(GL_DEPTH_TEST);
stbi_set_flip_vertically_on_load(true);
data = stbi_load("texture.jpg", &tex_width, &tex_height, &nr_channels, 0);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex_width, tex_height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data);
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &glvsrc, NULL);
glCompileShader(vertex_shader);
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &glfsrc, NULL);
glCompileShader(fragment_shader);
shader_program = glCreateProgram();
glAttachShader(shader_program, vertex_shader);
glAttachShader(shader_program, fragment_shader);
glLinkProgram(shader_program);
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
free(vsrc);
free(fsrc);
// HELLO ABDULMOHSIN!
// README!!!
// So, We generate both VAO and VBO, with VAO first and VBO second.
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
// Then we *bind* the VAO to the uninited VBO.
glBindVertexArray(VAO);
// Now, we switch focus to the VBO and upload it's data.
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// Continue as normal.
// I can't belive you can't even follow a tutorial.
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);
// Also, you only really need to call this once. You are not doing hot reloading
// for shaders.
glUseProgram(shader_program);
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_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Your optimzation is terrible. keep_window_open is a boolean,
// you don't need to check if it's 1 or not, just insert the statement.
// This is because for while, if, and for statements
// 0 = false
// >=1 = true
// And also, if you are using the argument for "it's more readable",
// go switch to a higher level language. This is C. The 0, >=1 rule is something
// you should know.
while(keep_window_open) {
tick();
}
SDL_GL_DeleteContext(glctx);
return 0;
}
int tick() {
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_QUIT:
keep_window_open = 0;
break;
case SDL_WINDOWEVENT:
if(event.window.event == SDL_WINDOWEVENT_RESIZED) {
width = event.window.data1;
height = event.window.data2;
glViewport(0, 0, width, height);
}
}
}
glClearColor(0.5f, 0.2f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
vec3 axis;
axis[0] = 1.0f;
axis[1] = 1.0f;
axis[2] = 1.0f;
vec3 translation_factor;
translation_factor[0] = 0.0f;
translation_factor[1] = 0.0f;
translation_factor[2] = -3.0f;
mat4 view;
glm_mat4_identity(view);
mat4 projection;
glm_mat4_identity(projection);
glm_perspective(1.98968f, 640.0f / 480.0f, 0.1f, 100.0f, projection);
u32 projection_location = glGetUniformLocation(shader_program, "projection");
glUniformMatrix4fv(projection_location, 1, GL_FALSE, projection[0]);
vec3 cube_positions[10] = {
0.0f, 0.0f, 0.0f,
2.0f, 5.0f, -15.0f,
-1.5f, -2.2f, -2.5f,
-3.8f, -2.0f, -12.3f,
2.4f, -0.4f, -3.5f,
-1.7f, 3.0f, -7.5f,
1.3f, -2.0f, -2.5f,
1.5f, 2.0f, -2.5f,
1.5f, 0.2f, -1.5f,
-1.3f, 1.0f, -1.5f
};
f32 radius = 10.0f;
f32 cam_x = sin(SDL_GetTicks64() / 1000.0f) * radius;
f32 cam_z = cos(SDL_GetTicks64() / 1000.0f) * radius;
vec3 a = {cam_x, 0.0f, cam_z};
vec3 b = {0.0f, 0.0f, 0.0f};
vec3 c = {0.0f, 1.0f, 0.0f};
glm_lookat(a, b, c, view);
u32 view_location = glGetUniformLocation(shader_program, "view");
glUniformMatrix4fv(view_location, 1, GL_FALSE, view[0]);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glBindVertexArray(VAO);
for(int i = 0; i < 10; i++) {
mat4 model;
glm_mat4_identity(model);
glm_translate(model, cube_positions[i]);
glDrawArrays(GL_TRIANGLES, 0, 36);
f32 angle = 0.349066f * i;
glm_spin(model, angle, axis);
u32 model_location = glGetUniformLocation(shader_program, "model");
glUniformMatrix4fv(model_location, 1, GL_FALSE, model[0]);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
//glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
SDL_GL_SwapWindow(window);
return 0;
}
void framebuffer_size_callback(SDL_Window* window, u32 window_width, u32 window_height) {
glViewport(0, 0, window_width, window_height);
}
char* read_whole_file(FILE *f) {
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
char* str = malloc(fsize + 1);
fread(str, fsize, 1, f);
str[fsize] = 0;
return str;
}