这是我第二次制作游戏引擎,但现在遇到了一些困难,因为无论我发送什么对象,OpenGL只会在屏幕中央绘制一个白色三角形,就像这样。
我甚至从上一个引擎的渲染器和相机对象中复制了旧代码,但仍然表现相同,所以我猜测这与渲染脚本有关。
渲染器:
渲染器:
Renderer2D::Renderer2D(const Shader& shader) {
this->shader = shader;
this->Init();
}
Renderer2D::~Renderer2D() {
glDeleteVertexArrays(1, &this->QuadVAO);
}
void Renderer2D::Render(Texture & texture, iVec2 position, iVec2 size, float rotation, iVec3 color) {
this->shader.Use();
iMat4 model;
using namespace glm;
model = translate(model, iVec3(position.x, position.y, 0.0f));
/*
model = translate(model, iVec3(size.x * 0.5f, size.y * 0.5f, 0.0f));
model = rotate(model, rotation, iVec3(0.0f, 0.0f, 1.0f));
model = translate(model, iVec3(size.x * -0.5f, size.y * -0.5f, 0.0f));
*/
model = glm::scale(model, iVec3(size.x, size.y, 1.0f));
this->shader.SetMatrix4("model2D", model);
this->shader.SetVector3f("color2D", color);
glActiveTexture(GL_TEXTURE0);
texture.Bind();
glBindVertexArray(this->QuadVAO);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
}
void Renderer2D::Init() {
U16 VBO;
float vertices[] = {
// Pos // Tex
0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 0.0f, 1.0f, 0.0f
};
glGenVertexArrays(1, &this->QuadVAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindVertexArray(this->QuadVAO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
顶点着色器:
#version 330 core
layout (location = 0) in vec4 vertex; // <vec2 position, vec2 texCoords>
out vec2 TexCoords;
uniform mat4 view2D;
uniform mat4 model2D;
uniform mat4 projection2D;
void main()
{
TexCoords = vertex.zw;
gl_Position = projection2D * view2D * mode2Dl * vec4(vertex.xy, 0.0, 1.0);
}
编辑:
顶点着色器:
#version 330 core
layout (location = 0) in vec4 vertex; // <vec2 position, vec2 texCoords>
out vec2 TexCoords;
uniform mat4 view2D;
uniform mat4 model2D;
uniform mat4 projection2D;
void main()
{
TexCoords = vertex.zw;
gl_Position = projection2D * view2D * model2D * vec4(vertex.xy, 0.0, 1.0);
}
片元着色器:
#version 330 core
in vec2 TexCoords;
out vec4 color2D; //Fixed the error!
uniform sampler2D image2D;
uniform vec3 color2D;
void main()
{
color2D = vec4(color2D, 1.0) * texture(image2D, TexCoords);
}
Resources.cpp
Shader Resources::LoadShaderFromFile(const char * vertexSource, const char * fragmentSource) {
using namespace std;
string vertexCode;
string fragmentCode;
try {
ifstream vertexShaderFile(vertexSource);
ifstream fragmentShaderFile(fragmentSource);
stringstream vShaderStream, fShaderStream;
vShaderStream << vertexShaderFile.rdbuf();
fShaderStream << fragmentShaderFile.rdbuf();
vertexShaderFile.close();
fragmentShaderFile.close();
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
}
catch (exception e) {
cout << "ERROR::SHADER: Failed to read shader files" << std::endl;
}
const char *vShaderCode = vertexCode.c_str();
const char *fShaderCode = fragmentCode.c_str();
Shader shader;
shader.Compile(vShaderCode, fShaderCode);
return shader;
}