openGLes3.0 动态添加的cube为什么只显示第一个?
void BeatingHeartSample::InitModel(GLuint mid){
if(mid==0){
LOGCATE("panpan= MID = %d", mid);
Cube *_mCube = new Cube();
_mCube->Initialize(0.0f, 0.0f, 5.0f,2000.0f,2000.0,50.0f);
cubeVector.push_back(_mCube);
Cube *_mCube1 = new Cube();
_mCube1->Initialize(20.0f, 0.0f, -0.1f,50.f,2000.0,600.f);
cubeVector.push_back(_mCube1);
Cube *_mCube2 = new Cube();
_mCube2->Initialize(-20.0f, 0.0f, -0.1f,50.f,2000.0,600.f);
cubeVector.push_back(_mCube2);
Cube *_mCube3 = new Cube();
_mCube3->Initialize(-10.0f, 0.0f, -0.1f,50.f,2000.0,600.f);
cubeVector.push_back(_mCube3);
}else if(mid==1){
LOGCATE("panpan1= MID = %d", mid);
Cube *_mCube = new Cube();
_mCube->Initialize(3.0f, 0.0f, 0.0f,500.0f,2000.0,50.0f);
cubeVector.push_back(_mCube);
}else if(mid==2){
Cube *_mCube5 = new Cube();
_mCube5->Initialize(0.0f, 10.5f, -0.1f,2000.f,50.0,600.f);
cubeVector.push_back(_mCube5);
Cube *_mCube6= new Cube();
_mCube6->Initialize(0.0f, 20.0f, -0.1f,2000.f,50.0,600.f);
cubeVector.push_back(_mCube6);
Cube *_mCube7= new Cube();
_mCube7->Initialize(0.0f, -20.0f, -0.1f,2000.f,50.0,600.f);
cubeVector.push_back(_mCube7);
drawScene(m_sceneMvpLoc, m_sceneMvpLightLoc, m_sceneModelLoc);
}else if(mid ==3){
cubeVector.pop_back();
}
}
```c++
//
// Created by yuepanpan on 2023/1/7.
//
#include "Cube.h"
Cube::Cube()
{
}
Cube::~Cube()
{
glDeleteBuffers(3, m_VboIds);
glDeleteVertexArrays(1, &m_VaoId);
}
void Cube::destroy(){
glDeleteBuffers(3, m_VboIds);
glDeleteVertexArrays(1, &m_VaoId);
}
void Cube::Initialize(float posX,float posY,float posZ,float scaleX,float scaleY,float scaleZ) {
modelPos[0]=posX;
modelPos[1]=posY;
modelPos[2]=posZ;
modelScale[0]=scaleX;
modelScale[1]=scaleY;
modelScale[2]=scaleZ;
GLfloat *positions;
GLuint *indices;
GLfloat *normals;
GLfloat *tex;
cubeNumIndices = esGenCube(1.0f,&positions,&normals,&tex,&indices);
glGenBuffers(4, m_VboIds);
// Position VBO for cube model
glBindBuffer(GL_ARRAY_BUFFER, m_VboIds[0]);
glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat) * 3, positions, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, m_VboIds[1]);
glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat) * 3, normals, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, m_VboIds[2]);
glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat) * 2, tex, GL_STATIC_DRAW);
// Index buffer object for cube model
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_VboIds[3]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * cubeNumIndices, indices, GL_STATIC_DRAW);
// Generate VAO Id
glGenVertexArrays(1, &m_VaoId);
glBindVertexArray(m_VaoId);
glBindBuffer(GL_ARRAY_BUFFER, m_VboIds[0]);
glEnableVertexAttribArray(POSITION_LOC);
glVertexAttribPointer(POSITION_LOC, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (const void*) nullptr);
glBindBuffer(GL_ARRAY_BUFFER, m_VboIds[1]);
glEnableVertexAttribArray(NORMALS_LOC);
glVertexAttribPointer(NORMALS_LOC, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (const void*) nullptr);
glBindBuffer(GL_ARRAY_BUFFER, m_VboIds[2]);
glEnableVertexAttribArray(TEX_LOC);
glVertexAttribPointer(TEX_LOC, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (const void*) nullptr);
glBindBuffer(GL_ARRAY_BUFFER, GL_NONE);
// Bind the index buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_VboIds[3]);
glBindVertexArray(GL_NONE);
delete positions;
positions = NULL;
delete indices;
indices = NULL;
delete normals;
normals = NULL;
delete tex;
tex = NULL;
}
void Cube::updateMVP(int m_Width, int m_Height,float eyePosition[3],float lightPosition[3],int rotateAngle[3]) {
ESMatrix perspective;
ESMatrix ortho;
ESMatrix view;
esMatrixLoadIdentity(&cubeModelView);
esMatrixLoadIdentity(&cubeMvpMatrix);
float aspect;
// Compute the window aspect ratio
aspect = (GLfloat)m_Width / (GLfloat)m_Height;
// Generate a perspective matrix with a 45 degree FOV for the scene rendering
esMatrixLoadIdentity(&perspective);
esPerspective(&perspective, 45.0f, aspect, 10.0f, 45000.0f);
// 用光源位置建立一个MVP矩阵
// 通过连接 正交投影、模型和视图变换矩阵生成的MVP变换矩阵
// Generate an orthographic projection matrix for the shadow map rendering
esMatrixLoadIdentity(&ortho);
esOrtho(&ortho, -8000, 8000, -8000, 8000, 1, 15000);
// CUBE
// position the cube
esMatrixLoadIdentity(&cubeModel);
esRotate(&cubeModel, -rotateAngle[0], 0.0f, 1.0f, 0.0f);
esRotate(&cubeModel,rotateAngle[1],1.0f, 0.0f, 0.0f);
// LOGCATE("panpan=rotateAngle[0] = %d,rotateAngle[1] = %d", rotateAngle[0], rotateAngle[1]);
esScale(&cubeModel, modelScale[0], modelScale[1], modelScale[2]);
esTranslate(&cubeModel, modelPos[0], modelPos[1], modelPos[2]);
esMatrixLookAt(&view,
eyePosition[0], eyePosition[1], eyePosition[2],
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
esMatrixMultiply(&cubeModelView, &cubeModel, &view);
esMatrixMultiply(&cubeMvpMatrix, &cubeModelView, &perspective);
esMatrixLookAt(&view,
lightPosition[0], lightPosition[1], lightPosition[2],
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
esMatrixMultiply(&cubeModelView, &cubeModel, &view);
esMatrixMultiply(&cubeMvpLightMatrix, &cubeModelView, &ortho);
}
void Cube::draw(GLint mvpLoc, GLint mvpLightLoc, GLint modelLoc, GLint m_TextureId, GLint m_SamplerLoc,GLint c_SamplerLoc,GLint modelMatrixLoc,GLint m_TextureUseLoc,float lightPosition[3], NativeImage renderImage){
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, m_TextureId);
glUniform1i(m_SamplerLoc, 3);
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, (GLfloat*)&cubeModelView.m[0][0]);
glUniformMatrix4fv(mvpLoc, 1, GL_FALSE, (GLfloat*)&cubeMvpMatrix.m[0][0]);
glUniformMatrix4fv(mvpLightLoc, 1, GL_FALSE, (GLfloat*)&cubeMvpLightMatrix.m[0][0]);
glUniformMatrix4fv(modelMatrixLoc, 1, GL_FALSE, (GLfloat*)&cubeModel.m[0][0]);
glUniform1i(m_TextureUseLoc, 1);
// Set the cube color to red
glVertexAttrib4f(1, 0.9f, 0.9f, 0.9f, 1.0f);
glVertexAttrib4f(2, lightPosition[0], lightPosition[1], lightPosition[2], 1.0f);
glBindVertexArray(m_VaoId);
glDrawElements(GL_TRIANGLES, cubeNumIndices, GL_UNSIGNED_INT, (const void*) nullptr);
}
/*void Cube::updateRenderImage(NativeImage _renderImage){
renderImage=_renderImage;
}*/
```c++
void BeatingHeartSample::Draw(int screenW, int screenH)
{
mscreenW=screenW;
mscreenH=screenH;
GLint defaultFramebuffer = 0;
// Initialize matrices
//_mGround->updateMVP(screenW, screenH, eyePosition,lightPosition,rotateAngle);
for(int i = 0;i<cubeVector.size();i++)
{
cubeVector[i]->updateMVP(screenW, screenH, eyePosition,lightPosition,rotateAngle);
}
//LOGCATE("panpan=size3333 = %d", cubeVector.size());
glEnable(GL_CULL_FACE);
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &defaultFramebuffer);
// FIRST PASS: Render the scene from light position to generate the shadow map texture
glBindFramebuffer(GL_FRAMEBUFFER, shadowMapBufferId);
// Set the viewport
glViewport(0, 0, shadowMapTextureWidth, shadowMapTextureHeight);
// 为了使用这些着色器,清除深度缓冲区并禁用颜色渲染。
// 可以使用多边形偏移命令增大写入纹理的深度值,以避免因为精度问题而造成的阴影渲染伪像
// clear depth buffer
glClear(GL_DEPTH_BUFFER_BIT);
// disable color rendering, only write to depth buffer
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
// reduce shadow rendering artifact
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(5.0f, 100.0f);
glClear( GL_DEPTH_BUFFER_BIT);
glClearColor(0.75f, 0.82f, 0.9f, 0.0f);
glCullFace(GL_FRONT);
glUseProgram(shadowMapProgramObject);
drawScene(shadowMapMvpLoc, shadowMapMvpLightLoc, m_sceneModelLoc);
glCullFace(GL_BACK);
glDisable(GL_POLYGON_OFFSET_FILL);
//glPolygonOffset(4.0f,100.f);
// SECOND PASS: Render the scene from eye location using the shadow map texture created in the first pass
glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glViewport(0, 0, screenW, screenH);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glUseProgram(sceneProgramObject);
// Bind the shadow map texture
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, shadowMapTextureId);
glUniform1i(shadowMapSamplerLoc, 1);
drawScene(m_sceneMvpLoc, m_sceneMvpLightLoc, m_sceneModelLoc);
}
```c++
void BeatingHeartSample::drawScene(GLint mvpLoc, GLint mvpLightLoc, GLint modelLoc) {
glUniform3f(m_ViewPosLoc, eyePosition[0], eyePosition[1],eyePosition[2]);
for(int i = 0;i<cubeVector.size();i++)
{
cubeVector[i]->draw(mvpLoc,mvpLightLoc,modelLoc,m_TextureIds[m_TextureValue2], m_SamplerLoc, c_SamplerLoc,m_modelMatrixLoc, m_TextureUseLoc, lightPosition,m_RenderImages[1]);
}
//_mGround->draw(mvpLoc,mvpLightLoc,modelLoc,m_TextureIds[m_TextureValue], m_SamplerLoc, m_modelMatrixLoc, m_TextureUseLoc, lightPosition);
//GO_CHECK_GL_ERROR();
}
如图所示,点击柜子2,cubeVector.push_back(_mCube1);cubeVector.push_back(_mCube1); push了两块板子,但是只是出现第一个板子。但是cubeVector的size是正确的。