南邮图形学第一次课堂测试 - 3D魔方转动
效果图:
右侧(键盘方向→键)旋转:
右侧(键盘方向↑键)旋转:
基本思路:
3 × 3 的 魔 方 , 实 现 上 、 下 层 , 左 、 右 层 转 动 , 那 么 最 方 便 的 想 法 应 该 是 创 建 27 个 小 的 立 方 体 对 象 。 3×3的魔方,实现上、下层,左、右层转动,那么最方便的想法应该是创建27个小的立方体对象。 3×3的魔方,实现上、下层,左、右层转动,那么最方便的想法应该是创建27个小的立方体对象。
接 着 通 过 数 组 索 引 , 模 拟 给 这 27 个 立 方 体 的 顶 点 标 号 、 分 层 。 确 定 旋 转 轴 和 旋 转 的 小 立 方 体 编 号 即 可 。 接着通过数组索引,模拟给这27个立方体的顶点标号、分层。确定旋转轴和旋转的小立方体编号即可。 接着通过数组索引,模拟给这27个立方体的顶点标号、分层。确定旋转轴和旋转的小立方体编号即可。
代码:
main.cpp
#define GLEW_STATIC
#include <iostream>
#include <glew.h>
#include "Camera.h"
#include <glfw3.h>
#include "Shader.h"
#include "SOIL2/SOIL2.h"
#include "SOIL2/stb_image.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
const GLint WIDTH = 800, HEIGHT = 600; //新建窗口
void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mode);
void MouseCallback(GLFWwindow* window, double xPos, double yPos);
void ScrollCallback(GLFWwindow* window, double xOffset, double yOffset);
void DoMovement();
bool keys[1024]; //记录按键
GLfloat lastX = 0.0f, lastY = 0.0f;
Camera camera(glm::vec3(0.0f, 0.0f, 2.0f));
GLfloat deltaTime = 0.0f;
GLfloat lastTime = 0.0f;
bool firstMouse = true;
int l = 3; //魔方层级,0:顶层, 1:底层, 2:左侧, 3:右侧
int main()
{
glfwInit();
//OpenGL 版本
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
//窗口设置
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); //改为 GL_TRUE,改变窗口,纵横比会变
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "B18090114", nullptr,
nullptr);
if (nullptr == window)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
// next two lines are for mac retina display
int screenWidth, screenHeight;
glfwGetFramebufferSize(window, &screenWidth, &screenHeight); //获取窗口大小
glfwMakeContextCurrent(window); //可以新建很多 window
glfwSetKeyCallback(window, KeyCallback);
//glfwSetCursorPosCallback(window, MouseCallback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetScrollCallback(window, ScrollCallback);
glewExperimental = GL_TRUE;
if (GLEW_OK != glewInit())
{
std::cout << "Failed to initialise GLEW" << std::endl;
return -1;
}
//后片元处理
glEnable(GL_BLEND);//透明度
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//允许深度测试,深度测试方程, z坐标小的能覆盖之前的
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glViewport(0, 0, screenWidth, screenHeight); //从(0,0)开始画点,直到 WIDTH 和 HEIGHT
Shader shader1 = Shader("res/Shader/core.vs", "res/Shader/core.fs");
Shader shader[30];
for (int i = 1; i <= 27; i++) shader[i] = shader1;
GLfloat positions[30][3] =
{
{-0.9,0.9,0.67},
//front 1-9
{-0.9,0.9,0.67},
{0.0,0.9,0.67},
{0.9,0.9,0.67},
{-0.9,0.0,0.67},
{0.0,0.0,0.67},
{0.9,0.0,0.67},
{-0.9,-0.9,0.67},
{0.0,-0.9,0.67},
{0.9,-0.9,0.67},
//back 10-18
{-0.9,0.9,-0.67},
{0.0,0.9,-0.67},
{0.9,0.9,-0.67},
{-0.9,0.0,-0.67},
{0.0,0.0,-0.67},
{0.9,0.0,-0.67},
{-0.9,-0.9,-0.67},
{0.0,-0.9,-0.67},
{0.9,-0.9,-0.67},
//mid 19-27
{-0.9,0.9,0.0},
{0.0,0.9,0.0},
{0.9,0.9,0.0},
{-0.9,0.0,0.0},
{0.0,0.0,0.0},
{0.9,0.0,0.0},
{-0.9,-0.9,0.0},
{0.0,-0.9,0.0},
{0.9,-0.9,0.0},
};
int level[10][10] =
{
{0,1,2,3,10,11,12,19,20,21}, //up
{0,7,8,9,16,17,18,25,26,27}, //buttom
{0,1,4,7,10,13,16,19,22,25}, //left
{0,3,6,9,12,15,18,21,24,27} //right
};
int vec[2][3] = { {0.0,1.0,0.0},{1.0,0.0,0.0} };
//now the verte information comes below
GLfloat vertices[] = {
//back //color
//红色
-0.5f,-0.5f,-0.5f, 1.0f,0.0f,0.0f,
0.5f,-0.5f,-0.5f, 1.0f,0.0f,0.0f,
0.5f,0.5f,-0.5f, 1.0f,0.0f,0.0f,
0.5f,0.5f,-0.5f, 1.0f,0.0f,0.0f,
-0.5,0.5f,-0.5f, 1.0f,0.0f,0.0f,
-0.5,-0.5f,-0.5f, 1.0f,0.0f,0.0f,
//front
-0.5f,-0.5f,0.5f, 0.0f,1.0f,0.0f,
0.5f,-0.5f,0.5f, 0.0f,1.0f,0.0f,
0.5f,0.5f,0.5f, 0.0f,1.0f,0.0f,
0.5f,0.5f,0.5f, 0.0f,1.0f,0.0f,
-0.5,0.5f,0.5f, 0.0f,1.0f,0.0f,
-0.5,-0.5f,0.5f, 0.0f,1.0f,0.0f,
//left
-0.5f,0.5f,0.5f, 0.0f,0.0f,1.0f,
-0.5f,0.5f,-0.5f, 0.0f,0.0f,1.0f,
-0.5f,-0.5f,-0.5f, 0.0f,0.0f,1.0f,
-0.5f,-0.5f,-0.5f, 0.0f,0.0f,1.0f,
-0.5,-0.5f,0.5f, 0.0f,0.0f,1.0f,
-0.5,0.5f,0.5f, 0.0f,0.0f,1.0f,
//right
0.5f,0.5f,0.5f, 0.0f,1.0f,1.0f,
0.5f,0.5f,-0.5f, 0.0f,1.0f,1.0f,
0.5f,-0.5f,-0.5f, 0.0f,1.0f,1.0f,
0.5f,-0.5f,-0.5f, 0.0f,1.0f,1.0f,
0.5,-0.5f,0.5f, 0.0f,1.0f,1.0f,
0.5,0.5f,0.5f, 0.0f,1.0f,1.0f,
//bottom
-0.5f,-0.5f,-0.5f, 1.0f,0.0f,1.0f,
0.5f,-0.5f,-0.5f, 1.0f,0.0f,1.0f,
0.5f,-0.5f,0.5f, 1.0f,0.0f,1.0f,
0.5f,-0.5f,0.5f, 1.0f,0.0f,1.0f,
-0.5f,-0.5f,0.5f, 1.0f,0.0f,1.0f,
-0.5f,-0.5f,-0.5f, 1.0f,0.0f,1.0f,
//up
-0.5f,0.5f,-0.5f, 1.0f,1.0f,0.0f,
0.5f,0.5f,-0.5f, 1.0f,1.0f,0.0f,
0.5f,0.5f,0.5f, 1.0f,1.0f,0.0f,
0.5f,0.5f,0.5f, 1.0f,1.0f,0.0f,
-0.5f,0.5f,0.5f, 1.0f,1.0f,0.0f,
-0.5f,0.5f,-0.5f, 1.0f,1.0f,0.0f,
};
//the date should be transfered to the memory on the Graphics Card,传到显存
GLuint VAO, VBO; //VAO:Vertex Array Object VBO:Vertex Buffer Object传数据
glGenVertexArrays(1, &VAO); //创建 VAO
glGenBuffers(1, &VBO);
glBindVertexArray(VAO); //设当前直线
glBindBuffer(GL_ARRAY_BUFFER, VBO); //VAO 和 VBO 成对出现
// transfer the data:传数据
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); //静态访问,几乎不修改
// set the attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
GLuint VAO1, VBO1;
glGenVertexArrays(1, &VAO1);
glGenBuffers(1, &VBO1);
glBindVertexArray(VAO1);
glBindBuffer(GL_ARRAY_BUFFER, VBO1);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
//通道选择(core.vs中的location) 取几个浮点型 隔几个浮点型取下一个参数 起始位置
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
//解绑定
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
//画图
while (!glfwWindowShouldClose(window))
{
GLfloat currentTime = glfwGetTime();
deltaTime = currentTime - lastTime;
lastTime = currentTime;
glViewport(0, 0, screenWidth, screenHeight);
glfwPollEvents(); //把所有事件系统都取过来:键盘/鼠标等操作
DoMovement();
glClearColor(0.2f, 0.3f, 0.3f, 1.0f); //窗口背景颜色,RGB,最后一个是透明度
glEnable(GL_DEPTH_TEST); //深度测试
glDepthFunc(GL_LESS); //深度信息小于当期信息,就把进行测试
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glm 从 0.9.9 版本起,默认会将矩阵类型初始化为一个零矩阵(所有元素均为 0)
glm::mat4 view = camera.GetViewMatrix();
glm::mat4 projection = glm::perspective(glm::radians(camera.GetZoom()),
static_cast<GLfloat>(screenWidth) / static_cast<GLfloat>(screenHeight), 0.1f, 100.0f);
glm::mat4 transform = glm::mat4(1.0f);
GLuint transLoc = glGetUniformLocation(shader1.Program, "transform");
GLuint viewLoc = glGetUniformLocation(shader1.Program, "view");
GLuint proLoc = glGetUniformLocation(shader1.Program, "projection");
for (int i = 1; i <= 27; i++)
{
shader[i].Use();
transform = glm::mat4(1.0f);
//if(i<=9)
for(int j=1;j<=9;j++)
if (i == level[l][j])
{
int k = l / 2;
transform = glm::rotate(transform, glm::radians(500.0f) * static_cast<GLfloat>(glfwGetTime()), glm::vec3(vec[k][0], vec[k][1], vec[k][2]));//旋转
break;
}
transform = glm::scale(transform, glm::vec3(0.1f, 0.1f, 0.1f));//缩放
transform = glm::translate(transform, glm::vec3(positions[i][0], positions[i][1], positions[i][2]));//平移
transLoc = glGetUniformLocation(shader[i].Program, "transform");
glUniformMatrix4fv(transLoc, 1, GL_FALSE, glm::value_ptr(transform));
viewLoc = glGetUniformLocation(shader[i].Program, "view");
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
proLoc = glGetUniformLocation(shader[i].Program, "projection");
glUniformMatrix4fv(proLoc, 1, GL_FALSE, glm::value_ptr(projection));
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 36); //n个点
glBindVertexArray(0);
}
glfwSwapBuffers(window);
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glfwTerminate();
return 0;
}
void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mode) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
if (key == GLFW_KEY_UP) l = 0;
if (key == GLFW_KEY_DOWN) l = 1;
if (key == GLFW_KEY_LEFT) l = 2;
if (key == GLFW_KEY_RIGHT) l = 3;
}
void MouseCallback(GLFWwindow* window, double xPos, double yPos) {
if (firstMouse) {
lastX = xPos;
lastY = yPos;
firstMouse = false;
}
GLfloat xOffset = xPos - lastX;
GLfloat yOffset = lastY - yPos;
lastX = xPos;
lastY = yPos;
camera.ProcessMouseMovement(xOffset, yOffset);
}
void DoMovement() {
if (keys[GLFW_KEY_W] || keys[GLFW_KEY_UP]) {
camera.ProcessKeyboard(FOWARD, deltaTime);
}
if (keys[GLFW_KEY_S] || keys[GLFW_KEY_DOWN]) {
camera.ProcessKeyboard(BACKWARD, deltaTime);
}
if (keys[GLFW_KEY_A] || keys[GLFW_KEY_LEFT]) {
camera.ProcessKeyboard(LEFT, deltaTime);
}
if (keys[GLFW_KEY_D] || keys[GLFW_KEY_RIGHT]) {
camera.ProcessKeyboard(RIGHT, deltaTime);
}
}
void ScrollCallback(GLFWwindow* window, double xOffset, double yOffset) {
camera.ProcessScroll(yOffset);
}
shader.h
#pragma once
#include<string>
#include<fstream> //可以打开文件
#include<sstream>
#include<iostream>
#include<glew.h>
class Shader {
GLuint vertex, fragment;
public:
GLuint Program;
Shader() {}
; Shader(const GLchar* vertexPath, const GLchar* fragmentPath)
{
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
vShaderFile.exceptions(std::ifstream::badbit);
fShaderFile.exceptions(std::ifstream::badbit);
try {
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
std::stringstream vShaderStream, fShaderStream;
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
//文件关闭顺序,先 v 再 f
vShaderFile.close();
fShaderFile.close();
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
}
catch (std::ifstream::failure a) {
std::cout <<
"ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ"
<< std::endl;
}
//类型转换
const GLchar* vShaderCode = vertexCode.c_str();
const GLchar* fShaderCode = fragmentCode.c_str();
//import and compile the shader
vertex = glCreateShader(GL_VERTEX_SHADER); //不用重新定义
glShaderSource(vertex, 1, &vShaderCode, NULL);
glCompileShader(vertex); //编译
GLint success;
GLchar infoLog[512];
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success); //编译是否完成的位置
if (!success) {
glGetShaderInfoLog(vertex, 512, NULL, infoLog);
std::cout <<
"ERROR::SHADER::VERTEX::COMPILATION_FAILED\n"
<< infoLog << std::endl;
}
//边缘调色器
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment); //编译
glGetShaderiv(fragment, GL_COMPILE_STATUS, &success); //编译是否完成的位置
if (!success) {
glGetShaderInfoLog(fragment, 512, NULL, infoLog);
std::cout <<
"ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n"
<< infoLog << std::endl;
}
//create the program and link the program
this->Program = glCreateProgram(); //创建着色器程序
glAttachShader(this->Program, vertex);
glAttachShader(this->Program, fragment);
glLinkProgram(this->Program); //链接
glValidateProgram(this->Program); //可省略
glGetProgramiv(this->Program, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(this->Program, 512, NULL, infoLog); //获取链接情况
std::cout <<
"ERROR::SHADER::PROGRAM::LINKING_FAILED\n" <<
infoLog << std::endl;
}
}
~Shader() {
glDetachShader(this->Program, vertex);
glDetachShader(this->Program, fragment);
glDeleteShader(vertex);
glDeleteShader(fragment);
glDeleteProgram(this->Program);
}
void Use() {
glUseProgram(this->Program);
}
};
Camera.h
#pragma once
#include<vector>
#define GLEW_STATIC
#include<glew.h>
#include<glm/glm.hpp>
#include<glm/gtc/matrix_transform.hpp>
enum Camera_Movement {
FOWARD,
BACKWARD,
LEFT,
RIGHT
};
const GLfloat YAW = -90.0f;
const GLfloat PITCH = 0.0f;
const GLfloat SPEED = 6.0f;
const GLfloat SENSITIVITY = 0.25f;
const GLfloat ZOOM = 45.0f;
class Camera {
public:
Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f), GLfloat yaw = YAW,
GLfloat pitch = PITCH) :front(glm::vec3(0.0f, 0.0f, -1.0f)), movementSpeed(SPEED),
mouseSensitivity(SENSITIVITY), zoom(ZOOM) {
this->position = position;
this->worldUp = up;
this->yaw = yaw;
this->pitch = pitch;
this->updateCameraVectors();
}
Camera(GLfloat posX, GLfloat posY, GLfloat posZ, GLfloat upX, GLfloat upY, GLfloat upZ, GLfloat yaw = YAW,
GLfloat pitch = PITCH) :front(glm::vec3(0.0f, 0.0f, -1.0f)), movementSpeed(SPEED),
mouseSensitivity(SENSITIVITY), zoom(ZOOM) {
this->position = glm::vec3(posX, posY, posZ);
this->worldUp = glm::vec3(upX, upY, upZ);
this->yaw = yaw;
this->pitch = pitch;
this->updateCameraVectors();
}
void ProcessKeyboard(Camera_Movement direction, GLfloat deltaTime) {
GLfloat velocity = this->movementSpeed * deltaTime;
if (direction == FOWARD) {
this->position += this->front * velocity;
}
if (direction == BACKWARD) {
this->position -= this->front * velocity;
}
if (direction == LEFT) {
this->position -= this->right * velocity;
}
if (direction == RIGHT) {
this->position += this->right * velocity;
}
}
void ProcessMouseMovement(GLfloat xOffset, GLfloat yOffset) {
xOffset *= this->mouseSensitivity;
yOffset *= this->mouseSensitivity;
this->yaw += xOffset;
this->pitch += yOffset;
this->updateCameraVectors();
}
void ProcessScroll(GLfloat yOffset) {
this->zoom += yOffset;
}
GLfloat GetZoom() {
return this->zoom;
}
glm::mat4 GetViewMatrix() {
return glm::lookAt(this->position, this->position + this->front, this->up);
}
private:
GLfloat yaw;
GLfloat pitch;
GLfloat movementSpeed;
GLfloat mouseSensitivity;
GLfloat zoom;
glm::vec3 position;
glm::vec3 front;
glm::vec3 up;
glm::vec3 right;
glm::vec3 worldUp;
void updateCameraVectors() {
glm::vec3 front;
front.x = cos(glm::radians(this->pitch)) * cos(glm::radians(this->yaw));
front.y = sin(glm::radians(this->pitch));
front.z = cos(glm::radians(this->pitch)) * sin(glm::radians(this->yaw));
this->front = glm::normalize(front);
this->right =
glm::normalize(glm::cross(this->front, this->worldUp));
this->up = glm::normalize(glm::cross(this->right, this->front));
}
};
顶点着色器:
core.vs
//顶点着色器
#version 330 core
layout(location = 0) in vec3 position;
layout (location = 1) in vec3 color;
out vec3 ourColor;
uniform mat4 transform;
uniform mat4 view;
uniform mat4 projection;
void main()
{
gl_Position = projection * view * transform * vec4(position, 1.0f);
ourColor = color;
}
片元着色器:
core.fs
#version 330 core
in vec3 ourColor;
out vec4 color;
void main()
{
color = vec4(ourColor, 1.0f);
}
light.fs
//片元着色器
#version 330 core
in vec3 ourColor;
out vec4 color;
void main()
{
color = vec4(1.0f, 1.0f, 1.0f, 1.0f);
}
light.vs
#version 330 core
layout(location = 0) in vec3 position;
layout (location = 1) in vec3 color;
out vec3 ourColor;
uniform mat4 transform;
uniform mat4 view;
uniform mat4 projection;
void main()
{
gl_Position = projection * view * transform * vec4(position, 1.0f);
ourColor = color;
}