PDA

View Full Version : Issue with depth in opengl with wxWidgets



nenekira
05-20-2016, 03:53 AM
I started making a 3D app with wxWidgets in C++, and to start I tried to draw a multicolor cube.
But I have issue with the depth, I lack faces.
Here is the code of my wxGLContext, which initialize and draw :

MyGLContext.hpp


#ifndef MYGLCONTEXT_HPP
#define MYGLCONTEXT_HPP

#include "wx/glcanvas.h"
#include <GL/glut.h>
#include <iostream>

#include "MyGLCanvas.hpp"

using namespace std;

class MyGLContext : public wxGLContext
{
protected:

public:
MyGLContext(wxGLCanvas* canvas) : wxGLContext(canvas)
{
SetCurrent(*canvas);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.0, 1.0, 1.0, 100.0);
glEnable(GL_DEPTH_TEST);
};
~MyGLContext() {};
void DrawSomething();
};

#endif



MyGLContext.cpp


#include "wx/wxprec.h"

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

#include "MyGLContext.hpp"

using namespace std;

void MyGLContext::DrawSomething()
{
glDepthMask(GL_TRUE);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

// glTranslated(0.0, 0.0, -5.0);
gluLookAt(2.0, 2.0, 2.0,
0.0, 0.0, 0.0,
0.0, 0.0, 1.0);

glBegin(GL_QUADS);

glColor3ub(255,0,0);
glVertex3d(1,1,1);
glVertex3d(-1,1,1);
glVertex3d(-1,1,-1);
glVertex3d(1,1,-1);

glColor3ub(0,255,0);
glVertex3d(1,-1,1);
glVertex3d(1,1,1);
glVertex3d(1,1,-1);
glVertex3d(1,-1,-1);

glColor3ub(0,255,255);
glVertex3d(1,-1,1);
glVertex3d(-1,-1,1);
glVertex3d(-1,-1,-1);
glVertex3d(1,-1,-1);

glColor3ub(255,255,0);
glVertex3d(-1,-1,1);
glVertex3d(-1,1,1);
glVertex3d(-1,1,-1);
glVertex3d(-1,-1,-1);

glColor3ub(0,0,255);
glVertex3d(1,-1,1);
glVertex3d(1,1,1);
glVertex3d(-1,1,1);
glVertex3d(-1,-1,1);

glColor3ub(255,0,255);
glVertex3d(1,-1,-1);
glVertex3d(1,1,-1);
glVertex3d(-1,1,-1);
glVertex3d(-1,-1,-1);

glEnd();

glFlush();

GLenum errLast = GL_NO_ERROR;
while(1)
{
GLenum err = glGetError();
if(err == GL_NO_ERROR)
{
cout << "NO ERROR" << endl;
return;
}
if ( err == errLast )
{
cout << "OpenGL error state couldn't be reset." << endl;
return;
}
errLast = err;
cout << "OpenGL error" << err << endl;
}
}



I notices that if I comment the line "glEnable(GL_DEPTH_TEST);" and if i remove the " GL_DEPTH_BUFFER_BIT" from the "glClear" I got the same result. I tried to add "glDepthMask(GL_TRUE);" because I have seen when i made research that it solved the problem for several people but it don't work on my case.
I guess it's an issue with the glEnable and/or the glClear. But I don't know how to correct it, so please help me !

nenekira
05-23-2016, 01:19 AM
More precisions :

If i try to use the same drawing code and initialization code with the SDL instead of wxWidgets, it works.
And with textures (like in the cube sample give with wxWidgets) it works, but with simple colors it don't works.


Help please :(
Did someone already managed to draw a cube with wxWidgets with several colors on the faces ?

GClements
05-23-2016, 02:28 AM
You can't reliably use SetCurrent() or any OpenGL functions in the context constructor because the associated canvas may not be visible at that point. The SetCurrent() method should only be called in situations where the window is guaranteed to be visible, e.g. in response to paint or resize events.

nenekira
05-23-2016, 03:02 AM
Ok thanks for the answer. So I will try to put SetCurrent() on a paint event