- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 C 语言开发 OPENGL 项目。在这个项目中,会有一个球,它会随机运动,与 window 的墙壁碰撞并沿随机方向移动。玩家需要点击球,左上角显示的分数将增加1,球的速度将增加1。我们还在右上角显示1分钟的计时器。 1分钟后游戏结束,最终得分将显示给玩家。
到目前为止我已经完成了球的随机运动。显示分数文本。问题是得分随球移动,而不是静止在左上角。那么该怎么做呢?
这是我的代码:
#include<GL/glut.h>
#include<math.h>
#include<stdbool.h>
#define PI 3.14159265f
//Variable defined outside globally
GLfloat ballRadius = 0.2; //Radius of the bouncing ball
GLfloat ballX = 0.0f; //Ball's center(x,y) position
GLfloat ballY = 0.0f;
GLfloat ballXMax,ballXMin,ballYMax,ballYMin; //Ball's center (x,y) bounds
GLfloat xSpeed = 0.02f; //Ball's speed in x and y direction
GLfloat ySpeed = 0.007f;
int refreshMills = 30;
int x1,xa,ya; //refresh period in milliseconds
int score=0;
int last_mx = 0, last_my = 0, cur_mx = 0, cur_my = 0;
int arcball_on = false;
//Projection clipping area
GLdouble clipAreaXLeft,clipAreaXRight,clipAreaYBottom,clipAreaYTop;
// Initialize OpenGL Graphics
void initGL()
{
glClearColor(0.0,0.0,0.0,1.0); //Set background(clear) color to green
}
// Callback handler for window re-paint event
void display()
{
glClear(GL_COLOR_BUFFER_BIT); //Clear the color buffer
glMatrixMode(GL_MODELVIEW); //To operate on the model-view matrix
glLoadIdentity(); //Reset model-view matrix
glTranslatef(ballX,ballY,0.0f); //Translate to (xPos,yPos)
glBegin(GL_TRIANGLE_FAN); //Use triangular segments to form a circle
glColor3ub( rand()%1000, rand()%1000, rand()%1000 ); //Red
glVertex2f(0.0f,0.0f); //Center of circle
int numSegments = 100; //ball shape temp...
GLfloat angle;
int i;
for(i=0;i<=numSegments;i++) //Last vertex same as first vertex
{
angle = i*2.0f*PI/numSegments; //360 degree for all segments
glVertex2f(cos(angle)*ballRadius,sin(angle)*ballRadius);
}
glEnd();
glFlush(); //Swap front and back buffers
//Animation Control - compute the location for next refresh
ballX += xSpeed;
ballY += ySpeed;
//Check if the ball exceeds the edges
if(ballX > ballXMax)
{ xa=ballX;
ballX = ballXMax;
xSpeed = -xSpeed;
}
else if(ballX < ballXMin)
{ xa=ballX;
ballX = ballXMin;
xSpeed = -xSpeed;
}
if(ballY > ballYMax)
{ ya=ballY;
ballY = ballYMax;
ySpeed = -ySpeed;
}
else if(ballY < ballYMin)
{ ya=ballY;
ballY = ballYMin;
ySpeed = -ySpeed;
}
glColor3f(1.0,0.0,0.0);
glRasterPos2f(-1.0,0.0);
glutBitmapCharacter (GLUT_BITMAP_8_BY_13,'S');
glutBitmapCharacter (GLUT_BITMAP_8_BY_13,'C');
glutBitmapCharacter (GLUT_BITMAP_8_BY_13,'O');
glutBitmapCharacter (GLUT_BITMAP_8_BY_13,'R');
glutBitmapCharacter (GLUT_BITMAP_8_BY_13,'E');
glutBitmapCharacter (GLUT_BITMAP_8_BY_13,':');
glFlush();
}
void onMouse(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
arcball_on = true;
last_mx = cur_mx = x;
last_my = cur_my = y;
} else {
arcball_on = false;
if(cur_mx==x && cur_my==y)
{
score=score+1;
}
printf("%d",score);
}
}
void onMotion(int x, int y) {
if (arcball_on) { // if left button is pressed
cur_mx = x;
cur_my = y;
}
}
/*void mouseClicks(int button, int state, int x, int y) {
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
utton == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
}
*/
//Call back when the windows is re-sized
void reshape(GLsizei width,GLsizei height)
{
//Compute aspect ratio of the new window
if(height ==0) height = 1; //To prevent divide by 0
GLfloat aspect = (GLfloat)width / (GLfloat)height;
//Set the viewport to cover the new window
glViewport(0,0,width,height);
//Set the aspect ratio of the clipping area to match the viewport
glMatrixMode(GL_PROJECTION); //To operate on the Projection matrix
glLoadIdentity(); //Reset the Projection Matrix
if(width >=height)
{
clipAreaXLeft = -1.0 * aspect;
clipAreaXRight = 1.0 * aspect;
clipAreaYBottom = -1.0;
clipAreaYTop = 1.0;
}
else
{
clipAreaXLeft = -1.0;
clipAreaXRight = 1.0 ;
clipAreaYBottom = -1.0 / aspect;
clipAreaYTop = 1.0 / aspect;
}
gluOrtho2D(clipAreaXLeft,clipAreaXRight,clipAreaYBottom,clipAreaYTop+0.25);
ballXMin = clipAreaXLeft + ballRadius;
ballXMax = clipAreaXRight - ballRadius;
ballYMin = clipAreaYBottom + ballRadius;
ballYMax = clipAreaYTop - ballRadius;
}
//Call back when the timer expired
void Timer(int value)
{
glutPostRedisplay(); //Post a paint request to activate display()
glutTimerFunc(refreshMills,Timer,5); //subsequent timer call at milliseconds
}
int windowWidth = 500; //Window mode's width
int windowHeight = 500; //Window mode's height
int windowPosX = 100; //Window mode's top-left corner x
int windowPosY = 100;
//Main function: GLUT runs as a console application starting at main()
int main(int argc,char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); //Enable double buffered mode
glutInitWindowSize(windowWidth,windowHeight); //Initial window width and height
glutInitWindowPosition(windowPosX,windowPosY); //Initial window top-left corner(x,y)
glutCreateWindow("Bouncing Ball");
glutMouseFunc(onMouse);
glutMotionFunc(onMotion);
glutDisplayFunc(display); //Register callback handler for window re-paint
//glutMouseFunc(mouseClicks);
glutReshapeFunc(reshape);
glutPostRedisplay(); //Register callback handler for window re-shape
glutTimerFunc(0,Timer,0); //First timer call immediately
initGL(); //Our own OpenGL initialization
glutMainLoop(); //Enter event processing loop
}
最佳答案
省略球的绘制代码,您将得到以下代码序列:
glLoadIdentity(); //Reset model-view matrix
glTranslatef(ballX,ballY,0.0f); //Translate to (xPos,yPos)
...
glRasterPos2f(-1.0,0.0);
glutBitmapCharacter (GLUT_BITMAP_8_BY_13,'S');
当前模型 View 变换将应用于您使用 glRasterPos2f()
指定的位置,其中包括您使用 glTranslatef()
指定的翻译。
您有几个选项可以解决此问题:
在调用glRasterPos2f()
之前重置转换:
glLoadIdentity();
glRasterPos2f(-1.0,0.0);
glutBitmapCharacter (GLUT_BITMAP_8_BY_13,'S');
插入/弹出用于绘制球的变换,以在完成绘制球后恢复之前的变换:
glLoadIdentity(); //Reset model-view matrix
glPushMatrix();
glTranslatef(ballX,ballY,0.0f); //Translate to (xPos,yPos)
...
glPopMatrix();
glRasterPos2f(-1.0,0.0);
glutBitmapCharacter (GLUT_BITMAP_8_BY_13,'S');
使用 glWindowPos()
而不是 glRasterPos()
,这样您可以指定以像素为单位的位置,而不是要转换的坐标。
关于c - 修复 glRasterPos() 位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29236363/
我正在使用 C 语言开发 OPENGL 项目。在这个项目中,会有一个球,它会随机运动,与 window 的墙壁碰撞并沿随机方向移动。玩家需要点击球,左上角显示的分数将增加1,球的速度将增加1。我们还在
我一直在尝试向我的 OpenGL/C++ 项目中添加一些基本的类似 HUD 的文本。 所以我决定 glutBitmapCharacter( type, character) 成为我的首选武器。 我意识
我有以下代码在我的应用程序中呈现文本,首先我获取世界中的鼠标坐标,然后使用这些坐标将我的文本放置在世界中,因此它会跟随我的鼠标位置: 编辑:在代码示例中添加了 buildfont() 函数: GLvo
我想做的是,当我绘制每个对象时,我检查它是否被标记,如果被标记,在对象附近写一些文本,使用 glRasterPos2f/i 在屏幕上设置位置,然后使用 glutBitmapCharacter . 对于
我是一名优秀的程序员,十分优秀!