#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#define NUMSQUARES 64
#define WIDTH 1
#define HEIGHT 1
//Strcut square and board represent the minesweeper field
struct square
{
int x;
int y; // X and Y position of square
char letter; //Letter on the square
};
struct board
{
int width; //width of squares
int height; //height of squares
struct square grid[NUMSQUARES]; //amount of squares
};
int R=100,G=100,B=100,elapsedTime=0; //RGB and timer values
int windowH=480,windowW=640;
struct board gameGrid;
//function to fill the Board struct with proper coordinates
void createGrid(struct board* inGrid)
{
int i=0,tempx=0,tempy=0;
inGrid->grid[0].y=0;
inGrid->grid[0].x=0;
inGrid->grid[0].letter='a';
printf("grid 0 x=%d y=%d letter=%c \n",inGrid->grid[0].x,inGrid->grid[0].y);
for(i=1;i<64;i++)
{
if(i%8==0)
{
tempy+=inGrid->height+1;
tempx=0-inGrid->width-1;
}
tempx+=inGrid->width+1;
inGrid->grid.x=tempx;
inGrid->grid.y=tempy;
inGrid->grid.letter='a';
printf("grid %d x=%d y=%d letter=%c \n",i,inGrid->grid.x,inGrid->grid.y);
}
}
//Keyboard input function
void keyboard(unsigned char key, int x, int y)
{
//if r is pressed get a random number for each color variable
if (key==112)
{
printf("P pressed \n");
}
else if(key==113) //if q is pressed exit program!
{
exit(0);
}
}
//game is the timer function.
void game(int value)
{
elapsedTime++; //add to elapsed time
glutTimerFunc(1000,game,0); //create another timerfunc so we have concurency
}
void mydisplay(){
int i;
glClear(GL_COLOR_BUFFER_BIT);
for(i=0;i<64;i++)
{
R=(int)(rand() / (((double)RAND_MAX + 1)/ 255));
G=(int)(rand() / (((double)RAND_MAX + 1)/ 255));
B=(int)(rand() / (((double)RAND_MAX + 1)/ 255));
glLoadIdentity();
glScalef(0.1,0.1,0.1);
glTranslated(gameGrid.grid.x,gameGrid.grid.y,10);
glColor3ub(R,G,B); //use the timer random colors
int Xmodifier=(gameGrid.grid.x+((gameGrid.grid.x+gameGrid.width)/2));
int Ymodifier=(gameGrid.grid.y+((gameGrid.grid.y+gameGrid.height)/2));
//use grid square moved to be around the origin
glBegin(GL_POLYGON);
glVertex2d(gameGrid.grid.x-Xmodifier, gameGrid.grid.y-Ymodifier);
glVertex2d(gameGrid.grid.x-Xmodifier, gameGrid.grid.y+gameGrid.height-Ymodifier);
glVertex2d(gameGrid.grid.x+gameGrid.width-Xmodifier, gameGrid.grid.y+gameGrid.height-Ymodifier);
glVertex2d(gameGrid.grid.x+gameGrid.width-Xmodifier, gameGrid.grid.y-Ymodifier);
glEnd();
}
glFlush();
}
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION); /* switch matrix mode */
glLoadIdentity();
glOrtho(0,w,0,h,-1,1);
glMatrixMode(GL_MODELVIEW); /* return to modelview mode */
}
int main(int argc, char** argv){
glutInit(&argc, argv);
gameGrid.width=WIDTH;
gameGrid.height=HEIGHT;
createGrid(&gameGrid);
void(*foo)(int value);
glutReshapeFunc(myReshape);
glutInitWindowSize (400, 300);
glutTimerFunc(1000,game,0);
glutCreateWindow("simple");
glutKeyboardFunc (keyboard);
glutDisplayFunc(mydisplay);
glutMainLoop();
}
OpenGL setting 0,0 to be middle of the screen
I have to make a minesweeper game for my graphics class, but for some reason, openGL is setting (0,0,0) to be the middle of the screen.
here is the source code for my program. It is just supposed to display a bunch of squares in a grid formation.
There is a struct for a square, and one for the full grid, and it is a 1d array.
[Edited by - yaazz on October 11, 2008 2:13:04 PM]
I didn't look at the code (most of it's commented out, and the formatting on the pointer dereferences is messed up), but...
OpenGL is a 3-d rendering API. It can very easily be used to render 2-d graphics, but by default it thinks of the camera (in 3-d terms) as sitting at the origin and looking down the -z axis. Without any further adjustments, this puts the point (0, 0, 0) in the middle of the screen.
You can however set up the various OpenGL matrices so that the origin is arbitrarily positioned (upper-left corner, lower-left corner, etc.). For 2-d rendering, a common way to achieve this is by using an orthographic projection.
Quote:I have to make a minesweeper game for my graphics class, but for some reason, openGL is setting (0,0,0) to be the middle of the screen.It's not 'for some reason'; it's how OpenGL is designed to work, and it's correct.
OpenGL is a 3-d rendering API. It can very easily be used to render 2-d graphics, but by default it thinks of the camera (in 3-d terms) as sitting at the origin and looking down the -z axis. Without any further adjustments, this puts the point (0, 0, 0) in the middle of the screen.
You can however set up the various OpenGL matrices so that the origin is arbitrarily positioned (upper-left corner, lower-left corner, etc.). For 2-d rendering, a common way to achieve this is by using an orthographic projection.
Yeah I understand the concepts of openGL, I just dont know how to change this. I have tried various numbers in glOrtho, but none of them seem to change anything
I've compiled and ran your code. The position of 0,0 is at the top left corner. Though at first it was upside down, at the bottom left corner. If you want top left, then you need to call glOrtho(0,w,h,0,-1,1);.
However it seems that your drawing code is attempting to draw extremely small stuff that is so small that it doesn't even get drawn.
To test the position of 0,0 I used this code:
in the mydisplay function.
However it seems that your drawing code is attempting to draw extremely small stuff that is so small that it doesn't even get drawn.
To test the position of 0,0 I used this code:
glLoadIdentity();glBegin(GL_QUADS);glVertex2f(0, 0);glVertex2f(0, 100);glVertex2f(100, 100);glVertex2f(100, 0);glEnd();
in the mydisplay function.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement