OpenGL learning (2)

Draw graphics using OpenGL

The previous article recorded the configuration of the opengl programming environment and the creation of a window for drawing graphics. This article records how to draw graphics in the created window. First, we must understand the mechanism of the rendering pipeline, as follows:

Rendering Pipeline Diagram

insert image description here
Here a new Mesh.h header file and a Mesh.cpp file
Mesh.h are created

#pragma once
#include <glm/glm.hpp>
#include <GL/glew.h>

class Vertex
{
    
    
public:
	Vertex(const glm::vec3& pos)
	{
    
    
		this->pos = pos;
	}
protected:
private:
	glm::vec3 pos;

};

class Mesh
{
    
    
public:
	Mesh(Vertex* vertices, unsigned int numVertices);
	void Draw();
	Mesh();
protected:
private:
	enum
	{
    
    
		POSITION,
		NUM_BUFFERS
	};
	GLuint m_vertxeArrayObject;
	GLuint m_vertexArrayBuffers[NUM_BUFFERS];
	unsigned int m_drawCount;
};

Mesh.cpp

#include "Mesh.h"

Mesh::Mesh(Vertex* vertices, unsigned int numVertices)
{
    
    
	m_drawCount = numVertices;

	glGenVertexArrays(1,&m_vertxeArrayObject);
	glBindVertexArray(m_vertxeArrayObject);

	glGenBuffers(NUM_BUFFERS,m_vertexArrayBuffers);
	glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[POSITION]);

	glBufferData(GL_ARRAY_BUFFER,numVertices*sizeof(vertices[0]),vertices,GL_STATIC_DRAW);

	glEnableVertexAttribArray(0);
	glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);

	glBindVertexArray(0);

}

Mesh::Mesh() 
{
    
    
	glDeleteVertexArrays(1, &m_vertxeArrayObject);
}

void Mesh::Draw()
{
    
    
	glBindVertexArray(m_vertxeArrayObject);

	glDrawArrays(GL_TRIANGLES,0,m_drawCount);

	glBindVertexArray(0);
}

Running effect:
insert image description here
The above codes are obtained from the official tutorial of Unreal University. When copying and pasting, you should follow your thinking to understand. Let's encourage each other

Guess you like

Origin blog.csdn.net/weixin_43541308/article/details/121405487