这是我的代码:
int main(int argc, char* argv[]) {
if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
return false;
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 ) ;
SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 4 ) ;
if((window = SDL_CreateWindow("SDL opengl",100,100,640, 480, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN)) == NULL) {
return false;
}
maincontext = SDL_GL_CreateContext(window);
SDL_GL_SetSwapInterval(0);
glewExperimental=true;
GLenum err=glewInit();
Chapter* chapter1 = new Chapter();
chapter1->init();
glClearColor(0.0,0.0,0.0,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,640.0/480.0,1.0,500.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
SDL_Event Event;
while(Running) {
while(SDL_PollEvent(&Event)) {
event_update(&Event);
}
//main loop
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_TRIANGLES);
glVertex3f(0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glEnd();
glTranslatef(3.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glEnd();
SDL_GL_SwapWindow(window);
}
return 0;
}
我的问题是,只有清除的颜色才能看到更多内容。有什么问题呢? (当我开始没有错误或其他什么时)我使用Visual Stuido 2010,如果它很重要。
答案 0 :(得分:1)
从它的外观来看,你的近剪裁平面为1.0,远剪裁平面为500.0
但你画的z距离为0.0
我会尝试将绘图移动到平截头体(即在[1.0,500.0]的范围内),看看是否可以将您的几何体看作第一步。
glVertex3f (x, y, z); // here your z value is 0, I suggest this could be the problem.
我想我可能会在这里详细说明一下,因为我之前没有多少时间。
当您在更新代码中执行glTranslate...
时,每次更新都会调用一次,这通常是人们可能会假设的,导致您的几何在每次调用时在x轴上移动3个单位(即可能不是你的意思)。相反,我建议推动矩阵进行此转换,并在完成翻译后弹出它。 OpenGL提供了一个矩阵堆栈,所以基本上当你按下矩阵时你要“存储它以便以后”,然后你进行变换和渲染,然后弹出矩阵,导致你回到正方形。这样,您不会在每次更新时将矩阵转换为新距离,而是将矩阵从origo移动到要绘制每次更新的位置。
所以我的建议是将你的代码改为主循环代码:
//main loop
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f, 1.0f, 1.0f);
/* push the current state of the matrix (i.e. store it so we don't make the mentioned change per update */
glPushMatrix();
/* think of it as moving your camera, you positioned your camera (frustum) at origo looking at everything inside a frustum shape one unit in front of the camera to 500 units in front of the camera, at an angle and so on.. so we want to draw our triangle to the left (from your view) 3 units and 10 units into the screen */
glTranslate3f(-3.0f, 0.0f, -10.0f);
glBegin(GL_TRIANGLES);
glVertex3f(0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glEnd();
/* translate again, i.e. "move your camera" this time 6 units to the right, 3 units to compensate for the move you did to draw your triangle, and 3 units to place it in the right end. this time we do not move it in the z-axis, because we are already 10 pixels into the screen. */
glTranslatef(6.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glEnd();
/* pop the matrix changes we made with glTranslatef, so that we are back at the origo looking down negative z-axis so that the next draw-call we will again go 3 units left, 10 units down the z-axis and draw a triangle and so on.. hope it makes sense */
glPopMatrix();
SDL_GL_SwapWindow(window);
我希望它至少可以帮助你以可见的方式呈现内容!如果这一切看起来都不清楚我可以尝试进一步解释它,但希望这会以某种方式呈现,以便您可以开始玩它并感受它如何组合在一起。