当我运行此代码时,它应该显示一个红色立方体,但屏幕完全是黑色的。我使用的是glm的C版本(https://github.com/recp/cglm)对于矩阵计算,可能其中有一些错误,因为我正在使用C++和标准glm库学习OpenGL教科书,并试图将其转换为C.
#include <SDL2/SDL.h>
#include <OpenGL/gl3.h>
#include "cglm.h"
#include <stdbool.h>
#define CODE(...) #__VA_ARGS__
int main(){
static int width = 600;
static int height = 600;
// create window
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_Window* window = SDL_CreateWindow("Cube", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, 0);
SDL_GLContext context = SDL_GL_CreateContext(window);
SDL_GL_SetSwapInterval(1);
GLuint mvLocation, projLocation;
float aspect;
mat4 perspectiveMatrix, modelMatrix, viewMatrix, modelViewMatrix;
vec3 cameraVector = {0.0f, 0.0f, 8.0f};
vec3 cubeVector = {0.0f, -2.0f, 0.0f};
float vertexPositions[108] = {
-1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f,
1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f,
1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f,
1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f
};
// vertex shader
const GLchar* vs_code = "#version 330n" CODE(
layout (location = 0) in vec3 aPos;
uniform mat4 mv_matrix;
uniform mat4 proj_matrix;
void main(){
gl_Position = proj_matrix * mv_matrix * vec4(position, 1.0);
}
);
unsigned int vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &vs_code, NULL);
glCompileShader(vs);
// fragment shader
const GLchar* fs_code = "#version 330n" CODE(
out vec4 color;
uniform mat4 mv_matrix;
uniform mat4 proj_matrix;
void main(void)
{ color = vec4(1.0, 0.0, 0.0, 1.0);
}
);
unsigned int fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &fs_code, NULL);
glCompileShader(fs);
// create program
unsigned int program = glCreateProgram();
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glDeleteShader(vs);
glDeleteShader(fs);
// create a vertex array object
unsigned int vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
// create a vertex buffer object
unsigned int vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexPositions), vertexPositions, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GL_FLOAT), NULL);
glEnableVertexAttribArray(0);
// start render loop
bool quit = false;
while(!quit){
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
mvLocation = glGetUniformLocation(program, "mv_matrix");
projLocation = glGetUniformLocation(program, "proj_matrix");
aspect = (float)width / (float)height;
glm_perspective(1.047f, aspect, 0.1f, 1000.0f, perspectiveMatrix);
glm_translate(viewMatrix, cameraVector);
glm_translate(modelMatrix, cubeVector);
glm_mat4_mul(viewMatrix, perspectiveMatrix, modelViewMatrix);
glUniformMatrix4fv(mvLocation, 1, GL_FALSE, (float*)modelViewMatrix);
glUniformMatrix4fv(projLocation, 1, GL_FALSE, (float*)perspectiveMatrix);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glDrawArrays(GL_TRIANGLES, 0, 36);
SDL_GL_SwapWindow(window);
SDL_Event event;
while(SDL_PollEvent(&event)){
if(event.type == SDL_QUIT)
quit = true;
}
}
glDeleteProgram(program);
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);
SDL_Quit();
}
您必须初始化矩阵。例如:glm_translate
不仅仅指定一个转换矩阵。该函数将输入矩阵乘以平移矩阵。请参阅存储库的自述文件:https://github.com/recp/cglm.
需要将视图矩阵和模型矩阵相乘,得到模型视图矩阵:
mat4 viewMatrix = {
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
};
glm_translate(viewMatrix, cameraVector);
mat4 modelMatrix = {
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
};
glm_translate(modelMatrix, cubeVector);
while(!quit){
glm_mat4_mul(viewMatrix, modelMatrix, modelViewMatrix);
/* [...] */
}