使用glm进行BufferSubData计算

时间:2015-11-15 23:50:13

标签: c++ opengl glm-math

我已经在互联网上搜索并找到了许多替代方案,但我的代码中没有一种可用。

我正在尝试使用GLM和OpenGL为我的计算机图形引擎创建一个摄像头。 cmr的类型为Camera

cmr->createOrthoCamera(-2.0f, 2.0f, -2.0f, 2.0f, 1.0f, 10.0f); //initialize OrthoCamera
cmr->createPersCamera(30.0f, 640.0f / 480.0f, 1.0f, 10.0f); //initialize Pers Matrix
cmr->createViewMatrix(glm::vec3(-7.f,-7.f,-7.f),glm::vec3(0.f,0.f,0.f),glm::vec3(0.f,1.f,0.f)); //initialize View Matrix with lookat
cmr->setUboId(shPrgm->getUniformBlockIndex("SharedMatrices")); //itialize uboId with value from loaded shader
cmr->initCamera(UBO_BP);
cmr->switchProjection(); //just to start one of the vars

所以我首先为两个mat4矩阵分配空间(initCamera):

glGenBuffers(1, &uboId);
glBindBuffer(GL_UNIFORM_BUFFER, uboId);
glBufferData(GL_UNIFORM_BUFFER, sizeof(glm::mat4) * 2, 0, GL_STREAM_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER, UBO_BP, uboId); 

然后当我打电话给我时,我做了:

glBindBuffer(GL_UNIFORM_BUFFER, uboId);
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(glm::mat4), glm::value_ptr(ViewMatrix[0]));
glBufferSubData(GL_UNIFORM_BUFFER, sizeof(glm::mat4), sizeof(glm::mat4), glm::value_ptr(ProjMatrix[0]));
glBindBuffer(GL_UNIFORM_BUFFER, 0);

但是图像全是黑色的。我知道问题出在这个区域,因为如果我从glm切换到GLfloat矩阵,它就可以了。 问题必须在sizeof()或draw()中。 任何人都可以帮助我吗?

我的相机定义:

#pragma once

#include "Dependencies\glew\glew.h"
#include "Dependencies\freeglut\freeglut.h"
#include "Dependencies\glm\glm.hpp"
#include "Dependencies\glm\gtc\matrix_transform.hpp"
#include "Dependencies\glm\gtc\type_ptr.hpp"

namespace CGEngine {

class Camera {
private:

    GLuint uboId;

    glm::mat4 ViewMatrix;

    glm::mat4 PersMatrix;
    glm::mat4 OrthoMatrix;

    glm::mat4* ProjMatrix = &OrthoMatrix;

    bool perspective = false;

public:



    Camera();

    void initCamera(GLuint UBO_BP);

    void createOrthoCamera(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat nearZ, GLfloat farZ);
    void createPersCamera(GLfloat flovy, GLfloat aspect, GLfloat nearZ, GLfloat farZ);

    void createViewMatrix(glm::vec3 eye, glm::vec3 center, glm::vec3 up);

    void setUboId(GLuint newUboId);


    void draw();

    void switchProjection();

};

}

0 个答案:

没有答案