Ik ben het boek Computer Graphics with OpenGL aan het doorwerken en ik probeer een opdracht te maken, waarin je een stuk code moet aanpassen, zodanig dat de zeshoek die getekend wordt altijd in het midden terecht komt. Mijn idee was om het middelpunt van de zeshoek op punt (0,0) te definieren en het coordinatenstelsel telkens van [-breedte/2,breedte/2]x[-hoogte/2,hoogte/2] te laten lopen. Dit heeft echter totaal niet het verwachtte effect.
Iemand enig idee waarom dit niet werkt?
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
| #include <GL/glut.h>
#include <math.h>
#include<stdlib.h>
#include <stdio.h>
const double TWO_PI = 6.2831853;
// Initial display window size
GLuint regHex;
int winWidth, winHeight;
class screenPt
{
private:
GLint x,y;
public:
//Default constructor: initialize coordinate points to (0,0)
screenPt()
{
x = y = 0;
}
void setCoords(GLint xCoord, GLint yCoord)
{
x = xCoord;
y = yCoord;
}
GLint getx() const
{
return x;
}
GLint gety() const
{
return y;
}
};
static void init (void)
{
screenPt hexVertex;
GLdouble theta;
GLint k;
glClearColor(1.0, 1.0, 1.0, 0.0); //Display window color is white
/* Set up a display list for a red regular hexagon.
* Vertices for the hexagon are six equally spaced
* points around the circumference of a circle.
*/
regHex = glGenLists(1); //Get an identifier for the display list.
glNewList(regHex,GL_COMPILE);
glColor3f(1.0, 0.0, 0.0); //Set fill color for the hexagon to red
glBegin(GL_POLYGON);
for (k=0;k<6;k++)
{
theta = TWO_PI*k/6.0;
hexVertex.setCoords(150*cos(theta),
150*sin(theta));
glVertex2i(hexVertex.getx(), hexVertex.gety());
}
glEnd();
glEndList();
}
void regHexagon(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glCallList(regHex);
glFlush();
}
void winReshapeFcn(int newWidth, int newHeight)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D( -newWidth/2, newWidth/2, -newWidth/2, newWidth/2);
}
int main( int argc,char ** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(100,100);
glutInitWindowSize(400, 400);
glutCreateWindow("Reshape-function & Display List example");
init();
glutDisplayFunc(regHexagon);
glutReshapeFunc(winReshapeFcn);
glutMainLoop();
exit(EXIT_SUCCESS);
} |
Iemand enig idee waarom dit niet werkt?