本文介绍了C - GLFW窗口不在Debian上打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在Debian上开始使用GLFW,我已经尝试编译并运行示例程序,但是它拒绝运行。在几个printf语句的帮助下,我发现当程序尝试打开一个GLFW窗口时,它失败,然后退出 - 但我不知道为什么。任何帮助将是惊人的。 #include< stdlib.h> //对于malloc()等。
#include< stdio.h> //对于printf(),fopen()等。
#include< math.h> //对于sin(),cos()等。
#include< GL / glfw.h> //对于GLFW,OpenGL和GLU
// ---------------------------- ------------------------------------------
// Draw( ) - 主要的OpenGL绘图功能,被称为每个框架
// --------------------------------- -------------------------------------
void Draw(void)
{
int width,height; //窗口尺寸
double t; //时间(以秒为单位)
int k; // Loop counter
//获取当前时间
t = glfwGetTime();
//获取窗口大小
glfwGetWindowSize(& width,& height);
//确保高度不为零,以避免除以零
height = height< 1? 1:身高
//设置视口
glViewport(0,0,width,height);
//清除颜色和depht缓冲区
glClearColor(0.0f,0.0f,0.0f,0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//设置投影矩阵
glMatrixMode(GL_PROJECTION); //选择投影矩阵
glLoadIdentity(); //从一个单元矩阵开始
gluPerspective(//设置透视图
65.0,//视场= 65度
(双)width /(double)height,// Window aspect (假设正方形像素)
1.0,//近Z剪切平面
100.0 // Far Z clippling plane
);
//设置modelview矩阵
glMatrixMode(GL_MODELVIEW); //选择modelview矩阵
glLoadIdentity(); //从一个单位矩阵开始
gluLookAt(//设置摄像机位置和方向
0.0,0.0,10.0,//摄像头位置(x,y,z)
0.0,0.0,0.0 ,//查看点(x,y,z)
0.0,1.0,0.0 //向上(x,y,z)
);
// ****绘制一个圆圈***
//将当前模型视图矩阵保存在堆栈上
glPushMatrix( );
//将点移动(移动)到显示屏的左上角
glTranslatef(-4.0f,3.0f,0.0f);
//旋转z轴和x轴的点
glRotatef(35.0f *(float)t,0.0f,0.0f,1.0f);
glRotatef(60.0f *(float)t,1.0f,0.0f,0.0f);
//现在绘制点 - 我们使用for循环来构建一个圆
glColor3f(1.0f,1.0f,1.0f);
glBegin(GL_POINTS); (k = 0; k {
glVertex3f(2.0f *(float)cos(0.31416 *(double)k),
2.0f *(float)sin(0.31416 *(double)k),
0.0f);
}
glEnd();
//恢复modelview矩阵
glPopMatrix();
// ****绘制一行行***
//将当前模型视图矩阵保存在堆栈上
glPushMatrix( );
//将行移动到显示的右上角
glTranslatef(4.0f,3.0f,0.0f);
//旋转z轴和x轴的点
glRotatef(45.0f *(float)t,0.0f,0.0f,1.0f);
glRotatef(55.0f *(float)t,1.0f,0.0f,0.0f);
//现在画线 - 我们使用for循环来构建一个圆
glBegin(GL_LINE_LOOP);
for(k = 0; k {
glColor3f(1.0f,0.05f *(float)k,0.0f);
glVertex3f(2.0f *(float)cos(0.31416 *(double)k),
2.0f *(float)sin(0.31416 *(double)k),
0.0f);
}
glEnd();
//恢复modelview矩阵
glPopMatrix();
// ****使用trinagles绘制一张光碟***
//将当前的模型视图矩阵保存在堆栈上
glPushMatrix( );
//将三角形翻译(移动)到显示屏的左下角
glTranslatef(-4.0f,-3.0f,0.0f);
//旋转z轴和x轴的三角形
glRotatef(25.0f *(float)t,0.0f,0.0f,1.0f);
glRotatef(75.0f *(float)t,1.0f,0.0f,0.0f);
//现在绘制三角形 - 我们使用for-loop构建光盘
//由于我们正在构建一个三角形风扇,我们还指定了一个第一个
//光盘中心点的顶点。
glBegin(GL_TRIANGLE_FAN);
glColor3f(0.0f,0.5f,1.0f);
glVertex3f(0.0f,0.0f,0.0f); (k = 0; k {
glColor3f(0.0f,0.05f *(float)k,1.0f)
glVertex3f(2.0f *(float)cos(0.31416 *(double)k),
2.0f *(float)sin(0.31416 *(double)k),
0.0f);
}
glEnd();
//恢复modelview矩阵
glPopMatrix();
// ****使用多边形绘制光盘***
//将当前模型视图矩阵保存在堆栈上
glPushMatrix ();
//将多边形转换(移动)到显示的右下角
glTranslatef(4.0f,-3.0f,0.0f);
//旋转多边形关于z轴和x轴
glRotatef(65.0f *(float)t,0.0f,0.0f,1.0f);
glRotatef(-35.0f *(float)t,1.0f,0.0f,0.0f);
//现在绘制多边形 - 我们使用for-loop构建光盘
glBegin(GL_POLYGON); (k = 0; k
glColor3f(1.0f,0.0f,0.05f *(float)k);
glVertex3f(2.0f *(float)cos(0.31416 *(double)k),
2.0f *(float)sin(0.31416 *(double)k),
0.0f);
}
glEnd();
//恢复modelview矩阵
glPopMatrix();
// ****绘制一个四边形***
//将当前的模型视图矩阵保存在堆栈上
glPushMatrix() ;
//围绕y轴旋转四边形
glRotatef(60.0f *(float)t,0.0f,1.0f,0.0f);
//现在绘制quad
glBegin(GL_QUADS);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.5f,-1.5f,0.0f);
glColor3f(1.0f,1.0f,0.0f);
glVertex3f(1.5f,-1.5f,0.0f);
glColor3f(1.0f,0.0f,1.0f);
glVertex3f(1.5f,1.5f,0.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-1.5f,1.5f,0.0f);
glEnd();
//恢复modelview矩阵
glPopMatrix();
}
// ------------------------------- ---------------------------------------
// main() - 程序入口点
// ------------------------------------------ ----------------------------
int main(int argc,char ** argv)
{
int ok; //标记告诉窗口是否打开
int running; //标记告诉程序是否运行
//初始化GLFW
glfwInit();
//打开窗口
ok = glfwOpenWindow(
100,100,//窗口宽度和高度
8,8,8,//红色,绿色和颜色缓冲区的蓝色位
8,// alpha缓冲区的位数
24,//深度缓冲区(Z缓冲区)的位数
0,// stencil buffer
GLFW_WINDOW //我们想要一个桌面窗口(可以是GLFW_FULLSCREEN)
);
printf(here);
//如果我们无法打开一个窗口,现在退出
if(!ok)
{
glfwTerminate();
return 0;
}
printf(not here);
//设置窗口标题
glfwSetWindowTitle(我的OpenGL程序);
//启用粘键
glfwEnable(GLFW_STICKY_KEYS);
//主渲染循环
do
{
//调用我们的渲染函数
Draw();
//交换前后缓冲区(我们使用双缓冲显示)
glfwSwapBuffers();
//检查转义键是否被按下,或者窗口是否关闭
running =!glfwGetKey(GLFW_KEY_ESC)& amp;&
glfwGetWindowParam(GLFW_OPENED);
}
while(running);
//终止GLFW
glfwTerminate();
//退出程序
返回0;
}
解决方案
你确定是glfwOpenWindow那是失败的?我不知道为什么这可能是,也许你使用太多的位为您的z缓冲区?这是我唯一可以想到的。
尝试这个
GLFWvidmode dvm;
glfwGetDesktopMode(& dvm);
glfwOpenWindow(winWidth,winHeight,dvm.RedBits,dvm.GreenBits,dvm.BlueBits,0,0,0,GLFW_WINDOW);
查看是否仍然失败。
I'm trying to get started with GLFW on Debian, I've tried compiling and running an example program but it refuses to run. With the help of a couple of printf statements I've found that when the program tries to open a GLFW window it fails, then exits - but I don't know why. Any help would be amazing.
#include <stdlib.h> // For malloc() etc.
#include <stdio.h> // For printf(), fopen() etc.
#include <math.h> // For sin(), cos() etc.
#include <GL/glfw.h> // For GLFW, OpenGL and GLU
//----------------------------------------------------------------------
// Draw() - Main OpenGL drawing function that is called each frame
//----------------------------------------------------------------------
void Draw( void )
{
int width, height; // Window dimensions
double t; // Time (in seconds)
int k; // Loop counter
// Get current time
t = glfwGetTime();
// Get window size
glfwGetWindowSize( &width, &height );
// Make sure that height is non-zero to avoid division by zero
height = height < 1 ? 1 : height;
// Set viewport
glViewport( 0, 0, width, height );
// Clear color and depht buffers
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
// Set up projection matrix
glMatrixMode( GL_PROJECTION ); // Select projection matrix
glLoadIdentity(); // Start with an identity matrix
gluPerspective( // Set perspective view
65.0, // Field of view = 65 degrees
(double)width/(double)height, // Window aspect (assumes square pixels)
1.0, // Near Z clipping plane
100.0 // Far Z clippling plane
);
// Set up modelview matrix
glMatrixMode( GL_MODELVIEW ); // Select modelview matrix
glLoadIdentity(); // Start with an identity matrix
gluLookAt( // Set camera position and orientation
0.0, 0.0, 10.0, // Camera position (x,y,z)
0.0, 0.0, 0.0, // View point (x,y,z)
0.0, 1.0, 0.0 // Up-vector (x,y,z)
);
// **** Draw a circle of points ***
// Save the current modelview matrix on the stack
glPushMatrix();
// Translate (move) the points to the upper left of the display
glTranslatef( -4.0f, 3.0f, 0.0f );
// Rotate the points about the z-axis and the x-axis
glRotatef( 35.0f * (float)t, 0.0f, 0.0f, 1.0f );
glRotatef( 60.0f * (float)t, 1.0f, 0.0f, 0.0f );
// Now draw the points - we use a for-loop to build a circle
glColor3f( 1.0f, 1.0f, 1.0f );
glBegin( GL_POINTS );
for( k = 0; k < 20; k ++ )
{
glVertex3f( 2.0f * (float)cos( 0.31416 * (double)k ),
2.0f * (float)sin( 0.31416 * (double)k ),
0.0f );
}
glEnd();
// Restore modelview matrix
glPopMatrix();
// **** Draw a circle of lines ***
// Save the current modelview matrix on the stack
glPushMatrix();
// Translate (move) the lines to the upper right of the display
glTranslatef( 4.0f, 3.0f, 0.0f );
// Rotate the points about the z-axis and the x-axis
glRotatef( 45.0f * (float)t, 0.0f, 0.0f, 1.0f );
glRotatef( 55.0f * (float)t, 1.0f, 0.0f, 0.0f );
// Now draw the lines - we use a for-loop to build a circle
glBegin( GL_LINE_LOOP );
for( k = 0; k < 20; k ++ )
{
glColor3f( 1.0f, 0.05f * (float)k, 0.0f );
glVertex3f( 2.0f * (float)cos( 0.31416 * (double)k ),
2.0f * (float)sin( 0.31416 * (double)k ),
0.0f );
}
glEnd();
// Restore modelview matrix
glPopMatrix();
// **** Draw a disc using trinagles ***
// Save the current modelview matrix on the stack
glPushMatrix();
// Translate (move) the triangles to the lower left of the display
glTranslatef( -4.0f, -3.0f, 0.0f );
// Rotate the triangles about the z-axis and the x-axis
glRotatef( 25.0f * (float)t, 0.0f, 0.0f, 1.0f );
glRotatef( 75.0f * (float)t, 1.0f, 0.0f, 0.0f );
// Now draw the triangles - we use a for-loop to build a disc
// Since we are building a triangle fan, we also specify a first
// vertex for the centre point of the disc.
glBegin( GL_TRIANGLE_FAN );
glColor3f( 0.0f, 0.5f, 1.0f );
glVertex3f( 0.0f, 0.0f, 0.0f );
for( k = 0; k < 21; k ++ )
{
glColor3f( 0.0f, 0.05f * (float)k, 1.0f );
glVertex3f( 2.0f * (float)cos( 0.31416 * (double)k ),
2.0f * (float)sin( 0.31416 * (double)k ),
0.0f );
}
glEnd();
// Restore modelview matrix
glPopMatrix();
// **** Draw a disc using a polygon ***
// Save the current modelview matrix on the stack
glPushMatrix();
// Translate (move) the polygon to the lower right of the display
glTranslatef( 4.0f, -3.0f, 0.0f );
// Rotate the polygon about the z-axis and the x-axis
glRotatef( 65.0f * (float)t, 0.0f, 0.0f, 1.0f );
glRotatef( -35.0f * (float)t, 1.0f, 0.0f, 0.0f );
// Now draw the polygon - we use a for-loop to build a disc
glBegin( GL_POLYGON );
for( k = 0; k < 20; k ++ )
{
glColor3f( 1.0f, 0.0f, 0.05f * (float)k );
glVertex3f( 2.0f * (float)cos( 0.31416 * (double)k ),
2.0f * (float)sin( 0.31416 * (double)k ),
0.0f );
}
glEnd();
// Restore modelview matrix
glPopMatrix();
// **** Draw a single quad ***
// Save the current modelview matrix on the stack
glPushMatrix();
// Rotate the quad about the y-axis
glRotatef( 60.0f * (float)t, 0.0f, 1.0f, 0.0f );
// Now draw the quad
glBegin( GL_QUADS );
glColor3f( 1.0f, 0.0f, 0.0f );
glVertex3f( -1.5f, -1.5f, 0.0f );
glColor3f( 1.0f, 1.0f, 0.0f );
glVertex3f( 1.5f, -1.5f, 0.0f );
glColor3f( 1.0f, 0.0f, 1.0f );
glVertex3f( 1.5f, 1.5f, 0.0f );
glColor3f( 0.0f, 0.0f, 1.0f );
glVertex3f( -1.5f, 1.5f, 0.0f );
glEnd();
// Restore modelview matrix
glPopMatrix();
}
//----------------------------------------------------------------------
// main() - Program entry point
//----------------------------------------------------------------------
int main( int argc, char **argv )
{
int ok; // Flag telling if the window was opened
int running; // Flag telling if the program is running
// Initialize GLFW
glfwInit();
// Open window
ok = glfwOpenWindow(
100, 100, // Width and height of window
8, 8, 8, // Number of red, green, and blue bits for color buffer
8, // Number of bits for alpha buffer
24, // Number of bits for depth buffer (Z-buffer)
0, // Number of bits for stencil buffer
GLFW_WINDOW // We want a desktop window (could be GLFW_FULLSCREEN)
);
printf("here");
// If we could not open a window, exit now
if( !ok )
{
glfwTerminate();
return 0;
}
printf("not here");
// Set window title
glfwSetWindowTitle( "My OpenGL program" );
// Enable sticky keys
glfwEnable( GLFW_STICKY_KEYS );
// Main rendering loop
do
{
// Call our rendering function
Draw();
// Swap front and back buffers (we use a double buffered display)
glfwSwapBuffers();
// Check if the escape key was pressed, or if the window was closed
running = !glfwGetKey( GLFW_KEY_ESC ) &&
glfwGetWindowParam( GLFW_OPENED );
}
while( running );
// Terminate GLFW
glfwTerminate();
// Exit program
return 0;
}
解决方案
You're sure it's glfwOpenWindow that's failing? I don't know why that might be, perhaps you're using too many bits for your z-buffer? That's the only thing I can think of.
Try this
GLFWvidmode dvm;
glfwGetDesktopMode(&dvm);
glfwOpenWindow(winWidth, winHeight, dvm.RedBits, dvm.GreenBits, dvm.BlueBits, 0, 0, 0, GLFW_WINDOW);
And see if it still fails.
这篇关于C - GLFW窗口不在Debian上打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!