code_junkie

opengl: how to avoid texture scaling

Nov 12th, 2011
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.53 KB | None | 0 0
  1. const XSize = 640, YSize = 480
  2. glMatrixMode (GL_PROJECTION)
  3. glLoadIdentity ()
  4. glOrtho (0, XSize, YSize, 0, 0, 1)
  5. glMatrixMode (GL_MODELVIEW)
  6. glDisable(GL_DEPTH_TEST)
  7. glClear(GL_COLOR_BUFFER_BIT)
  8.  
  9. // Now draw with 2i or 2f vertices instead of the normal vertex3f functions.
  10. // And for ES, of course set up your data structures and call drawarrays ofr drawelements.
  11.  
  12. SwapBuffers()
  13.  
  14. #include <GL/glut.h>
  15. #include <cstdlib>
  16. #include <cmath>
  17.  
  18. static GLuint texName;
  19.  
  20. void init(void)
  21. {
  22. glClearColor(0,0,0,0);
  23.  
  24. // create random texture
  25. const int texWidth = 8;
  26. const int texHeight = 8;
  27. GLubyte tex[texHeight][texWidth][4];
  28. for(int i = 0; i < texHeight; i++)
  29. {
  30. for(int j = 0; j < texWidth; j++)
  31. {
  32. tex[i][j][0] = (GLubyte) rand()%255;
  33. tex[i][j][1] = (GLubyte) rand()%255;
  34. tex[i][j][2] = (GLubyte) rand()%255;
  35. tex[i][j][3] = (GLubyte) 255;
  36. }
  37. }
  38. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  39.  
  40. glGenTextures(1, &texName);
  41. glBindTexture(GL_TEXTURE_2D, texName);
  42. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  43. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  44. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  45. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  46. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex);
  47.  
  48. // planes for texture coordinate generation
  49. GLfloat xp[] = {1,0,0,0};
  50. GLfloat yp[] = {0,1,0,0};
  51. GLfloat zp[] = {0,0,1,0};
  52. GLfloat wp[] = {0,0,0,1};
  53. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  54. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  55. glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  56. glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  57. glTexGenfv(GL_S, GL_EYE_PLANE, xp);
  58. glTexGenfv(GL_T, GL_EYE_PLANE, yp);
  59. glTexGenfv(GL_R, GL_EYE_PLANE, zp);
  60. glTexGenfv(GL_Q, GL_EYE_PLANE, wp);
  61. glEnable(GL_TEXTURE_GEN_S);
  62. glEnable(GL_TEXTURE_GEN_T);
  63. glEnable(GL_TEXTURE_GEN_R);
  64. glEnable(GL_TEXTURE_GEN_Q);
  65.  
  66. glEnable(GL_DEPTH_TEST);
  67. glShadeModel(GL_SMOOTH);
  68. glEnable(GL_TEXTURE_2D);
  69. glEnable(GL_CULL_FACE);
  70. glEnable(GL_LIGHTING);
  71. glEnable(GL_LIGHT0);
  72. }
  73.  
  74. void display(void)
  75. {
  76. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  77.  
  78. // projection
  79. glMatrixMode(GL_PROJECTION); glLoadIdentity();
  80. int viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport);
  81. gluPerspective(60.0, (GLdouble)viewport[2]/(GLdouble)viewport[3], 1.0, 100.0 );
  82.  
  83. // texture matrix trickery
  84. int tw,th;
  85. glMatrixMode(GL_TEXTURE); glLoadIdentity();
  86. glBindTexture(GL_TEXTURE_2D, texName);
  87. glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &tw);
  88. glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &th);
  89. glScaled( (viewport[2]/2)/(GLdouble)tw, (viewport[3]/2)/(GLdouble)th, 0 );
  90. GLdouble proj[16];
  91. glGetDoublev(GL_PROJECTION_MATRIX, proj); // grab projection matrix
  92. glMultMatrixd(proj);
  93.  
  94. // view transform
  95. glMatrixMode(GL_MODELVIEW); glLoadIdentity();
  96. glTranslatef(0,0,-2.5);
  97.  
  98. // render textured teapot
  99. glPushMatrix();
  100. const float ANGLE_SPEED = 60; // degrees/sec
  101. float angle = ANGLE_SPEED * (glutGet(GLUT_ELAPSED_TIME) / 1000.0f);
  102. glRotatef(angle*0.5f, 1, 0, 0);
  103. glRotatef(angle, 0, 1, 0);
  104. glRotatef(angle*0.7f, 0, 0, 1);
  105. glScalef(-1,-1,-1); // teapot is wound backwards (GL_CW), so flip it
  106. glutSolidTeapot(1);
  107. glPopMatrix();
  108.  
  109. glutSwapBuffers();
  110. }
  111.  
  112. void reshape(int w, int h)
  113. {
  114. // make width/height evenly divisible by 2
  115. w -= (w%2);
  116. h -= (h%2);
  117. glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  118. }
  119.  
  120. void keyboard (unsigned char key, int x, int y)
  121. {
  122. switch (key)
  123. {
  124. case 27: exit(0); break;
  125. default: break;
  126. }
  127. }
  128.  
  129. void idle() { glutPostRedisplay(); }
  130.  
  131. int main(int argc, char** argv)
  132. {
  133. glutInit(&argc, argv);
  134. glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
  135. glutInitWindowSize(640, 480);
  136. glutInitWindowPosition(100, 100);
  137. glutCreateWindow (argv[0]);
  138. glutDisplayFunc(display);
  139. glutReshapeFunc(reshape);
  140. glutKeyboardFunc(keyboard);
  141. glutIdleFunc(idle);
  142.  
  143. init();
  144. glutMainLoop();
  145. return 0;
  146. }
  147.  
  148. #include <GL/glut.h>
  149. #include <cstdlib>
  150. #include <cmath>
  151.  
  152. void glutTexturedCube(GLdouble size)
  153. {
  154. GLfloat texc[] = {
  155. 1,1, 0,1, 0,0, 1,0,
  156. 0,1, 0,0, 1,0, 1,1,
  157. 1,0, 1,1, 0,1, 0,0,
  158. 1,1, 0,1, 0,0, 1,0,
  159. 0,0, 1,0, 1,1, 0,1,
  160. 0,0, 1,0, 1,1, 0,1,
  161. };
  162.  
  163. GLfloat norm[] = {
  164. 0,0,1, 0,0,1, 0,0,1, 0,0,1,
  165. 1,0,0, 1,0,0, 1,0,0, 1,0,0,
  166. 0,1,0, 0,1,0, 0,1,0, 0,1,0,
  167. -1,0,0, -1,0,0, -1,0,0, -1,0,0,
  168. 0,-1,0, 0,-1,0, 0,-1,0, 0,-1,0,
  169. 0,0,-1, 0,0,-1, 0,0,-1, 0,0,-1,
  170. };
  171.  
  172. GLfloat vert[] = {
  173. 1,1,1, -1,1,1, -1,-1,1, 1,-1,1,
  174. 1,1,1, 1,-1,1, 1,-1,-1, 1,1,-1,
  175. 1,1,1, 1,1,-1, -1,1,-1, -1,1,1,
  176. -1,1,1, -1,1,-1, -1,-1,-1, -1,-1,1,
  177. -1,-1,-1, 1,-1,-1, 1,-1,1, -1,-1,1,
  178. 1,-1,-1, -1,-1,-1, -1,1,-1, 1,1,-1,
  179. };
  180.  
  181. GLuint idxs[] = {
  182. 0, 1, 2, 3,
  183. 4, 5, 6, 7,
  184. 8, 9, 10, 11,
  185. 12, 13, 14, 15,
  186. 16, 17, 18, 19,
  187. 20, 21, 22, 23,
  188. };
  189.  
  190. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  191. glEnableClientState(GL_NORMAL_ARRAY);
  192. glEnableClientState(GL_VERTEX_ARRAY);
  193. // feed vertices in as texture coordinates
  194. glTexCoordPointer(3, GL_FLOAT, 0, vert);
  195. glNormalPointer(GL_FLOAT, 0, norm);
  196. glVertexPointer(3, GL_FLOAT, 0, vert);
  197.  
  198. glPushMatrix();
  199. glColor4f(1, 1, 1, 1);
  200. glScaled(size, size, size);
  201. glDrawElements(GL_QUADS, sizeof(idxs)/sizeof(idxs[0]), GL_UNSIGNED_INT, idxs);
  202. glPopMatrix();
  203.  
  204. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  205. glDisableClientState(GL_NORMAL_ARRAY);
  206. glDisableClientState(GL_VERTEX_ARRAY);
  207. }
  208.  
  209. static GLuint texName;
  210.  
  211. void init(void)
  212. {
  213. glClearColor(0,0,0,0);
  214.  
  215. // create random texture
  216. const int texWidth = 8;
  217. const int texHeight = 8;
  218. GLubyte tex[texHeight][texWidth][4];
  219. for(int i = 0; i < texHeight; i++)
  220. {
  221. for(int j = 0; j < texWidth; j++)
  222. {
  223. tex[i][j][0] = (GLubyte) rand()%255;
  224. tex[i][j][1] = (GLubyte) rand()%255;
  225. tex[i][j][2] = (GLubyte) rand()%255;
  226. tex[i][j][3] = (GLubyte) 255;
  227. }
  228. }
  229. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  230.  
  231. glGenTextures(1, &texName);
  232. glBindTexture(GL_TEXTURE_2D, texName);
  233. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  234. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  235. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  236. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  237. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex);
  238.  
  239. glEnable(GL_DEPTH_TEST);
  240. glShadeModel(GL_SMOOTH);
  241. glEnable(GL_TEXTURE_2D);
  242. glEnable(GL_CULL_FACE);
  243. glEnable(GL_LIGHTING);
  244. glEnable(GL_LIGHT0);
  245. }
  246.  
  247. void display(void)
  248. {
  249. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  250.  
  251. // projection
  252. glMatrixMode(GL_PROJECTION); glLoadIdentity();
  253. int viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport);
  254. gluPerspective(60.0, (GLdouble)viewport[2]/(GLdouble)viewport[3], 1.0, 100.0 );
  255.  
  256. // view transform
  257. glMatrixMode(GL_MODELVIEW); glLoadIdentity();
  258. glTranslatef(0,0,-3);
  259.  
  260. // render textured teapot
  261. glPushMatrix();
  262. const float ANGLE_SPEED = 10; // degrees/sec
  263. float angle = ANGLE_SPEED * (glutGet(GLUT_ELAPSED_TIME) / 1000.0f);
  264. glRotatef(angle*0.5f, 1, 0, 0);
  265. glRotatef(angle, 0, 1, 0);
  266. glRotatef(angle*0.7f, 0, 0, 1);
  267.  
  268. // texture matrix trickery
  269. int tw,th;
  270. glBindTexture(GL_TEXTURE_2D, texName);
  271. glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &tw);
  272. glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &th);
  273. GLint mmode = 0;
  274. glGetIntegerv(GL_MATRIX_MODE, &mmode);
  275. glMatrixMode(GL_TEXTURE); glLoadIdentity();
  276. glScaled( (viewport[2]/2)/(GLdouble)tw, (viewport[3]/2)/(GLdouble)th, 0 );
  277. GLdouble mat[16];
  278. glGetDoublev(GL_PROJECTION_MATRIX, mat);
  279. glMultMatrixd(mat);
  280. glGetDoublev(GL_MODELVIEW_MATRIX, mat);
  281. glMultMatrixd(mat);
  282. glMatrixMode(mmode);
  283.  
  284. glutTexturedCube(1);
  285. glPopMatrix();
  286.  
  287. glutSwapBuffers();
  288. }
  289.  
  290. void reshape(int w, int h)
  291. {
  292. // make width/height evenly divisible by 2
  293. w -= (w%2);
  294. h -= (h%2);
  295. glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  296. }
  297.  
  298. void keyboard (unsigned char key, int x, int y)
  299. {
  300. switch (key)
  301. {
  302. case 27: exit(0); break;
  303. default: break;
  304. }
  305. }
  306.  
  307. void idle() { glutPostRedisplay(); }
  308.  
  309. int main(int argc, char** argv)
  310. {
  311. glutInit(&argc, argv);
  312. glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
  313. glutInitWindowSize(640, 480);
  314. glutInitWindowPosition(100, 100);
  315. glutCreateWindow (argv[0]);
  316. glutDisplayFunc(display);
  317. glutReshapeFunc(reshape);
  318. glutKeyboardFunc(keyboard);
  319. glutIdleFunc(idle);
  320.  
  321. init();
  322. glutMainLoop();
  323. return 0;
  324. }
Add Comment
Please, Sign In to add comment