I am pretty new to OpenGL and have run into a problem trying to render a skybox.
This picture illustrates the issue fairly well.
The sides of the skybox do not show up, and when I view an edge (like in the pic),
it just looks like the images are rendered right next to each other, instead of as
different sides of a cube.
void display ( void ) // Create The Display Function
{
glPushMatrix();
glDepthMask(0);
/* replace this code with your height field implementation */
/* you may also want to precede it with your rotation/translation/scaling */
/* Clear buffers */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt( 0.0, 0.0, 0.0, // eye position
0.1, 0.0, 0.1, // camera direction
0.0, 1.0, 0.0); // up direction
glPushAttrib(GL_ENABLE_BIT);
glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDisable(GL_BLEND);
glColor4f(1.0, 1.0, 1.0, 1.0);
/*!!!!!!!!!!!!!! FRONT FACE !!!!!!!!!!!!!!!*/
glBindTexture(GL_TEXTURE_2D,frontTextureId); // select which texture to use
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f( 0.5f, 0.5f, -0.5f );
glTexCoord2f(1, 0); glVertex3f( -0.5f, 0.5f, -0.5f );
glTexCoord2f(1, 1); glVertex3f( -0.5f, -0.5f, -0.5f );
glTexCoord2f(0, 1); glVertex3f( 0.5f, -0.5f, -0.5f );
glEnd();
/*!!!!!!!!!!!!!! LEFT FACE !!!!!!!!!!!!!!!*/
glBindTexture(GL_TEXTURE_2D, leftTextureId); // select which texture to use
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f( 0.5f, 0.5f, 0.5f );
glTexCoord2f(1, 0); glVertex3f( 0.5f, 0.5f, -0.5f );
glTexCoord2f(1, 1); glVertex3f( 0.5f, -0.5f, -0.5f );
glTexCoord2f(0, 1); glVertex3f( 0.5f, -0.5f, 0.5f );
glEnd();
/*!!!!!!!!!!!!!! RIGHT FACE !!!!!!!!!!!!!!!*/
glBindTexture(GL_TEXTURE_2D, rightTextureId); // select which texture to use
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f( -0.5f, 0.5f, -0.5f );
glTexCoord2f(1, 0); glVertex3f( -0.5f, 0.5f, 0.5f );
glTexCoord2f(1, 1); glVertex3f( -0.5f, -0.5f, 0.5f );
glTexCoord2f(0, 1); glVertex3f( -0.5f, -0.5f, -0.5f );
glEnd();
/*!!!!!!!!!!!!!! BACK FACE !!!!!!!!!!!!!!!*/
glBindTexture(GL_TEXTURE_2D, backTextureId); // select which texture to use
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f( -0.5f, 0.5f, 0.5f );
glTexCoord2f(1, 0); glVertex3f( 0.5f, 0.5f, 0.5f );
glTexCoord2f(1, 1); glVertex3f( 0.5f, -0.5f, 0.5f );
glTexCoord2f(0, 1); glVertex3f( -0.5f, -0.5f, 0.5f );
glEnd();
/*!!!!!!!!!!!!!! TOP FACE !!!!!!!!!!!!!!!*/
glBindTexture(GL_TEXTURE_2D, upTextureId); // select which texture to use
glBegin(GL_QUADS);
glTexCoord2f(0, 1); glVertex3f( -0.5f, 0.5f, -0.5f );
glTexCoord2f(0, 0); glVertex3f( -0.5f, 0.5f, 0.5f );
glTexCoord2f(1, 0); glVertex3f( 0.5f, 0.5f, 0.5f );
glTexCoord2f(1, 1); glVertex3f( 0.5f, 0.5f, -0.5f );
glEnd();
/*!!!!!!!!!!!!!! BOTTOM FACE !!!!!!!!!!!!!!!*/
glBindTexture(GL_TEXTURE_2D, downTextureId); // select which texture to use
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f( -0.5f, -0.5f, -0.5f );
glTexCoord2f(0, 1); glVertex3f( -0.5f, -0.5f, 0.5f );
glTexCoord2f(1, 1); glVertex3f( 0.5f, -0.5f, 0.5f );
glTexCoord2f(1, 0); glVertex3f( 0.5f, -0.5f, -0.5f );
glEnd();
/*!!!!!!!!!!!!!! end of drawing of a textured quad !!!!!!!!!!!!!!!*/
glPopAttrib();
glPopMatrix();
/* Swap buffers, so one we just drew is displayed */
glutSwapBuffers();
}
I feel like I am missing something basic here, but I am not sure what. The code is
more or less verbatim from the tutorial site (http://sidvind.com/wiki/Skybox_tutorial).
The code was compiled with g++ 4.5.2 on Ubuntu Linux, but the same problem arises under
visual studio as well.
I should state, as well, that the numbers used in the code above are the values that
produced the linked picture.
Your textures need to form a seamless join across cube edges. In the screenshot you've supplied, these images don't form a seamless join (look at the sky).
Also check your projection matrix - are you setting a perspective transform?
To set up a perspective projection (the most common for 3D apps):
glMatrixMode(GL_PROJECTION); // switch to projection matrix
glLoadIdentity(); // reset projection
gluPerspective(90.0,4.0/3.0,0.01,10.0); // 90deg FOV, 4:3 aspect ratio, 0.01 near clip plane, 10.0 far clip plane
glMatrixMode(GL_MODELVIEW); // back to model matrix
It looks to me like the texture on the right ought to be on the left of the left image. So I'd guess you are texturing the wrong sides of your cube.
Related
I read similar suggested questions and their solutions, but could not find an answer.
I'm trying to draw a scene with an isometric view in OpenGL.
Draw func:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glRotatef(atan(0.5f) * 180.0f / PI, 1.0f, 0.0f, 0.0f);
glRotatef(-45.0f, 0.0f, 1.0f, 0.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBegin(GL_QUADS);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.0f, 1.0f);
glEnd();
glPopMatrix();
In the end, I get this result. The camera does have an isometric projection, but for some reason polygons are clipped.
If I add glTranslatef(-0.8f, 0, -0.8f) before drawing the quad, the result is as follows:
The problem is that I don't apply any optimization to OpenGL render. But why do polygons have to be cut off?
The polygons are clipped by the near or far plane of the viewing volume.
When you do not set a projection matrix, then view space, clip space and normalized device space are the same. The normalized device space is a unique cube with the left, bottom, near of (-1, -1, -1) and right, top, far of (1, 1, 1). All the geometry which is not inside this cube is clipped.
Actually you draw a quad with a side length of 1. One vertex of the quad is at the origin of the view (0, 0, 0). The quad is rotated around the origin by glRotate. Since the length of the diagonal of the quad is sqrt(2.0), one vertex of the rotated quad is clipped by either the near plane or the far plane.
If you construct and rotate a quad whose center is (0, 0 ,0), it will not be clipped, because the length form the center to each vertex is sqrt(2.0)/2.0. That is less than 1 (distance to near and far plane form the center of the viewing volume)
glBegin(GL_QUADS);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(-0.5f, 0.0f, -0.5f);
glVertex3f( 0.5f, 0.0f, -0.5f);
glVertex3f( 0.5f, 0.0f, 0.5f);
glVertex3f(-0.5f, 0.0f, 0.5f);
glEnd();
respectively
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(atan(0.5f) * 180.0f / PI, 1.0f, 0.0f, 0.0f);
glRotatef(-45.0f, 0.0f, 1.0f, 0.0f);
glTranslate(-0.5f, 0.0f, -0.5f);
glBegin(GL_QUADS);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.0f, 1.0f);
glEnd();
Alternatively you can set an Orthographic projection, which enlarges the viewing volume by glOrtho:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -2.0, 2.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(atan(0.5f) * 180.0f / PI, 1.0f, 0.0f, 0.0f);
glRotatef(-45.0f, 0.0f, 1.0f, 0.0f);
glBegin(GL_QUADS);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.0f, 1.0f);
glEnd();
I'm having a problem in OpenGL, when I press W, A, S, D everything works normal, but when I press space or shift it happens:
The cube disappears before the end of the screen. Depending on the value of xrot and yrot another axis gets this problem.
I'm a beginner in openGL 3D and I don't know why that happens.
code:
float xrot = 100.0f;
float yrot = -100.0f;
float tra_x = 0.0f;
float tra_y = 0.0f;
float tra_z = 0.0f;
GLFWwindow* window;
void drawBox()
{
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glEnable(3553);
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0f);
// FRONT
glVertex3f(-0.5f, -0.5f, 0.5f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-0.5f, 0.5f, 0.5f);
// BACK
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glColor3f(0.0f, 1.0f, 0.0f);
// LEFT
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-0.5f, -0.5f, -0.5f);
// RIGHT
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.5f, 0.5f, 0.5f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.5f, -0.5f, 0.5f);
glColor3f(0.0f, 0.0f, 1.0f);
// TOP
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glColor3f(1.0f, 0.0f, 0.0f);
// BOTTOM
glVertex3f(-0.5f, -0.5f, 0.5f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
glEnd();
}
void display(void)
{
glDisable(GL_BLEND);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glRotatef(yrot, -1.0f, 0.0f, 0.0f);
glRotatef(xrot, 0.0f, 1.0f, 0.0f);
glTranslatef(-tra_x, tra_y, -tra_z);
drawBox();
glFlush();
glfwSwapBuffers(window);
}
void framebuffer_resize_callback(GLFWwindow* window, int fbW, int fbH)
{
glViewport(0, 0, fbW, fbH);
}
int main(void)
{
if (!glfwInit())
return -1;
glfwWindowHint(GLFW_SAMPLES, 2);
window = glfwCreateWindow(640, 480, "Test", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
glfwSetFramebufferSizeCallback(window, framebuffer_resize_callback);
glfwMakeContextCurrent(window);
glClearColor(0.93f, 0.93f, 0.93f, 0.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glClearDepth(1.0f);
while (!glfwWindowShouldClose(window))
{
display();
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
tra_x += 0.1f;
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
tra_x -= 0.1f;
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
tra_z -= 0.1f;
}
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
tra_z += 0.1f;
}
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
tra_y += 0.1f;
}
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) {
tra_y -= 0.1f;
}
glfwPollEvents();
}
}
The object is clipped by the fare plane of the Orthographic projection. The projection matrix defines the volume (clip space) which is projected on to 2 dimensional viewport. Actually you don't set a projection matrix, thus the projection is the Identity matrix. Hence, view space, clip space and normalized device space are the same and the viewing volume is a unique cube, with the left, bottom, near of (-1, -1, -1) and the right, top, far of (1, 1, 1). Hence the near plane is at -1 and the far plane is at 1. All the geometry which is out of this space is clipped.
Use glOrtho to define a clip space with a larger scale. For instance:
void display(void)
{
// [...]
# set projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, -1, -10, 10); // near = -10, far = 10
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// [...]
Alternatively you can use Perspective projection. At perspective projection the viewing volume is a [Frustum](https://en.wikipedia.org/wiki/Viewing_frustum. Hence you have to shift the object along the negative z axis in between the near and far plane.
Create a perspective projection by gluPerspective and move the object in between then near and far plane by gluLookAt:
void display(void)
{
// [...]
# set projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(-1, 1, -1, -1, -10, 10); // near = -10, far = 10
gluPerspective(90.0, 480.0/640.0, 0.1, 20.0)
// set view matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 5.0, 0, 0, 0, 0, 1.0, 0);
// [...]
What exactly are eye space coordinates?
Im working on a 3D platformer where theres a large platform and a character made up of multiple cubes.
However when I try rotating the Y axis of the characters cubes it doesn't rotate them at their centerpoint. They just orbit as a whole around the platform in a huge circle. I'm guessing my glRotatef's are in the wrong order but I cant seem to figure out what order they should be to make the group of cubes rotate only around their center point.
My code for drawing them is:
for (int i = 0; i < Models.size(); i++){ // theres only one model this has to go through that stores the group of cubes for the character
glPushMatrix(); // set rotation for the whole group (I would expect...)
glRotatef(Models.at(i)->ModelRotation.X,1,0,0);
glRotatef(Models.at(i)->ModelRotation.Y,0,1,0);
glRotatef(Models.at(i)->ModelRotation.Z,0,0,1);
for (int j = 0; j < Models.at(i)->Parts.size(); j++) // For each cube in the character,
Models.at(i)->Parts.at(j)->Render(); // draw the cube
glPopMatrix();
}
Model is a struct that just has a vector3 called "ModelRotation" and a normal vector called "Parts" which stores all the cubes for the character.
My function for rendering the cube in the cube class is:
void Render(){
glPushMatrix();
glTranslatef( Position.X,
Position.Y,
Position.Z
);
glColor3f( Color.R,
Color.G,
Color.B
);
glScalef( Size.X,
Size.Y,
Size.Z
);
// Render the front quad
//glBindTexture(GL_TEXTURE_2D, Faces[0]);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f( 1.0f, -1.0f, -1.0f );
glTexCoord2f(1, 0); glVertex3f( -1.0f, -1.0f, -1.0f );
glTexCoord2f(1, 1); glVertex3f( -1.0f, 1.0f, -1.0f );
glTexCoord2f(0, 1); glVertex3f( 1.0f, 1.0f, -1.0f );
glEnd();
// Render the left quad
//glBindTexture(GL_TEXTURE_2D, Faces[1]);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f( 1.0f, -1.0f, 1.0f );
glTexCoord2f(1, 0); glVertex3f( 1.0f, -1.0f, -1.0f );
glTexCoord2f(1, 1); glVertex3f( 1.0f, 1.0f, -1.0f );
glTexCoord2f(0, 1); glVertex3f( 1.0f, 1.0f, 1.0f );
glEnd();
// Render the back quad
//glBindTexture(GL_TEXTURE_2D, Faces[2]);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f( -1.0f, -1.0f, 1.0f );
glTexCoord2f(1, 0); glVertex3f( 1.0f, -1.0f, 1.0f );
glTexCoord2f(1, 1); glVertex3f( 1.0f, 1.0f, 1.0f );
glTexCoord2f(0, 1); glVertex3f( -1.0f, 1.0f, 1.0f );
glEnd();
// Render the right quad
//glBindTexture(GL_TEXTURE_2D, Faces[3]);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f( -1.0f, -1.0f, -1.0f );
glTexCoord2f(1, 0); glVertex3f( -1.0f, -1.0f, 1.0f );
glTexCoord2f(1, 1); glVertex3f( -1.0f, 1.0f, 1.0f );
glTexCoord2f(0, 1); glVertex3f( -1.0f, 1.0f, -1.0f );
glEnd();
// Render the top quad
//glBindTexture(GL_TEXTURE_2D, Faces[4]);
glBegin(GL_QUADS);
glTexCoord2f(0, 1); glVertex3f( -1.0f, 1.0f, -1.0f );
glTexCoord2f(0, 0); glVertex3f( -1.0f, 1.0f, 1.0f );
glTexCoord2f(1, 0); glVertex3f( 1.0f, 1.0f, 1.0f );
glTexCoord2f(1, 1); glVertex3f( 1.0f, 1.0f, -1.0f );
glEnd();
// Render the bottom quad
//glBindTexture(GL_TEXTURE_2D, Faces[5]);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f( -1.0f, -1.0f, -1.0f );
glTexCoord2f(0, 1); glVertex3f( -1.0f, -1.0f, 1.0f );
glTexCoord2f(1, 1); glVertex3f( 1.0f, -1.0f, 1.0f );
glTexCoord2f(1, 0); glVertex3f( 1.0f, -1.0f, -1.0f );
glEnd();
glPopMatrix();
};
Any help would be very much appreciated.
I'm guessing it's just a simple mistake I can't put together.
I'll show more code if it's needed.
The way OpenGL applies the transformations is from bottom to top between PopMatrix() and PushMatrix(), that is the inverse from the way you are actually coding them.
In your code, the order in which you are calling is flipped like this:
Rotatef();
Translatef();
Scalef();
It should be:
Translatef();
Rotatef();
Scalef();
In other words, you want to translate always as last (meaning putting it into your code as the very first line after the PopMatrix()) unless you have a good reason (a particular graphic effect or scene rendering), so that you first apply scaling and rotation transformations and then you translate the object in world coordinates.
For rotating multipart objects (as your case) you should probably translate twice, as explained here: Rotating a multipart object
So your code outside the cube class could be something like:
for (int i = 0; i < Models.size(); i++){
glPushMatrix();
glTranslatef(
Models.at(i)->WorldPosition.X,
Models.at(i)->WorldPosition.Y,
Models.at(i)->WorldPosition.Z,
);
glRotatef(Models.at(i)->ModelRotation.X,1,0,0);
glRotatef(Models.at(i)->ModelRotation.Y,0,1,0);
glRotatef(Models.at(i)->ModelRotation.Z,0,0,1);
for (int j = 0; j < Models.at(i)->Parts.size(); j++)
Models.at(i)->Parts.at(j)->Render(); // draw the cube
glPopMatrix();
}
I'm currently learning OpenGL and from what I understood, I have to call
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
before drawing lines. Then, to draw shapes I need to call
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
to draw shapes such as GL_TRIANGLES and GL_QUADS.
I wrote this code with the goal of drawing a single line at top and 3 shapes, but only the line was drawn.
Here is my code.
void drawScene() {
//Clear information from last draw
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective
glBegin(GL_LINES);
glColor3f(100,200,100);
glLineWidth(10.0f);
glVertex2f(-1.0f,0.8f);
glVertex2f(1.0f,0.8f);
glEnd();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBegin(GL_QUADS); //Begin quadrilateral coordinates
//Trapezoid
glVertex3f(-0.7f, -1.5f, -5.0f);
glVertex3f(0.7f, -1.5f, -5.0f);
glVertex3f(0.4f, -0.5f, -5.0f);
glVertex3f(-0.4f, -0.5f, -5.0f);
glEnd(); //End quadrilateral coordinates
glBegin(GL_TRIANGLES); //Begin triangle coordinates
//Pentagon
glVertex3f(0.5f, 0.5f, -5.0f);
glVertex3f(1.5f, 0.5f, -5.0f);
glVertex3f(0.5f, 1.0f, -5.0f);
glVertex3f(0.5f, 1.0f, -5.0f);
glVertex3f(1.5f, 0.5f, -5.0f);
glVertex3f(1.5f, 1.0f, -5.0f);
glVertex3f(0.5f, 1.0f, -5.0f);
glVertex3f(1.5f, 1.0f, -5.0f);
glVertex3f(1.0f, 1.5f, -5.0f);
//Triangle
glVertex3f(-0.5f, 0.5f, -5.0f);
glVertex3f(-1.0f, 1.5f, -5.0f);
glVertex3f(-1.5f, 0.5f, -5.0f);
glEnd(); //End triangle coordinates
glutSwapBuffers(); //Send the 3D scene to the screen
}
Could someone please explain to me how to switch between GL_PROJECTION and GL_MODELVIEW and how do they work?
The Z-value of your polygons is -5.0 which is outside the default [-1, 1] range for the device coordinates, so they get discarded.
Replace all glVertex3f(x, y, z) calls with glVertex2f(x,y).
I am Trying to Render 3 textures,
-Background
-Black/White Foreground Mask
-Foreground
I have used this OpenGL - mask with multiple textures
because it acurately descirbes my problem. But i can not get it to work. I only get the Last rendererd Texture, in this case the Foreground. I have called glutInitDisplayMode(GLUT_ALPHA); to get Alpha rendering as sugested in the Answer.
Can anyone spot errors from my side?
My code is as follows:
double stretch = ((double)m_videoResY * (double)m_depthResX) / ((double)m_videoResX * (double)m_depthResY);
glEnable(GL_BLEND);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(+0.5, -0.5, +0.5, -0.5, 0.001f, 1.0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -0.5f);
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glDisable(GL_DEPTH_TEST);
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBlendFunc(GL_ONE, GL_ZERO);
glBindTexture(GL_TEXTURE_2D, m_backgroundTexture);//Draw BGTexture
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(-0.5f, -0.5f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(-0.5f, 0.5f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(0.5f, 0.5f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
glEnd();
glBlendFuncSeparate(GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ZERO);
//mask with userID
glBindTexture(GL_TEXTURE_2D, m_userIDTexture);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(-0.5f, -0.5f, 0.0f);
glTexCoord2f(1.0f, 1.0f * stretch);
glVertex3f(-0.5f, 0.5f, 0.0f);
glTexCoord2f(0.0f, 1.0f * stretch);
glVertex3f(0.5f, 0.5f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
glEnd();
//blend with Video of User
glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA);
glBindTexture(GL_TEXTURE_2D, m_videoTexture);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(-0.5f, -0.5f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(-0.5f, 0.5f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(0.5f, 0.5f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
glEnd();
I suppose your mistakes are:
When you drawing your background with glBlendFunc(GL_ONE, GL_ZERO);
As result you normaly draw it in framebuffer, but providing no needed blending operation, more effective on this pass is don't use blending at all. So, more effective is glDisable(GL_BLEND), but your pass work here like you expect.
At second pass you drawing with glBlendFuncSeparate(GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ZERO); I don't know why you using so sofisticated function here and separatively blend colors and alpha values.
So, looking on third pass, I suppose you want to modify your background alpha value by your foreground black/white color mask. If It's true, you must use glBlendFuncSeparate(GL_ZERO, GL_ONE, GL_ZERO, GL_SRC_COLOR); - Yep, little mistake.
And at third pass when you drawing foreground you have glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA), what means you wanna draw those regions, where your black/white mask was white and blending with attenuation for more darker mask regions.
If you have any questions about glBlendFunc, I can help you.