【发布时间】:2016-04-27 01:07:25
【问题描述】:
如何在圆内绘制随机点?我有以下代码绘制随机点,但我似乎无法弄清楚如何将它们绘制在一个圆圈内!我一直在使用距离公式来生成随机点,但没有运气。我希望在一个圆圈内生成点,但我只是得到一个空白屏幕。不知道我做错了什么。
这是我的代码:
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#include <vector>
#include <cstdlib>
#define __gl_h_
#include <cmath>
#include <iostream>
struct Point
{
float x, y;
unsigned char r, g, b, a;
};
std::vector< Point > points;
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50, 50, -50, 50, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// draw
glColor3ub( 255, 255, 255 );
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_COLOR_ARRAY );
glVertexPointer( 2, GL_FLOAT, sizeof(Point), &points[0].x );
glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(Point), &points[0].r );
glPointSize( 3.0 );
glDrawArrays( GL_POINTS, 0, points.size() );
glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_COLOR_ARRAY );
glFlush();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(640,480);
glutCreateWindow("Random Points");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
// populate points
for( size_t i = 0; i < 1000; ++i )
{
Point pt;
//pt.x = -50 + (rand() % 100);
//pt.y = -50 + (rand() % 100);
int angle = (rand() % 100 + 1) * 3.1416 * 2;
int radius = (rand() % 100 + 1) * 50;
pt.x = ((radius * cos(angle))-50);
pt.y = ((radius * sin(angle))-50);
pt.r = 125;
pt.g = 125;
pt.b = 125;
pt.a = 255;
points.push_back(pt);
}
glutMainLoop();
return 0;
}
【问题讨论】:
-
1) 你做了什么? (你已经回答了这个)。 2) 你预期会发生什么? 3) 究竟发生了什么?