How come we are looking at negative z by default in openGL? - 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

Related

OpenGL immediate mode in window coordinates [duplicate]

I am having trouble setting the openGL origin to the upper left corner of the view. So, in my window resize handler, I do something as;
// ox and oy are some offsets and width and height are the
// required viewport width and height
glViewport(ox, oy, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
This keeps the origin at bottom left and I can render my texture as:
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2f(0, 0);
glTexCoord2f(1, 0); glVertex2f(width, 0);
glTexCoord2f(1, 1); glVertex2f(width, height);
glTexCoord2f(0, 1); glVertex2f(0, height);
glEnd();
As far as I can tell from reading the pages here, to flip the origin I simply need to replace the glOrtho call with
glOrtho(0, width, height, 0, -1, 1);
However, doing this and using the render code above does not render my texture anymore and I just see a blank screen.
By flipping around the y-axis you flipped the chirality of the world space. Which means that the winding of your faces comes out differently. CCW becomes CW and vice versa. Most likely you have face culling enabled, so to account for the chirality flip you have to swap CCW for CW face culling.

opengl: Change the origin to upper left corner

I am having trouble setting the openGL origin to the upper left corner of the view. So, in my window resize handler, I do something as;
// ox and oy are some offsets and width and height are the
// required viewport width and height
glViewport(ox, oy, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
This keeps the origin at bottom left and I can render my texture as:
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2f(0, 0);
glTexCoord2f(1, 0); glVertex2f(width, 0);
glTexCoord2f(1, 1); glVertex2f(width, height);
glTexCoord2f(0, 1); glVertex2f(0, height);
glEnd();
As far as I can tell from reading the pages here, to flip the origin I simply need to replace the glOrtho call with
glOrtho(0, width, height, 0, -1, 1);
However, doing this and using the render code above does not render my texture anymore and I just see a blank screen.
By flipping around the y-axis you flipped the chirality of the world space. Which means that the winding of your faces comes out differently. CCW becomes CW and vice versa. Most likely you have face culling enabled, so to account for the chirality flip you have to swap CCW for CW face culling.

Blending in Different OpenGL editions

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...

Render to a texture, then render result to screen in OpenGL

I need to render an OpenGL scene to a texture in order to then manipulate that texture in a shader. I've solved this by using Framebuffer Objects, which I think I understand fairly well by now. At many points in my effect pipeline, I need to render a fullscreen quad and texture it with the dynamically rendered texture, which is where my problem is.
This is what my scene looks like: https://www.mathematik.uni-marburg.de/~thomak/planet.jpg
I render this to a texture and map that texture to a fullscreen quad. However, the resulting image is distorted in this way: https://www.mathematik.uni-marburg.de/~thomak/planettexture.jpg
Here is the code that renders the quad and sets the texture coordinates:
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glBegin(GL_QUADS);
glTexCoord2i(0, 0);
glVertex3i(-1, -1, -1);
glTexCoord2i(0, 1);
glVertex3i( 1, -1, -1);
glTexCoord2i(1, 1);
glVertex3i( 1, 1, -1);
glTexCoord2i(1, 0);
glVertex3i(-1, 1, -1);
glEnd();
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
And the shader code is here:
sampler2D BlitSamp = sampler_state
{
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = Clamp;
AddressV = Clamp;
};
float4 AlphaClearPS(float2 texcoords : TEXCOORD0) : COLOR
{
return float4(tex2D(BlitSamp, texcoords).rgb, 1.0f);
}
Where BlitSamp is the texture I rendered to and then passed to the shader. What could be going on here?
It's possible that your tex-coords are off. Your code, my comments:
glTexCoord2i(0, 0); //Bottom-Left
glVertex3i(-1, -1, -1); //Bottom-Left
glTexCoord2i(0, 1); //Top-Left
glVertex3i( 1, -1, -1); //Bottom-Right???
glTexCoord2i(1, 1); //Top-Right
glVertex3i( 1, 1, -1); //Top-Right
glTexCoord2i(1, 0); //Bottom-Right
glVertex3i(-1, 1, -1); //Bottom-Left??
Your code to render the quad looks fine so that would point to a mismatch in the size of the quad and the size of the viewport.
Could you have swapped the width and height when you created the render texture, by any chance ?

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).