Stroke is not drawn correctly (OpenGL) - c++

I am trying to make a stroke for chests in a minecraft game and I almost succeeded, but for some reason it is not drawn correctly.
ScreenShot
The screenshot shows how part of the line has disappeared. This happens when the line is on the chest. Maybe it's a texture mapping or something else. In this case, the stroke is drawn through the blocks normally.
ScreenShot2
Also, when the chest is in the dark, the outline becomes dark, although I turned off GL_LIGHTING
ScreenShot3
Here is a piece of code with which I draw the stroke
glPushAttrib(GL_ALL_ATTRIB_BITS);
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_CULL_FACE);
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
glLineWidth(2);
glColor3ub(255, 255, 255);
glBegin(GL_LINES);
glVertex3f(1, 0, 0);
glVertex3f(1, -1, 0);
glVertex3f(0, 0, 0);
glVertex3f(1, 0, 0);
glVertex3f(0, 0, 0);
glVertex3f(0, -1, 0);
glVertex3f(0, -1, 0);
glVertex3f(1, -1, 0);
glVertex3f(1, -1, 0);
glVertex3f(1, -1, -1);
glVertex3f(1, -1, -1);
glVertex3f(1, 0, -1);
glVertex3f(1, -1, -1);
glVertex3f(0, -1, -1);
glVertex3f(0, -1, -1);
glVertex3f(0, -1, 0);
glVertex3f(0, -1, -1);
glVertex3f(0, 0, -1);
glVertex3f(0, 0, -1);
glVertex3f(1, 0, -1);
glVertex3f(0, 0, -1);
glVertex3f(0, 0, 0);
glVertex3f(1, 0, -1);
glVertex3f(1, 0, 0);
glEnd();
glPopAttrib();
What can be wrong?

From your screenshots it seems that the order of rendering is the following:
terrain
outline
chest
Now since you have disabled GL_DEPTH_TEST in step 2, the fragments (pixels) that turned white (those on which the outline is drawn), won't get their depth information updated. That's because disabling GL_DEPTH_TEST not only disables depth testing, but also disables updating the depth information of fragments.
So when you finally draw the chest (with depth testing re-enabled), it will be drawn over all the fragments whose depth value indicates that they are behind it. That means, also over the fragments where the outline is drawn.
The black color of the outline on the third screenshot may be caused by your call to glBindTexture(GL_TEXTURE_2D, 0);. But that's just a hunch.

Related

Unable to apply texture on my cube using opengl & sfml

I want to load a texture on one of my faces on a cube. All the other faces have a specific color that I put on them. The problem is that, when I try to apply the texture, it will not display it. Instead, the last texture used on the cube is being applied.
Here is a code sample :
Engine::Engine() : m_isMovingUp(false), m_isMovingDown(false), m_isMovingLeft(false), m_isMovingRight(false), m_context(24, 8, 4, 3, 3), m_angleX(0), m_angleZ(0), m_mov(3.0f, 3.0f, 3.0f)
{
//Window settings
m_win.create(sf::VideoMode(800, 600), "SFML Event and with some OpenGL graphics", sf::Style::Default, m_context);
m_win.setFramerateLimit(60);
//Opengl settings
glEnable(GL_TEXTURE_2D);
m_textureID = loadTexture("/Data/test.jpg");
}
void Engine::run()
{
bool running = true;
while (running)
{
processEvents();
update();
render();
}
}
void Engine::cube()
{
//glRotated(m_angleX, 1, 0, 0);
//glRotated(m_angleZ, 0, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, m_textureID);
glBegin(GL_QUADS);
//face Rouge
//glColor3ub(255, 0, 0);
glTexCoord2d(0, 1); glVertex3d(1, 1, 1);
glTexCoord2d(0, 0); glVertex3d(1, 1, -1);
glTexCoord2d(1, 0); glVertex3d(-1, 1, -1);
glTexCoord2d(1, 1); glVertex3d(-1, 1, 1);
glEnd();
glBegin(GL_QUADS);
//face verte
glColor3ub(0, 255, 0);
glVertex3d(1, -1, 1);
glVertex3d(1, -1, -1);
glVertex3d(1, 1, -1);
glVertex3d(1, 1, 1);
//face bleu
glColor3ub(0, 0, 255);
glVertex3d(1, -1, 1);
glVertex3d(-1, -1, 1);
glVertex3d(-1, -1, -1);
glVertex3d(1, -1, -1);
//face jaune
glColor3ub(255, 255, 0);
glVertex3d(-1, 1, 1);
glVertex3d(-1, 1, -1);
glVertex3d(-1, -1, -1);
glVertex3d(-1, -1, 1);
//face cyan
glColor3ub(0, 255, 255);
glVertex3d(1, 1, -1);
glVertex3d(1, -1, -1);
glVertex3d(-1, -1, -1);
glVertex3d(-1, 1, -1);
//face magenta
glColor3ub(255, 0, 255);
glVertex3d(1, -1, 1);
glVertex3d(1, 1, 1);
glVertex3d(-1, 1, 1);
glVertex3d(-1, -1, 1);
glEnd();
glFlush();
}
GLuint Engine::loadTexture(string file)
{
GLuint textureID;
if (!m_image.loadFromFile(file))
EXIT_FAILURE;
glGenTextures(1, &textureID);
//glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, 4, m_image.getSize().x, m_image.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_image.getPixelsPtr());
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
return textureID;
}
This is where I'm at after fixing as much as I could.
PS : CODE UPDATED & PICTURE ADDED
Picture of my cube
I see two mistakes in the pastebin code:
Line 27:
glBindTexture(GL_TEXTURE_2D, 0);
You can not change the bound texture in between glBegin and glEnd calls. You will have to split rendering of the textured and not textured faces to their own begin-end blocks.
Line 74:
glTexImage2D(GL_TEXTURE_2D, 0, 4, m_image.getSize().x, m_image.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, &m_image.getPixelsPtr);
The last parameter should be m_image.getPixelsPtr() not &m_image.getPixelsPtr. You are taking address of the method and using it as image data. The method getPixelsPtr will give you pointer to the image data if you call it.
Also it seems that you are creating new texture and loading image into it on every frame. If it is just in this example that renders once, then it is ok. But in actual program it would have big impact on performance and the program would soon run out of memory.
There may be more problems, it can't be told without seeing the whole code. If you can, upload somewhere a minimal complete example.

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

Texture problems in LWJGL (OpenGL) using slick-util library

I’m just starting out with 3D graphics programming and I’ve got a nicely encapsulated Cube object. This class has a render() method, which proceeds to push a new matrix, perform transformations, glBegin, specify all the vertices and texture coordinates, glEnd, and pop the matrix. Now, I have written this Cube class with methods for using different textures on different faces of the cube, but that isn't happening at runtime. I understood textures to be like colors, which I can easily change per face of the cube by making a call to glColor before the appropriate vertices, but using texture.bind() (from the slick-util library) seems to do nothing.
This is my render method:
public void render(){
glPushMatrix();
top.bind();
top.setTextureFilter(GL_NEAREST);
glColor4f(1, 1, 1, 0);
glTranslatef(x, y, z);
glRotatef(yaw, 0, 1, 0);
glBegin(GL_QUADS);
//Top
glTexCoord2f(0, 0); glVertex3f(0, size, 0);
glTexCoord2f(0, 1); glVertex3f(0, size, size);
glTexCoord2f(1, 1); glVertex3f(size, size, size);
glTexCoord2f(1, 0); glVertex3f(size, size, 0);
//Front
top.release();
side.bind();
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
glTexCoord2f(0, 1); glVertex3f(0, size, 0);
glTexCoord2f(1, 1); glVertex3f(size, size, 0);
glTexCoord2f(1, 0); glVertex3f(size, 0, 0);
//Left
glTexCoord2f(0, 0); glVertex3f(0, 0, size);
glTexCoord2f(0, 1); glVertex3f(0, size, size);
glTexCoord2f(1, 1); glVertex3f(0, size, 0);
glTexCoord2f(1, 0); glVertex3f(0, 0, 0);
//Right
glTexCoord2f(0, 0); glVertex3f(size, 0, size);
glTexCoord2f(0, 1); glVertex3f(size, size, size);
glTexCoord2f(1, 1); glVertex3f(size, size, 0);
glTexCoord2f(1, 0); glVertex3f(size, 0, 0);
//Back
glTexCoord2f(0, 0); glVertex3f(0, 0, size);
glTexCoord2f(0, 1); glVertex3f(0, size, size);
glTexCoord2f(1, 1); glVertex3f(size, size, size);
glTexCoord2f(1, 0); glVertex3f(size, 0, size);
//Bottom
side.release();
bottom.bind();
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
glTexCoord2f(0, 1); glVertex3f(0, 0, size);
glTexCoord2f(1, 1); glVertex3f(size, 0, size);
glTexCoord2f(1, 0); glVertex3f(size, 0, 0);
bottom.release();
glEnd();
glPopMatrix();
}
Variables top, side, and bottom are the textures I want to put on the faces of the cube.
The result is that every face of the cube has the “top” texture.
The result is the same whether I call texture.release() or not.
You can't bind textures between glBegin() and glEnd(). Only a limited set of GL calls can be made between glBegin() and glEnd() (see https://www.opengl.org/sdk/docs/man2/xhtml/glBegin.xml for the full list), and glBindTexture() is not one of them.
To bind a different texture for each side, you need to start a new begin/end pair for each side:
//Top
top.bind();
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, size, 0);
glTexCoord2f(0, 1); glVertex3f(0, size, size);
glTexCoord2f(1, 1); glVertex3f(size, size, size);
glTexCoord2f(1, 0); glVertex3f(size, size, 0);
glEnd();
//Front
side.bind();
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
...
glEnd();
//Bottom
bottom.bind();
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
...
glEnd();

glutLookAt misunderstanding

Lets say I do have the following set up :
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f(0,0); glVertex3f(-10, 0, 0);
glTexCoord2f(1,0); glVertex3f(10, 0, 0);
glTexCoord2f(0,1); glVertex3f(-10, 0, 5);
glTexCoord2f(1,1); glVertex3f(10, 0, 5);
glEnd();
And I do the following
gluLookAt(0,0,10, 0,5,0, 0,1,0);
I should end up with a "virtual world" like the one below, right ?
For a strange reason, nothing appears on screen and I can't figure out why. Any idea ?
It seems like you inverted Y axis and Z axis in your triangles. You probably wanted to write this:
glTexCoord2f(0,0); glVertex3f(-10, 0, 0);
glTexCoord2f(1,0); glVertex3f(10, 0, 0);
glTexCoord2f(0,1); glVertex3f(-10, 5, 0);
glTexCoord2f(1,1); glVertex3f(10, 5, 0);
This corresponds better to the figure you attached to your question.

Cube drawing opengl, but a confused image of cube is drawn

Hi
I am trying to draw a cube of size 5*5*5 with six diffrent face colors. How ever,I can not see all the faces colored diffrently, all I see is a cube, with confusing colors format.
Some faces are clearly visible, while top face , parallel to zx plane is not visible.
Thanks in advance
void init(void)
{
glClearColor(0,0,0,0);
glShadeModel(GL_FLAT);
}
void DrawCube(void)
{
glLoadIdentity();
gluLookAt(10, 10, 10, 0, 0, 0, 0, 1, 0);
glBegin(GL_QUADS);
//face in xy plane
glColor3f(0.82, 0.41, 0.12);//this the color with which complete cube is drawn.
glVertex3f(0,0 ,0 );
glVertex3f(5, 0, 0);
glVertex3f(5, 5, 0);
glVertex3f(0, 5, 0);
//face in yz plane
glColor3f(1, 0, 0);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, 5);
glVertex3f(0, 5, 0);
glVertex3f(0, 5, 5);
//face in zx plance
glColor3f(0, 1, 0);
glVertex3f(0, 0, 0 );
glVertex3f(0, 0, 5);
glVertex3f(5, 0, 5);
glVertex3f(5, 0, 0);
//|| to xy plane.
glColor3f(0, 0, 1);
glVertex3f(0, 0, 5);
glVertex3f(5, 0, 5);
glVertex3f(5, 5, 5);
glVertex3f(0, 5, 5);
//|| to yz plane
glColor3f(0.73, 0.58, 0.58);
glVertex3f(0,0 ,5 );
glVertex3f(5, 0, 5);
glVertex3f(5, 5, 5);
glVertex3f(0, 5, 5);
//|| to zx plane //this face is not visible. I am not understanding why.
glVertex3f(0.58, 0, 0.82);
glVertex3f(0, 5, 0 );
glVertex3f(0, 5, 5);
glVertex3f(5, 5, 5);
glVertex3f(5, 5, 0);
glEnd();
glFlush();
}
void reshape(int w,int h){
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1, 1, -1, 1, 1.5, 20);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv){
glutInit(&argc, argv);//we initizlilze the glut. functions
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(DrawCube);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
//|| to zx plane //this face is not visible. I am not understanding why.
glVertex3f(0.58, 0, 0.82);
You want glColor3f. I'm guessing this is a typo.
Two unrelated things:
Your || to xy plane quad is the same as your || to yz plane quad.
You aren't clearing the color buffer before drawing. You probably want glClear(GL_COLOR_BUFFER_BIT) at the start of DrawCube