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 !