Seamless Skybox on iPhone, iPod touch
I’ll assume your Xcode Project is setup, you have at least 4 textures to display representing the sky, and you have XCode or OpenGL setup and drawing successfully with some Objective C knowledge in the iPhone simulator.
1. We have our routine to draw the object on screen.. It switches between the sides of the skybox, changes textures, then draws.
2. We have our function to select the object data to draw. Texture coordinates, Vertex points, Normals.
3. We have our file Box.h which has most if not all the data in it for #2′s data.
License; Free to use as you please. No warranties are expressed or implied. I came up with this code myself. (eSpecialized) As I know not which license is best. I’ll just say this code is licensed for free by Willy Nilly Inc. And finally use at your own risk, patent law-yers abound looking for ways to ruin businesses/people.
Copyright (c) 2010 Willy Nilly Inc, All Rights Reserved.
//##################################################### -(void)drawSky2 { [self SelectBoxModel]; glPushMatrix(); // Reset and transform the matrix. glLoadIdentity(); glDisable(GL_DEPTH_TEST); glDisable(GL_LIGHTING); glDisable(GL_BLEND); glDisable(GL_CULL_FACE); glCullFace(GL_FRONT); //GL_FRONT GL_BACK // Just in case we set all vertices to white. glColor4f(1,1,1,1); glScalef(25, 25, 25); //glRotatef(skyrot, 0, 1, 0); //make a global float var, so you can rotate the skybox //now draw your sides, selecting each side texture. //CubeVertexPoints = 6 points per side, 36 points total. //cubeIndex is the start point, 12 triangle faces. each side is 2 faces. int drawIdx = 0; glBindTexture(GL_TEXTURE_2D, _skybox[0]); glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, &CubeIndex[drawIdx] ); //36 points. 12 lines x 3.. draws a cube drawIdx+=6; //glBindTexture(GL_TEXTURE_2D, _skybox[1]); //top or bottom? //glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, &CubeIndex[drawIdx] ); drawIdx+=6; glBindTexture(GL_TEXTURE_2D, _skybox[1]); glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, &CubeIndex[drawIdx] ); drawIdx+=6; //top or bottom //glBindTexture(GL_TEXTURE_2D, _skybox[3]); //glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, &CubeIndex[drawIdx] ); drawIdx+=6; glBindTexture(GL_TEXTURE_2D, _skybox[2]); glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, &CubeIndex[drawIdx] ); drawIdx+=6; glBindTexture(GL_TEXTURE_2D, _skybox[3]); glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, &CubeIndex[drawIdx] ); //drawIdx+=6; //glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, CubeIndex[drawIdx] ); //end parts glCullFace(GL_BACK); //GL_FRONT GL_BACK glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_BLEND); glEnable(GL_CULL_FACE); glPopMatrix(); } //##################################################### - (void)SelectBoxModel { //disable the buffers, so normal memory can be accessed glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glVertexPointer(3, GL_FLOAT, 0, CubeVertices); //the 3 here never changes, thats the points glEnableClientState(GL_VERTEX_ARRAY); glTexCoordPointer(2, GL_FLOAT, 0, CubeTexcoords); //the 2 here never changes, that is the number of UV coords. glEnableClientState(GL_TEXTURE_COORD_ARRAY); glNormalPointer(GL_FLOAT, 0, CubeNormals); glEnableClientState(GL_NORMAL_ARRAY); DrawWhichModel = 1; } //##################################################### //=== and our final file; Box.h which has our data /* * Box.h * iSudokuGeek * * Created by eSpecialized on 3/30/10. * Copyright 2010 WILLY NILLY INC. All rights reserved. * */ static const int CubeVertexPoints = 36; //12 total faces, 3 vertex points. = 36 static const GLfloat CubeVertices[] = { 1.0,-1.0,1.0, 1.0,1.0,1.0, -1.0,-1.0,1.0, -1.0,1.0,1.0, 1.0,-1.0,-1.0, -1.0,-1.0,-1.0, -1.0,1.0,-1.0, 1.0,1.0,-1.0, -1.0,-1.0,1.0, 1.0,-1.0,1.0, -1.0,-1.0,1.0, -1.0,1.0,1.0, -1.0,-1.0,-1.0, 1.0,1.0,1.0, -1.0,1.0,-1.0, -1.0,1.0,1.0, 1.0,-1.0,-1.0, -1.0,-1.0,-1.0, -1.0,1.0,-1.0, 1.0,1.0,-1.0, 1.0,-1.0,-1.0, 1.0,1.0,-1.0, 1.0,1.0,1.0, 1.0,-1.0,1.0, }; static const GLfloat CubeNormals[] = { 0.0,0.0,1.0, 0.0,0.0,1.0, 0.0,0.0,1.0, 0.0,0.0,1.0, 0.0,-1.0,0.0, 0.0,-1.0,0.0, -1.0,0.0,0.0, 0.0,1.0,0.0, 0.0,-1.0,0.0, 0.0,-1.0,0.0, -1.0,0.0,0.0, -1.0,0.0,0.0, -1.0,0.0,0.0, 0.0,1.0,0.0, 0.0,1.0,0.0, 0.0,1.0,0.0, 0.0,0.0,-1.0, 0.0,0.0,-1.0, 0.0,0.0,-1.0, 0.0,0.0,-1.0, 1.0,0.0,0.0, 1.0,0.0,0.0, 1.0,0.0,0.0, 1.0,0.0,0.0, }; //texture coordinates are moved in 1 pixel and corrected in blender. static const GLfloat CubeTexcoords[] = { 0.001953,0.001953, 0.001953,-0.998047, 0.998047,0.001953, 0.998047,-0.998047, 0.001953,0.001953, 0.998047,0.001953, 0.998047,-0.998047, 0.001953,-0.998047, 0.998047,-0.998047, 0.001953,-0.998047, 0.001953,0.001953, 0.001953,-0.998047, 0.998047,0.001953, 0.001953,0.001953, 0.998047,-0.998047, 0.998047,0.001953, 0.998047,0.001953, 0.001953,0.001953, 0.001953,-0.998047, 0.998047,-0.998047, 0.001953,0.001953, 0.001953,-0.998047, 0.998047,-0.998047, 0.998047,0.001953, }; //must use static const GLushort CubeIndex[] = { 1,3,2, 0,1,2, 8,5,4, 9,8,4, 10,11,6, 12,10,6, 13,7,14, 15,13,14, 16,17,18, 19,16,18, 20,21,22, 23,20,22 };