I have been having trouble with my grid these past few days, I have been able to get the grid to show with some help but whenever I add an extra drawing the placement is all wrong, I decided to make a different approach with my code by having what I have drawn out in its right place which was the boundary that I have drawn however the grid isn't showing properly as the placement is all wrong.
This is how I want my grid to be like
#include "include\freeglut.h" // OpenGL toolkit - in the local shared folder
#include <iostream>
using namespace std;
#define X_CENTRE 0.0 /* 0,0 */
#define Y_CENTRE 0.0
#define LENGTH 20.0
GLboolean grid = false;
int w;
int h;
/* reshape callback function executed when window is moved or resized */
void reshape(int width, int height)
{
h = width;
w = height;
//set the matrix mode to PROJECTION
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, width, height);
// use orthographic (parallel) projection
glOrtho(-200.0, 200.0, -200.0, 200.0, -200.0, 200.0);
//set the matrix mode to MODEL
glMatrixMode(GL_MODELVIEW);
}
void drawGrid() {
GLint i;
glEnable(GL_LINE_STIPPLE); //Activates the line-style feature
glLineStipple(1, 0xAAAA); // Plots a dashed polyline
glColor3f(0.8, 0.8, 0.8);
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);
}
}
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 */
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT); /* clear window */
glLoadIdentity();
drawGrid();
drawBoundary(170, 0, 0);
glutSwapBuffers();
}
GLvoid IdleFunc(GLvoid)
{
glutPostRedisplay();
}
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 */
}
int main(int argc, char* argv[])//standard c entry signature
{
/* window management code ... */
/* initialises GLUT and processes any command line arguments */
glutInit(&argc, argv);
/* use double-buffered window and RGBA colour model */
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
/* window width = 400 pixels, height = 400 pixels */
glutInitWindowSize(500, 500);
/* window upper left corner at (100, 100) */
glutInitWindowPosition(100, 100);
/* creates an OpenGL window and set its title bar*/
glutCreateWindow("Coursework 1");
init();
glutDisplayFunc(display);
//glutSpecialFunc(specialKeys);
// glutSpecialUpFunc(keyGoneUp);
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);
// SetupRC();
glutIdleFunc(IdleFunc);
glutMainLoop();
return 0;
}
Aucun commentaire:
Enregistrer un commentaire