4 Replies - 4134 Views - Last Post: 22 April 2011 - 05:42 AM Rate Topic: -----

#1 theholygod  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 342
  • Joined: 05-February 06

Setting up openGL viewport

Posted 20 April 2011 - 04:58 AM

Hi

I'm trying to do some fairly simple openGL stuff and I'm probably making it a lot harder for myself than it really is. Right now I have:

http://gyazo.com/c36...6b93d8a94fb.png

That's meant to be a red cube, I can't figure out setting up the view at all though. Here's my setup code:

void winReshapeFunc(GLint w, GLint h)
{
	// specify current matrix
	glMatrixMode(GL_PROJECTION);

	// load an identity matrix
	glLoadIdentity();	

	// create a projection matrix... i.e. 2D projection onto xy plane
	glOrtho( -500, 500, -500, 500, -100, 100);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	// set up the viewport
	glViewport(
		0,				// lower left x position
		-100,			// lower left y position
		(GLsizei) 800,	// viewport width
		(GLsizei) 800	// viewport height
	);
}

void init(){

	GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0};
	GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};  // Infinite light location.

	// Enable a single OpenGL light.
	glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
	glLightfv(GL_LIGHT0, GL_POSITION, light_position);
	glEnable(GL_LIGHT0);
	//glEnable(GL_LIGHTING);

	// Use depth buffering for hidden surface elimination.
	glEnable(GL_DEPTH_TEST);

	// Setup the view
	glMatrixMode(GL_PROJECTION);
	//perspective FOV, aspect ratio, Z near, Z far
	gluPerspective( 40.0, 1.0, 1.0, 10.0);
	glMatrixMode(GL_MODELVIEW);
	gluLookAt(0.0, 0.0, 5.0,  // eye is at (0,0,5)
				0.0, 0.0, 0.0, // center is at (0,0,0)
				0.0, 1.0, 0.0); // up is in positive Y direction



}

int _tmain(int argc, char** argv){

	load_AZ("test.az");
	//import_OBJ("car.obj");

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(800, 600);
	glutCreateWindow("Autobench Aztec 2011");

	//callback funcs
	//glutReshapeFunc(winReshapeFunc);
	glutDisplayFunc(renderScene);	
	glutIdleFunc(update);

	init();

	glutMainLoop();	

	return 0;
}


When I uncomment the glutReshapeFunc I get a black window.

How do I set up my viewport properly?

Is This A Good Question/Topic? 0
  • +

Replies To: Setting up openGL viewport

#2 Travis1012  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 220
  • Joined: 05-August 09

Re: Setting up openGL viewport

Posted 20 April 2011 - 05:57 AM

The glutReshapeFunc accepts a function name as an argument. The function given as an argument is called everytime the window is resized. I believe this may be run when the program window is initially displayed as well.

I am learning C++/GLUT/OpenGL at the moment. I have got a program that I have been working on. In my program the function used as an argument for glutReshapeFunc function is used to run the glViewport and gluPerspective functions and it seems to work well.

The glOrtho function is used for 2D perspectives if this is what you wish to do.
Was This Post Helpful? 0
  • +
  • -

#3 UG Cyber  Icon User is offline

  • D.I.C Addict

Reputation: 32
  • View blog
  • Posts: 546
  • Joined: 24-July 08

Re: Setting up openGL viewport

Posted 20 April 2011 - 11:00 AM

This is a sample reshape func that i use for 3D scenes

void Reshape(int w, int h)
{
   float ratio = w/h;
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glViewPort(0, 0, w, h);
   gluPerspective(45, ratio, 0.01, 50);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}


I wrote this in the browser but it should be right!

[request]Please move to Game Programming section for best assistance.
Was This Post Helpful? 0
  • +
  • -

#4 theholygod  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 342
  • Joined: 05-February 06

Re: Setting up openGL viewport

Posted 21 April 2011 - 10:53 AM

Still getting a black screen with that reshape func. Do I need to call lookat again?
Was This Post Helpful? 0
  • +
  • -

#5 theholygod  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 342
  • Joined: 05-February 06

Re: Setting up openGL viewport

Posted 22 April 2011 - 05:42 AM

Hah, sorry guys, turns out it just looks like it's clipping because there's no lighting.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1