How do I set the opacity of a vertex in OpenGL? - c++

The following snippet draws a gray square.
glColor3b(50, 50, 50);
glBegin(GL_QUADS);
glVertex3f(-1.0, +1.0, 0.0); // top left
glVertex3f(-1.0, -1.0, 0.0); // bottom left
glVertex3f(+1.0, -1.0, 0.0); // bottom right
glVertex3f(+1.0, +1.0, 0.0); // top right
glEnd();
In my application, behind this single square exists a colored cube.
What function should I use to make square (and only this square) opaque?

In the init function, use these two lines:
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
And in your render function, ensure that glColor4f is used instead of glColor3f, and set the 4th argument to the level of opacity required.
glColor4f(1.0, 1.0, 1.0, 0.5);
glBegin(GL_QUADS);
glVertex3f(-1.0, +1.0, 0.0); // top left
glVertex3f(-1.0, -1.0, 0.0); // bottom left
glVertex3f(+1.0, -1.0, 0.0); // bottom right
glVertex3f(+1.0, +1.0, 0.0); // top right
glEnd();

glColor4f(float r,float g, float b, flaot alpha);
(in your case maybe clColor4b)
also make sure, that blending is enabled.
(you have to reset the color to non-alpha afterwads, which might involve a glGet* to save the old vertexcolor)

You can set colors per vertex
glBegin(GL_QUADS);
glColor4f(1.0, 0.0, 0.0, 0.5); // red, 50% alpha
glVertex3f(-1.0, +1.0, 0.0); // top left
// Make sure to set the color back since the color state persists
glVertex3f(-1.0, -1.0, 0.0); // bottom left
glVertex3f(+1.0, -1.0, 0.0); // bottom right
glVertex3f(+1.0, +1.0, 0.0); // top right
glEnd();

Use glColor4 instead of glColor3. For example:
glBlendFunc(GL_SRC_ALPHA,GL_ONE);
glColor4f(1.0f,1.0f,1.0f,0.5f);

Related

Applying 2d texture to single cube face with change in z direction in opengl

I am trying to apply a texture to 1 face of a cube, but the problem I have is the cube is in the z direction as well.
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, Tex);
glBegin(GL_POLYGON);
glTexCoord2f( 1.0, 1.0); glVertex3f( 1.0, 1.0, 1);
glTexCoord2f( 1.0, -1.0); glVertex3f( 1.0, -1.0, 1);
glTexCoord2f( 1.0, -1.0); glVertex3f( 1.0, -1.0, -1);
glTexCoord2f( 1.0, 1.0); glVertex3f( 1.0, 1.0, -1);
glEnd();
glDisable(GL_TEXTURE_2D);
Obviously by doing it this way I end up with repeated texture vertices (1,1) and (1,-1) because they don't include the z axis. How can I resolve this issue?
It is not necessary that the texture coordinates are associated to the x and y component of the vertex coordinates or even have to be the same. The texture coordinates are completely independent.
You have to define how the 2 dimensional texture is wrapped on a surface in 3 dimensional space. For each side of the cube you've to define individual texture coordinates.
Chang the texture coordinates. e.g.:
glBegin(GL_POLYGON);
glTexCoord2f( 1.0, 1.0); glVertex3f( 1.0, 1.0, 1.0);
glTexCoord2f( 1.0, 0.0); glVertex3f( 1.0, -1.0, 1.0);
glTexCoord2f( 0.0, 0.0); glVertex3f( 1.0, -1.0, -1.0);
glTexCoord2f( 0.0, 1.0); glVertex3f( 1.0, 1.0, -1.0);
glEnd();

How do I correctly draw 3D axis in OpenGL?

I am trying to draw the 3D axis. I have also a cone which is rotating around its vertex and I would want the axis starting from there.
Here I have my function to draw the cone and I wrote the functions to draw the axis after gluLookAt:
GLfloat xRotated, yRotated, zRotated;
// Cone
GLdouble base=0.5;
GLdouble height=1.3;
GLint slices =20;
GLint stacks =20;
std::vector<std::array<GLfloat, 3>> data;
void displayCone(void)
{
// set matrix mode
glMatrixMode(GL_MODELVIEW);
// clear model view matrix
glLoadIdentity();
// multiply view matrix to current matrix
gluLookAt(0,2.,0.,0.,0.,-4.5,0,1,0); // <----------------------- add
// ******
glPushMatrix();
glLoadIdentity();
glTranslatef(0.0, 0.0, -4.5);
glBegin(GL_LINES);
glColor3f (1.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(2.0, 0.0, 0.0);
glColor3f (1.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 2.0, 0.0);
glColor3f (1.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 2.0);
glEnd();
glPopMatrix();
// ******
// clear the drawing buffer.
glClear(GL_COLOR_BUFFER_BIT);
// traslate the draw by z = -4.0
// Note this when you decrease z like -8.0 the drawing will looks far , or smaller.
glTranslatef(0.0,0.0,-4.5);
// Red color used to draw.
glColor3f(0.8, 0.2, 0.1);
// changing in transformation matrix.
// rotation about X axis
glRotatef(xRotated,1.0,0.0,0.0);
// rotation about Y axis
glRotatef(yRotated,0.0,1.0,0.0);
// rotation about Z axis
glRotatef(zRotated,0.0,0.0,1.0);
// scaling transfomation
glScalef(1.0,1.0,1.0);
// built-in (glut library) function , draw you a Cone.
// move the peak of the cone to the origin
glTranslatef(0.0, 0.0, -height);
glutSolidCone(base,height,slices,stacks);
// Flush buffers to screen
// gluLookAt(3,3,3,0,0,-4.5,0,1,0); <----------------------- delete
glFlush();
// sawp buffers called because we are using double buffering
// glutSwapBuffers();
}
I thought to draw the 3D axis in this way but I am making some mistakes.
What am I missing?
The lies are cleared immediately after they've been drawn by glClear. Do glClear(GL_COLOR_BUFFER_BIT); at the begin of displayCone:
If you want to draw the lines in view space, then the view matrix has to be set. Remove glLoadIdentity before drawing the lines.
e.g.
void displayCone(void)
{
// clear the drawing buffer.
glClear(GL_COLOR_BUFFER_BIT); // <---- add
// set matrix mode
glMatrixMode(GL_MODELVIEW);
// clear model view matrix
glLoadIdentity();
// multiply view matrix to current matrix
gluLookAt(3.0, 3.0, 3.0-4.5, 0.0, 0.0,-4.5,0,1,0);
// ******
glPushMatrix();
// glLoadIdentity(); <---- delete
glTranslatef(0.0, 0.0, -4.5);
glBegin(GL_LINES);
glColor3f (1.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(2.0, 0.0, 0.0);
glColor3f (1.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 2.0, 0.0);
glColor3f (1.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 2.0);
glEnd();
glPopMatrix();
// clear the drawing buffer.
// glClear(GL_COLOR_BUFFER_BIT); // <---- delete
// traslate the draw by z = -4.0
// Note this when you decrease z like -8.0 the drawing will looks far , or smaller.
glTranslatef(0.0,0.0,-4.5);
// Red color used to draw.
glColor3f(0.8, 0.2, 0.1);
// changing in transformation matrix.
// rotation about X axis
glRotatef(xRotated,1.0,0.0,0.0);
// rotation about Y axis
glRotatef(yRotated,0.0,1.0,0.0);
// rotation about Z axis
glRotatef(zRotated,0.0,0.0,1.0);
// scaling transfomation
glScalef(1.0,1.0,1.0);
// built-in (glut library) function , draw you a Cone.
// move the peak of the cone to the origin
glTranslatef(0.0, 0.0, -height);
glutSolidCone(base,height,slices,stacks);
// Flush buffers to screen
// gluLookAt(3,3,3,0,0,-4.5,0,1,0); <----------------------- delete
glFlush();
// sawp buffers called because we are using double buffering
// glutSwapBuffers();
}

Objects shake when rotating

I'm facing a problem in my opengl code
I'm trying to build a house, and rotate it 360°, for simplicity let's assume the house has the front wall with window and dor, and a back wall.
I'm using DEPTH_BUFFER not to see the back wall when viewing the front wall, and the other way around, but when I rotate the house the door and window start to shake and get distorced.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);
glLoadIdentity();
gluLookAt(0.0, 0.0, 40.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glRotatef(angle, 0.0, 1.0, 0.0);
glEnable(GL_DEPTH_TEST);
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(8.0, 3.0, 0.0);
glVertex3f(8.0, -10.0, 0.0);
glVertex3f(1.0, -10.0, 0.0);
glVertex3f(1.0, 3.0, 0.0);
glEnd();
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(-9.0, -4.0, 0.0);
glVertex3f(-9.0, 3.0, 0.0);
glVertex3f(-2.0, 3.0, 0.0);
glVertex3f(-2.0, -4.0, 0.0);
glEnd();
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(10.0, -10.0, -20.0);
glVertex3f(-10.0, -10.0, -20.0);
glVertex3f(-10.0, 10.0, -20.0);
glVertex3f(10.0, 10.0, -20.0);
glEnd();
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(10.0, -10.0, 0.0);
glVertex3f(10.0, 10.0, 0.0);
glVertex3f(-10.0, 10.0, 0.0);
glVertex3f(-10.0, -10.0, 0.0);
glEnd();
glDisable(GL_DEPTH_TEST);
glutSwapBuffers();
The issue is called Z-fighting. This is caused, because depth of the "door", "window" and "wall" are equal. The vertiex coordinates are transformed by the model view matrix and projection matrix and interpolated for each fragment which is covered by the polygon. This results in inaccuracies of the final z coordinate (depth).
Enable the polygon fill offset (glPolygonOffset) by before drawing the walls, to solve the issue:
glEnable(GL_DEPTH_TEST);
glDisable( GL_POLYGON_OFFSET_FILL );
// draw door and window
// ...
glEnable( GL_POLYGON_OFFSET_FILL );
glPolygonOffset( 1, 1 );
// draw walls
// ...
Polygon fill offset manipulates the depth of a fragment by a minimum amount. This causes that the depth of the "walls" is different to the depth of the "window" and "door", even after the transformation by the model view and projection matrix.
Since an offset is added to the depth of the "wall", the "wall" is always behind the window and the door, independent of the point of view.

GL_QUADS state does not support multiple glBindTexture?

To be short, why couldn't I put two glBindTexture into one glBegin(GL_QUADS)...glEnd()?
I am following chapter 9 of OpenGL red book, http://www.glprogramming.com/red/chapter09.html
In example 9-5, program texbind.c produces two quads with different texture, like this:
The quad on the left has a white checker board texture while the quad on the right has a red one.
Here is the code which maps the textures to two quads:
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, texName[0]);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-2.0, 1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 1.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(0.0, -1.0, 0.0);
glEnd();
glBindTexture(GL_TEXTURE_2D, texName[1]);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(2.41421, 1.0, -1.41421);
glTexCoord2f(1.0, 0.0); glVertex3f(2.41421, -1.0, -1.41421);
glEnd();
glFlush();
}
We can see that even though the code uses GL_QUADS it draws one quad in each GL_QUADS state. However, if I comment out the glEnd() and glBegin(GL_QUADS) in the middle of this code, like this:
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, texName[0]);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-2.0, 1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 1.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(0.0, -1.0, 0.0);
// glEnd();
glBindTexture(GL_TEXTURE_2D, texName[1]);
// glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(2.41421, 1.0, -1.41421);
glTexCoord2f(1.0, 0.0); glVertex3f(2.41421, -1.0, -1.41421);
glEnd();
glFlush();
}
It turns out the second glBindTexture(GL_TEXTURE_2D, texName1) does not work anymore. And the result is:
Could anyone tell me why I cannot put two glBindTexture into one GL_QUADS state?
Because OpenGL doesn't support doing that.
The only calls you can make to OpenGL while inside glBegin/glEnd are vertex submission calls like glTexCoord*, glVertex*, etc. Anything else is an error.
As to "why", one reason is "because the specification says so". More likely, the GPU processes the entire data set (or significant chunks of it) in parallel, during which it cannot switch textures.
Also consider the newer way of drawing with OpenGL: uploading your data to a Vertex Buffer Object and calling one of the glDraw* commands. There's no way you can even call glBindTexture in between primitives using that.

How to enlarge the area of the object in OpenGL

OK, I have made simple rectangular using OpenGL, and it looks pretty simple
glNormal3f(0.0f, 0.0f, 0.0f);
glVertex3f(-0.0, 2.0, 0.0);
glVertex3f(1.0, 2.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
and small as well. So my Q. is how can I make this twice as bigger using one single line of code or how can I make lets say only X axis values to be doubled. I know it's possible, but I have no idea what function to search for. Thanks in advance.
Well, you can move the camera closer by using glTranslate or scale the scene using glScale. But frankly?
x *= 2.0;
y *= 2.0;
glVertex3f(-0.0, y*2.0, 0.0);
glVertex3f(x*1.0, y*2.0, 0.0);
glVertex3f(x*1.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
... this is the way to go.