making an iphone cube class - iphone

I followed this tutorial and I'm currently trying to create a class to draw cubes. The class populates the buffers in a function called setup and draws them in a function called draw.
There are no errors however nothing is displayed on the screen.
My question is basicly how do I go about making a class from which I can render cubes.
Using Xcode 4.2, Opengl and Objective-C.
[edit]
-(void)SetShader:(GLuint)InShader
{
glUseProgram(InShader);
_positionSlot = glGetAttribLocation(InShader, "Position");
_colorSlot = glGetAttribLocation(InShader, "SourceColor");
glEnableVertexAttribArray(_positionSlot);
glEnableVertexAttribArray(_colorSlot);
_projectionUniform = glGetUniformLocation(InShader, "Projection");
_modelViewUniform = glGetUniformLocation(InShader, "Modelview");
_texCoordSlot = glGetAttribLocation(InShader, "TexCoordIn");
glEnableVertexAttribArray(_texCoordSlot);
_textureUniform = glGetUniformLocation(InShader, "Texture");
}
-(void)SetupVBO
{
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(CubeVertices), CubeVertices, GL_STATIC_DRAW);
GLuint indexBuffer;
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(CubeIndices), CubeIndices, GL_STATIC_DRAW);
}
-(void)draw
{
glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float) * 3));
glVertexAttribPointer(_texCoordSlot, 2, GL_FLOAT, GL_FALSE,
sizeof(Vertex), (GLvoid*) (sizeof(float) * 7));
glDrawElements(GL_TRIANGLES, sizeof(CubeIndices)/sizeof(CubeIndices[0]), GL_UNSIGNED_BYTE, 0);
}
-(void)render:(CADisplayLink*)displayLink
{
glClearColor(0, 0, 0.2, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
CC3GLMatrix *projection = [CC3GLMatrix matrix];
float h = 4.0f * self.frame.size.height / self.frame.size.width;
[projection populateFromFrustumLeft:-2 andRight:2 andBottom:-h/2 andTop:h/2 andNear:4 andFar:10];
glUniformMatrix4fv(_projectionUniform, 1, 0, projection.glMatrix);
CC3GLMatrix *modelView = [CC3GLMatrix matrix];
[modelView populateFromTranslation:CC3VectorMake(0, 0, -7)];
[modelView translateBy:CC3VectorMake(0,5,0)];
glUniformMatrix4fv(_modelViewUniform, 1, 0, modelView.glMatrix);
glViewport(0, 0, self.frame.size.width, self.frame.size.height);
[m_cube draw];
[_context presentRenderbuffer:GL_RENDERBUFFER];
}

It was as mentioned a problem with the arrangement of my viewport and perspective setups.

Related

OpenGl ES 2.0 and GLKit : From GLKBaseEffect shaders to OpenGl

I'm coding a 2D game with OpenGL ES 2.0 and GLKit. My architecture is based on the Games by Ian Terrel tutorials.
I discover recently that the GLKBaseEffect (provide simple shaders mgmt) leaks and makes sometimes my app crash. I'm now using my own shaders files (based on the Ray Wenderlich tutorial) but, at this time, I've just succeed to display openGl background color. My shapes colors and textures aren't displayed anymore.
IMPORTANT : I was setting the current context badly, now openGL says invalid Drawable.
Here is a part of my shape.m code :
#implementation P3Shape
const GLushort indices[] = {
0,1,2,3
};
typedef struct {
float Position[2];
float Color[4];
float TexCoord[2];
} Vertex;
//testing
const Vertex Vertices[] = {
// Front
{{10, -15}, {1, 0, 0, 1}, {1, 0}},
{{10, 15}, {0, 1, 0, 1}, {1, 0}},
{{-10, 10}, {0, 0, 1, 1}, {0, 0}},
{{-10, -1}, {0, 0, 0, 1}, {0, 1}},
};
- (id)initWithLayer:(CAEAGLLayer *)layer {
self = [super init];
if (self) {
[...]
_currentContext = [P3SessionState currentContext];
_eaglLayer = layer;
_eaglLayer.opaque = YES;
[self setupRenderBuffer];
[self setupFrameBuffer];
[self addToProgram];
[self createBuffers];
}
return self;
}
- (int)numVertices {
return 0;
}
- (void) addToProgram {
// setted before in the gamemanager
GLuint programHandle = [P3SessionState programHandle];
_positionSlot = glGetAttribLocation(programHandle, "Position");
_colorSlot = glGetAttribLocation(programHandle, "SourceColor");
_projectionUniform = glGetUniformLocation(programHandle, "Projection");
_modelViewUniform = glGetUniformLocation(programHandle, "Modelview");
_texCoordSlot = glGetAttribLocation(programHandle, "TexCoordIn");
_textureUniform = glGetUniformLocation(programHandle, "Texture");
}
- (void)setupRenderBuffer {
glGenRenderbuffers(1, &_colorRenderBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, _colorRenderBuffer);
[_currentContext renderbufferStorage:GL_RENDERBUFFER fromDrawable:_eaglLayer];
}
- (void)setupFrameBuffer {
GLuint framebuffer;
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _colorRenderBuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _depthRenderBuffer);
}
- (void)createBuffers {
// Static index data
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
}
// initialize the vertex data
- (GLKVector2 *)vertices {
if (vertexData == nil) {
vertexData = [NSMutableData dataWithLength:sizeof(GLKVector2)*self.numVertices];
}
return [vertexData mutableBytes];
}
// set a textureImage, allocate a glname and enable the GL_TEXTURE_2D and all
- (void)setTextureImage:(UIImage *)image {
CGImageRef spriteImage = image.CGImage;
if (!spriteImage) {
NSLog(#"Failed to load image %#", image);
exit(1);
}
size_t width = CGImageGetWidth(spriteImage);
size_t height = CGImageGetHeight(spriteImage);
GLubyte * spriteData = (GLubyte *) calloc(width*height*4, sizeof(GLubyte));
CGContextRef spriteContext = CGBitmapContextCreate(spriteData, width, height, 8, width*4, CGImageGetColorSpace(spriteImage), kCGImageAlphaPremultipliedLast);
CGContextDrawImage(spriteContext, CGRectMake(0, 0, width, height), spriteImage);
CGContextRelease(spriteContext);
GLuint texName;
glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData);
free(spriteData);
_imageTexture = texName;
}
- (GLKVector2 *)textureCoordinates {
if (textureCoordinateData == nil)
textureCoordinateData = [NSMutableData dataWithLength:sizeof(GLKVector2)*self.numVertices];
return [textureCoordinateData mutableBytes];
}
- (void)render {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0, 104.0/255.0, 55.0/255.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUniformMatrix4fv(_modelViewUniform, 1, 0, modelview.m);
glUniformMatrix4fv(_projectionUniform, 1, 0, projectionMatrix.m);
glEnableVertexAttribArray(_positionSlot);
glEnableVertexAttribArray(_colorSlot);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glVertexAttribPointer(_positionSlot, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float) * 3));
if(texture != nil) {
glEnableVertexAttribArray(_texCoordSlot);
glActiveTexture(GL_TEXTURE0); // unneccc in practice
glBindTexture(GL_TEXTURE_2D, _imageTexture);
glUniform1i(_textureUniform, 0); // unnecc in practice
glVertexAttribPointer(_texCoordSlot, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float) * 7));
}
glDrawElements(GL_TRIANGLE_FAN, sizeof(indices)/sizeof(GLushort), GL_UNSIGNED_SHORT, (void*)0);
}
//is cleanup code that tells the shader that we’re done using vertex position data.
glDisableVertexAttribArray(GLKVertexAttribPosition);
if (texture != nil)
glDisableVertexAttribArray(GLKVertexAttribTexCoord0);
[_currentContext presentRenderbuffer:GL_RENDERBUFFER];
glDisable(GL_BLEND);
}
// draw the shape in the selected scene
- (void)renderInScene:(P3Scene *)theScene {
projectionMatrix = [theScene projectionMatrix];
modelview = GLKMatrix4Multiply(GLKMatrix4MakeTranslation(0, 0, 0), GLKMatrix4MakeRotation(0, 0, 0, 1));
modelview = GLKMatrix4Multiply(modelview, GLKMatrix4MakeScale(scale.x, scale.y, 1));
[self render];
}
-(void)update:(NSTimeInterval)dt {
if(self.toDraw)
[spriteAnimation update:dt];
}
#end
As you can see, I'm testing with a Vertices array and a basic color but it makes no differences. OpenGl seems to be set correctly because my view is green thanks to the glClearColor(0, 104.0/255.0, 55.0/255.0, 1.0); in the render function.
Here the files on pastebin :
P3Shape.m on pastebin
And a manager who create the ogl program
P3GameManager.m on pastebin
Any ideas ?
EDIT : maybe a problem with my shaders ?
vertex :
attribute vec4 Position;
attribute vec4 SourceColor;
varying vec4 DestinationColor;
uniform mat4 Projection;
uniform mat4 Modelview;
attribute vec2 TexCoordIn; // New
varying vec2 TexCoordOut; // New
void main(void) {
DestinationColor = SourceColor;
gl_Position = Projection * Modelview * Position;
TexCoordOut = TexCoordIn; // New
}
and fragment :
varying lowp vec4 DestinationColor;
varying lowp vec2 TexCoordOut; // New
uniform sampler2D Texture; // New
void main(void) {
gl_FragColor = DestinationColor * texture2D(Texture, TexCoordOut); // New
}
I recommend using GLKViewController and GLKView to simplify your setup. Similarly, replace your texture loading with GLKTextureLoader.
I haven't seen any issues with GLKBaseEffect leaking memory and causing crashes. I would suggest posting a new question about this particular problem, and returning to using GLKBaseEffect if you don't need custom shaders (it looks like you don't). Will make your code a lot easier to manage and debug.

Open GL - ES 2.0 : Multiple Vertex Buffer Object

I had a quick question on openGL ES 2.0 I am trying to create some drawings on the screen which will require multiple geometries. This is how I am creating the geometries.
(void)setupBoxGeometry{
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(BoxVertices), BoxVertices, GL_STATIC_DRAW);
GLuint indexBuffer;
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(BoxIndices), BoxIndices, GL_STATIC_DRAW);
}
- (void)setupLineGeometry{
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(LineVertices), LineVertices, GL_STATIC_DRAW);
GLuint indexBuffer;
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(LineIndices), LineIndices, GL_STATIC_DRAW);
}
This is my render function
- (void)render:(CADisplayLink*)displayLink {
glClearColor(0.0/255.0, 0.0/255.0, 0.0/255.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
float h = 4.0f * self.frame.size.height / self.frame.size.width;
glViewport(0, 0, self.frame.size.width, self.frame.size.height);
glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE,
sizeof(Vertex), 0);
glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE,
sizeof(Vertex), (GLvoid*) (sizeof(float) * 3));
// Do some thing to the projection
CC3GLMatrix *projection = [CC3GLMatrix matrix];
[projection populateFromFrustumLeft:-2 andRight:2 andBottom:-h/2 andTop:h/2 andNear:4 andFar:10];
glUniformMatrix4fv(_projectionUniform, 1, 0, projection.glMatrix);
// Create the base for the square to be drawn on the screen
_baseMatrix = [CC3GLMatrix matrix];
[_baseMatrix populateFromTranslation:CC3VectorMake(-3.0, -5.2 + _yIncrease, -10)];
[_baseMatrix scaleByX:0.5];
[_baseMatrix scaleByY:_yIncrease];
[_baseMatrix rotateBy:CC3VectorMake(0, _rotationAngle, 0)];
glUniformMatrix4fv(_modelViewUniform, 1, 0, _baseMatrix.glMatrix);
glDrawElements(GL_TRIANGLES, sizeof(BoxIndices)/sizeof(BoxIndices[0]),
GL_UNSIGNED_BYTE, 0);
}
I call the box geometry function before I call the line geometry function. The problem is now my render function only draws lines. It does not draw any boxes. What am I doing wrong. If the question is not clear I can give more clarifications/
Where are you binding buffers before drawing?
Call
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
before
glVertexAttribPointer(...);
And call
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
before
glDrawElements(...);
Also you need to enable vertex pointers you are using:
glEnableVertexAttribArray(_positionSlot);
glEnableVertexAttribArray(_colorSlot);

How to change the perspective on a 3D cube in OpenGL ES?

I draw a cube with OpenGL ES and I want to have an above perspective on it.But I don't know what should I modify...Maybe you can help.
- (void)render:(CADisplayLink*)displayLink {
//-(void) render{
glClearColor(255.0, 255.0/255.0, 255.0/255.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
//projection
CC3GLMatrix *projection = [CC3GLMatrix matrix];
float h = 4.0f * self.frame.size.height / self.frame.size.width;
[projection populateFromFrustumLeft:-2 andRight:2 andBottom:-h/2 andTop:h/2 andNear:4 andFar:8];
glUniformMatrix4fv(_projectionUniform, 1, 0, projection.glMatrix);
CC3GLMatrix *modelView = [CC3GLMatrix matrix];
// _currentScaling += displayLink.duration * 1.0;
[modelView populateFromTranslation:CC3VectorMake(sin(30), _currentScaling , -7)];
[modelView rotateBy:CC3VectorMake(0, -15, 0)];
// [modelView scaleByY:_currentScaling];
if(_currentScaling > 2 ){
[displayLink invalidate];
_running = 1;
// scale = 0;
}
glUniformMatrix4fv(_modelViewUniform, 1, 0, modelView.glMatrix);
// 1
glViewport(0, 0, self.frame.size.width, self.frame.size.height);
// 2
glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE,
sizeof(Vertex), 0);
glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE,
sizeof(Vertex), (GLvoid*) (sizeof(float) * 3));
// 3
glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]),
GL_UNSIGNED_BYTE, 0);
[_context presentRenderbuffer:GL_RENDERBUFFER];
}
I recently started looking into OpenGL myself and found this same example. I believe what you are looking for is to change this line to change the perspective:
[modelView rotateBy:CC3VectorMake(0, -15, 0)];
and specify the degrees of rotation you want for the x,y,z axes.

GLKit & adding tints to textures

I am having issue with tinting a PNG image with GLKit.
I have a white PNG image that I load into the application and then use it to create a texture:
UIImage *image = [ UIImage imageNamed:#"brushImage" ];
NSError *error = nil;
texture_m = [[ GLKTextureLoader textureWithCGImage:image.CGImage options:nil error:&error] retain ];
if (error) {
NSLog(#"Error loading texture from image: %#",error);
}
the texture is created with no errors.
however when I want to render the texture with blend activated the colour seems to be ignored and I get a white image. I had no issues when I was doing this in OpenGL1 were the image would pick up the colour that was defined in glColor4f(). Here is my render code:
-(void)render{
if (texture_m != nil) {
effect_m.texture2d0.enabled = GL_TRUE;
effect_m.texture2d0.envMode = GLKTextureEnvModeReplace;
effect_m.texture2d0.target = GLKTextureTarget2D;
effect_m.texture2d0.name = texture_m.name;
}
[effect_m prepareToDraw];
glClear(GL_COLOR_BUFFER_BIT);
GLfloat squareVertices[] = {
50, 50,
150, 50,
50, 150,
150, 150
};
GLfloat squareTexture[] = {
0, 0,
1, 0,
0, 1,
1, 1
};
glColor4f( 1, 0, 0, 1 );
glEnableVertexAttribArray(GLKVertexAttribPosition);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glEnable(GL_BLEND);
glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, squareVertices);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0, squareTexture );
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisable(GL_BLEND);
glDisableVertexAttribArray(GLKVertexAttribPosition);
glDisableVertexAttribArray(GLKVertexAttribTexCoord0);
}
Could anybody help to solve this issue
Thanks
Reza
I have managed to solve my problem and here is the solution
-(void)render
{
if (texture_m != nil) {
effect_m.texture2d0.enabled = GL_TRUE;
// here you need to env mode to GLKTextureEnvModeModulate rather than GLKTextureEnvModeReplace
effect_m.texture2d0.envMode = GLKTextureEnvModeModulate;
effect_m.texture2d0.target = GLKTextureTarget2D;
effect_m.texture2d0.name = texture_m.name;
}
// then here I have added the tint colour to the GLKBaseEffect class as constant colour which I imagine replaces the calls to glColor4f for OpenGL1.1
effect_m.useConstantColor = YES;
float alphaValue = 0.7;
GLKVector4 colour = GLKVector4Make( 0* alphaValue, 1* alphaValue, 1* alphaValue, alphaValue );
effect_m.constantColor = colour;
// remember multiplying the alpha value to each colour component
[effect_m prepareToDraw];
glClear(GL_COLOR_BUFFER_BIT);
GLfloat squareVertices[] = {
50, 50,
150, 50,
50, 150,
150, 150
};
GLfloat squareTexture[] = {
0, 0,
1, 0,
0, 1,
1, 1
};
// glColor4f not necessary
// glColor4f( 1, 0, 0, 1 );
glEnableVertexAttribArray(GLKVertexAttribPosition);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glEnable(GL_BLEND);
glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, squareVertices);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0, squareTexture );
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisable(GL_BLEND);
glDisableVertexAttribArray(GLKVertexAttribPosition);
glDisableVertexAttribArray(GLKVertexAttribTexCoord0);
}

How to set background image using OpenGL ES 1.1 for iPhone

Please help me to set a background image in EaglView. Here my source code with the image:
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
brushImage = [UIImage imageNamed:#"Particle.png"].CGImage;
width = CGImageGetWidth(brushImage);
height = CGImageGetHeight(brushImage);
if(brushImage) {
brushData = (GLubyte *) calloc(width * height * 4, sizeof(GLubyte));
brushContext = CGBitmapContextCreate(brushData, width, height, 8, width * 4, CGImageGetColorSpace(brushImage), kCGImageAlphaPremultipliedLast);
CGContextDrawImage(brushContext, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), brushImage);
CGContextRelease(brushContext);
glGenTextures(1, &brushTexture);
glBindTexture(GL_TEXTURE_2D, brushTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, brushData);
free(brushData);
}
self.contentScaleFactor = 1.0;
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
CGRect frame = self.bounds;
CGFloat scale = self.contentScaleFactor;
glOrthof(0, frame.size.width * scale, 0, frame.size.height * scale, -1, 1);
glViewport(0, 0, frame.size.width * scale, frame.size.height * scale);
glMatrixMode(GL_MODELVIEW);
glDisable(GL_DITHER);
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_POINT_SPRITE_OES);
glTexEnvf(GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, GL_TRUE);
glPointSize(width / kBrushScale);`
Instead of using a point sprite should simply draw a textured quad.
GLfloat vert_tex[] = {
0., 0.,
1., 0.,
1., 1.,
0., 1.
};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, vert_tex);
glTexCoordPointer(2, GL_FLOAT, 0, vert_tex);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 1, 0, 1, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture_name);
glDrawArrays(GL_QUADS, 0, 4);
EDIT2:
// Erases the screen
- (void) erase
{
[EAGLContext setCurrentContext:context];
CGRect frame = self.bounds;
CGFloat scale = self.contentScaleFactor;
GLfloat vert_tex[] = {
0., 0.,
frame.size.width * scale, 0.,
frame.size.width * scale, frame.size.height * scale,
0., frame.size.height * scale
};
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, vert_tex);
glTexCoordPointer(2, GL_FLOAT, 0, vert_tex);
// Setup the view port in Pixels
glMatrixMode(GL_PROJECTION);
glOrthof(0, frame.size.width * scale, 0, frame.size.height * scale, -1, 1);
glViewport(0, 0, frame.size.width * scale, frame.size.height * scale);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDisable(GL_DITHER);
glEnable(GL_TEXTURE_2D);
// background_image texture must be initialized with the desired
// content; see initWithCoder of original GLPaint example for
// example code how to load textures.
glBindTexture(GL_TEXTURE_2D, background_image);
// Clear the buffer with background image
glDrawArrays(GL_QUADS, 0, 4);
// Display the buffer
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
}
In renderLineFromPoint you must add a line to bind the brush texture before drawing:
// Drawings a line onscreen based on where the user touches
- (void) renderLineFromPoint:(CGPoint)start toPoint:(CGPoint)end
{
/* ... */
// add this line here v v v v v v v v v v v
glBindTexture(GL_TEXURE_2D, brushTexture);
// ^ ^ ^ ^^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
// Render the vertex array
glVertexPointer(2, GL_FLOAT, 0, vertexBuffer);
glDrawArrays(GL_POINTS, 0, vertexCount);
// Display the buffer
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];