0
這段代碼來自紅皮書,例子2-15(好吧,代碼並不完全是書中的代碼)。照顧我注意到的筆記。OpenGL qeustion:爲什麼glutSwapBuffer()函數沒有工作?
#include <fstream>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glut.h>
#pragma comment(lib,"glew32.lib")
using namespace std;
#define BUFFER_OFFSET(offset) ((GLubyte *)NULL+offset)
#define XStart -0.8
#define XEnd 0.8
#define YStart -0.8
#define YEnd 0.8
#define NumXPoints 11
#define NumYPoints 11
#define NumPoints (NumXPoints * NumYPoints)
#define NumPointsPerStrip (2*NumXPoints)
#define NumStrips (NumYPoints-1)
#define RestartIndex 0xffff
void display(void)
{
int i,start;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);
glDrawElements(GL_TRIANGLE_STRIP,NumStrips*(NumPointsPerStrip+1),GL_UNSIGNED_SHORT,BUFFER_OFFSET(0));
//glFlush();//it works,show a white square with black backgroud
glutSwapBuffers();///it doesn't work,show what tha area looked like before
}
void init (void)
{
GLuint vbo,ebo;
GLfloat *vertices;
GLushort *indices;
glewInit();
glGenBuffers(1,&vbo);
glBindBuffer(GL_ARRAY_BUFFER,vbo);
glBufferData(GL_ARRAY_BUFFER,2*NumPoints*sizeof(GLfloat),NULL,GL_STATIC_DRAW);
vertices=(GLfloat *)glMapBuffer(GL_ARRAY_BUFFER,GL_WRITE_ONLY);
if(vertices==NULL)
{
fprintf(stderr,"Unable to map vertex buffer\n");
exit(EXIT_FAILURE);
}
else
{
int i,j;
GLfloat dx=(XEnd-XStart)/(NumXPoints-1);
GLfloat dy=(YEnd-YStart)/(NumYPoints-1);
GLfloat *tmp=vertices;
int n=0;
for(j=0;j<NumYPoints;++j)
{
GLfloat y=YStart+j*dy;
for(i=0;i<NumXPoints;++i)
{
GLfloat x=XStart + i*dx;
*tmp++=x;
*tmp++=y;
}
}
glUnmapBuffer(GL_ARRAY_BUFFER);
glVertexPointer(2,GL_FLOAT,0,BUFFER_OFFSET(0));
glEnableClientState(GL_VERTEX_ARRAY);
}
glGenBuffers(1,&ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,NumStrips*(NumPointsPerStrip+1)*sizeof(GLushort),NULL,GL_STATIC_DRAW);
indices=(GLushort *)glMapBuffer(GL_ELEMENT_ARRAY_BUFFER,GL_WRITE_ONLY);
if(indices==NULL)
{
fprintf(stderr,"Unable to map index buffer\n");
exit(EXIT_FAILURE);
}
else
{
int i,j;
GLushort *index=indices;
for(j=0;j<NumStrips;++j)
{
GLushort bottomRow=j*NumYPoints;
GLushort topRow=bottomRow+NumYPoints;
for(i=0;i<NumXPoints;++i)
{
*index++=topRow+i;
*index++=bottomRow+i;
}
*index++=RestartIndex;
}
glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
}
glPrimitiveRestartIndex(RestartIndex);
glEnable(GL_PRIMITIVE_RESTART);
}
void reshape (int w, int h)
{
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluOrtho2D (-1,1,-1,1);
glViewport (0,0,w,h);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (200, 200);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc (keyboard);
glutMainLoop();
return 0;
}
爲http://www.opengl.org/resources/libraries/glut/spec3/node21.html說:
隱式glFlush由glutSwapBuffers它返回之前完成。在調用glutSwapBuffers後可以立即發出後續的OpenGL命令,但在緩衝區交換完成之前不會執行。
如果使用的層沒有雙緩衝,則glutSwapBuffers不起作用。
如何知道使用的層是否爲雙緩衝?給出一個雙緩衝的example.is這段代碼是雙緩衝的?
感謝您的正確答案^ _ ^。我已經得到了這些觀點。 – 2012-08-07 15:36:21
沒問題。歡迎來到StackOverflow。記住如果答案正確回答你的問題,你應該考慮[接受](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)它。 – 2012-08-07 15:37:30
我接受了你的答案。(¯ε(#¯) – 2012-08-08 11:49:57