Я не уверен, почему, но мой VBO исчезает после одного кадра (начальный кадр показывает его в исходном углу неправильно). Ниже приведен код для моего класса матрицы, метода рисования и вершинного шейдера.
Matrix::Matrix() {
for (int i = 0; i < 16; i++) {
values[i] = 0;
}
}
Matrix Matrix::operator*(const Matrix &matrix) {
Matrix result;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 4; k++) {
result.values[i * 4 + j] += this->values[i * 4 + k] * matrix.values[k * 4 + j];
}
}
}
return result;
}
Matrix Matrix::Perspective(float fov, float aspect, float znear, float zfar) {
Matrix matrix;
float xymax = znear * tan(fov * 0.00872664);
float q = -(zfar * znear) / (zfar - znear);
float val = znear / xymax;
matrix.values[0] = val / aspect;
matrix.values[5] = val;
matrix.values[10] = q;
matrix.values[11] = -1;
matrix.values[14] = 2 * q;
return matrix;
}
Matrix Matrix::LookAt(Vector3f eye, Vector3f target, Vector3f up) {
Vector3f zaxis = Vector3f::Normalize(target - eye);
Vector3f xaxis = Vector3f::Normalize(Vector3f::CrossProduct(up, zaxis));
Vector3f yaxis = Vector3f::CrossProduct(zaxis, xaxis);
// vy doesn't need to be normalized because it's a cross product of 2 normalized vectors
Matrix viewMatrix;
viewMatrix.values[0] = xaxis.x;
viewMatrix.values[1] = xaxis.y;
viewMatrix.values[2] = xaxis.z;
viewMatrix.values[3] = 0;
viewMatrix.values[4] = yaxis.x;
viewMatrix.values[5] = yaxis.y;
viewMatrix.values[6] = yaxis.z;
viewMatrix.values[7] = 0;
viewMatrix.values[8] = zaxis.x;
viewMatrix.values[9] = zaxis.y;
viewMatrix.values[10] = zaxis.z;
viewMatrix.values[11] = 0;
viewMatrix.values[12] = 0;
viewMatrix.values[13] = 0;
viewMatrix.values[14] = 0;
viewMatrix.values[15] = 1;
Matrix translationMatrix = Matrix::Identity();
translationMatrix.values[12] = -eye.x;
translationMatrix.values[13] = -eye.y;
translationMatrix.values[14] = -eye.z;
return translationMatrix * viewMatrix;
}
Matrix Matrix::Identity() {
Matrix matrix;
matrix.values[0] = 1;
matrix.values[5] = 1;
matrix.values[10] = 1;
matrix.values[15] = 1;
return matrix;
}
Переменные:
static const GLfloat g_vertex_buffer_data[] =
{
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f
};
GLuint vertexbuffer;
GLuint programID;
Метод рисования:
void draw() {
glClearColor(0.5, 0.5, 0.5, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glUseProgram(programID);
Matrix projection = Matrix::Perspective(45.0f, (float)(screen_size.x) / screen_size.y, 0.1f, 1000.0f);
Matrix view = Matrix::LookAt(Vector3f(4, 3, 3), Vector3f(0, 0, 0), Vector3f(0, 1, 0));
Matrix model = Matrix::Identity();
Matrix mvp = projection * view * model;
GLuint matrixID = glGetUniformLocation(programID, "mvp");
glUniformMatrix4fv(matrixID, 1, GL_FALSE, mvp.values);
int positionId = glGetAttribLocation(programID, "position");
glEnableVertexAttribArray(positionId);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(positionId, 3, GL_FLOAT, GL_FALSE, 0, (void *)0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(positionId);
glutSwapBuffers();
}
Вершинный шейдер:
in vec3 position;
uniform mat4 mvp;
void main() {
gl_Position = mvp * vec4(position, 1);
}
Задача ещё не решена.
Других решений пока нет …