Answer the question
In order to leave comments, you need to log in
How to draw such an openGL star?
You need to draw just such an asterisk:
As far as I understand, you must first calculate the location of the points on the generating circle in cycles (because there are 10 vertices, then n=10), and then somehow connect them.
float x = R * cos(2 * 3.14*i / n);
float y = R * sin(2 * 3.14*i / n);
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
float R = 100;
for (int i = 0; i < 10; i++) {
int x = R * cos(2 * 3.14*i / 10) + 320;
int y = R * sin(2 * 3.14*i / 10)+ 240;
glVertex2i(x, y);
}
R = 120;
for (int i = 0; i < 10; i++) {
int x = R * cos(2 * 3.14*i / 10) + 320;
int y = R * sin(2 * 3.14*i / 10) + 240;
glVertex2i(x, y);
}
R = 160;
for (int i = 0; i < 10; i++) {
int x = R * cos(2 * 3.14*i / 10) + 320;
int y = R * sin(2 * 3.14*i / 10) + 240;
glVertex2i(x, y);
}
glEnd();
glFlush();
}
Answer the question
In order to leave comments, you need to log in
The figure consists of 10 vertices connected by 10 edges.
In the first step, you need to evenly distribute 10 vertices around the unit circle. This must be done in a specially designated array of vertices, because indexed primitives cannot be inferred in this old mode.
At the second stage, we switch the context to the output of a closed line (GL_LINE_LOOP) and output all 10 points from the vertex array, but with an offset of 3 points and modulo 10.
Pseudocode:
void display()
{
glClear( GL_COLOR_BUFFER_BIT );
glBegin( GL_LINE_LOOP );
for( size_t index = 0; index < vertices.size(); ++index )
{
const Vertex& vertex = vertices[ ( index * 3 ) % vertices.size() ];
glVertex2i( vertex.x, vertex.y );
}
glEnd();
glFlush();
}
I'll post the full code, maybe someone will need it sometime (I sympathize in advance)
#include "OpenGL\glut.h"
#include "OpenGL\gl.h"
#include "math.h"
#define PI 3.1415926535897931159979635
//Определение координаты x точки по формуле x = xc+R*cos(f+(2*pi*i/n)),
//где xc - координата х центра, а f - угловая координата первой вершины
float __x(float start_angle, int radius, int i, int n, int offset) {
return radius * cos(start_angle + (2 * PI * i / n)) + offset;
}
//Определение координаты y точки по формуле y = xc+R*sin(f+(2*pi*i/n)),
//где xc - координата х центра, а f - угловая координата первой вершины
float __y(float start_angle, int radius, int i, int n, int offset) {
return radius * sin(start_angle + (2 * PI * i / n)) + offset;
}
//Отрисовка звезды, в ф-цию можно передать радиус, кол-во вершин, смещение по оси х и у,
//а так же угловую координату начальной точки
void drawStar(int radius, int vertexes, int offset_x, int offset_y, float start_angle) {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE_LOOP);
float* o_x = new float[vertexes];
float* o_y = new float[vertexes];
for (int i = 0; i < vertexes; i++) {
o_x[i] = __x(start_angle, radius, i, vertexes, offset_x);
o_y[i] = __y(start_angle, radius, i, vertexes, offset_y);
}
for (int i = 0; i < vertexes; i++) {
int ind = (i * 3) % vertexes; //Точки выводим со смещением на 3
glVertex2f(o_x[ind], o_y[ind]); //и по модулю 10ти
}
glEnd();
glFlush();
}
void display(){
//Такой вызов отрисовывает звезду аналогичную звезде из условия,
//угловая координата выбрана "методом тыка"
drawStar(200, 10, 320, 240, 6.6);
//так же, кол-во вершин лучше ставить кратное 10ти, иначе фигура,
//скорее всего, не отрисуется
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 150);
glutCreateWindow("Star");
glClearColor(1., 1., 1., 0.);
glColor3ub(0, 0, 0);
glLineWidth(4.);
glPointSize(4.);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0., 640., 0., 480.);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question