|
今天编写了一个程序:
[code:1]
/* simple.c third editon */
/* this program draws a color triangle on a black background and translate it and print fps */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <GL/gl.h>
#include <GL/glut.h>
#define PI 3.1415926535897932384626433
float v[3] = {PI + PI/6, PI/2, -PI/6};
void display (void);
void init (void);
void myreshape (int, int);
void myidle (void);
long long mySwapBuffers (void);
int main (int argc, char *argv[]){
glutInit (&argc, argv);
glutInitWindowSize (600,600);
glutInitDisplayMode (GLUT_DOUBLE|GLUT_RGB);
glutCreateWindow (argv[0]);
// glutFullScreen ();
glutDisplayFunc (display);
// glutDisplayString ("Hello");
glutReshapeFunc (myreshape);
glutIdleFunc (myidle);
init ();
glutMainLoop ();
return 0;
}
void display (void){
GLclampf red[] = {1.0, 0.0, 0.0};
GLclampf green[] = {0.0, 1.0, 0.0};
GLclampf blue[] = {0.0, 0.0, 1.0};
/* 使用v[3]中存储的三个角度创建3个顶点坐标 */
float p[][2] = {
{cos(v[0]), sin(v[0])},
{cos(v[1]), sin(v[1])},
{cos(v[2]), sin(v[2])}
};
glClear (GL_COLOR_BUFFER_BIT);
glBegin (GL_POLYGON);
glColor3fv (red);
glVertex2fv (p[0]);
glColor3fv (green);
glVertex2fv (p[1]);
glColor3fv (blue);
glVertex2fv (p[2]);
glEnd ();
// glFlush ();
//glutSwapBuffers (); /* for Dobule Buffer */
printf ("fps=%lld.\n",mySwapBuffers ());
}
void init (void){
GLclampf red[] = {1.0, 0.0, 0.0};
GLclampf black[] = {0.0, 0.0, 0.0, 0.0};
glClearColor (black[0], black[1], black[2], black[3]);
// glColor3fv (red);
//glShadeModel (GL_FLAT);
glShadeModel (GL_SMOOTH);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D (-1.0, 1.0, -1.0, 1.0);
}
void myreshape (int w, int h){
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
if (w<=h)
gluOrtho2D (-2.0, 2.0, -2.0*(GLfloat)h/(GLfloat)w,
2.0*(GLfloat)h / (GLfloat)w);
else
gluOrtho2D (-2.0*(GLfloat)w / (GLfloat)h,
2.0 * (GLfloat)w / (GLfloat)h, -2.0, 2.0);
glMatrixMode (GL_MODELVIEW);
glViewport (0, 0 ,w, h);
}
void myidle (void){
static GLdouble a=0;
a +=1.0;
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glRotated (a, 1.0, 1.0, 0.0);
glutPostRedisplay();
}
long long mySwapBuffers (void){
static int first = 1;
static long long fps_count = 0; /* 计算fps的计数器 */
static clock_t s_time; /* 当前时间 */
static clock_t p_time; /* 前一个时间 */
static long long fps = 0; /* 存储fps */
printf ("first = %d\n", first);
if (first){
printf ("CLOCKS_PER_SEC = %ld\n", CLOCKS_PER_SEC);
first = !first;
p_time = clock ();
}
s_time = clock (); /* 获取当前时间 */
++fps_count;
printf ("p_time = %ld\ts_time = %ld\n", p_time, s_time);
printf ("fps_count = %lld\n", fps_count);
[color=red]
// if ( (s_time - p_time) >= CLOCKS_PER_SEC ){ /* 测试是否经过一秒 */
if ( (s_time - p_time) >= 10000 ){ /* 测试是否经过一秒 */
p_time = s_time; /* 每过一秒,更新”前一个时间“ */
fps = fps_count;
fps_count = 0;
}[/color]
glutSwapBuffers ();
return fps;
}
[/code:1]
在函数long long mySwapBuffers (void)中有了问题:
1、我使用time.h中的clock()获取当前时间。但是glibc的CLOCKS_PER_SEC看起来有问题。clock()每过一秒增加10000,但是CLOCKS_PER_SEC的值确实1000 000。在C标准库的定义中,这两个值是相等的。
2、使用clock()的时候,发现它每过一秒递增10000,而不是1点1点递增的。
在TCPL中对clock()的定义如下:
clock_t clock (void)
clock函数用于返回程序自运行到目前为止所占用的处理机时间;如果处理机时间不可使用,那么返回值是-1。clock()/CLOCKS_PER_SEC是以秒为单位表示的时间。
是不是glibc有bug呢?
期待高手解惑。
我的Linux是Fedora Core4。
[code:1]$gcc -v
使用内建 specs。
目标:i386-redhat-linux
配置为:../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--enable-checking=release --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-libgcj-multifile
--enable-languages=c,c++,objc,java,f95,ada --enable-java-awt=gtk
--with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --host=i386-redhat-linux
线程模型:posix
gcc 版本 4.0.0 20050519 (Red Hat 4.0.0-8)
[/code:1]
[code:1]$ /lib/libc.so.6
GNU C Library development release version 2.3.5, by Roland McGrath et al.
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 4.0.0 20050525 (Red Hat 4.0.0-9).
Compiled on a Linux 2.4.20 system on 2005-05-30.
Available extensions:
GNU libio by Per Bothner
crypt add-on version 2.1 by Michael Glad and others
Native POSIX Threads Library by Ulrich Drepper et al
The C stubs add-on version 2.1.2.
BIND-8.2.3-T5B
NIS(YP)/NIS+ NSS modules 0.19 by Thorsten Kukuk
Glibc-2.0 compatibility add-on by Cristian Gafton
GNU Libidn by Simon Josefsson
Thread-local storage support included.
For bug reporting instructions, please see:
<http://www.gnu.org/software/libc/bugs.html>.
[/code:1]
编译使用
[code:1]
$gcc -Wall -g -lglut -lGL -lGLU -lX11 -lm -L/usr/X11R6/lib simple3.c simple3
[/code:1] |
|