Recently I had problems with drawing a grid and making a pop-up menu for it to turn the grid on and off, I have now solved this problem with a bit of help but now whenever I want to draw something (I am now trying to draw a boundary like this
when I try to add the boundary with my grid that I have created I have been having problems, probably with my coordinates but I don't know how to solve it, this is how it is looking.
#include "include\freeglut.h" // OpenGL toolkit - in the local shared folder
#include <iostream>
//set up some constants
#define carX_CENTRE -150.0 /* centre point of the car */
#define carY_CENTRE -150.0
#define X_CENTRE 0.0 /* centre point of square */
#define Y_CENTRE 0.0
#define LENGTH 20.0
//GLfloat red = 1.0, green = 1.0, blue = 1.0;
GLboolean grid = false;
int w = 1;
int h = 1;
/* 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) */
w = width;
h = height;
glMatrixMode(GL_PROJECTION);
glOrtho(0.0, (float)w, (float)h, 0.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
void drawBoundary(float length, float x, float y)
{
glBegin(GL_LINE_LOOP);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(X_CENTRE - length - 20, Y_CENTRE - length); //these 4 points draw the game boundary
glVertex2f(X_CENTRE - length - 20, Y_CENTRE + length); //these 4 points draw the game boundary
glVertex2f(X_CENTRE + length + 20, Y_CENTRE + length); //these 4 points draw the game boundary
glVertex2f(X_CENTRE + length + 20, Y_CENTRE - length); //these 4 points draw the game boundary
glEnd();
glFlush(); /* execute drawing commands in buffer */
}
/* display callback function
called whenever contents of window need to be re-displayed */
//this is the all important drawing method - all drawing code goes in here
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT); /* clear window */
//glColor3f(red, green, blue); /* white drawing objects */
glColor3f(0.8, 0.8, 0.8);
GLint i;
glEnable(GL_LINE_STIPPLE); //Activates the line-style feature
glLineStipple(1, 0xAAAA); // Plots a dashed polyline
if (grid) {
glBegin(GL_LINES);
for (i = 2; i <= 9; i++)
{
glVertex3f(i * 0.1 * w, 0.0, 0.0);
glVertex3f(i * 0.1 * w, 0.9 * h, 0.0);
}
for (i = 1; i <= 9; i++)
{
glVertex3f(0.1 * w, i * 0.1 * h, 0.0);
glVertex3f(w, i * 0.1 * h, 0.0);
}
glEnd();
glDisable(GL_LINE_STIPPLE);
}
drawBoundary(170, 0, 0);
glFlush(); /* execute drawing commands in buffer */
}
void myGridmenu(GLint id)
{
if (id == 1)
{
grid = 1.0;
}
else
{
grid = 0.0;
}
glutPostRedisplay();
}
/* graphics initialisation */
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0); /* window will be cleared to black */
}
//rename this to main(...) and change example 2 to run this main function
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(500, 500);
/* window upper left corner at (100, 100) */
glutInitWindowPosition(100, 100);
/* creates an OpenGL window with command argument in its title bar */
glutCreateWindow("Coursework 1");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
//Create a grid that the user can turn on and off
glutCreateMenu(myGridmenu);
glutAddMenuEntry("Grid on", 1);
glutAddMenuEntry("Grid off", 2);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();
return 0;
}
Aucun commentaire:
Enregistrer un commentaire