D
D
danij_com2020-02-10 17:36:40
C++ / C#
danij_com, 2020-02-10 17:36:40

How to draw such an openGL star?

You need to draw just such an asterisk:
5e4168c9ce6f8785111445.png
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);

But so far, the maximum that I have been able to draw is this: I
5e4169a1d412e526962360.png
haven’t even tried to connect the dots yet, because in the original image two decagons are rotated relative to the center by a few degrees, but I haven’t figured out how to at least rotate the dots, unfortunately.
I would be grateful for advice, maybe I'm not digging there at all ...

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(); 
}

PS Please don't hit hard, I just got into openGL, maybe (most likely) I still don't understand a lot.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Shatunov, 2020-02-10
@danij_com

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(); 
}

D
danij_com, 2020-02-10
@danij_com

I'll post the full code, maybe someone will need it sometime (I sympathize in advance)

spoiler

#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 question

Ask a Question

731 491 924 answers to any question