Unable to display 2d box on top of 3d picture.

Started by
1 comment, last by z42 16 years, 1 month ago
I'd like to draw one object in the center (which is a triangle) in 3d and then draw on top of that a 2d object (a square.) However, when I tried running the below code, all that I got was a blank screen (absolutely nothing showed up.) Is there a smooth, relatively speaking, way to transition from 3d mode to 2d in order to do some basic drawing? Source:
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>

#include <pthread.h>
#include <iostream>
#include <cstdlib>

#include "theMachine.h"

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

	glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);

	glutInitWindowSize(1200, 600);  

	glutInitWindowPosition(0, 0);

	window = glutCreateWindow("The Machine");

	glutDisplayFunc(DrawGLScene);

	glutReshapeFunc(ReSizeGLScene);

	glutKeyboardFunc(keyPressed);

	glutSpecialFunc(specialKeyPressed);

	glutMouseFunc(mouse);

	//glutPassiveMotionFunc(constantPassiveMoveMouse);

	//glutMotionFunc(constantMoveMouse);

	InitGL(1200, 600);
  
	glutMainLoop();

	return 0;
}

void InitGL(GLsizei Width, GLsizei Height)
{
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

	glClearDepth(1.0);
	glDepthFunc(GL_LESS);
	glEnable(GL_DEPTH_TEST);

	glShadeModel(GL_SMOOTH);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	gluPerspective(45.0f, (GLfloat)Width / (GLfloat)Height, 0.1f, 100.0f);

	glMatrixMode(GL_MODELVIEW);
}

void ReSizeGLScene(int Width, int Height)
{
	if(Height == 0)
	{
		Height = 1;
	}

	width = Width;
	height = Height;

	glViewport(0, 0, Width, Height);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	gluPerspective(45.0f, (GLfloat)Width / (GLfloat)Height, 0.1f, 100.0f);

	glMatrixMode(GL_MODELVIEW);
}

void DrawGLScene()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();

	std::cout << "DrawGLScene method." << std::endl;

	glTranslatef(0.0f, 0.0f, -6.0f);

	glBegin(GL_TRIANGLES);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex3f(0.0, 1., 0.0);
		glColor3f(1.0f, 1.0f, 0.0f);
		glVertex3f(-0.5, 0.0, 0.0);
		glColor3f(0.0f, 0.0f, 1.0f);
		glVertex3f(0.5, 0.0, 0.0);
	glEnd();

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0, width, height, 0, 0, 1);
	glMatrixMode(GL_MODELVIEW);
	//glDisable(GL_DEPTH_TEST);

	glBegin(GL_QUADS);
		glVertex2f(0.0, 0.0);
		glVertex2f(40.0, 0.0);
		glVertex2f(40.0, 20.0);
		glVertex2f(0.0, 20.0);
	glEnd();

	glutSwapBuffers();
}

void keyPressed(unsigned char key, int x, int y)
{
	nap();

	switch(key)
	{
	case ESCAPE:
	case 'Q':
	case 'q':
		glutDestroyWindow(window); 

		exit(1);

		break;

    default:
		std::cout << "Key " << key << " pressed. No action there yet." 
			<< std::endl;

		break;
	}
}

void specialKeyPressed(int key, int x, int y)
{
	nap();

	switch(key)
	{
	case GLUT_KEY_PAGE_UP:

		break;

	case GLUT_KEY_PAGE_DOWN:

		break;

    default:
		break;
	}
}

void mouse(int button, int state, int x, int y)
{
	nap();

	std::cout << "X: " << x << " Y: " << y << std::endl;

	switch(button)
	{
	case GLUT_LEFT_BUTTON:
		if(GLUT_DOWN == state)
		{
			std::cout << "Left mouse button down." << std::endl;
		}
		else if(GLUT_UP == state)
		{
			std::cout << "Left mouse button up." << std::endl;
		}

		break;

	case GLUT_MIDDLE_BUTTON:
		if(GLUT_DOWN == state)
		{
			std::cout << "Middle mouse button down." << std::endl;
		}
		else if(GLUT_UP == state)
		{
			std::cout << "Middle mouse button up." << std::endl;
		}

		break;

	case GLUT_RIGHT_BUTTON:
		if(GLUT_DOWN == state)
		{
			std::cout << "Right mouse button down." << std::endl;
		}
		else if(GLUT_UP == state)
		{
			std::cout << "Right mouse button up." << std::endl;
		}

		break;

	default:
		break;
	}
}

void constantPassiveMoveMouse(int x, int y)
{
	std::cout << "X: " << x << " Y: " << y << std::endl;
}

void constantMoveMouse(int x, int y)
{
	std::cout << "* X: " << x << " Y: " << y << std::endl;
}

void nap()
{
	const struct timespec ts = {0, 800};
	struct timespec remainder;

	nanosleep( & ts, & remainder);
}

Header:

#ifndef THEMACHINE_H
#define THEMACHINE_H

#define ESCAPE 27

int window;

float angle = 0.0;

int width;
int height;

void InitGL(int Width, int Height);

void keyPressed(unsigned char key, int x, int y);

void specialKeyPressed(int key, int x, int y);

void DrawGLScene();

void ReSizeGLScene(GLsizei Width, GLsizei Height);

void mouse(int button, int state, int x, int y);

void constantPassiveMoveMouse(int x, int y);

void constantMoveMouse(int x, int y);

void nap();

#endif
Unless specified otherwise, I am developing in a Linux (Ubuntu 7.10) operating system.
Advertisement
Move your projection matrix set-up code to the very start of your draw function. And then after your set-up your ortho matrix, reset the modelview back to identity.
void DrawGLScene(){//moved from InitGL:	glMatrixMode(GL_PROJECTION);	glLoadIdentity();	gluPerspective(45.0f, (GLfloat)Width / (GLfloat)Height, 0.1f, 100.0f);	glMatrixMode(GL_MODELVIEW);//end moved section...	glMatrixMode(GL_PROJECTION);	glLoadIdentity();	glOrtho(0, width, height, 0, 0, 1);	glMatrixMode(GL_MODELVIEW);	glLoadIdentity();///New...
Thanks, that did it.
Unless specified otherwise, I am developing in a Linux (Ubuntu 7.10) operating system.

This topic is closed to new replies.

Advertisement