Now i have another problem. I want to draw a shape with 6 vertices but i need the vertices to be random, also i need them to be within a decimal range of -1.0 to 1.0. Here’s the code i’ve been trying out so far.
#include “shared/gltools.h” // OpenGL toolkit - in the local shared folder
#include <stdlib.h>
#include <math.h>
//set up some constants
#define X_CENTRE 0.0 /* centre point of square /
#define Y_CENTRE 0.0
#define LENGTH 1.0 / lengths of sides of square */
float yRotationAngle = 0;
/* reshape callback function
executed when window is moved or resized /
void reshape(int width, int height)
{
glViewport(0, 0, width, height);
/ uses orthographic (parallel) projection
use xmin = -1, xmax = 1
ymin = -1, ymax = 1
znear = -1, zfar = 1 - not relevant here (2D) */
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glMatrixMode( GL_MODELVIEW );
}
//* display callback function
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT); /* clear window /
glColor3f(1.0, 1.0, 1.0); / white drawing objects */
glPushMatrix();
glLoadIdentity();
glRotatef(yRotationAngle, 0.0, 0.0, 1.0);
glBegin(GL_LINE_LOOP);
//specify the vertices of the spaceship
glVertex3f(0.0,0.2,0.0); //top of spaceship
glVertex3f(-0.2,-0.2,0.0); //left point of spaceship
glVertex3f(0.0,0.0,0.0); //centre of spaceship
glVertex3f(0.2,-0.2,0.0); //right point of spaceship
glEnd();
glPopMatrix();
for(int i = 0; i < 60; i++); {
GLfloat vertices[i] =(rand() % 1.0) + 0.2;
}
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, vertices);
glDrawArrays(GL_POLYGON, 0, 6);
glDisableClientState(GL_VERTEX_ARRAY);
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
switch ( key )
{
case ‘r’: yRotationAngle = yRotationAngle + 10.0;
glutPostRedisplay();
break;
case ‘e’: yRotationAngle = yRotationAngle - 10.0;
glutPostRedisplay();
default: break;
}
}
/* graphics initialisation /
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0); / window will be cleared to black */
}
int main(int argc, char** argv)
{
/* window management code … /
/ initialises GLUT and processes any command line arguments /
glutInit(&argc, argv);
/ use single-buffered window and RGBA colour model /
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
/ window width = 400 pixels, height = 400 pixels /
glutInitWindowSize (400, 400);
/ window upper left corner at (100, 100) /
glutInitWindowPosition (100, 100);
/ creates an OpenGL window with command argument in its title bar */
glutCreateWindow (“Example 1”);
init();
for(int i = 0; i < 60; i++); {
GLfloat vertices[i] = (rand() % 1) + -1;
}
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
If you could tell me why this doesn’t work and guide me to fixing it or a better way of going about it that would be amazing, thanks.