以下是简单代码实例,着色器就不放了,比较简单
#define GLEW_STATIC
#define STB_IMAGE_IMPLEMENTATION
#include
#include
#include
#include"shader.h"
#include
#include
#include
#include
#include
using namespace std;
bool firstmouse = true;
bool keys[1024];
//视角
GLfloat pitch = 0.0f;
GLfloat yaw = -90.0f;
GLfloat lastx = 400.0f;
GLfloat lasty = 300.0f;
GLfloat fov=45.0f;
GLfloat deltaTime = 0.0f; // 当前帧和上一帧之间的时间差
GLfloat lastFrame = 0.0f; // 上一帧时间
//camera
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -2.0f);//方向向量=原点-pos
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
//事件回调函数
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
void do_moment();
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void roll_callback(GLFWwindow* window, double xoffset, double yoffset);
int main()
{
//glfw initial
if (!glfwInit()){
cout << "Fail to initial glfw!" << endl;
return -1;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
//window
GLFWwindow *window = glfwCreateWindow(800, 600, "hello triangle", nullptr, nullptr);
if (window == NULL){
cout << "Fail to create window!" << endl;
glfwTerminate();
return -1;
}
//创建窗口的context指定为当前context
glfwMakeContextCurrent(window);
//注册事件回调函数
glfwSetKeyCallback(window, key_callback);
glfwSetCursorPosCallback(window, mouse_callback); //监听鼠标函数
glfwSetScrollCallback(window, roll_callback);//监听鼠标滚轮函数
//glew initial
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK){
cout << "Fail to initial glew!" << endl;
glfwTerminate();
return -1;
}
//viewport
glViewport(0, 0, 800, 600);
//dot
float