在同一屏幕上显示两个不同的对象
Posted
技术标签:
【中文标题】在同一屏幕上显示两个不同的对象【英文标题】:Displaying two different objects on same screen 【发布时间】:2019-03-29 15:29:08 【问题描述】:我试图在同一屏幕上显示一个球体和一个圆环,但似乎是“glutInitDisplayMode”导致我只显示一个。
当我将其保留为 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA |GLUT_DEPTH)
时,圆环会显示。
当我将其保留为 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH)
时,球体显示。我该怎么做才能同时显示两者?
渲染代码如下:
static void render(void)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
// clear the drawing buffer.
glClear(GL_COLOR_BUFFER_BIT);
// clear the identity matrix.
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0);
glColor3f(0.0, 0.0, 0.0);
glRotatef(zRotated, 0.0, 0.0, 1.0);
// scaling transfomation
glScalef(1.0, 1.0, 1.0);
// built-in (glut library) function , draw you a sphere.
glutSolidTorus(innerRadius, outerRadius, sides, rings);
// Flush buffers to screen
glFlush();
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// activate our shader program
glUseProgram(programId);
// turn on depth buffering
glEnable(GL_DEPTH_TEST);
float aspectRatio = (float)width / (float)height;
glm::mat4 projection = glm::perspective(glm::radians(45.0f), aspectRatio, 0.1f, 1000.0f);
// view matrix - orient everything around our preferred view
glm::mat4 view = glm::lookAt(
glm::vec3(40,30,30), // eye/camera location
glm::vec3(0,0,0), // where to look
glm::vec3(0,1,0) // up
);
// model matrix: translate, scale, and rotate the model
glm::vec3 rotationAxis(0,1,0);
glm::mat4 model = glm::mat4(1.0f);
model = glm::rotate(model, glm::radians(angle), glm::vec3(0, 1, 0)); // rotate about the y-axis
model = glm::scale(model, glm::vec3(25.0f, 25.0f, 25.0f));
// model-view-projection matrix
glm::mat4 mvp = projection * view * model;
GLuint mvpMatrixId = glGetUniformLocation(programId, "MVP");
glUniformMatrix4fv(mvpMatrixId, 1, GL_FALSE, &mvp[0][0]);
// texture sampler - a reference to the texture we've previously created
// send the texture id to the texture sampler
GLuint textureUniformId = glGetUniformLocation(programId, "textureSampler");
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureId);
glUniform1i(textureUniformId, 0);
// find the names (ids) of each vertex attribute
GLint positionAttribId = glGetAttribLocation(programId, "position");
GLint colourAttribId = glGetAttribLocation(programId, "colour");
GLint textureCoordsAttribId = glGetAttribLocation(programId, "textureCoords");
GLint normalAttribId = glGetAttribLocation(programId, "normal");
// provide the vertex positions to the shaders
glBindBuffer(GL_ARRAY_BUFFER, positions_vbo);
glEnableVertexAttribArray(positionAttribId);
glVertexAttribPointer(positionAttribId, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
// provide the vertex texture coordinates to the shaders
glBindBuffer(GL_ARRAY_BUFFER, textureCoords_vbo);
glEnableVertexAttribArray(textureCoordsAttribId);
glVertexAttribPointer(textureCoordsAttribId, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
// provide the vertex normals to the shaders
glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
glEnableVertexAttribArray(normalAttribId);
glVertexAttribPointer(normalAttribId, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
// draw the triangles
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glDrawElements(GL_TRIANGLES, numVertices, GL_UNSIGNED_INT, (void*)0);
// disable the attribute arrays
glDisableVertexAttribArray(positionAttribId);
glDisableVertexAttribArray(textureCoordsAttribId);
glDisableVertexAttribArray(normalAttribId);
glDisableVertexAttribArray(colourAttribId);
// make the draw buffer to display buffer (i.e. display what we have drawn)
glutSwapBuffers();
【问题讨论】:
【参考方案1】:glutSolidTorus
使用固定函数属性绘制圆环。为此,兼容性配置文件OpenGL Context 必不可少。
glutSolidTorus
与您的着色器程序不兼容。使当前安装的程序失效,在glutSolidTorus
之前:
glUseProgram(0);
glutSolidTorus(innerRadius, outerRadius, sides, rings);
注意,OpenGL 是一个状态引擎。如果安装了一个程序,它会一直保留,直到安装另一个程序或它失效,甚至超出帧。在您的情况下,在第一帧中不是问题,但它仍会在第二帧开始时安装,此时调用 glutSolidTorus
。
如果您使用双缓冲窗口 (GLUT_DOUBLE
),您必须调用 glutSwapBuffers
。 glutSwapBuffers
交换缓冲区。绘制的缓冲区在窗口中显示。 show int eh 窗口的缓冲区已准备好在下一帧中绘制。
在单个缓冲窗口中,您可以执行glFlush
。这会强制 OpenGL 完成绘图。由于绘制到的缓冲区是相同的,并且显示在窗口上,这导致所有正在绘制到缓冲区的内容都会立即显示在窗口中。
将单个 glFlush
调用从 render
函数的中间移到其末尾,就在 glutSwapBuffers
之前:
void render()
// ...
glUseProgram(0);
glutSolidTorus(innerRadius, outerRadius, sides, rings);
// glFlush(); <---- delete
// ...
glUseProgram(programId);
// ...
glDrawElements(GL_TRIANGLES, numVertices, GL_UNSIGNED_INT, (void*)0);
// ...
glFlush();
glutSwapBuffers();
【讨论】:
非常感谢!帮了大忙!以上是关于在同一屏幕上显示两个不同的对象的主要内容,如果未能解决你的问题,请参考以下文章