计算机图形学(六)——画一个旋转的立方体

画一个旋转的立方体

//画立方体

#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.5f,       0.0f,1.0f,0.0f,
		-0.5f,-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.5f,-0.5f,0.5f,       0.0f,0.0f,1.0f,
		-0.5f,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.5f,-0.5f,0.5f,        0.0f,1.0f,1.0f,
		0.5f,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,

	};

	GLuint VAO, VBO;
	glGenVertexArrays(1, &VAO);
	glGenBuffers(1, &VBO);

	glBindVertexArray(VAO);
	glBindBuffer(GL_ARRAY_BUFFER, VBO);

	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
	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);


	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);
}

#include #include "stdafx.h" #define GLUT_DISABLE_ATEXIT_HACK #include //#pragma comment(lib, "glut32.lib") GLfloat AngleX;//旋转向量 GLfloat AngleY; void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); //这个函数其实就是对接下来要做什么进行一下声明 GL_MODELVIEW 模型视图 glLoadIdentity(); glPushMatrix();// 提供了相应的接口 { glRotatef(AngleX, 1.0f, 0.0f, 0.0f); glRotatef(AngleY, 0.0f, 1.0f, 0.0f); glBegin(GL_POLYGON); //前表面 glColor3f(1.0f,1.0f,1.0f);//颜色设置为白色 glVertex3f(50.0f, 50.0f, 50.0f); glColor3f(1.0f,1.0f,0.0f);//颜色设置为黄色 glVertex3f(50.0f, -50.0f, 50.0f); glColor3f(1.0f,0.0f,0.0f);//颜色设置为红色 glVertex3f(-50.0f, -50.0f, 50.0f); glColor3f(1.0f,0.0f,1.0f);//颜色设置为品红色 glVertex3f(-50.0f, 50.0f, 50.0f); glEnd(); glBegin(GL_POLYGON); //后表面 glColor3f(0.0f, 1.0f, 1.0f);//颜色设置为青色 glVertex3f(50.0f, 50.0f, -50.0f); glColor3f(0.0f, 1.0f, 0.0f);//颜色设置为绿色 glVertex3f(50.0f, -50.0f, -50.0f); glColor3f(0.0f, 0.0f, 0.0f);//颜色设置为黑色 glVertex3f(-50.0f, -50.0f, -50.0f); glColor3f(0.0f, 0.0f, 1.0f);//颜色设置为蓝色 glVertex3f(-50.0f, 50.0f, -50.0f); glEnd(); glBegin(GL_POLYGON); //右表面 glColor3ub((GLubyte)255, (GLubyte)255, (GLubyte)255);//颜色设置为白色 glVertex3f(50.0f, 50.0f, 50.0f); glColor3f(0.0f, 1.0f, 1.0f);//颜色设置为青色 glVertex3f(50.0f, 50.0f, -50.0f); glColor3f(0.0f, 1.0f, 0.0f);//颜色设置为绿色 glVertex3f(50.0f, -50.0f, -50.0f); glColor3ub((GLubyte)255, (GLubyte)255, (GLubyte)0);//颜色设置为黄色 glVertex3f(50.0f, -50.0f, 50.0f); glEnd(); glBegin(GL_POLYGON); //左表面 glColor3d(0.0, 0.0, 1.0);//颜色设置为蓝色 glVertex3f(-50.0f, 50.0f, -50.0f); glColor3f(0.0f, 0.0f, 0.0f);//颜色设置为黑色 glVertex3f(-50.0f, -50.0f, -50.0f); glColor3ub((GLubyte)255, (GLubyte)0, (GLubyte)0);//颜色设置为红色 glVertex3f(-50.0f, -50.0f, 50.0f); glColor3f(1.0f, 0.0f, 1.0f);//颜色设置为品红色 glVertex3f(-50.0f, 50.0f, 50.0f); glEnd(); glBegin(GL_POLYGON); //上表面 glColor3d(0.0, 1.0, 1.0);//颜色设置为青色 glVertex3f(50.0f, 50.0f, -50.0f); glColor3d(1.0, 1.0, 1.0);//颜色设置为白色 glVertex3f(50.0f, 50.0f, 50.0f); glColor3d(1.0, 0.0, 1.0);//颜色设置为品红色 glVertex3f(-50.0f, 50.0f, 50.0f); glColor3d(0.0, 0.0, 1.0);//颜色设置为蓝色 glVertex3f(-50.0f, 50.0f, -50.0f); glEnd(); glBegin(GL_POLYGON); //下表面 glColor3f(0.0f, 1.0f, 0.0f);//颜色设置为绿色 glVertex3f(50.0f, -50.0f, -50.0f); glColor3ub((GLubyte)255, (GLubyte)255, (GLubyte)0);//颜色设置为黄色 glVertex3f(50.0f, -50.0f, 50.0f); glColor3f(1.0f, 0.0f, 0.0f);//颜色设置为红色 glVertex3f(-50.0f, -50.0f, 50.0f); glColor3f(0.0f, 0.0f, 0.0f);//颜色设置为黑色 glVertex3f(-50.0f, -50.0f, -50.0f); glEnd(); } glPopMatrix(); glutSwapBuffers(); } void reshape(int w, int h) { GLfloat aspect = (GLfloat)w / (GLfloat)h; GLfloat nRange = 100.0f; glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); //将当前矩阵指定为投影模式 glLoadIdentity(); //设置三维投影区 if (w 355.0f) { AngleX = 0.0f; } if (AngleX 355.0f) AngleY = 0.0f; if (AngleY < 0.0f) { AngleY = 355.0f; } glutPostRedisplay(); } void init() { AngleX = 45.0f; AngleY = 315.0f; glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glEnable(GL_DEPTH_TEST); //初始化OpenGL glEnable(GL_DITHER); //抖动是激活的。 glShadeModel(GL_SMOOTH);//两点间颜色有过渡效果 } void main(int argc, char* argv[]) { glutInit(&argc;, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); //使用双缓存 使用深度缓存。 glutInitWindowSize(480, 480); glutCreateWindow("OpenGL颜色立方体"); glutReshapeFunc(reshape); //窗口改变的时候调用的函数 glutDisplayFunc(display); glutSpecialFunc(key_board); //函数注册鼠标响应事件 init(); glutMainLoop(); }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值