OpenGL ES Texture Problems - jhbau1000 - Feb 8, 2009 04:21 PM
Ok I'm new to OpenGL and I'm working on the iPhone trying to add textures to a cube I've been trying for a week to get the cube textured but can't can someone help me out here is the code I'm using:
Code:
- (void)drawView:(GLView*)view;
{
static GLfloat rquad;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glTranslatef(0.0f,0.0f,-15.0f);
glRotatef(rquad, 0.0f, 1.0f, 0.0f);
glVertexPointer(3, GL_FLOAT, 0, box1_vertices);
glTexCoordPointer(2, GL_FLOAT, 0, box1_Texture);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
for(int i = 0; i < num_box1_indices; i += 3)
{
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, &box1_indices[i]);
}
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D);
static NSTimeInterval lastDrawTime;
if (lastDrawTime)
{
NSTimeInterval timeSinceLastDraw = [NSDate timeIntervalSinceReferenceDate] - lastDrawTime;
rquad-=70 * timeSinceLastDraw;
}
lastDrawTime = [NSDate timeIntervalSinceReferenceDate];
}
-(void)setupView:(GLView*)view
{ CGImageRef spriteImage;
size_t width, height;
CGContextRef spriteContext;
GLubyte *spriteData;
GLuint spriteTexture;
const GLfloat zNear = 0.1,
zFar = 100.0,
fieldOfView = 60.0;
GLfloat size;
glMatrixMode(GL_PROJECTION);
glEnable(GL_DEPTH_TEST);
size = zNear * tanf(DEGREES_TO_RADIANS(fieldOfView) / 2.0);
CGRect rect = view.bounds;
glFrustumf(-size, size, -size / (rect.size.width / rect.size.height), size /
(rect.size.width / rect.size.height), zNear, zFar);
glViewport(0, 0, rect.size.width, rect.size.height);
glMatrixMode(GL_MODELVIEW);
spriteImage = [UIImage imageNamed:@"box.bmp"].CGImage;
width = CGImageGetWidth(spriteImage);
height = CGImageGetHeight(spriteImage);
if(spriteImage) {
spriteData = (GLubyte *) malloc(width * height * 4);
spriteContext = CGBitmapContextCreate(spriteData, width, height, 8, width * 4, CGImageGetColorSpace(spriteImage), kCGImageAlphaPremultipliedLast);
.
CGContextDrawImage(spriteContext, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), spriteImage);
CGContextRelease(spriteContext);
glGenTextures(1, &spriteTexture);
glBindTexture(GL_TEXTURE_2D, spriteTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData);
free(spriteData);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glEnable(GL_TEXTURE_2D);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
}
//glLoadIdentity();
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}
##########################################
Thats the draw code this is what's being drawn I've put it in a .h file.
Code:
#define box1_H
const int num_box1_indices = 36;
const int num_box1_vertices = 8;
float box1_vertices [] = {
-3.2126,0.0000,1.1594,
-3.2126,0.0000,-0.4348,
-1.8116,0.0000,-0.4348,
-1.8116,0.0000,1.1594,
-3.2126,1.6425,1.1594,
-1.8116,1.6425,1.1594,
-1.8116,1.6425,-0.4348,
-3.2126,1.6425,-0.4348,
};
float box1_Texture[] = {
1.0000,0.0000,
1.0000,1.0000,
0.0000,1.0000,
0.0000,0.0000,
};
short box1_indices [] = {
0,1,2,
2,3,0,
4,5,6,
6,7,4,
0,3,5,
5,4,0,
3,2,6,
6,5,3,
2,1,7,
7,6,2,
1,0,4,
4,7,1,
};
################################
Now this draws a cube and textures two sides that it can't get the whole cube textured any help is appreciated.
RE: OpenGL ES Texture Problems - Kezhaya - Jul 12, 2010 05:57 AM
I'm a noob at OpenGL too, and maybe this answer makes no sense anymore.
Even so, I think you don't need that for cycle to use glDrawElements.
At least I only make one call and pass the array as the last argument, and it works.
|