严格来说 opengl 本身没有提供摄像机接口,而是在glu库中提供了gluLookAt 这个接口,所以摄像机这个概念在三维库opengl中并没有,实际是通过对矩阵的控制来实现摄像机。
相关函数:
//gluLookAt 函数 本质是通过参数产生一个观察矩阵 然后和 被观察模型矩阵相乘 得到被观察物体位置
// _eye 表示摄像机位置
// _lookAt 表示观察的目标点
// upx upy upz 表示摄像机姿态的 向上的正方向 哪个点变成摄像机的Y轴
// 所以通过修改参数就可以控制摄像机
gluLookAt(_eye.x, _eye.y, _eye.z, _lookAt.x, _lookAt.y, _lookAt.z, 0, 1, 0);
代码如下:
/*
* 该实例演示 使用及键盘、鼠标控制摄像机(gluLookAt) 通过不同视角 观察物体
*/
#define GLEW_STATIC
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/vec3.hpp> // glm::vec3
#include <glm/vec4.hpp> // glm::vec4
#include <glm/mat4x4.hpp> // glm::mat4
#include <glm/ext/matrix_transform.hpp> // glm::translate, glm::rotate, glm::scale
#include "FreeImage.h"
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) //这行是取消显示控制台
char szTitle[64] = "opengl view";
//glm::vec4 vec(1.0f, 0.0f, 0.0f, 1.0f);//创建一个点
//glm::mat4 trans = glm::mat4(1.0f);//创建单位矩阵
//Window dimensions
const GLuint WIDTH = 800, HEIGHT = 600;
GLfloat ratio = 1.f;
GLfloat xpos, ypos;
glm::vec3 _eye(0, 0, 10);
glm::vec3 _lookAt(0, 0, 0);
float size = 100.0f;
//顶点数据
struct Vertex {
float x, y, z;
float u, v;
};
Vertex g_cubeVertices[] =
{
{ -1.0f, -1.0f, 1.0f, 0.0f, 0.0f },
{ 1.0f, -1.0f, 1.0f, 1.0f, 0.0f },
{ 1.0f, 1.0f, 1.0f, 1.0f, 1.0f },
{ -1.0f, 1.0f, 1.0f, 0.0f, 1.0f },
{ -1.0f, -1.0f, -1.0f, 1.0f, 0.0f },
{ -1.0f, 1.0f, -1.0f, 1.0f, 1.0f },
{ 1.0f, 1.0f, -1.0f, 0.0f, 1.0f },
{ 1.0f, -1.0f, -1.0f, 0.0f, 0.0f },
{ -1.0f, 1.0f, -1.0f, 0.0f, 1.0f },
{ -1.0f, 1.0f, 1.0f, 0.0f, 0.0f },
{ 1.0f, 1.0f, 1.0f, 1.0f, 0.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, 0.0f, 1.0f },
{ 1.0f, -1.0f, 1.0f, 0.0f, 0.0f },
{ -1.0f, -1.0f, 1.0f, 1.0f, 0.0f },
{ 1.0f, -1.0f, -1.0f, 1.0f, 0.0f },
{ 1.0f, 1.0f, -1.0f, 1.0f, 1.0f },
{ 1.0f, 1.0f, 1.0f, 0.0f, 1.0f },
{ 1.0f, -1.0f, 1.0f, 0.0f, 0.0f },
{ -1.0f, -1.0f, -1.0f, 0.0f, 0.0f },
{ -1.0f, -1.0f, 1.0f, 1.0f, 0.0f },
{ -1.0f, 1.0f, 1.0f, 1.0f, 1.0f },
{ -1.0f, 1.0f, -1.0f, 0.0f, 1.0f },
// 地面数据
{ -size, -1.0f, -size, 0.0f, 0.0f },
{ -size, -1.0f, size, 100.0f, 0.0f },
{ size, -1.0f, size, 100.0f, 100.0f },
{ size, 0.0f, -size, 0.0f, 100.0f }
};
//纹理ID
GLuint _texture;
GLuint _vbo;
//地面纹理
GLuint _texGround;
static void error_callback(int error, const char* description) {
fputs(description, stderr);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
std::cout << "key " << key << std::endl;
switch(key) {
case GLFW_KEY_ESCAPE:
glfwSetWindowShouldClose(window, GL_TRUE);
break;
case GLFW_KEY_UP:
_eye.z -= 0.1f; //这里修改摄像机参数
break;
case GLFW_KEY_DOWN:
_eye.z += 0.1f;
break;
case GLFW_KEY_RIGHT:
_eye.x -= 1.0f;
break;
case GLFW_KEY_LEFT:
_eye.x += 1.0f;
break;
default:
break;
}
}
static void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
if(action != GLFW_PRESS)
return;
switch(button) {
case GLFW_MOUSE_BUTTON_LEFT:
std::cout << "Mosue left button clicked!" << std::endl;
break;
case GLFW_MOUSE_BUTTON_MIDDLE:
std::cout << "Mosue middle button clicked!" << std::endl;
break;
case GLFW_MOUSE_BUTTON_RIGHT:
std::cout << "Mosue right button clicked!" << std::endl;
break;
default:
return;
}
return;
}
static void cursor_position_callback(GLFWwindow* window, double x, double y) {
//std::cout << "Mouse position move to X: " << x << " Y: " << y << std::endl;
xpos = float((x - WIDTH / 2) / WIDTH) * 2;
ypos = float(0 - (y - HEIGHT / 2) / HEIGHT) * 2;
return;
}
static void scroll_callback(GLFWwindow* window, double x, double y) {
return;
}
static unsigned createTexture(int w, int h, const void* data, GLenum type) {
unsigned texId;
glGenTextures(1, &texId);
glBindTexture(GL_TEXTURE_2D, texId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, type, w, h, 0, type, GL_UNSIGNED_BYTE, data);
return texId;
}
/**
* 使用FreeImage加载图片
*/
static unsigned createTextureFromImage(const char* fileName) {
//1 获取图片格式
FREE_IMAGE_FORMAT fifmt = FreeImage_GetFileType(fileName, 0);
if(fifmt == FIF_UNKNOWN) {
return 0;
}
//2 加载图片
FIBITMAP *dib = FreeImage_Load(fifmt, fileName, 0);
FREE_IMAGE_COLOR_TYPE type = FreeImage_GetColorType(dib);
//3 获取数据指针
FIBITMAP* temp = dib;
dib = FreeImage_ConvertTo32Bits(dib);
FreeImage_Unload(temp);
BYTE* pixels = (BYTE*)FreeImage_GetBits(dib);
int width = FreeImage_GetWidth(dib);
int height = FreeImage_GetHeight(dib);
for(int i = 0; i < width * height * 4; i += 4) {
BYTE temp = pixels[i];
pixels[i] = pixels[i + 2];
pixels[i + 2] = temp;
}
unsigned res = createTexture(width, height, pixels, GL_RGBA);
FreeImage_Unload(dib);
return res;
}
static void onInit() {
_texture = createTextureFromImage("../res/1.jpg");
_texGround = createTextureFromImage("../res/dimian.jpg");
glGenBuffers(1, &_vbo);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_cubeVertices), g_cubeVertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
static void onDestory() {
glDeleteTextures(1, &_texture);
glDeleteTextures(1, &_texGround);
glDeleteBuffers(1, &_vbo); //删除VBO 显存中释放VBO内存
}
// 绘制
static void render(GLFWwindow * window) {
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//gluLookAt 函数 本质是通过参数产生一个观察矩阵 然后和 被观察模型矩阵相乘 得到被观察物体位置
// _eye 表示摄像机位置
// _lookAt 表示观察的目标点
// upx upy upz 表示摄像机姿态的 向上的正方向 哪个点变成摄像机的Y轴
// 所以通过修改参数就可以控制摄像机
gluLookAt(_eye.x, _eye.y, _eye.z, _lookAt.x, _lookAt.y, _lookAt.z, 0, 1, 0);
//摄像机围绕目标旋转
//static float angle = 0; //旋转角度
//float r = 10; //旋转半径
//_eye.x = cos(angle * glm::pi<double>() / 180) * r;
//_eye.z = sin(angle * glm::pi<double>() / 180) * r;
//angle += 1; //角度每帧加一
glBindTexture(GL_TEXTURE_2D, _texture);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
//开始使用ID为 _vbo 的buffer 接下来的操作针对这个VBO
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
float* addrVertex = (float*)0;
float* uvAddress = (float*)12;
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), addrVertex);
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), uvAddress);
// 绘制 立方体
glDrawArrays(GL_QUADS, 0, 24);
//绘制地面
//glLoadIdentity(); //这里打开 地面就不会跟着旋转 说明摄像机操作的是模型矩阵
glBindTexture(GL_TEXTURE_2D, _texGround);
glDrawArrays(GL_QUADS, 24, 4);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glfwSwapBuffers(window);
glfwPollEvents();
}
int main(void) {
GLFWwindow * window;
glfwSetErrorCallback(error_callback);
if(!glfwInit())
return -1;
window = glfwCreateWindow(WIDTH, HEIGHT, szTitle, NULL, NULL);
if(!window) {
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwSetKeyCallback(window, key_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwSetCursorPosCallback(window, cursor_position_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
glewInit();
onInit();
glViewport(0, 0, WIDTH, HEIGHT); //设置opengl视口 即看到的显示区域
glMatrixMode(GL_PROJECTION); //设置当前操作的是投影矩阵队列
glLoadIdentity(); //将队列最上层的一个投影矩阵清空成单位矩阵
//gluPerspective (GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar);
//fovy 角度 视角
//aspect 宽高比
//zNear 近裁剪面
//zFar 远裁剪面
gluPerspective(60, double(WIDTH) / double(HEIGHT), 0.1, 1000);//产生一个新的投影矩阵并和上面的投影矩阵列表最上面单位矩阵相乘,实际上就是产生一个投影矩阵并放到投影矩阵列表的最上面
while(!glfwWindowShouldClose(window)) {
render(window);
}
onDestory();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}