0

I display my first primitive asteroid 2D model, but trying loop for show 4 asteroids on init program, in glBegin()...glEnd() not have a success.

Loop For or While can be used here? I'm not working with classes here or vector push_back pop_back the glPushMatrix glPopMatrix work like that?

/command compiler g++ console tested with Visual Studio Code
//g++ GLasteroid.cpp -o GLasteroid.exe -L"C:/MinGW/freeglut/lib" -lglu32 -lopengl32 -lfreeglut -I"C:\MinGW\freeglut\include\GL"
/*
 * GL01Hello.cpp:With Load Background Image and Poligon Test OpenGL/GLUT C/C++ Setup
 * Tested Visual Studio Code with MinGW
 * To compile with -lfreeglut -lglu32 -lopengl32 and 
 */
#include <windows.h>  // for MS Windows
#include <stdio.h>      /* printf, scanf, puts, NULL */
#include <iostream>
#include <stdlib.h>     /* srand, rand */
#include <ctime> 
#include <time.h>
#include <freeglut.h>  // GLUT, include glu.h and gl.h
using namespace std;
bool hited = false;
float spin = 0.0;
float astposy;
float astposx = -1.5 + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(1.5-(-1.5))));
const float astvelymax = 0.5f;
const float astvelxmax = 0.35f;
// for random color primitive polygon
static GLubyte redc,greenc,bluec;

/* Initialize OpenGL Graphics just n this case for colors */
void initGL() {
   // Set "clearing" or background color
   glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black and opaque

   //randomnumber color by ctime library
   srand(time(NULL));
   redc = rand()%255;
   greenc = rand()%255;
   bluec = rand()%255;
}

void draw_asteroid1(){
   glPushMatrix();
   glTranslatef(astposx, astposy, 0);// position and velocity of asteroids
   glRotatef(spin , 0., 0., 1.);   
   glBegin(GL_LINE_LOOP);
         glColor3ub(redc, greenc, bluec);
         glVertex2f(-0.04f/scale, 0.23f/scale);
         glVertex2f(-0.13f/scale, 0.205f/scale);
         glVertex2f(-0.285f/scale, 0.385f/scale);
         glVertex2f(-0.38f/scale, 0.105f/scale);
         glVertex2f(-0.185f/scale, -0.235f/scale);
         glVertex2f(-0.115f/scale, -0.06f/scale);
         glVertex2f(-0.035f/scale, -0.15f/scale);
         glVertex2f(-0.035f/scale, -0.05f/scale);
         glVertex2f(0.135f/scale, -0.15f/scale);
         glVertex2f(0.285f/scale, 0.15f/scale); 
         glVertex2f(0.1f/scale, 0.25f/scale);
         glVertex2f(0.135f/scale, 0.55f/scale);
         glVertex2f(-0.085f/scale, 0.485f/scale);        
   glEnd();   
   glPopMatrix();  
}
void move_asteroid1(){
     //Asteroids velocity
   if (astposy < astvelymax)
         astposy = astposy - 0.000425;

   if (astposy < -1.0 )
         astposy = 1.0f;      
   if (astposy > 0.0 && astposy > astvelymax)
       astposy = astposy - 0.000425;

   if (astposx < astvelxmax)
         astposx = astposx - 0.000275;
   if (astposx < -2.0 )
         astposx = 2.0f;
   if (astposx > 0 && astposx > astvelxmax)
       astposx = astposx - 0.000275;
}
/* Called back when there is no other event to be handled */
void idle() {

   spin = spin + 0.075;
   move_asteroid1();

   if (spin > 360.0)
         spin = 0;

   glutPostRedisplay();   // Post a re-paint request to activate display()
}
void display() {
   glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
   glClear(GL_COLOR_BUFFER_BIT);// Clear the color buffer (background

   glEnable( GL_TEXTURE_2D );

   background();
   gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
   glDisable( GL_TEXTURE_2D );

   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity(); 

   draw_asteroid1();
   // glFlush();  // Render now

   glutSwapBuffers();   // Double buffered - swap the front and back buffers
}
/* Main function: GLUT runs as a console application starting at main()  */
int main(int argc, char** argv) {
   glutInit(&argc, argv);  // Initialize GLUT
   glutInitDisplayMode(GLUT_DOUBLE);  // Enable double buffered mode
   glutInitWindowSize(1360, 768);   // Set the window's initial width & height
   glutInitWindowPosition(0, 0);
   // Position the window's initial top-left corner
   glutCreateWindow("OpenGL Setup Test"); // Create a window with the given title
   glutKeyboardFunc(keyboard);
   glutSpecialFunc(specialKeys); // Register callback handler for special-key event
   glutDisplayFunc(display); // Register display callback handler for window re-paint
   glutReshapeFunc(reshape);
   glutIdleFunc(idle);
  // GLuint texture;
   texture = LoadTexture( "stars.bmp" );
   initGL();

   glutMainLoop();// Enter the event-processing loop
   //Free our texture
   glDeleteTextures( 1, &texture );
   return 0;
}

enter image description here

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 2
    There's no loop in your code. As for drawing multiple "objects" using the same function, just use different coordinates for all of them. Perhaps using an array of coordinate pairs? – Some programmer dude Sep 24 '19 at 14:43
  • ... Chang the function signature `void draw_asteroid1(float astposx, float astposy)`. Call `draw_asteroid1` in a loop with different coordinate pairs from an array. – Rabbid76 Sep 24 '19 at 15:21
  • i made your change on draw_asteroid1 function putting float astposx, float astposy then tested draw_asteroid1(astposx - 1,astposy - 0.5); draw_asteroid1(astposx + 0.5,astposy + 0.5); draw_asteroid1(astposx -0.7,astposy - 0.1); draw_asteroid1(astposx + 0.3,astposy + 0.1); worked but i think is not good best alternative loop i think better but cant find solution for the loop where the 4 objects is a new object cand be hited and distroyed one by one – yourbdeal dresdenptc Sep 24 '19 at 16:17
  • This????for (int i = 0; i < 3; i++) { draw_asteroid1(astposx - i,astposy + i); Sleep(0.016); } The problem there when the first Asteroid astposy > -1 restart all asteroids back to up of screen they dont working separated – yourbdeal dresdenptc Sep 24 '19 at 16:26
  • `struct coordinate { float posx, posy; }; coordinate asteroids[4];` Now you have an array of four coordinate pairs that you can use for your asteroids. It seems you could need to take a couple of steps back to learn some of the basics of C++ before continuing. Perhaps [get a couple of good books](https://stackoverflow.com/a/388282/440558). – Some programmer dude Sep 24 '19 at 16:32
  • *"what i need is made the asteroids the individual objects [...] the matrix can made that?"* The (model) matrix defines the location, orientation and scale of an object in the scene (world). If you set a matrix with an individual location, before you draw an asteroid, then each asteroids is drawn at an individual position in the scene. – Rabbid76 Sep 25 '19 at 06:01
  • then if i put float randPosX = rand() %asteroid.posx + 1200; and float randPosY = rand() %asteroid.poxy + 720; and glTranslatef(randPosX, randPosY, 0); will random position at start asteroids? – yourbdeal dresdenptc Oct 10 '19 at 23:40

0 Answers0