Answer the question
In order to leave comments, you need to log in
Depth test not working in OpenGl and SFML. What to do?
What should I do? I decided to write a rotating cube in SFML and OpenGL (I have been working with SFML for a long time, but I just started learning OpenGL). I did everything as shown in the video tutorial, but for some reason it does not work. My cube is spinning, which is good, but the depth test is not working or not working properly. Why? Where is the mistake? What's wrong? Its faces are drawn in accordance with the way I indicated, that is, those faces that I drew last are drawn before others, but I need the faces to be drawn correctly, and the cube is not translucent.
Here is the code itself:
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include <SFML/Graphics.hpp>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <windows.h>
using namespace sf;
GLuint LoadTexture(const char *name)
{
Image image;
image.loadFromFile(name);
GLuint texture = 0;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, image.getSize().x, image.getSize().y, GL_RGBA, GL_UNSIGNED_BYTE, image.getPixelsPtr());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
return texture;
}
int main(int argc, char **argv)
{
RenderWindow window(VideoMode(400, 400), "");
Event event;
/*GLuint texture = 0;
{
Image image;
image.loadFromFile("D:/C++/GlutExample/resources/img.png");
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, image.getSize().x, image.getSize().y, GL_RGBA, GL_UNSIGNED_BYTE, image.getPixelsPtr());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
}*/
GLuint box[6];
box[0] = LoadTexture("D:/C++/GlutExample/resources/back.png");
box[1] = LoadTexture("D:/C++/GlutExample/resources/front.png");
box[2] = LoadTexture("D:/C++/GlutExample/resources/left.png");
box[3] = LoadTexture("D:/C++/GlutExample/resources/right.png");
box[4] = LoadTexture("D:/C++/GlutExample/resources/bottom.png");
box[5] = LoadTexture("D:/C++/GlutExample/resources/top.png");
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
glClearDepth(1.f);
glDepthFunc(GL_LESS);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0f, 1.f, 0.0f, 500.f);
glEnable(GL_TEXTURE_2D);
Clock clock;
while(window.isOpen()) {
while(window.pollEvent(event)) {
if(event.type == Event::Closed)
window.close();
}
float time = clock.getElapsedTime().asSeconds() * 180;
float size = 25;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glClearColor(0.5f, 0.0f, 0.0f, 0.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, -100);
glRotatef(time / 4, 50, 50, 0);
glBindTexture(GL_TEXTURE_2D, box[0]);
glBegin(GL_QUADS);
//back
glTexCoord2f(0, 0); glVertex3f(-size, -size, -size);
glTexCoord2f(1, 0); glVertex3f( size, -size, -size);
glTexCoord2f(1, 1); glVertex3f( size, size, -size);
glTexCoord2f(0, 1); glVertex3f(-size, size, -size);
glEnd();
glBindTexture(GL_TEXTURE_2D, box[1]);
glBegin(GL_QUADS);
//front
glTexCoord2f(0, 0); glVertex3f( size, -size, size);
glTexCoord2f(1, 0); glVertex3f(-size, -size, size);
glTexCoord2f(1, 1); glVertex3f(-size, size, size);
glTexCoord2f(0, 1); glVertex3f( size, size, size);
glEnd();
glBindTexture(GL_TEXTURE_2D, box[2]);
glBegin(GL_QUADS);
//left
glTexCoord2f(0, 0); glVertex3f(-size, -size, size);
glTexCoord2f(1, 0); glVertex3f(-size, -size, -size);
glTexCoord2f(1, 1); glVertex3f(-size, size, -size);
glTexCoord2f(0, 1); glVertex3f(-size, size, size);
glEnd();
glBindTexture(GL_TEXTURE_2D, box[3]);
glBegin(GL_QUADS);
//right
glTexCoord2f(0, 0); glVertex3f( size, -size, -size);
glTexCoord2f(1, 0); glVertex3f( size, -size, size);
glTexCoord2f(1, 1); glVertex3f( size, size, size);
glTexCoord2f(0, 1); glVertex3f( size, size, -size);
glEnd();
glBindTexture(GL_TEXTURE_2D, box[4]);
glBegin(GL_QUADS);
//bottom
glTexCoord2f(0, 0); glVertex3f(-size, -size, size);
glTexCoord2f(1, 0); glVertex3f( size, -size, size);
glTexCoord2f(1, 1); glVertex3f( size, -size, -size);
glTexCoord2f(0, 1); glVertex3f(-size, -size, -size);
glEnd();
glBindTexture(GL_TEXTURE_2D, box[5]);
glBegin(GL_QUADS);
//bottom
glTexCoord2f(0, 0); glVertex3f(-size, size, -size);
glTexCoord2f(1, 0); glVertex3f( size, size, -size);
glTexCoord2f(1, 1); glVertex3f( size, size, size);
glTexCoord2f(0, 1); glVertex3f(-size, size, size);
glEnd();
window.display();
}
}
Answer the question
In order to leave comments, you need to log in
Most likely, everything is normal. Wrong side turned out.
Try to reverse the vertex enumeration order between
glBegin(GL_QUADS) ... glEnd
UPD: I have the following cube drawing code in Java, try it:
class Cube {
private FloatBuffer mVertexBuffer;
private FloatBuffer mColorBuffer;
private ByteBuffer mIndexBuffer;
private float vertices[] = {
-1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f
};
private float colors[] = {
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 0.5f, 0.0f, 1.0f,
1.0f, 0.5f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
1.0f, 0.0f, 1.0f, 1.0f
};
private byte indices[] = {
0, 4, 5, 0, 5, 1,
1, 5, 6, 1, 6, 2,
2, 6, 7, 2, 7, 3,
3, 7, 4, 3, 4, 0,
4, 7, 6, 4, 6, 5,
3, 0, 1, 3, 1, 2
};
public Cube() {
ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
mVertexBuffer = byteBuf.asFloatBuffer();
mVertexBuffer.put(vertices);
mVertexBuffer.position(0);
byteBuf = ByteBuffer.allocateDirect(colors.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
mColorBuffer = byteBuf.asFloatBuffer();
mColorBuffer.put(colors);
mColorBuffer.position(0);
mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
mIndexBuffer.put(indices);
mIndexBuffer.position(0);
}
public void draw(GL10 gl) {
gl.glFrontFace(GL10.GL_CW);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, mColorBuffer);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glDrawElements(GL10.GL_TRIANGLES, 36, GL10.GL_UNSIGNED_BYTE,
mIndexBuffer);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question