#include "stdafx.h" #define NUM_PC 4 #define BUFSIZE 512 GLuint selectBuf[BUFSIZE]; GLfloat ctrlpoints[NUM_PC][3], initctrlpoints[NUM_PC][3] = { { -4.0, -4.0, 0.0 },{ -2.0, 4.0, 0.0 }, { 2.0, -4.0, 0.0 },{ 4.0, 4.0, 0.0 } }, corr_points[NUM_PC][3] = { { .0, -1.0, 0.0 },{ 1.0, 1.0, 0.0 }, { -1.0, 0., 0.0 },{ .0, 1.0, 0.0 } }; void init_puntos_de_control(void) { int i, j; for (i = 0; iNUM_PC)return; for (i = 0; i<3; i++) ctrlpoints[name - 1][i] += corr_points[name - 1][i]; glClearColor(0.0, 0.0, 0.0, 0.0); glShadeModel(GL_FLAT); glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, NUM_PC, &ctrlpoints[0][0]); glMapGrid1f(31, 0.0, 1.0); glEnable(GL_MAP1_VERTEX_3); glutPostRedisplay(); } /* processHits prints out the contents of the * selection array. */ void processHits(GLint hits, GLuint buffer[]) { if (hits == 0) {/*restore all colors*/ init(); return; } unsigned int i, j; GLuint names, *ptr; ptr = (GLuint *)buffer; for (i = 0; i < hits; i++) { /* for each hit */ names = *ptr; ptr += 3; for (j = 0; j < names; j++) /* for each name */ anallizename(*ptr++); } } void display(int mode) { int i; glColor3f(1.0, 1.0, 1.0); glEvalMesh1(GL_LINE, 0, 31); /* The following code displays the control points as dots. */ glColor3f(1.0, 1.0, 0.0); for (i = 0; i < NUM_PC; i++) { if (mode == GL_SELECT) glPushName(i + 1); /* En lugar de GL_POINTS, mejor hacer un cuadrado alrededor de cada punto de control:*/ glBegin(GL_QUADS); glVertex3f(ctrlpoints[i][0] - 0.5, ctrlpoints[i][1] - 0.5, 0); glVertex3f(ctrlpoints[i][0] - 0.5, ctrlpoints[i][1] + 0.5, 0); glVertex3f(ctrlpoints[i][0] + 0.5, ctrlpoints[i][1] + 0.5, 0); glVertex3f(ctrlpoints[i][0] + 0.5, ctrlpoints[i][1] - 0.5, 0); glEnd(); if (mode == GL_SELECT) glPopName(); } } void pickPunto_De_Control(int button, int state, int x, int y) { GLuint selectBuf[BUFSIZE]; GLint hits; GLint viewport[4]; if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN) return; glGetIntegerv(GL_VIEWPORT, viewport); glSelectBuffer(BUFSIZE, selectBuf); (void)glRenderMode(GL_SELECT); glInitNames(); glPushName(0); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); /* create 5x5 pixel picking region near cursor location */ gluPickMatrix((GLdouble)x, (GLdouble)(viewport[3] - y), 5.0, 5.0, viewport); glOrtho(-5.0, 5.0, -5.0, 5.0, -5.0, 5.0); display(GL_SELECT); glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); /*respeta la logica de maniopulacion de glMatrixMode tomada en beziercurv()*/ hits = glRenderMode(GL_RENDER); processHits(hits, selectBuf); glutPostRedisplay(); } void RenderScene() { glClear(GL_COLOR_BUFFER_BIT); display(GL_RENDER); glFlush(); } void reshape(int w, int h) { glViewport(0, 0, (GLsizei)w, (GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-5.0, 5.0, -5.0, 5.0, -5.0, 5.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void keyboard(unsigned char key, int x, int y) { switch (key) { case 27: exit(0); break; } } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow(argv[0]); init(); glutMouseFunc(pickPunto_De_Control); glutDisplayFunc(RenderScene); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutMainLoop(); return 0; }