I am drawing a pixel using GLKit. I can successfully draw the pixel at (10, 10) coordinates if I have:
glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Prepare the effect for rendering
[self.effect prepareToDraw];
GLfloat points[] =
{
10.0f, 10.0f,
};
glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
GLuint bufferObjectNameArray;
glGenBuffers(1, &bufferObjectNameArray);
glBindBuffer(GL_ARRAY_BUFFER, bufferObjectNameArray);
glBufferData(
GL_ARRAY_BUFFER,
sizeof(points),
points,
GL_STATIC_DRAW);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(
GLKVertexAttribPosition,
2,
GL_FLOAT,
GL_FALSE,
2*4,
NULL);
glDrawArrays(GL_POINTS, 0, 1);
But I want to decide at runtime how many and exactly where I want to draw pixels, so I tried this but it is drawing pixel at (10, 0), something's wrong here:
glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Prepare the effect for rendering
[self.effect prepareToDraw];
GLfloat *points = (GLfloat*)malloc(sizeof(GLfloat) * 2);
for (int i=0; i<2; i++) {
points[i] = 10.0f;
}
glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
GLuint bufferObjectNameArray;
glGenBuffers(1, &bufferObjectNameArray);
glBindBuffer(GL_ARRAY_BUFFER, bufferObjectNameArray);
glBufferData(
GL_ARRAY_BUFFER,
sizeof(points),
points,
GL_STATIC_DRAW);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(
GLKVertexAttribPosition,
2,
GL_FLOAT,
GL_FALSE,
2*4,
NULL);
glDrawArrays(GL_POINTS, 0, 1);
Kindly help me out.
Edit:
Problem
Actually the problem is: I can't figure out what is the difference between:
GLfloat points[] =
{
10.0f, 10.0f,
};
AND
GLfloat *points = (GLfloat*)malloc(sizeof(GLfloat) * 2);
for (int i=0; i<2; i++) {
points[i] = 10.0f;
}
My bet will be that the problem is the sizeof(points) in the glBufferData data call: in this case it will return the size of the pointer which is a word i.e. 4 bytes (or something like this). You should pass the real size of your array that will be the same what you calculate in the malloc code.
Related
I'm drawing a few lines using OpenGL ES and I need to change their thickness from 1 pixel to 3 pixels smoothly, but glLineWidth doesn't allow to set line thickness between 1.0 and 2.0.
Is it possible?
Here is my code
- (void)setupGL
{
[EAGLContext setCurrentContext:self.context];
self.effect = [[GLKBaseEffect alloc] init];
glEnable(GL_DEPTH_TEST);
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(thinLines), thinLines, GL_STATIC_DRAW);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glBindVertexArrayOES(0);
}
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindVertexArrayOES(_vertexArray);
self.effect.constantColor = GLKVector4Make(lineR, lineG, lineB, 1.0f);
[self.effect prepareToDraw];
glLineWidth(1 + scaleQ);
glDrawArrays(GL_LINES, 0, thinLinesCount*2);
}
OpenGL ES (including 3.0) does not support antialiased lines. From documentation to glLineWidth:
The actual width is determined by rounding the supplied width to the
nearest integer.
So unfortunately you can't "smoothly" change line thickness.
I am trying and failing to draw a line with color using glkit / OpenGL Es2. I can draw the line, but it comes out as black.
My current approach is attempting to use a VBO containing both positional and color data.
Set-up:
typedef struct {
float Position[2];
float Color[4];
} LinePoint;
LinePoint line[] =
{
{{0.0, 0.0},{1.0,0.0,0.0,1}},
{{2.0, 3.0},{1.0,0.0,0.0,1}}
};
glGenBuffers(1, &_lineBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _lineBuffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(LinePoint)*2,line,GL_STATIC_DRAW);
In my drawInRect method:
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
[EAGLContext setCurrentContext:self.context];
[self.effect prepareToDraw];
glClearColor(0.5, 0.5, 0.5, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
glBindBuffer(GL_ARRAY_BUFFER, _lineBuffer);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition,2,GL_FLOAT,GL_FALSE,sizeof(LinePoint),(const GLvoid *) offsetof(LinePoint, Position));
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor,4,GL_FLOAT,GL_FALSE,sizeof(LinePoint),(const GLvoid *) offsetof(LinePoint, Color));
// Set the line width
glLineWidth(50.0);
// Render the line
glDrawArrays(GL_LINE_STRIP, 0, 2);
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
}
I'm having an issue where I have an open GL ES 2.0 based view that first draws to a texture and then renders that texture to the screen.
I instantiate this view as a subview of a main UIView with other buttons. The texture for some reason doesn't render immediately when drawn, rather only after one of the buttons is pressed.
Has anyone experienced anything like this? Thank you in advance
Uba Duba
Here is the drawing and rendering code:
// Drawings a line onscreen based on where the user touches
- (void)renderLineFromPoint:(CGPoint)start toPoint:(CGPoint)end
{
static GLfloat* vertexBuffer = NULL;
static NSUInteger vertexMax = 64;
NSUInteger vertexCount = 0,
count,
i;
[EAGLContext setCurrentContext:context];
glBindFramebuffer(GL_FRAMEBUFFER, fbTemp);
// Convert locations from Points to Pixels
CGFloat scale = self.contentScaleFactor;
start.x *= scale;
start.y *= scale;
end.x *= scale;
end.y *= scale;
// Allocate vertex array buffer
if(vertexBuffer == NULL)
vertexBuffer = malloc(vertexMax * 2 * sizeof(GLfloat));
// Add points to the buffer so there are drawing points every X pixels
count = MAX(ceilf(sqrtf((end.x - start.x) * (end.x - start.x) + (end.y - start.y) * (end.y - start.y)) / kBrushPixelStep), 1);
for(i = 0; i < count; ++i) {
if(vertexCount == vertexMax) {
vertexMax = 2 * vertexMax;
vertexBuffer = realloc(vertexBuffer, vertexMax * 2 * sizeof(GLfloat));
}
vertexBuffer[2 * vertexCount + 0] = start.x + (end.x - start.x) * ((GLfloat)i / (GLfloat)count);
vertexBuffer[2 * vertexCount + 1] = start.y + (end.y - start.y) * ((GLfloat)i / (GLfloat)count);
vertexCount += 1;
}
// Load data to the Vertex Buffer Object
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, vertexCount*2*sizeof(GLfloat), vertexBuffer, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(pointShader.positionLoc);
glVertexAttribPointer(pointShader.positionLoc, 2, GL_FLOAT, GL_FALSE, 0, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, brushTexture.id);
// Draw
glUseProgram(pointShader.programObject);
// the brush texture will be bound to texture unit 0
glUniform1i(pointShader.textureLoc, 0);
// viewing matrices
GLKMatrix4 projectionMatrix = GLKMatrix4MakeOrtho(0, backingWidth, 0, backingHeight, -1, 1);
GLKMatrix4 modelViewMatrix = GLKMatrix4Identity; // this sample uses a constant identity modelView matrix
GLKMatrix4 MVPMatrix = GLKMatrix4Multiply(projectionMatrix, modelViewMatrix);
glUniformMatrix4fv(pointShader.MVPLoc, 1, GL_FALSE, MVPMatrix.m);
// point size
glUniform1f(pointShader.pointSizeLoc, brushTexture.width / kBrushScale);
// initialize brush color
glUniform4fv(pointShader.vertexColorLoc, 1, brushColor);
glDrawArrays(GL_POINTS, 0, vertexCount);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);
[self drawResult];
}
and the drawResult function that does the rendering:
// Draw current result at the present render buffer.
- (void) drawResult
{
NSLog(#"Calling Draw Result");
// Bind the master frame buffer.
glBindFramebuffer(GL_FRAMEBUFFER, fbMaster);
// Clear the buffer.
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
// Draw the background board.
[self drawTexture:masterTexture ];
// Draw the temporary (drawing) board.
[self drawTexture:tempTexture];
// Bind the render buffer and present it to the view's context.
glBindRenderbuffer(GL_RENDERBUFFER, renderBuffer);
[context presentRenderbuffer:GL_RENDERBUFFER];
}
the draw texture function that binds the texture
// Draw a texture at the full screen quad
- (void) drawTexture:(GLuint) textureID
{
// Positions to draw a texture at the full screen quad.
const GLfloat vVerticesStraight[] = {
-1.0f, 1, 0.0f, // Position 0
0.0f, 1.0f, // TexCoord 0
-1.0f, -1.0f, 0.0f, // Position 1
0.0f, 0.0f, // TexCoord 1
1.0f, -1.0f, 0.0f, // Position 2
1.0f, 0.0f, // TexCoord 2
1.0f, 1.0f, 0.0f, // Position 3
1.0f, 1.0f // TexCoord 3
};
// Index order for draw element.
const GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
// Use the program object
glUseProgram ( drawTexture.programObject );
// Load the vertex position
glVertexAttribPointer ( drawTexture.a_positionLoc, 3, GL_FLOAT,
GL_FALSE, 5 * sizeof(GLfloat), vVerticesStraight );
// Load the texture coordinate
glVertexAttribPointer ( drawTexture.a_texCoordLoc, 2, GL_FLOAT,
GL_FALSE, 5 * sizeof(GLfloat), &vVerticesStraight[3] );
// Enable attribute for the vertex array.
glEnableVertexAttribArray ( drawTexture.a_positionLoc );
glEnableVertexAttribArray ( drawTexture.a_texCoordLoc );
// Bind the texture.
glActiveTexture(GL_TEXTURE0);
glBindTexture ( GL_TEXTURE_2D, textureID);
glUniform1i ( drawTexture.s_textureLoc, 0 );
// Draw the texture.
glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );
// Bind the current texture to the default texture.
glBindTexture(GL_TEXTURE_2D, 0);
}
There is no background threading, what's very strange is the first drawing seems to render to the texture but not to the screen, but after pressing another button on the view, the texture renders to the screen.
I am trying to combine 2D and 3D in OpenGL ES "2.0" and even though there are many questions here on openGL es 1.0 and some on 2.0 I am having trouble trying to figure this out. So for 2D I am going off of this tutorial: http://www.raywenderlich.com/9743/how-to-create-a-simple-2d-iphone-game-with-opengl-es-2-0-and-glkit-part-1 and for the 3D I am using the existing cube rotating xcode template...
I am getting EXC_BAD_ACCESS on the second glDrawArray (somehow its thinking that the original buffer still applies? anyway to unbind this before drawing the 2D texture?) error with the following render function. It works if I disable the lines:
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(0));
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(12));
What's going on? Thanks in advance.
[code]
glEnable(GL_DEPTH_TEST);
// glGenVertexArraysOES(1, &_vertexArrayNum);
// glBindVertexArrayOES(_vertexArrayNum);
glGenBuffers(1, &_vertexBufferNum);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferNum);
glBufferData(GL_ARRAY_BUFFER, sizeof(gCubeVertexData), gCubeVertexData, GL_STATIC_DRAW);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(0));
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(12));
glBindVertexArrayOES(0);
float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.1f, 100.0f);
self.effect.transform.projectionMatrix = projectionMatrix;
GLKMatrix4 baseModelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -4.0f);
// baseModelViewMatrix = GLKMatrix4Rotate(baseModelViewMatrix, _rotation, 0.0f, 1.0f, 0.0f);
// Compute the model view matrix for the object rendered with GLKit
// GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, 1.5f);
GLKMatrix4 modelViewMatrix = GLKMatrix4MakeRotation(_rotation, 0, 1, 0);
//modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, _rotation, 1.0f, 1.0f, 1.0f);
modelViewMatrix = GLKMatrix4Multiply(baseModelViewMatrix, modelViewMatrix);
self.effect.transform.modelviewMatrix = modelViewMatrix;
glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindVertexArrayOES(_vertexArrayNum);
// Render the object with GLKit
// [self.effect prepareToDraw];
// glDrawArrays(GL_TRIANGLES, 0, 36);
modelViewMatrix = GLKMatrix4MakeScale(2.0f, 2.0f, 2.0f);
projectionMatrix = GLKMatrix4MakeOrtho(0, 480, 0, 320, -1024, 1048);
self.effect.transform.projectionMatrix = projectionMatrix;
self.effect.transform.modelviewMatrix = modelViewMatrix;
[self.effect prepareToDraw];
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
[self.player render];
[/code]
UPDATE:
Added glEnableClientState(GL_VERTEX_ARRAY); wrapped around the cube drawing part... which got rid of the bad access error, but neither the cube nor the sprite is being drawn (unless of couse I comment out the code part altogether)...
UPDATE2:
so the problen is obviously that after i call glDrawArray to draw the cube (which works fine)... I call glDrawArray again as follows. But somehow its still trying to render previous array?
// 1
self.effect.texture2d0.name = self.textureInfo.name;
self.effect.texture2d0.enabled = YES;
// 2
[self.effect prepareToDraw];
// 3
glEnableVertexAttribArray(GLKVertexAttribPosition);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
// 4
long offset = (long)&_quad;
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void *) (offset + offsetof(TexturedVertex, geometryVertex)));
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void *) (offset + offsetof(TexturedVertex, textureVertex)));
// 5
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
Stupid me. Works as soon as I do:
glBindBuffer(GL_ARRAY_BUFFER, 0);
to clear the buffer. I kept looking for glUnbind something... forgot openGL works off of memory references... The 2D texutre is now applying to the cube, but I am sure thats an easy fix :).... That you all for the help >:o
Texture issue fixed :)
I have been struggling to find a solution for a simple masking using Stencil Buffer in OPEN GL 1.0 on the iOs.
I draw the background and then I draw on top of it using glPushMatrix a frame which is drawn using drawFrame.
My frame is made of a VideoFrameTexture which I try to mask using maskTexture using the stencil buffer. My objective is to draw only part of the videoFrameTexture (which is defined by the white area of my mask and which corresponds to a circle shape) onto the background.
I use the following code but the mask image is not taken into account. It's a Black an white png.
-(void) drawFrame{
GLfloat vertexes[] =
{
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f
};
GLshort texes[] = {
1, 1,
0, 1,
0, 0,
1, 0,
};
glColor4f(1.0, 1.0, 1.0, 1.0);
glVertexPointer(3, GL_FLOAT, 0, vertexes);
glEnableClientState(GL_VERTEX_ARRAY);
glTexCoordPointer(2, GL_SHORT, 0, texes);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_DEPTH_TEST);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glEnable(GL_STENCIL_TEST);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
glStencilFunc(GL_ALWAYS, 1, 0xffffffff);
/// Mask Drawing
glBindTexture(GL_TEXTURE_2D, maskTexture);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glEnable(GL_DEPTH_TEST);
glStencilFunc(GL_EQUAL, 1, 0xffffffff); /* draw if ==1 */
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
///Frame Drawing
glBindTexture(GL_TEXTURE_2D, videoFrameTexture);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glDisable(GL_STENCIL_TEST);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glDeleteTextures(1, &videoFrameTexture);
}
I draw my view using the following code:
- (void) drawView
{
[EAGLContext setCurrentContext:context];
//glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT| GL_STENCIL_BUFFER_BIT);
glOrthof(-1.0f, 1.0f, -1.5f, 1.5f, -10.0f, 10.0f);
glViewport(0, 0, backingWidth, backingHeight);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glScalef(1, 0.75, 1);
glTranslatef(0, -1, 0);
glRotatef(90, 0.0f, 0.0f, 1.0f);
[self drawBackground];
glPopMatrix();
glPushMatrix();
glScalef(1, 1, 1);
glTranslatef(0, 1, 0);
glRotatef(90, 0.0f, 0.0f, 1.0f);
[self drawFrame];
glPopMatrix();
//present frame buffer
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
}
Stencil buffer is not available for 1.0. Only 2.0