summaryrefslogtreecommitdiff
path: root/src/mapUtils.cpp
blob: d250c982f8e3368660b3ad4a1381c9bb368a1737 (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * mapUtils.h
 *
 *  Created by Tim Redfern on 20/12/2011.
 *  global utils for projection mapping
 *
 */

#include "ofMain.h"

//texture binding with normalised coords
void bindTexture(ofBaseHasTexture &t) {
    ofTexture &tex = t.getTextureReference();
    tex.bind();

    glMatrixMode(GL_TEXTURE);
    glPushMatrix();
    glLoadIdentity();

    ofTextureData texData = tex.getTextureData();
    if(texData.textureTarget == GL_TEXTURE_RECTANGLE_ARB) {
        glScalef(tex.getWidth(), tex.getHeight(), 1.0f);
    } else {
        glScalef(tex.getWidth() / texData.tex_w, tex.getHeight() / texData.tex_h, 1.0f);
    }

    glMatrixMode(GL_MODELVIEW);
}
void unbindTexture(ofBaseHasTexture &t) {
    t.getTextureReference().unbind();

    glMatrixMode(GL_TEXTURE);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
}
void bindTex(ofTexture &tex) {
    tex.bind();

    glMatrixMode(GL_TEXTURE);
    glPushMatrix();
    glLoadIdentity();

    ofTextureData texData = tex.getTextureData();
    if(texData.textureTarget == GL_TEXTURE_RECTANGLE_ARB) {
        glScalef(tex.getWidth(), tex.getHeight(), 1.0f);
    } else {
        glScalef(tex.getWidth() / texData.tex_w, tex.getHeight() / texData.tex_h, 1.0f);
    }

    glMatrixMode(GL_MODELVIEW);
}
void unbindTex(ofTexture &tex) {
    tex.unbind();

    glMatrixMode(GL_TEXTURE);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
}
ofPoint distort(ofPoint pt,float d){
    //normalised coords -1..1, d
    float r=pow(pow(pow(pt.x,2.0f)+pow(pt.y,2.0f),0.5f),1.0f+d);
    float a=atan2f(pt.x,pt.y);
    return ofPoint(r*sin(a),r*cos(a));
};

void drawBox(float size) {
    // this func just draws a perfectly normal box with some texture coordinates
    glBegin(GL_QUADS);
        // Front Face
        glTexCoord2f(0.0f, 0.0f); glVertex3f(-size, -size,  size);	// Bottom Left Of The Texture and Quad
        glTexCoord2f(1.0f, 0.0f); glVertex3f( size, -size,  size);	// Bottom Right Of The Texture and Quad
        glTexCoord2f(1.0f, 1.0f); glVertex3f( size,  size,  size);	// Top Right Of The Texture and Quad
        glTexCoord2f(0.0f, 1.0f); glVertex3f(-size,  size,  size);	// Top Left Of The Texture and Quad
        // Back Face
        glTexCoord2f(1.0f, 0.0f); glVertex3f(-size, -size, -size);	// Bottom Right Of The Texture and Quad
        glTexCoord2f(1.0f, 1.0f); glVertex3f(-size,  size, -size);	// Top Right Of The Texture and Quad
        glTexCoord2f(0.0f, 1.0f); glVertex3f( size,  size, -size);	// Top Left Of The Texture and Quad
        glTexCoord2f(0.0f, 0.0f); glVertex3f( size, -size, -size);	// Bottom Left Of The Texture and Quad
        // Top Face
        glTexCoord2f(0.0f, 1.0f); glVertex3f(-size,  size, -size);	// Top Left Of The Texture and Quad
        glTexCoord2f(0.0f, 0.0f); glVertex3f(-size,  size,  size);	// Bottom Left Of The Texture and Quad
        glTexCoord2f(1.0f, 0.0f); glVertex3f( size,  size,  size);	// Bottom Right Of The Texture and Quad
        glTexCoord2f(1.0f, 1.0f); glVertex3f( size,  size, -size);	// Top Right Of The Texture and Quad
        // Bottom Face
        glTexCoord2f(1.0f, 1.0f); glVertex3f(-size, -size, -size);	// Top Right Of The Texture and Quad
        glTexCoord2f(0.0f, 1.0f); glVertex3f( size, -size, -size);	// Top Left Of The Texture and Quad
        glTexCoord2f(0.0f, 0.0f); glVertex3f( size, -size,  size);	// Bottom Left Of The Texture and Quad
        glTexCoord2f(1.0f, 0.0f); glVertex3f(-size, -size,  size);	// Bottom Right Of The Texture and Quad
        // Right face
        glTexCoord2f(1.0f, 0.0f); glVertex3f( size, -size, -size);	// Bottom Right Of The Texture and Quad
        glTexCoord2f(1.0f, 1.0f); glVertex3f( size,  size, -size);	// Top Right Of The Texture and Quad
        glTexCoord2f(0.0f, 1.0f); glVertex3f( size,  size,  size);	// Top Left Of The Texture and Quad
        glTexCoord2f(0.0f, 0.0f); glVertex3f( size, -size,  size);	// Bottom Left Of The Texture and Quad
        // Left Face
        glTexCoord2f(0.0f, 0.0f); glVertex3f(-size, -size, -size);	// Bottom Left Of The Texture and Quad
        glTexCoord2f(1.0f, 0.0f); glVertex3f(-size, -size,  size);	// Bottom Right Of The Texture and Quad
        glTexCoord2f(1.0f, 1.0f); glVertex3f(-size,  size,  size);	// Top Right Of The Texture and Quad
        glTexCoord2f(0.0f, 1.0f); glVertex3f(-size,  size, -size);	// Top Left Of The Texture and Quad
    glEnd();
}
void drawBoard(float x,float y,float z) {
	glPushMatrix();
	glTranslated(x,y,z);
	glBegin(GL_QUADS);
	int cx,cz;
	for (int i=0;i<8;i++) {
		for (int j=0;j<4;j++) {
			cx=((i%2)*16)+(j*32)-56;
			cz=(i*16)-56;
			if ((i==7)&&(j==3)) { //draw special square
				glVertex3f(cx+8, 0, cz-8);
				glVertex3f(cx+8, 0, cz+8);
				glVertex3f(cx+4, 0, cz+4);
				glVertex3f(cx+4, 0, cz-4);

				glVertex3f(cx+4, 0, cz+4);
				glVertex3f(cx+8, 0, cz+8);
				glVertex3f(cx-8, 0, cz+8);
				glVertex3f(cx-4, 0, cz+4);

				glVertex3f(cx-4, 0, cz-4);
				glVertex3f(cx-4, 0, cz+4);
				glVertex3f(cx-8, 0, cz+8);
				glVertex3f(cx-8, 0, cz-8);

				glVertex3f(cx+8, 0, cz-8);
				glVertex3f(cx+4, 0, cz-4);
				glVertex3f(cx-4, 0, cz-4);
				glVertex3f(cx-8, 0, cz-8);
			}
			else {
				glVertex3f(cx+8, 0, cz-8);
				glVertex3f(cx+8, 0, cz+8);
				glVertex3f(cx-8, 0, cz+8);
				glVertex3f(cx-8, 0, cz-8);
			}
		}
	}
	glEnd();
	glPopMatrix();
}
void drawCylinder(float r,float h,float x,float y,float z) {
	glPushMatrix();
	glTranslated(x,y,z);
	glBegin(GL_QUADS);
	float step=TWO_PI/50.0;
	float txstep=1.0/50.0;
	for (float i=0,j=0;i<=TWO_PI;i+=step,j+=txstep) {
		glTexCoord2f(j,0); glVertex3f(cos(i)*r, 0, sin(i)*r);
		glTexCoord2f(j,1); glVertex3f(cos(i)*r, -h, sin(i)*r);
		glTexCoord2f(j+txstep,1); glVertex3f(cos(i+step)*r, -h, sin(i+step)*r);
		glTexCoord2f(j+txstep,0); glVertex3f(cos(i+step)*r, 0, sin(i+step)*r);
	}
	glEnd();
	glPopMatrix();
}