Jump to content
  • Advertisement
Sign in to follow this  

OpenGL OpenGL setting 0,0 to be middle of the screen

This topic is 3657 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

#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;
	printf("grid 0 x=%d y=%d letter=%c \n",inGrid->grid[0].x,inGrid->grid[0].y);
		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!
//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;


R=(int)(rand() / (((double)RAND_MAX + 1)/ 255));
G=(int)(rand() / (((double)RAND_MAX + 1)/ 255));
B=(int)(rand() / (((double)RAND_MAX + 1)/ 255));


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 
		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);        


void myReshape(int w, int h)
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION); /* switch matrix mode */
    glMatrixMode(GL_MODELVIEW); /* return to modelview mode */

int main(int argc, char** argv){
glutInit(&argc, argv);

	void(*foo)(int value);

	glutInitWindowSize (400, 300);
	glutKeyboardFunc (keyboard);	


[Edited by - yaazz on October 11, 2008 2:13:04 PM]

Share this post

Link to post
Share on other sites
I didn't look at the code (most of it's commented out, and the formatting on the pointer dereferences is messed up), but...
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.

Share this post

Link to post
Share on other sites
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

Share this post

Link to post
Share on other sites
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:


glVertex2f(0, 0);
glVertex2f(0, 100);
glVertex2f(100, 100);
glVertex2f(100, 0);

in the mydisplay function.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!