为什么我无法使用此代码显示任何内容?
#include <iostream>
#include "GL/glfw.h"
#ifndef MAIN
#define MAIN
#include "GL/gl.h"
#include "GL/glu.h"
#endif
using namespace std;
void display();
int main()
{
int running = GL_TRUE;
glfwInit();
if( !glfwOpenWindow( 640,480, 0,0,0,0,0,0, GLFW_WINDOW ) )
{
glfwTerminate();
return 0;
}
while( running )
{
//GL Code here
display();
glfwSwapBuffers();
// Check if ESC key was pressed or window was closed
running = !glfwGetKey( GLFW_KEY_ESC ) &&
glfwGetWindowParam( GLFW_OPENED );
}
glfwTerminate();
return 0;
}
void display()
{
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0,0,640,480);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//glTranslatef(0, 0, -2);
glBegin(GL_POLYGON);
glColor3f(1.0, 0.2, 0.2);
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.75, 0.25, 0.0);
glVertex3f(0.75, 0.75, 0.0);
glVertex3f(0.25, 0.75, 0.0);
glEnd();
glFlush();
}
答案 0 :(得分:2)
我认为问题是当你没有指定坐标系时 例如
void glOrtho(GLdouble left,
GLdouble right,
GLdouble bottom,
GLdouble top,
GLdouble nearVal,
GLdouble farVal);
或
void glFrustum(GLdouble left,
GLdouble right,
GLdouble bottom,
GLdouble top,
GLdouble nearVal,
GLdouble farVal);
默认坐标系是:
-1,1,-1,1,-1,1
所以glTranslate将对象移动到坐标系之外,openGL会剪切对象。
如果您将glTranslate()调用更改为1.0,则该对象可见。
答案 1 :(得分:0)
您尚未使用glViewport
设置视口,也未设置投影矩阵。如果您将清晰颜色更改为黑色以外的颜色,您会在窗口中看到这种颜色吗?
尝试将代码更改为:
void display()
{
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1, 1, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//glTranslatef(0, 0, -2);
glBegin(GL_POLYGON);
glColor3f(1.0, 0.2, 0.2);
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.75, 0.25, 0.0);
glVertex3f(0.75, 0.75, 0.0);
glVertex3f(0.25, 0.75, 0.0);
glEnd();
glFlush();
}
(创建OpenGL窗口时可能已正确设置视口,否则使用窗口宽度和高度初始化视图)
如果您的多边形有不正确的缠绕,{p <1}}
也会禁用背面剔除