画一个旋转的立方体
//画立方体
#include <iostream>
#define GLEW_STATIC
#include"Shader.h"
#include"Camera.h"
#include<GL/glew.h>
#include<GLFW/glfw3.h>
#include<glm/glm.hpp>
#include<glm/gtc/matrix_transform.hpp>
#include<glm/gtc/type_ptr.hpp>
int main()
{
glfwInit();
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);
int WIDTH = 800, HEIGHT = 600;
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT,
"Learn OpenGL ", nullptr, nullptr);
int screenWidth, screenHeight;
glfwGetFramebufferSize(window, &screenWidth, &screenHeight);
if (window == nullptr) {
std::cout << "Failed to creat GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
std::cout << "Failed to initialize GLEW" << std::endl;
glfwTerminate();
return -1;
}
//允许深度测试
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
Shader shader = Shader("res/shaders/code.vs", "res/shaders/code.fs");
//Cube
GLfloat vertices[] = {
//position //color
//back
-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.5f,0.5f,-0.5f, 1.0f,0.0f,0.0f,
-0.5f,-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.5f,0.5f,0.5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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);
while (!glfwWindowShouldClose(window)) {
glViewport(0, 0, screenWidth, screenHeight);
glfwPollEvents();
DoMovement();
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//初始化深度
shader.Use();
glm::mat4 transform = glm::mat4(1.0f);//生成一个4x4单位矩阵
//旋转变化
transform = glm::rotate(transform, glm::radians(20.0f)*static_cast<GLfloat>(glfwGetTime()), glm::vec3(1.0f, 1.0f, 1.0f));
//平移
transform = glm::translate(transform, glm::vec3(0.0f, 0.4f, 0.0f));
//缩放
transform = glm::scale(transform, glm::vec3(0.5f, 0.5f, 0.5f));
GLuint transLoc = glGetUniformLocation(shader.Program, "transform");//找到transform的位置
glUniformMatrix4fv(transLoc, 1, GL_FALSE, glm::value_ptr(transform));//只传一个矩阵,所以变量是1
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES,0,36);
glBindVertexArray(0);
glfwSwapBuffers(window);
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glfwTerminate();
return 0;
}
如果旋转、平移、变换操作调换了顺序,正方体的运动方式会发生改变,具体如何变化可自行尝试。
Shader.h
#pragma once
#include<string>
#include<fstream>
#include<sstream>
#include<iostream>
#include<GL/glew.h>
class Shader {
GLuint vertex, fragment;
public:
GLuint Program;
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();
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;
}
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);
}
};
code.vs
#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 color;
out vec3 Color;
uniform mat4 transform;
void main(){
gl_Position = transform * vec4(position, 1.0f);
Color = color;
}
code.fs
#version 330 core
in vec3 Color;
out vec4 color;
void main(){
color = vec4(Color, 1.0f);
}