OpenGL/glm显示黑屏



我是opengl的新手,通过以下https://medium.com/@bhargav.chippada/如何设置-opengl-on-mingw-w64-in-windows-10-64位-b77f350cea7e安装GLFWGLEW,我使用mingw和geany作为文本编辑器并创建了一个Makefile来构建和运行代码。我已经创建了一个opengl程序,我可以用GLSL绘制一个2d三角形。一切似乎都很正常。然后我安装GLM,我能够安装它,当我尝试添加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

更改以下行:

glm::mat4 model;

glm::mat4 model(1.0f);

应该可以。

最新更新