簡體   English   中英

OpenGL 立方體未顯示

[英]OpenGL Cube not showing

當我運行此代碼時,它應該顯示一個紅色立方體,但屏幕完全是黑色的。 I'm using the C version of glm ( https://github.com/recp/cglm ) for matrix calculations and maybe there is something wrong in those, because I'm following an OpenGL textbook using C++ and the standard glm library and試圖將其轉換為 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 330\n" 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 330\n" 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不只是指定一個翻譯矩陣。 function 將輸入矩陣乘以平移矩陣。 請參閱存儲庫的自述文件: https://github.com/recp/cglm
並且您需要將視圖矩陣與 model 矩陣相乘以得到 model 視圖矩陣:

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);

    /* [...] */
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM