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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
/*
* rasonly.c -
* Demonstrates the use of OpenGL for rasterization-only, with
* perspective-correct texture mapping.
*
* Michael I. Gold <gold@sgi.com>
* Silicon Graphics Computer Systems, May 1997
*
* Since current low-end 3D accelerators support only rasterization in
* hardware, a number of developers have expressed interested in using
* OpenGL as an interface to rasterization hardware while retaining
* control of transformations and lighting in the application code.
* Many OpenGL implementations detect and optimize for identity xforms,
* so this approach is entirely reasonable.
*
* Setting up rasterization-only is fairly straightforward. The projection
* matrix is set up as a one-to-one mapping between eye and clip coordinates,
* and the modelview matrix is set up as identity, e.g. object coordinates
* map directly to eye coordinates. This can be achieved as follows:
*
* glMatrixMode(GL_PROJECTION);
* glLoadIdentity();
* glOrtho(0.0f, (GLfloat) width, 0.0f, (GLfloat) height, -1.0f, 1.0f);
* glMatrixMode(GL_MODELVIEW);
* glLoadIdentity();
* glViewport(0, 0, width, height);
*
* where (width, height) represent the window dimensions.
*
* Now transformed geometry may be specified directly through the standard
* interfaces (e.g. glVertex*()). The only tricky part that remains is
* specifying texture coordinates such that perspective correction may
* occur. The answer is to use glTexCoord4*(), and perform the perspective
* divide on the texture coordinates directly.
*/
#include <stdlib.h>
#include <string.h>
#include <GL/glut.h>
GLboolean motion = GL_TRUE;
/* Matrices */
GLfloat rot = 0.0f;
GLfloat ModelView[16];
GLfloat Projection[16];
GLfloat Viewport[4];
/* Sample geometry */
GLfloat quadV[][4] = {
{ -1.0f, 0.0f, -1.0f, 1.0f },
{ 1.0f, 0.0f, -1.0f, 1.0f },
{ 1.0f, 0.5f, -0.2f, 1.0f },
{ -1.0f, 0.5f, -0.2f, 1.0f },
};
GLfloat quadC[][3] = {
{ 1.0f, 0.0f, 0.0f },
{ 0.0f, 1.0f, 0.0f },
{ 0.0f, 0.0f, 1.0f },
{ 1.0f, 1.0f, 1.0f },
};
GLfloat quadT[][2] = {
{ 0.0f, 0.0f },
{ 0.0f, 1.0f },
{ 1.0f, 1.0f },
{ 1.0f, 0.0f },
};
/*********************************************************************
* Utility functions
*/
int texWidth = 128;
int texHeight = 128;
/* Create and download the application texture map */
static void
setCheckedTexture(void)
{
int texSize;
void *textureBuf;
GLubyte *p;
int i,j;
/* malloc for rgba as worst case */
texSize = texWidth*texHeight*4;
textureBuf = malloc(texSize);
if (NULL == textureBuf) return;
p = (GLubyte *)textureBuf;
for (i=0; i < texWidth; i++) {
for (j=0; j < texHeight; j++) {
if ((i ^ j) & 8) {
p[0] = 0xff; p[1] = 0xff; p[2] = 0xff; p[3] = 0xff;
} else {
p[0] = 0x08; p[1] = 0x08; p[2] = 0x08; p[3] = 0xff;
}
p += 4;
}
}
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, texWidth, texHeight,
GL_RGBA, GL_UNSIGNED_BYTE, textureBuf);
free(textureBuf);
}
/* Perform one transform operation */
static void
Transform(GLfloat *matrix, GLfloat *in, GLfloat *out)
{
int ii;
for (ii=0; ii<4; ii++) {
out[ii] =
in[0] * matrix[0*4+ii] +
in[1] * matrix[1*4+ii] +
in[2] * matrix[2*4+ii] +
in[3] * matrix[3*4+ii];
}
}
/* Transform a vertex from object coordinates to window coordinates.
* Lighting is left as an exercise for the reader.
*/
static void
DoTransform(GLfloat *in, GLfloat *out)
{
GLfloat tmp[4];
GLfloat invW; /* 1/w */
/* Modelview xform */
Transform(ModelView, in, tmp);
/* Lighting calculation goes here! */
/* Projection xform */
Transform(Projection, tmp, out);
if (out[3] == 0.0f) /* do what? */
return;
invW = 1.0f / out[3];
/* Perspective divide */
out[0] *= invW;
out[1] *= invW;
out[2] *= invW;
/* Map to 0..1 range */
out[0] = out[0] * 0.5f + 0.5f;
out[1] = out[1] * 0.5f + 0.5f;
out[2] = out[2] * 0.5f + 0.5f;
/* Map to viewport */
out[0] = out[0] * Viewport[2] + Viewport[0];
out[1] = out[1] * Viewport[3] + Viewport[1];
/* Store inverted w for performance */
out[3] = invW;
}
/*********************************************************************
* Application code begins here
*/
/* For the sake of brevity, I'm use OpenGL to compute my matrices. */
void UpdateModelView(void)
{
glPushMatrix();
glLoadIdentity();
gluLookAt(0.0f, 1.0f, -4.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
glRotatef(rot, 0.0f, 1.0f, 0.0f);
/* Retrieve the matrix */
glGetFloatv(GL_MODELVIEW_MATRIX, ModelView);
glPopMatrix();
}
void InitMatrices(void)
{
/* Calculate projection matrix */
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPerspective(45.0f, 1.0f, 1.0f, 100.0f);
/* Retrieve the matrix */
glGetFloatv(GL_PROJECTION_MATRIX, Projection);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
UpdateModelView();
}
void Init(void)
{
glClearColor(0.2f, 0.2f, 0.6f, 1.0f);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
setCheckedTexture();
InitMatrices();
}
void Redraw(void)
{
GLfloat tmp[4];
int ii;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
for (ii = 0; ii < 4; ii++) {
/* Transform a vertex from object to window coordinates.
* 1/w is returned as tmp[3] for perspective-correcting
* the texture coordinates.
*/
DoTransform(quadV[ii], tmp);
/* Ideally the colors will be computed by the lighting equation,
* but I've hard-coded values for this example.
*/
glColor3fv(quadC[ii]);
/* Scale by 1/w (stored in tmp[3]) */
glTexCoord4f(quadT[ii][0] * tmp[3],
quadT[ii][1] * tmp[3], 0.0f, tmp[3]);
/* Note I am using Vertex3, not Vertex4, since we have already
* performed the perspective divide.
*/
glVertex3fv(tmp);
}
glEnd();
glutSwapBuffers();
}
void Motion(void)
{
rot += 3.0f;
if (rot >= 360.0f) rot -= 360.0f;
UpdateModelView();
Redraw();
}
/* ARGSUSED1 */
void Key(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
case 'm':
motion = !motion;
glutIdleFunc(motion ? Motion : NULL);
break;
}
}
/* ARGSUSED1 */
void Button(int button, int state, int x, int y)
{
switch (button) {
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN) {
rot -= 15.0f;
UpdateModelView();
Redraw();
}
break;
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN) {
rot += 15.0f;
UpdateModelView();
Redraw();
}
break;
}
}
void Reshape(int width, int height)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, (GLfloat) width, 0.0f, (GLfloat) height, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, width, height);
Viewport[0] = Viewport[1] = 0.0f;
Viewport[2] = (GLfloat) width;
Viewport[3] = (GLfloat) height;
}
int
main(int argc, char *argv[])
{
char *t;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(400, 400);
glutCreateWindow((t=strrchr(argv[0], '\\')) != NULL ? t+1 : argv[0]);
Init();
glutDisplayFunc(Redraw);
glutReshapeFunc(Reshape);
glutKeyboardFunc(Key);
glutMouseFunc(Button);
glutIdleFunc(Motion);
glutMainLoop();
return 0;
}
|