Blending in Different OpenGL editions - opengl

Below is a piece of code I use to achieve a demo about how blending works:
glDisable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
glBegin(GL_QUADS);
glColor4f(1.0f, 0.0f, 0.0f, 0.5f);
glVertex3i(2, 0, 0);
glVertex3i(2, 6, 0);
glVertex3i(6, 6, 0);
glVertex3i(6, 0, 0);
glEnd();
glEnable(GL_BLEND);
glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA);
glBegin(GL_QUADS);
glColor4f(0.0, 1.0, 0.0, 0.5f);
glVertex3i(3, 2, -1);
glVertex3i(3, 8, -1);
glVertex3i(8, 8, -1);
glVertex3i(8, 2, -1);
glEnd();
The problem is: It shows what I want on my laptop, which means that the intersection of the two quads is blended, and the area of the green quad left out on black background also blended with background whose alpha is 0.0. However, on another PC, only the red quad appears...
The OpenGL on the laptop is 2.0, and the one on the PC is over 4.0. I want to know whether the problem is the edition of OpenGL or not.
BTW: I know the order I should follow when I want to draw a translucent and an opaque object; I only use this demo to show how much trouble there will be if we do not follow it...

Related

How come we are looking at negative z by default in openGL?

I have this code
glColor3f(1, 0, 0);// red quad
glBegin(GL_QUADS);
glVertex3f(-1, 0, -0.1);
glVertex3f(1, 0, -0.1);
glVertex3f(1, 1, -0.1);
glVertex3f(-1, 1, -0.1);
glEnd();
glColor3f(0, 1, 0); //green quad
glBegin(GL_QUADS);
glVertex3f(-1, 0, -0.2);
glVertex3f(1, 0, -0.2);
glVertex3f(1, 1, -0.2);
glVertex3f(-1, 1, -0.2);
glEnd();
glutSwapBuffers();
Using default projection matrix, the one that appears is my green quad.
If we're looking to negative z (from 1 to -1), shouldn't the green quad behind the red quad?
All matrices in compatibility mode OpenGL start off as identity matrices; they don't apply any transformations.
In Normalized Device Coordinates, +Z is into the window; you're looking at +Z. Matrices and shaders can, of course, change this.
Also make sure that depth testing is enabled and you create your window with a depth buffer.
If red quad is outside frustum's near and far plane then your red quad will not be visible because it gets clipped out. More information

Why would glShadeModel(GL_FLAT) not work?

To my understanding, GL_FLAT is supposed to make a face a single color. I can't get GL_FLAT to actually work though; it's as though my program is only using GL_SMOOTH. I'm using light sources, but both look the same.
The triangle should be just one color. Here's my scene rendering code. I no where mention GL_SMOOTH, and I've tried GL_FLAT everywhere (inits, main, before light/material definitions, before drawing, while drawing, after drawing, etc.)
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_LIGHTING);
//Light
glEnable(GL_LIGHT1);
//Define Light source ...
//Define Material ...
//Draw Triangle
glShadeModel(GL_FLAT);
glNormal3f(0,0,1);
glBegin(GL_POLYGON);
glVertex3f( -.5, -.5, 0);
glVertex3f( 0, .5, 0);
glVertex3f( .5, -.5, 0);
glEnd();
glFlush();
That's it. Everything else in the program is the bare minimum it needs to run. Is GL_FLAT broken or something?

SDL2 + OpenGL colored geometry

I want to use OpenGL to draw on top of a webcam stream. I'm using an SDL_Surface named screen_surface_ containing webcam data, that I'm rendering to the screen using (1):
SDL_UpdateTexture(screen_texture_, NULL, screen_surface_->pixels, screen_surface_->pitch);
SDL_RenderClear(renderer_);
SDL_RenderCopy(renderer_, screen_texture_, NULL, NULL);
Then I try to draw some geometry on top:
glLoadIdentity();
glColor3f(1.0, 0.0, 1.0);
glBegin( GL_QUADS );
glVertex3f( 10.0f, 50.0f, 0.0f ); /* Top Left */
glVertex3f( 50.0f, 50.0f, 0.0f ); /* Top Right */
glVertex3f( 50.0f, 10.0f, 0.0f ); /* Bottom Right */
glVertex3f( 10.0f, 10.0f, 0.0f ); /* Bottom Left */
glEnd( );
glColor3f(1.0, 1.0, 1.0); //<- I need this to make sure the webcam stream isn't pink?
SDL_RenderPresent(renderer_);
I have initialized OpenGL using (excerpt):
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glViewport( 0, 0, res_width_, res_height_ );
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho(0.0f, res_width_, res_height_, 0.0f, -1.0f, 1.0f);
Subquestion: If I don't reset the glColor to white the whole webcam stream is colored pink. I find this odd, because I thought that SDl_RenderCopy had already rendered that texture before the first call to glColor. So how does SDL_RenderCopy actually work?
Main question: I get a neat 40x40 square in the top left of the screen on top of my webcam feed (good!). However, in stead of pink, it is a kind of flickering dark purple color; seemingly dependent on the camera feed in the background. Could you please tell me what I'm overlooking?
Edit:
As per #rodrigo's comment, these are some images with the color set to R, G, B and white, respectively:
Red Square
Green Square
Blue Square
White Square
Looking at these, it seems that the underlying texture has some effect on the color. Could it be that OpenGL is still applying (some part of) the texture to the quad?
Edit:
I suspect now that the geometry is drawn using the render texture as a texture, even though I've called glDisable(GL_TEXTURE_2D). Looking at the "White Square" screenshot (below), you can see that the white quad is the same color as the bottom-right pixel. I guess that the quad has no texture coordinates, so only the bottom-right texel is used. Knowing this, better question: how do I disable texturing?.
I have fixed the problem by simply adding
glcontext_ = SDL_GL_CreateContext(window_);
to the SDL_Init code. I think all my calls to openGL functions (like glDisable(GL_TEXTURE_2D) were applied to the wrong context. Explicitly creating a context from SDL sets the right context to be active, I'm guessing.
Something else to look out for: when using textured geometry after using SDL_RenderCopy; I had to make sure to reset
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
before calling glTexImage2d, because it uses
GL_UNPACK_ROW_LENGTH if it is greater than 0, the width argument to the pixel routine otherwise
(from the OpenGL docs)

qt + open Gl= paintgl function for drawing a triangle

i have coded what i have read in previous post about a well known subject but i continue to get a green window without any triangle inside.
Here is my paint function :
void mGLWidget::paintGL()
{
glClearColor( Qt::green );
QSize viewport_size = size();
glViewport(0, 0, viewport_size.width(), viewport_size.height());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1, 1, -1, 1, 5, 7); // near and far match your triangle Z distance
glMatrixMode(GL_MODELVIEW);
glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
glColor4f( 1.0, 1.0, 1.0, 1.0 );
glTranslatef(-1.5f,0.0f,-15.0f);
glBegin(GL_TRIANGLES); // Drawing Using Triangles
glVertex3f( 0.0f, 1.0f, 0.0f); // Top
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glEnd();
// Finished Drawing The Triangle
// swapBuffers();
}
I only get a green window without any triangle. My QT is qt4.8 and opengl 4.0.
What am I doing wrong ?-\
your near and far value for your glfrustrum is quite a small range, make it 1, 100 instead of 5, 7. might not be the actual solution but it will help if its not
Your near and far value is small and you are translating the triangle very far by -15f unit that means your triangle is not draw or act as a point. So you only see the window color.

What's wrong with my usage of glBindTexture?

I am rendering a chess board, using 2 different textures. One for the black squares and one for the white squares. However instead of each different square having their own texture, they all take on the last texture that I bound calling glBindTexture(GL_TEXTURE_2D, id);.
This is my approach:
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
// square 0, 0 ( front left )
glBindTexture(GL_TEXTURE_2D, textureBlackSquare->texID);
glNormal3f(0, 1, 0);
glTexCoord2f(0, 0); glVertex3f(-8.0, 0.5, 8.0);
glTexCoord2f(1, 0); glVertex3f(-6.0, 0.5, 8.0);
glTexCoord2f(1, 1); glVertex3f(-6.0, 0.5, 6.0);
glTexCoord2f(0, 1); glVertex3f(-8.0, 0.5, 6.0);
glEnd();
glBegin(GL_QUADS);
// square 1, 0
glBindTexture(GL_TEXTURE_2D, textureWhiteSquare->texID);
glTexCoord2f(0, 0); glVertex3f(-6.0, 0.5, 8.0);
glTexCoord2f(1, 0); glVertex3f(-4.0, 0.5, 8.0);
glTexCoord2f(1, 1); glVertex3f(-4.0, 0.5, 6.0);
glTexCoord2f(0, 1); glVertex3f(-6.0, 0.5, 6.0);
glEnd();
When I run this code, both quads have the white texture bound. How do I get each quad to have its own texture?
You cannot call glBindTexture in the middle of glBegin/End. You can only call vertex functions within begin/end.
Also, why don't you just make a single texture as an 8x8 checkerboard, and then just render a single quad to draw the whole checkerboard?
From the documentation:
GL_INVALID_OPERATION is generated if glBindTexture is executed between
the execution of glBegin and the corresponding execution of glEnd.
You forgot to check for errors, and thus missed that your program is invalid.
You can't bind a texture within a glBegin-glEnd block. Also you should avoid switching textures where possible, since switching the texture is among the most expensive things you can ask the GPU to do (a texture switch invalidates all texel fetch caches).
Instead you sort your scene objects by the texture they use and group them by this. So you first render all checkerboard quads using the first texture (say white), and after that all the quads using the second texture (black then).