You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OpenGL集成GLM后运行显示空白屏问题求助

OpenGL添加GLM后程序运行显示空白屏问题

我是OpenGL新手,已完成GLFW和GLEW的安装配置,使用MinGW编译、Geany作为编辑器,通过Makefile构建运行代码。原本可以正常编写OpenGL程序绘制带GLSL的2D三角形,一切功能正常。但安装GLM并添加相关代码后,代码编译/构建无错误,运行程序时却显示空白屏。

CPP代码

#include <stdio.h>
#include <string.h>

#include <GL/glew.h>
#include <GLFW/glfw3.h>

#define GLM_FORCE_RADIANS
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

const GLint WIDTH = 920, HEIGHT = 600;

GLuint VAO, VBO, shader, uniformModel;

bool direction = true;
float triOffset = 0.0f;
float triMaxOffset = 0.7f;
float triIncrement = 0.0005f;

// Vertex Shader
const GLchar* vShader = R"glsl(
#version 330 core

layout(location = 0) in vec3 pos;

uniform mat4 model;

void main(){
   gl_Position = model * vec4(0.4 *pos.x, 0.4 * pos.y, pos.z, 1.0);
}
)glsl";

// Fragment
const GLchar * fShader = R"glsl(
#version 330 core

out vec4 color;

void main(){
  color = vec4(1.0f, 0.5f, 0.2f, 1.0f);
}
)glsl";


void CreateTriangle() {
    // Set up vertex data (and buffer(s)) and attribute pointers
    GLfloat vertices[] = {
        -1.0f, -1.0f, 0.f, // left
         1.0f, -1.0f, 0.f, // right
         0.0f,  1.0f, 0.f, // top
    };

    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);

        glGenBuffers(1, &VBO);
        glBindBuffer(GL_ARRAY_BUFFER, VBO);
            glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

            glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
            glEnableVertexAttribArray(0);

        glBindBuffer(GL_ARRAY_BUFFER, 0);

    glBindVertexArray(0);
}

void CompileShaders() {
    shader = glCreateProgram();
    /* shader compilation code */
    uniformModel = glGetUniformLocation(shader, "model");
}

int main () {

    /* GLFW initialization */

    CreateTriangle();
    CompileShaders();

        // Define a loop which terminates when the window is closed
    while(!glfwWindowShouldClose(window)) {
        // clean screen
        //for handling events such as keyboard input
        glfwPollEvents();
        
        if(direction) {
            triOffset += triIncrement; 
        } else {
            triOffset -= triIncrement;
        }

        if(abs(triOffset) >= triMaxOffset) {
            direction = !direction;
        }

        glUseProgram(shader);

        glm::mat4 model;
        model = glm::translate(model, glm::vec3(triOffset, 0.0f, 0.0f));
        glUniformMatrix4fv(uniformModel, 1, GL_FALSE, glm::value_ptr(model));
    
        glBindVertexArray(VAO);
        glDrawArrays(GL_TRIANGLES, 0, 3);

        glBindVertexArray(0);
        glUseProgram(0);
        
        //Swap the draw buffer
    }

    // Properly de-allocate all resource once they've outlive
    
}

Makefile代码

CC=g++

OPENGL=-lopengl32 -lglew32 -lglfw3 -lglu32 -lgdi32 

$(file) = main
build:
    $(CC) -v $(file)  -o output $(OPENGL) -Wall -Werror

内容的提问来源于stack exchange,提问作者sonnet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 17:47:06