#include <windows.h>
#include <GL/glu.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <GL/glaux.h>
#include <math.h>
#include <stdio.h>
void drawScene()
{
glMatrixMode(GL_PROJECTION);
gluPerspective(60, 1.0, 1, -1);
//glOrtho(-100, 100, -100, 100, 150, -100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLfloat dDis = sqrtf(30000);
gluLookAt(0, 0, dDis, 0, 0, 0, 0, 1, 0);
//gluLookAt(0, 0, 150, 0, 0, 0, 0, 1, 0);
GLfloat x1 = -99;
GLfloat y1 = -99;
GLfloat x2 = 99;
GLfloat y2 = 99;
GLfloat z = -5;
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex3f(x1, y1, z);
glVertex3f(x2, y1, z);
glVertex3f(x2, y2, z);
glVertex3f(x1, y2, z);
glEnd();
x1 = -44;
y1 = -44;
x2 = 44;
y2 = 44;
z = -9;
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex3f(x1, y1, z);
glVertex3f(x2, y1, z);
glVertex3f(x2, y2, z);
glVertex3f(x1, y2, z);
glEnd();
x1 = -33;
y1 = -33;
x2 = 33;
y2 = 33;
z = -15;
glColor3f(1.0, 1.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex3f(x1, y1, z);
glVertex3f(x2, y1, z);
glVertex3f(x2, y2, z);
glVertex3f(x1, y2, z);
glEnd();
}
#define BUF_SIZE 512
void selectObjects()
{
GLuint buf[512];
glSelectBuffer(512, buf);
glRenderMode(GL_SELECT);
glInitNames();
glPushName(0);
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-100, 100, -100, 100, 150, -100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLoadName(444);
GLfloat x1 = -99;
GLfloat y1 = -99;
GLfloat x2 = 99;
GLfloat y2 = 99;
GLfloat z = -5;
glColor3f(1.0, 1.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex3f(x1, y1, z);
glVertex3f(x2, y1, z);
glVertex3f(x2, y2, z);
glVertex3f(x1, y2, z);
glEnd();
glLoadName(555);
x1 = -44;
y1 = -44;
x2 = 44;
y2 = 44;
z = -9;
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex3f(x1, y1, z);
glVertex3f(x2, y1, z);
glVertex3f(x2, y2, z);
glVertex3f(x1, y2, z);
glEnd();
x1 = -33;
y1 = -33;
x2 = 33;
y2 = 33;
z = -15;
glColor3f(1.0, 1.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex3f(x1, y1, z);
glVertex3f(x2, y1, z);
glVertex3f(x2, y2, z);
glVertex3f(x1, y2, z);
glEnd();
glPushMatrix();
glFlush();
GLint hits;
hits = glRenderMode(GL_SELECT);
int j = 0;
for (int i = 0; i<hits; i++)
{
printf("-- Name's Num: %d\n", buf[j]);
printf("-- Min Z: %d\n", buf[j+1]/0xffffffff);
printf("-- Max Z: %d\n", buf[j+2]/0xffffffff);
printf("-- Name : %d\n", buf[j+3]);
j+=4;
}
}
void init()
{
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_FLOAT);
}
void display()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawScene();
selectObjects();
glFlush();
}
int main()
{
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(200, 200);
glutInitWindowPosition(200, 400);
glutCreateWindow("Yulinxx");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
红宝书上的图形绘制太过麻烦,看得头晕.....
简化一下,绘制三个矩形,红色的名字为444
蓝色,黄色共一个名字:555
注意,选择的时候,需要将三个矩形重新绘制一遍.....................