给我里面所有代码步骤,清晰整理给我,不要改文档里的代码
时间: 2024-11-22 22:48:36 浏览: 8
### 代码步骤整理
#### 1. Mesh 类的定义和实现
**mesh.h**
```cpp
#pragma once
#include "../nclgl/vector3.h"
#include "../nclgl/vector2.h"
class Mesh {
public:
static Mesh* GenerateQuad();
protected:
int numVertices;
unsigned int type;
Vector3* vertices;
Vector2* textureCoords;
Vector4* colours;
void BufferData();
};
```
**mesh.cpp**
```cpp
#include "mesh.h"
Mesh* Mesh::GenerateQuad() {
Mesh* m = new Mesh();
m->numVertices = 4;
m->type = GL_TRIANGLE_STRIP;
m->vertices = new Vector3[m->numVertices];
m->textureCoords = new Vector2[m->numVertices];
m->colours = new Vector4[m->numVertices];
m->vertices[0] = Vector3(-1.0f, 1.0f, 0.0f);
m->vertices[1] = Vector3(-1.0f, -1.0f, 0.0f);
m->vertices[2] = Vector3(1.0f, 1.0f, 0.0f);
m->vertices[3] = Vector3(1.0f, -1.0f, 0.0f);
m->textureCoords[0] = Vector2(0.0f, 1.0f);
m->textureCoords[1] = Vector2(0.0f, 0.0f);
m->textureCoords[2] = Vector2(1.0f, 1.0f);
m->textureCoords[3] = Vector2(1.0f, 0.0f);
for (int i = 0; i < 4; ++i) {
m->colours[i] = Vector4(1.0f, 1.0f, 1.0f, 1.0f);
}
m->BufferData();
return m;
}
```
#### 2. Renderer 类的定义和实现
**renderer.h**
```cpp
#pragma once
#include "../nclgl/renderer.h"
#include "../nclgl/window.h"
#include "../nclgl/mesh.h"
#include "../nclgl/shader.h"
#include "../nclgl/matrix4.h"
#include "../nclgl/vector3.h"
class Renderer : public OGLRenderer {
public:
Renderer(Window& parent);
~Renderer(void);
void RenderScene() override;
void ToggleObject();
void ToggleDepth();
void ToggleAlphaBlend();
void ToggleBlendMode();
void MoveObject(float by);
protected:
GLuint textures[2];
Mesh* meshes[2];
Shader* shader;
Vector3 positions[2];
bool modifyObject;
bool usingDepth;
bool usingAlpha;
int blendMode;
};
```
**renderer.cpp**
```cpp
#include "renderer.h"
Renderer::Renderer(Window& parent) : OGLRenderer(parent) {
meshes[0] = Mesh::GenerateQuad();
meshes[1] = Mesh::GenerateTriangle();
textures[0] = SOIL_load_OGL_texture(
TEXTUREDIR"brick.tga",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
0
);
textures[1] = SOIL_load_OGL_texture(
TEXTUREDIR"stainedglass.tga",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
0
);
if (!textures[0] || !textures[1]) {
return;
}
positions[0] = Vector3(0, 0, -5);
positions[1] = Vector3(0, 0, -5);
shader = new Shader("TexturedVertex.glsl", "TexturedFragment.glsl");
if (!shader->LoadSuccess()) {
return;
}
usingDepth = false;
usingAlpha = false;
blendMode = 0;
modifyObject = true;
projMatrix = Matrix4::Perspective(1.0f, 100.0f, (float)width / (float)height, 45.0f);
init = true;
}
Renderer::~Renderer(void) {
delete meshes[0];
delete meshes[1];
delete shader;
glDeleteTextures(2, textures);
}
void Renderer::RenderScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
BindShader(shader);
UpdateShaderMatrices();
glUniform1i(glGetUniformLocation(shader->GetProgram(), "diffuseTex"), 0);
glActiveTexture(GL_TEXTURE0);
for (unsigned int i = 0; i < 2; ++i) {
glUniformMatrix4fv(glGetUniformLocation(shader->GetProgram(), "modelMatrix"), 1, false, (float*)&Matrix4::Translation(positions[i]));
glBindTexture(GL_TEXTURE_2D, textures[i]);
meshes[i]->Draw();
}
}
void Renderer::ToggleObject() {
modifyObject = !modifyObject;
}
void Renderer::MoveObject(float by) {
positions[(int)modifyObject].z += by;
}
void Renderer::ToggleBlendMode() {
blendMode = (blendMode + 1) % 4;
switch (blendMode) {
case 0: glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); break;
case 1: glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR); break;
case 2: glBlendFunc(GL_ONE, GL_ZERO); break;
case 3: glBlendFunc(GL_SRC_ALPHA, GL_ONE); break;
}
}
void Renderer::ToggleDepth() {
usingDepth = !usingDepth;
usingDepth ? glEnable(GL_DEPTH_TEST) : glDisable(GL_DEPTH_TEST);
}
void Renderer::ToggleAlphaBlend() {
usingAlpha = !usingAlpha;
usingAlpha ? glEnable(GL_BLEND) : glDisable(GL_BLEND);
}
```
#### 3. 主文件
**main.cpp**
```cpp
#include "../nclgl/window.h"
#include "renderer.h"
int main() {
Window w("Transparency and Depth!", 800, 600, false);
if (!w.HasInitialised()) {
return -1;
}
Renderer renderer(w);
if (!renderer.HasInitialised()) {
return -1;
}
while (w.UpdateWindow() && !Window::GetKeyboard()->KeyDown(KEYBOARD_ESCAPE)) {
if (Window::GetKeyboard()->KeyTriggered(KEYBOARD_1)) {
renderer.ToggleEntity();
}
if (Window::GetKeyboard()->KeyTriggered(KEYBOARD_2)) {
renderer.ToggleDepth();
}
if (Window::GetKeyboard()->KeyTriggered(KEYBOARD_3)) {
renderer.ToggleAlphaBlend();
}
if (Window::GetKeyboard()->KeyTriggered(KEYBOARD_4)) {
renderer.ToggleBlendMode();
}
if (Window::GetKeyboard()->KeyDown(KEYBOARD_UP)) {
renderer.MoveObject(1.0f);
}
if (Window::GetKeyboard()->KeyDown(KEYBOARD_DOWN)) {
renderer.MoveObject(-1.0f);
}
renderer.RenderScene();
}
return 0;
}
```
以上是文档中提到的所有代码步骤,已经按照类和文件进行了分类和整理。希望这对你有帮助!
阅读全文