Opengl台球数组
opengl array of pool balls
我正在使用c++在opengl中绘制一个台球数组我面临的问题是数组画在一条直线上。当我使用gltranslate时,当我编辑z轴和y轴时,球仍然只能沿着直线移动我想做的是把球摆成一个三角形,就像台球比赛结束一样我如何使用数组代码来设置像这样的球?如有任何帮助,不胜感激
balls[7];
for (int x = ball-start; x<ball-end;x++)
{
glTranslatef(0,0,0.5);
glColor3f(1,0,0);
ball[x].drawball();
}
假设:
struct Ball {
double x,y,z;
void drawball(void);
/* ... */
} ball[7];
试题:
for(int i=0; i<7 ;i++)
{
glPushMatrix();
glTranslated(ball[i].x,ball[i].y,ball[i].z);
glColor3f(1,0,0);
ball[i].drawball();
glPopMatrix();
}
细节可能有所不同,但希望您能理解。
这样做:
// first of all, include the x,y position (assuming 2D, since pool) in the Ball object:
class Ball
{
//...
private:
float xpos, ypos;
//...
};
然后,当你构建球数组时,而不是仅仅制作8个球,你会想要在堆上分配内存,以便它可以在整个游戏中持续使用。所以这样做:
Ball *ball= new Ball*[8];
ball[0] = new Ball(x0,y0);
ball[1] = new Ball(x1,y1);
ball[2] = new Ball(x2,y2);
ball[3] = new Ball(x3,y3);
// ...
确保当你的游戏结束时,你自己清理。
for (int i = 0; i < 8; i++)
delete ball[i];
delete [] ball;
然后在你的Ball::draw()中做这样的事情:
Ball::draw()
{
glColor3f(/*yellow*/); // Set the color to yellow
glTranslatef(-xpos, -ypos, 0); // Move to the position of the ball
// Draw the ball
glTranslatef(xpos, ypos, 0); // Move back to the default position
}
你所要做的就是找出正确的(x0,y0),(x1,y1),(x2,y2)…形成一个三角形!这样回答你的问题了吗?
相关文章:
- 在 glsl opengl 中将嵌套结构数组作为统一传递
- 如果我使用malloc()而不是堆栈数组,OpenGL不会渲染对象
- OpenGL 顶点数组对象与 tinyobjloader
- 假设传递给 OpenGL 的结构数组的内存布局存在潜在错误
- OpenGL:使用指向静态数据的指针数组传递缓冲区数据
- 如何从OpenGL 4.5中的单个Cube地图纹理中创建Cube地图数组纹理
- 如何使用OpenImageio将RGB值存储在数组中?(使用C ,OpenGL)
- 将 OpenGL 颜色附件存储在 constexpr GLenum 数组中
- opengl:两个不同的矢量可以绑定到同一个顶点数组对象吗
- 正确初始化 OpenGL 的结构数组
- 将opengl纹理保存到Byte数组中
- OpenGL 批处理:为什么我的绘制调用超出了数组缓冲区边界
- 将坐标数组传递给主数组(openGL,C++)
- 在 OpenGL 中重新组织图像/图片数组以适应 2 种纹理大小的幂
- openGL drawElements - 一个额外的三角形,使用索引数组
- Opengl 数组纹理,适用于原始数据,但不适用于图像数据
- 在 OpenGL 中呈现三角形结构引用数组
- OpenGL:两个顶点数组 + 两个索引数组
- OpenGL与C++:将类数组传递给glTexImage2d时出现vtable问题
- 纹理每个多边形在顶点数组- OpenGL