当前位置:网站首页>OpenGL camera and periodic review
OpenGL camera and periodic review
2022-07-24 07:44:00 【Liu jingyiru】
Catalog
1. Components : Generate model outline .
2. Assembly of components : Combined components , Manage rendering information .
3. Components : Generate texture .
----- If there is a mistake , Please point out in time , thank you !
This article will start from OpenGL From the perspective of production process , Arrangement learn OpenGL The steps of applying texture program and camera . Also on the OpenGL I've sorted out what I learned recently .
( One )OpenGL The processing flow can be divided into three parts :Asset District . Assembly line . Assembly of components .

(1) Asset

(2) Component encapsulation

(3) Assembly of components


( Two ) Component analysis
1. Components : Generate model outline .
Need to C++ data , Locate by anchor , Passed into the array of attributes corresponding to the vertex shader .

2. Assembly of components : Combined components , Manage rendering information .


3. Components : Generate texture .
You need to transfer the read image data to the vertex shader , And then passed from vertex shader to clip shader , Data processing is done by the fragment shader .


4. Components : The camera
The camera here is not encapsulated into a class . We can understand the camera as the transformation of coordinates . A point in space passes through the camera matrix , Change to camera angle , Then it becomes the screen angle of view through the projection matrix transformation . Matrix processing in vertex shaders can actually be seen as a component “ function ”.
That is, the camera can be regarded as The result of the matrix acting on the vertex position coordinates . The following is the code of vertex shader file .


Observe (1)(2) Find out , This is more like writing to Shader Comments for shaders . Our goal is to bring all 、C++ Data processing can be understood by shaders Shader data .
analysis :(1) Make it generate a matrix data dedicated to the camera and projection matrix . hinder (2) Explained uniform The name of the variable , What is the corresponding data . The method of transferring data is similar to macro replacement . And pay attention to shaderProgram After the program is activated , It will take effect only after the data is transferred .
There are some cases , About some binding and configuration operations It cannot be encapsulated into components . It should be applied flexibly according to the characteristics of components . For example, when it comes to shader The variables in the , It is necessary to separate the binding operation , Convenient for subsequent calls . In addition, components that may involve batch applications are not applicable ( Such as texture components : After importing multiple textures, you also need to specify texture units ).
Running results :

( 3、 ... and ) Code :
main.cpp
#define _CRT_SECURE_NO_DEPRECATE
#include"Base.h"
#include"shader.h"
#include"ffImage.h"
//================Asset============
unsigned int VAO,VBO;
unsigned int _texture=0;
unsigned int _texture2 = 1;
Shader _shaderProgram;
ffImage* _pImage = NULL;
glm::mat4 _viewMatrix(1.0f);
glm::mat4 _projMatrix(1.0f);
//================Asset============
//= Assembly line ====================
void initModel()
{
float vertices[] =
{
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
};
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices),vertices ,GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(sizeof(float)*3));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void initShader(const char* _vertexPath, const char* _fragPath)
{
_shaderProgram.initShader(_vertexPath, _fragPath);
}
void initTexture()
{
glGenTextures(1, &_texture);
_pImage = ffImage::readFromFile("res/wall.jpg");
glBindTexture(GL_TEXTURE_2D, _texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _pImage->getWidth(), _pImage->getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, _pImage->getData());
glGenTextures(2, &_texture2);
_pImage = ffImage::readFromFile("res/awesomeface.png");
glBindTexture(GL_TEXTURE_2D, _texture2);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _pImage->getWidth(), _pImage->getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, _pImage->getData());
}
void rend()
{
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
_viewMatrix = glm::lookAt(glm::vec3(3.0f, 3.0f, 3.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));// Customize the camera matrix
_projMatrix = glm::perspective(glm::radians(30.0f), 1.0f, 0.1f, 100.0f); // Custom projection matrix
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, _texture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, _texture2);
_shaderProgram.shader_Begin();
// Incoming matrix data
_shaderProgram.setInt("_texture", 0);
_shaderProgram.setInt("_texture2", 1);
_shaderProgram.setMartrix("_viewMatrix", _viewMatrix);// Input data to the matrix of the corresponding name
_shaderProgram.setMartrix("_projMatrix", _projMatrix);
// Pass in primitive vertex objects
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
_shaderProgram.shader_End();
}
//= Component call press ====================
int main()
{
GLFWwindow* wWindow;
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3.0);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3.0);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
wWindow = glfwCreateWindow(800, 800, "wuyutong", NULL, NULL);
if (!wWindow)
{
std::cout << "Fail to Create Window" << std::endl;
glfwTerminate();// Release resources
return -1;
}
glfwMakeContextCurrent(wWindow);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to intialize GLAD" << std::endl;
}
// Assembly area of components ================================================
initModel();
initTexture();
_shaderProgram.initShader("vertexShader.glsl", "fragmentShader.glsl");
// Assembly area of components ================================================
while (!glfwWindowShouldClose(wWindow))
{
rend();
glfwSwapBuffers(wWindow);
glfwPollEvents();
}
glDeleteBuffers(1, &VBO);
glfwTerminate();
return 0;
} Shader Code
fragmentShader.glsl
#version 330 core
out vec4 FragColor;
in vec2 outUV;
uniform sampler2D _texture;
uniform sampler2D _texture2;
void main()
{
vec4 _texture=texture(_texture,outUV).xyzw;
vec4 _texture2=texture(_texture2,outUV).xyzw;
FragColor =mix(_texture,_texture2,0.2);
};vertexShader.glsl
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aUV;
out vec2 outUV;
uniform mat4 _viewMatrix;
uniform mat4 _projMatrix;
void main()
{
gl_Position = _projMatrix*_viewMatrix * vec4(aPos.x, aPos.y, aPos.z,1.0);
outUV = aUV;
};
Shader class Shader.h
#pragma once
#include "Base.h"
class Shader
{
public:
Shader()
{
m_shaderProgram = 0;
}
~Shader(){}
void initShader(const char* _vertexPath, const char* _fragPath);
void shader_Begin()
{
glUseProgram(m_shaderProgram);
}
void shader_End()
{
glUseProgram(0);
}
void setInt(const std::string& name, int value) const
{
glUniform1i(glGetUniformLocation(m_shaderProgram, name.c_str()), value);
}
void setMartrix(const std::string& _name, glm::mat4 matrix)const;
private:
unsigned int m_shaderProgram;
};
Shader.cpp
#include "shader.h"
void Shader::initShader(const char* _vertexPath, const char* _fragPath)
{
std::string _vertexCode("");
std::string _fragCode("");
std::ifstream _vShaderFile;
std::ifstream _fShaderFile;
_vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
_fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try
{
_vShaderFile.open(_vertexPath);
_fShaderFile.open(_fragPath);
std::stringstream _vShaderStream, _fShaderStream;
_vShaderStream << _vShaderFile.rdbuf();
_fShaderStream << _fShaderFile.rdbuf();
_vertexCode = _vShaderStream.str();
_fragCode = _fShaderStream.str();
}
catch (std::ifstream::failure e)
{
std::string errStr = "read shader fail";
std::cout << errStr << std::endl;
}
const char* _vShaderStr = _vertexCode.c_str();
const char* _fShaderStr = _fragCode.c_str();
//shader Compile links for
unsigned int _vertexID = 0, _fragID = 0;
char _infoLog[512];
int _successFlag = 0;
// compile
_vertexID = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(_vertexID, 1, &_vShaderStr, NULL);
glCompileShader(_vertexID);
glGetShaderiv(_vertexID, GL_COMPILE_STATUS, &_successFlag);
if (!_successFlag)
{
glGetShaderInfoLog(_vertexID, 512, NULL, _infoLog);
std::string errStr(_infoLog);
std::cout << _infoLog << std::endl;
}
_fragID = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(_fragID, 1, &_fShaderStr, NULL);
glCompileShader(_fragID);
glGetShaderiv(_fragID, GL_COMPILE_STATUS, &_successFlag);
if (!_successFlag)
{
glGetShaderInfoLog(_fragID, 512, NULL, _infoLog);
std::string errStr(_infoLog);
std::cout << _infoLog << std::endl;
}
// link
m_shaderProgram = glCreateProgram();
glAttachShader(m_shaderProgram, _vertexID);
glAttachShader(m_shaderProgram, _fragID);
glLinkProgram(m_shaderProgram);
glGetProgramiv(m_shaderProgram, GL_LINK_STATUS, &_successFlag);
if (!_successFlag)
{
glGetProgramInfoLog(m_shaderProgram, 512, NULL, _infoLog);
std::string errStr(_infoLog);
std::cout << _infoLog << std::endl;
}
glDeleteShader(_vertexID);
glDeleteShader(_fragID);
}
void Shader::setMartrix(const std::string& _name, glm::mat4 _matrix)const
{
glUniformMatrix4fv(glGetUniformLocation(m_shaderProgram, _name.c_str()), 1, GL_FALSE, glm::value_ptr(_matrix));
//1 individual , Column priority , Data conversion array
}----- If there is a mistake , Please point out in time , thank you !
边栏推荐
- Implement a queue with two stacks.
- C language file operation
- DOM operation of JS -- style operation
- One click Copy and import of web interface data into postman
- Problems encountered in inserting large quantities of data into the database in the project
- 服务漏洞&FTP&RDP&SSH&rsync
- 系统集成项目管理工程师(软考中级)重点知识、背诵版
- 【sklearn】PCA
- Collection of binary tree topics
- hcip第八天笔记
猜你喜欢
![[hiflow] Tencent cloud hiflow scene connector realizes intelligent campus information management](/img/a9/7cdab9264902b1e2947a43463f6b32.png)
[hiflow] Tencent cloud hiflow scene connector realizes intelligent campus information management

Harbor2.2 quick check of user role permissions

游戏三子棋

XSS漏洞学习

爬虫学习-概述

Hcip 13th day notes

DevOps随笔

Postman extracts the token parameter value in the response header and sets it as an environment variable, with code attached

requests-爬取页面源码数据

Use JMeter to analyze and test the lottery probability of the lottery interface
随机推荐
剑指offer专项突击版第8天
numpy.cumsum
The goal you specified requires a project to execute but there is no POM in this directory
Network security B module windows operating system penetration test of national vocational college skills competition
Jackson parsing JSON detailed tutorial
25. Message subscription and publishing - PubSub JS
[sklearn] RF cross validation out of bag data parameter learning curve grid search
The growth path of software testing
numpy.arange
Hcip day 8 notes
Appium use
Sense dimension design responsive layout
[cloud native] MySQL index analysis and query optimization
【HiFlow】腾讯云HiFlow场景连接器实现校园信息管理智能化
Mutual implementation of stack and queue (c)
Selenium basic knowledge multi window processing
Selenium basics controls the scroll bar of the browser
Unable to auto assemble, bean of type "redistemplate" not found
PHP escape string
Simple installation of sqli Labs