How to use glClearBuffer* in Cocoa OpenGLView to clear color buffer - objective-c

I'm trying to understand how to use glClearBuffer* to change the background color in a (either single or double buffered) NSOpenGLView in Cocoa for OS X.
The following code fragment as suggested by the OpenGL Superbible fails with GL_INVALID_OPERATION:
GLfloat red[] = {1.0f, 0.0f, 0.0f, 1.0f};
glClearBufferfv(GL_COLOR, 0, red);
What do I need to supply for the second parameter?
I'm using a double buffered View extending OpenGLView.
#import "MyOpenGLView.h"
#include <OpenGL/gl3.h>
#implementation MyOpenGLView
-(void) drawRect: (NSRect) bounds
{
GLfloat red[] = {1.0f, 0.0f, 0.0f, 1.0f};
glClearBufferfv(GL_COLOR, 0, red);
GLenum e = glGetError();
// e == GL_INVALID_OPERATION after this call
// and the view is drawn in black
// The following two lines work as intended:
//glClearColor(1.0, 0.f, 0.f, 1.f);
//glClear(GL_COLOR_BUFFER_BIT);
[[self openGLContext] flushBuffer];
}
#end

Really? It is giving you GL_INVALID_OPEARATION?
This function is not supposed to generate that error... are you sure something earlier in your program did not create the error and you are mistaking the source?
The bigger problem however, is that using GL_COLOR as the buffer in this API call expects the second parameter to be an index into your set of draw buffers. It is unclear how your draw buffers are setup in this code, it is possible that you have GL_NONE. As there is no defined error behavior if you try to clear a draw buffer when GL_NONE is used, I suppose an implementation might choose to raise GL_INVALID_OPERATION.
In order for your current usage of glClearBufferfv (...) to make sense, I would expect to see something like this:
GLenum buffers [] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 };
GLfloat red [] = { 1.0f, 0.0f, 0.0f, 1.0f };
glDrawBuffers (2, buffers);
glClearBufferfv (GL_COLOR, 0, red);
Now this call will clear GL_COLOR_ATTACHMENT0, if you wanted to clear GL_COLOR_ATTACHMENT1, you could replace 0 with 1.

Related

Draw different shapes on NSOpenGLView in different NSWindow

I work on AudioVisualizer in objective c on Mac OS.
I have 2 NSWindows what has NSOpenGLView in it.
If open only 1 NSWindow with NSOpenGLView, it shows shapes without any issues.
But if open 2 NSWindows what has NSOpenGLView on it, only one GLView draws a shape, but the shape is not matched with its audio when play other audio in other NSWindow.
In NSOpenGLViews, it calls redraw method exactly whenever CADisplayLink needs display.
Here is the part related with OpenGLContext.
self.openGLContext = [[NSOpenGLContext alloc] initWithFormat:self.pixelFormat
shareContext:nil];
And here is redraw method
[self.openGLContext makeCurrentContext];
[self.openGLContext lock];
[self redrawWithPoints:self.info->points
pointCount:self.info->pointCount
baseEffect:self.baseEffect
vertexBufferObject:self.info->vbo
vertexArrayBuffer:self.info->vab
interpolated:self.info->interpolated
mirrored:self.shouldMirror
gain:self.gain];
[self.openGLContext flushBuffer];
[self.openGLContext unlock];
[NSOpenGLContext clearCurrentContext];
And here is redrawWithPoints Method
glClear(GL_COLOR_BUFFER_BIT);
GLenum mode = interpolated ? GL_TRIANGLE_STRIP : GL_LINE_STRIP;
float interpolatedFactor = interpolated ? 2.0f : 1.0f;
float xscale = 2.0f / ((float)pointCount / interpolatedFactor);
float yscale = 1.0f * gain;
GLKMatrix4 transform = GLKMatrix4MakeTranslation(-1.0f, 0.0f, 0.0f);
transform = GLKMatrix4Scale(transform, xscale, yscale, 1.0f);
baseEffect.transform.modelviewMatrix = transform;
glBindBuffer(GL_ARRAY_BUFFER, vbo);
[baseEffect prepareToDraw];
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition,
2,
GL_FLOAT,
GL_FALSE,
sizeof(GOAudioPlotGLPoint),
NULL);
glDrawArrays(mode, 0, pointCount);
if (mirrored)
{
baseEffect.transform.modelviewMatrix = GLKMatrix4Rotate(transform, M_PI, 1.0f, 0.0f, 0.0f);
[baseEffect prepareToDraw];
glDrawArrays(mode, 0, pointCount);
}
glFlush();
I want to show different Audio Visualizers in different NSOpenGLViews at same time.
Is it possible to do with OpenGL?
You're drawing using a vertex buffer object and vertex array buffer, but you don't appear to be sharing between the two contexts. That means that when you use the vertex buffer object in the second context, its ID doesn't exist in that context. However, if you use the first context as the shareContext: parameter to the second view's creation of the NSOpenGLContext, then you can share objects between contexts.
It can help to add calls to glGetError() periodically in your code to alert you to issues like this.
Based on reply of #user1118321, I made NSOpenGLContextManager for managing OpenglContext.
#interface GOOpenGLContextManager : NSObject
+ (GOOpenGLContextManager*) shared;
#property (nonatomic, strong) NSOpenGLContext* context;
#end
By using it, in All NSOpenGLViews, used only one shared NSOpenGLContext.
Here is the code what I have used.
self.openGLContext = [[NSOpenGLContext alloc] initWithFormat:self.pixelFormat
shareContext:[OpenGLContextManager shared].context];
if ([OpenGLContextManager shared].context == nil) {
[OpenGLContextManager shared].context = self.openGLContext;
}
It works like a charm.

glDrawElements with Stack Memory

I have two arrays that I declared on the stack in a function, and verified that they both contain the exact same data:
/// Rasters the textured quad using the specified parameters.
- (void)privateConfigureRasterWithTexture:(GLuint)theTexture
bottomLeftX:(GLfloat)bottomLeftX
bottomLeftY:(GLfloat)bottomLeftY
topRightX:(GLfloat)topRightX
topRightY:(GLfloat)topRightY
{
const GLfloat texices[] =
{ bottomLeftX, bottomLeftY, // bottom left corner
bottomLeftX, topRightY, // top left corner
topRightX, topRightY, // top right corner
topRightX, bottomLeftY }; // bottom right corner
const GLfloat texices2[] =
{ 0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f };
for(int x=0;x<8;x++)
if(texices[x] != texices2[x])
{
NSLog(#"Mismatch!");
abort();
}
When I execute the following line of code in (the bottom of) that function
glVertexAttribPointer(_attributeTexturedTexCoords, 2, GL_FLOAT, GL_FALSE,
2*sizeof(GLfloat), texices2);
I get the expected result, however if I instead execute
glVertexAttribPointer(_attributeTexturedTexCoords, 2, GL_FLOAT, GL_FALSE,
2*sizeof(GLfloat), texices);
I get a bad result. I don't get what the difference is.
Edit: Here is how I'm invoking this function.
[self privateConfigureRasterWithTexture:_atlasTexture1
bottomLeftX:0.0f
bottomLeftY:0.0f
topRightX:1.0f
topRightY:1.0f];
Nevermind, I think I've found it (probably at around the exact same time Martins did). It looks like glVertexAttribPointer doesn't make a copy of the data until the call to glDrawElements. Since my glDrawElements is outside of that function, the stack memory was being released (yet for some reason texices2 remained valid).
I've rewritten the code a bit so that texices is heap allocated once when that class is allocated, and just reused over and over again and free'd when the application deallocs.

Draw multiple objects with textures

I want to draw cubes using textures.
void OperateWithMainMatrix(ESContext* esContext, GLfloat offsetX, GLfloat offsetY, GLfloat offsetZ) {
UserData *userData = (UserData*) esContext->userData;
ESMatrix modelview;
ESMatrix perspective;
//Manipulation with matrix
...
glVertexAttribPointer(userData->positionLoc, 3, GL_FLOAT, GL_FALSE, 0, cubeFaces);
//in cubeFaces coordinates verticles cube
glVertexAttribPointer(userData->normalLoc, 3, GL_FLOAT, GL_FALSE, 0, cubeFaces);
//for normals (use in fragment shaider for textures)
glEnableVertexAttribArray(userData->positionLoc);
glEnableVertexAttribArray(userData->normalLoc);
// Load the MVP matrix
glUniformMatrix4fv(userData->mvpLoc, 1, GL_FALSE,
(GLfloat*)&userData->mvpMatrix.m[0][0]);
//Bind base map
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, userData->baseMapTexId);
//Set the base map sampler to texture unit to 0
glUniform1i(userData->baseMapLoc, 0);
// Draw the cube
glDrawArrays(GL_TRIANGLES, 0, 36);
}
(coordinates transformation is in OperateWithMainMatrix() )
Then Draw() function is called:
void Draw(ESContext *esContext)
{
UserData *userData = esContext->userData;
// Set the viewport
glViewport(0, 0, esContext->width, esContext->height);
// Clear the color buffer
glClear(GL_COLOR_BUFFER_BIT);
// Use the program object
glUseProgram(userData->programObject);
OperateWithMainMatrix(esContext, 0.0f, 0.0f, 0.0f);
eglSwapBuffers(esContext->eglDisplay, esContext->eglSurface);
}
This work fine, but if I try to draw multiple cubes (next code for example):
void Draw(ESContext *esContext)
{ ...
// Use the program object
glUseProgram(userData->programObject);
OperateWithMainMatrix(esContext, 2.0f, 0.0f, 0.0f);
OperateWithMainMatrix(esContext, 1.0f, 0.0f, 0.0f);
OperateWithMainMatrix(esContext, 0.0f, 0.0f, 0.0f);
OperateWithMainMatrix(esContext, -1.0f, 0.0f, 0.0f);
OperateWithMainMatrix(esContext, -2.0f, 0.0f, 0.0f);
eglSwapBuffers(esContext->eglDisplay, esContext->eglSurface);
}
A side faces overlapes frontal face. This process is illustrated on image:
Alternate picture (with colours and clean image):
The side face of the right cube overlaps frontal face of the center cube.
How can i remove this effect and display miltiple cubes without it?
To fix this you need to utilize what's known as the depth buffer. This is what's responsible for making sure that surfaces don't get drawn overtop of surfaces that are nearer (like the side of a cube showing over the front of a cube).
Luckily it's not much work involved to do so:
Enable depth testing at initialization with glEnable(GL_DEPTH_TEST)
Clear depth buffer on each frame by adding it's bit to the glClear call:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
After this you should no longer see your surfaces popping on top of nearer surfaces.

glColor4f Stops Textures From Loading Using GLKTextureLoader

I'm currently using OpenGL ES 2.0 under GLKView on the new iPad. It seems, whenever I make the call to glColor4f, nothing happens (ie. it doesn't color the polygons I want it to) other than causing a GL_INVALID_OPERATION error. Then, as soon as I try to load a texture, I get this error message:
Error loading file: The operation couldn't be completed. (GLKTextureLoaderErrorDomain error 8.)
But before the glColor4f is called, everything works fine. Here's my code below for drawInRect:
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Render the object with GLKit
self.effect.texture2d0.enabled = NO;
self.effect.transform.modelviewMatrix = _defaultModelMatrix;
[self.effect prepareToDraw];
// Render the grid
glBindBuffer(GL_ARRAY_BUFFER, _gridVertexBuffer);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, 0);
glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
glDrawArrays(GL_TRIANGLES, 0, TRIANGLES * 6);
glDisableVertexAttribArray(GLKVertexAttribPosition);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
If anyone can help, I'd really appreciate it. Some things to point out though. The object I'm trying to color is stored in a VBO and is rendered fine, in a white color (even though I'm trying to color it red).
Thanks
glColor4f is not an OpenGLES2.0 command (it's from GLES1.1)
What you want to do is declare a uniform in your shader called 'color' (or something similar), set that uniform as you typically would with any shader uniform, and multiply your fragment color with that uniform before writing the pixel.

OpenGL behaving strangely

OpenGL is acting very strangely for some reason. In my subclass of NSOpenGLView, I have the following code in the -prepareOpenGL method:
- (void)prepareOpenGL {
GLfloat lightAmbient[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat lightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat lightPosition[] = { 0.0f, 0.0f, 2.0f };
quality = 0;
zCoord = -6;
[self loadTextures];
glEnable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.2f, 0.2f, 0.2f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glLightfv(GL_LIGHT1, GL_AMBIENT, lightAmbient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightDiffuse);
glLightfv(GL_LIGHT1, GL_POSITION, lightPosition);
glEnable(GL_LIGHT1);
gameState = kGameStateRunning;
int i = 0; // HERE ********
[NSTimer scheduledTimerWithTimeInterval:0.03f target:self
selector:#selector(processKeys) userInfo:nil repeats:YES];
// Synchronize buffer swaps with vertical refresh rate
GLint swapInt = 1;
[[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
// Setup and start displayLink
[self setupDisplayLink];
}
I wanted to assign the timer that processes key input to an ivar so that I could invalidate it when the game is paused (and reinstantiate it on resume), however when I did that (as apposed to leaving it at [NSTimer scheduledTimer…), OpenGL doesn't display the cube I draw. When I take it away, it's fine. So i tried just adding a harmless statement, int i = 0; (maked // HERE *******), and that makes the lighting not work! When I take it away, everything is fine, but when I put it back, everything is dark. Can someone come up with a rational explanation for this? Just creating any variable makes it do this, even though there are already several declared.
Thanks.
Very strange, but I found the problem. In this line:
GLfloat lightPosition[] = { 0.0f, 0.0f, 2.0f };
I have only 3 elements instead of 4. I have no idea why it worked when I didn't have int i = 0;, but when I add 0.0f to the array everything works normally.