#include<mx/mx.h>
namespace glexample {
class glWindow : public mx::mxWnd {
public:
glWindow();
virtual ~glWindow() { }
virtual void renderScreen();
virtual void eventPassed(SDL_Event &e);
virtual void resizeWindow(int w, int h);
};
glWindow::glWindow() : mxWnd(640, 480, 0, 0, true) {
setTitle("Rotating Triangle");
resizeWindow(640, 480);
}
void glWindow::renderScreen() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glShadeModel(GL_SMOOTH);
static float rot_value = 1.0f;
rot_value += 1.0f;
glRotatef(rot_value, 1.0f, 1.0f, 11.0f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f,50.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(-50.0f, 0.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(50.0f, 0.0f, 0.0f);
glEnd();
glFlush();
SDL_GL_SwapBuffers();
}
void glWindow::eventPassed(SDL_Event &e) {
mxWnd::eventPassed(e);
}
void glWindow::resizeWindow(int w, int h) {
GLfloat range = 100.0f;
if(h == 0) h = 1;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glOrtho( -range, range, -range*h/w, range*h/w, -range, range);
else
glOrtho( -range*w/h, range*w/h, -range, range, -range, range);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
}
int main(int argc, char **argv) {
try {
glexample::glWindow window;
return window.messageLoop();
}
catch( mx::mxException<std::string> &e ) {
e.printError(std::cerr);
}
catch( std::exception &e ) {
std::cerr << e.what() << "\n";
}
catch ( ... ) {
std::cerr << "unknown exception\n";
}
return EXIT_FAILURE;
}
Download GL example
|