Skip to content

Commit d35a39d

Browse files
committed
добавил кубики и класс
1 parent f093ecc commit d35a39d

File tree

2 files changed

+191
-0
lines changed

2 files changed

+191
-0
lines changed

rendering/CubeRenderer.cpp

+164
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#include "CubeRenderer.h"
2+
3+
CubeRenderer::CubeRenderer(ShaderManager& shaderManager) : shaderManager(shaderManager), pCamera(nullptr) {
4+
SetupCubeData();
5+
shaderManager.loadVertexShader("cubeVertexShader", R"(
6+
#version 330 core
7+
layout(location = 0) in vec3 aPos;
8+
layout(location = 1) in vec3 aInstancePosition;
9+
layout(location = 2) in vec3 aInstanceColor;
10+
uniform mat4 projection;
11+
uniform mat4 view;
12+
uniform float scale;
13+
uniform vec3 cameraPosition;
14+
out vec3 vColor;
15+
out vec3 Normal;
16+
out vec3 FragPos;
17+
void main()
18+
{
19+
vec3 worldPosition = aInstancePosition + vec3(scale * 0.5, scale * 0.5, 0.0);
20+
vec3 scaledPosition = (aPos * scale * 0.8) + worldPosition;
21+
FragPos = scaledPosition;
22+
Normal = normalize(aPos);
23+
gl_Position = projection * view * vec4(scaledPosition, 1.0);
24+
vColor = aInstanceColor;
25+
}
26+
)");
27+
28+
shaderManager.loadFragmentShader("cubeFragmentShader", R"(
29+
#version 330 core
30+
in vec3 vColor;
31+
in vec3 Normal;
32+
in vec3 FragPos;
33+
uniform vec3 cameraPosition;
34+
uniform vec3 lightColor;
35+
out vec4 FragColor;
36+
void main()
37+
{
38+
vec3 norm = normalize(Normal);
39+
vec3 lightDir = normalize(cameraPosition - FragPos); // íàïðàâëåíèå ñâåòà îò êàìåðû ê ôðàãìåíòó
40+
float diff = max(dot(norm, lightDir), 0.0);
41+
vec3 diffuse = diff * lightColor;
42+
vec3 result = (0.3 + diffuse) * vColor; // 0.3 - ýòî àìáèåíòíûé öâåò
43+
FragColor = vec4(result, 1.0);
44+
}
45+
)");
46+
47+
shaderManager.linkProgram("cubeShaderProgram", "cubeVertexShader", "cubeFragmentShader");
48+
}
49+
50+
void CubeRenderer::SetCamera(const Camera& camera) {
51+
this->pCamera = &camera;
52+
}
53+
54+
void CubeRenderer::SetupCubeData() {
55+
// Âåðøèíû êóáà
56+
//float vertices[] = {
57+
// -0.5f, -0.5f, -0.5f,
58+
// 0.5f, -0.5f, -0.5f,
59+
// 0.5f, 0.5f, -0.5f,
60+
// -0.5f, 0.5f, -0.5f,
61+
// -0.5f, -0.5f, 0.5f,
62+
// 0.5f, -0.5f, 0.5f,
63+
// 0.5f, 0.5f, 0.5f,
64+
// -0.5f, 0.5f, 0.5f
65+
//};
66+
67+
//// Èíäåêñû äëÿ ïîñòðîåíèÿ êóáà
68+
//unsigned int indices[] = {
69+
// 0, 1, 2, 2, 3, 0, // front
70+
// 1, 5, 6, 6, 2, 1, // right
71+
// 7, 6, 5, 5, 4, 7, // back
72+
// 4, 0, 3, 3, 7, 4, // left
73+
// 4, 5, 1, 1, 0, 4, // bottom
74+
// 3, 2, 6, 6, 7, 3 // top
75+
//};
76+
float vertices[] = {
77+
-0.5f, -0.5f, -0.55f, // 0 - èçìåíåíà Z-êîîðäèíàòà
78+
0.5f, -0.5f, -0.55f, // 1 - èçìåíåíà Z-êîîðäèíàòà
79+
0.5f, 0.5f, -0.55f, // 2 - èçìåíåíà Z-êîîðäèíàòà
80+
-0.5f, 0.5f, -0.55f, // 3 - èçìåíåíà Z-êîîðäèíàòà
81+
-0.5f, -0.5f, 0.5f,
82+
0.5f, -0.5f, 0.5f,
83+
0.5f, 0.5f, 0.5f,
84+
-0.5f, 0.5f, 0.5f
85+
};
86+
87+
// Èíäåêñû îñòàþòñÿ òåìè æå, òàê êàê ïîðÿäîê âåðøèí íå èçìåíèëñÿ
88+
unsigned int indices[] = {
89+
0, 1, 2, 2, 3, 0, // front
90+
1, 5, 6, 6, 2, 1, // right
91+
7, 6, 5, 5, 4, 7, // back
92+
4, 0, 3, 3, 7, 4, // left
93+
4, 5, 1, 1, 0, 4, // bottom
94+
3, 2, 6, 6, 7, 3 // top
95+
};
96+
glGenVertexArrays(1, &VAO);
97+
glGenBuffers(1, &VBO);
98+
glGenBuffers(1, &EBO);
99+
glGenBuffers(1, &instanceVBO);
100+
101+
glBindVertexArray(VAO);
102+
103+
glBindBuffer(GL_ARRAY_BUFFER, VBO);
104+
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
105+
106+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
107+
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
108+
109+
// Âåðøèííûå àòðèáóòû äëÿ êóáà
110+
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
111+
glEnableVertexAttribArray(0);
112+
113+
// Óáåäèòåñü, ÷òî instanceVBO òàêæå ïðàâèëüíî íàñòðîåí
114+
glBindBuffer(GL_ARRAY_BUFFER, instanceVBO);
115+
glBindBuffer(GL_ARRAY_BUFFER, 0);
116+
glBindVertexArray(0);
117+
}
118+
119+
120+
void CubeRenderer::Render(const std::vector<Vector3d>& positions, const std::vector<Vector3d>& colors, float cellSize) {
121+
if (!pCamera) return;
122+
123+
GLuint shaderProgram = shaderManager.getProgram("cubeShaderProgram");
124+
glUseProgram(shaderProgram);
125+
glBindVertexArray(VAO);
126+
127+
// Íàñòðîéêà àòðèáóòîâ äëÿ èíñòàíñèíãà
128+
glBindBuffer(GL_ARRAY_BUFFER, instanceVBO);
129+
glBufferData(GL_ARRAY_BUFFER, positions.size() * sizeof(Vector3d) * 2, NULL, GL_STATIC_DRAW);
130+
for (size_t i = 0; i < positions.size(); ++i) {
131+
glBufferSubData(GL_ARRAY_BUFFER, i * sizeof(Vector3d) * 2, sizeof(Vector3d), &positions[i]);
132+
glBufferSubData(GL_ARRAY_BUFFER, (i * sizeof(Vector3d) * 2) + sizeof(Vector3d), sizeof(Vector3d), &colors[i]);
133+
}
134+
135+
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 2 * sizeof(Vector3d), (void*)0);
136+
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 2 * sizeof(Vector3d), (void*)sizeof(Vector3d));
137+
glEnableVertexAttribArray(1);
138+
glEnableVertexAttribArray(2);
139+
glVertexAttribDivisor(1, 1); // Ýòî ãîâîðèò OpenGL èñïîëüçîâàòü ñëåäóþùèé àòðèáóò äëÿ êàæäîãî èíñòàíñà
140+
glVertexAttribDivisor(2, 1);
141+
142+
GLint projectionLoc = glGetUniformLocation(shaderProgram, "projection");
143+
GLint viewLoc = glGetUniformLocation(shaderProgram, "view");
144+
GLint scaleLoc = glGetUniformLocation(shaderProgram, "scale");
145+
GLint cameraPositionLoc = glGetUniformLocation(shaderProgram, "cameraPosition");
146+
GLint lightColorLoc = glGetUniformLocation(shaderProgram, "lightColor");
147+
148+
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, pCamera->GetProjectionMatrix());
149+
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, pCamera->GetViewMatrix());
150+
glUniform1f(scaleLoc, cellSize);
151+
152+
// Ïîëó÷àåì ïîçèöèþ êàìåðû
153+
float posX, posY, posZ;
154+
pCamera->GetPosition(posX, posY, posZ);
155+
glUniform3f(cameraPositionLoc, posX, posY, posZ);
156+
157+
// Óñòàíîâêà öâåòà ñâåòà
158+
glUniform3f(lightColorLoc, 1.0f, 1.0f, 1.0f);
159+
160+
//glDrawElementsInstanced(GL_TRIANGLES, 18, GL_UNSIGNED_INT, 0, positions.size());
161+
glDrawElementsInstanced(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0, positions.size());
162+
163+
glBindVertexArray(0);
164+
}

rendering/CubeRenderer.h

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
#ifndef CUBERENDERER_H_
3+
#define CUBERENDERER_H_
4+
5+
#include "../system/GLFunctions.h"
6+
#include "../system/ShaderManager.h"
7+
#include "../mathematics/Vector3d.h"
8+
#include "Camera.h"
9+
#include <vector>
10+
#include <array>
11+
#include <string>
12+
13+
14+
class CubeRenderer {
15+
public:
16+
CubeRenderer(ShaderManager& shaderManager);
17+
void Render(const std::vector<Vector3d>& positions, const std::vector<Vector3d>& colors, float cellSize);
18+
void SetCamera(const Camera& camera);
19+
private:
20+
void SetupCubeData();
21+
ShaderManager& shaderManager;
22+
GLuint VAO, VBO, EBO, instanceVBO;
23+
GLuint instanceCount;
24+
const Camera* pCamera; // Óêàçàòåëü íà êàìåðó
25+
};
26+
27+
#endif // CUBERENDERER_H_

0 commit comments

Comments
 (0)