Я показываю свою первую примитивную 2D модель астероида, но пробный цикл для показа 4 астероидов в программе инициализации в glBegin () ... glEnd () не увенчался успехом.
Цикл For или While может бытьиспользуется здесь?Я не работаю с классами здесь или вектором push_back pop_back glPushMatrix glPopMatrix работает так?
/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;
}