I'm working on getting a simple animation loop going in a wxWidets openGl app. I am having a problem with the loop iterations not fully drawing the screen before displaying it on each iteration. It seems like .1s is more than enough to fully draw the simple scene.... Can anyone give me an idea of why it might not?
Thanks!
#include <wx/wx.h>
#include <wx/glcanvas.h>
#ifdef __WXMAC__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#ifndef WIN32
#include <unistd.h> // FIXME: ¿This work/necessary in Windows?
//Not necessary, but if it was, it needs to be replaced by process.h AND io.h
#endif
//#include "GlBox.cpp"
#include <iostream>
using namespace std;
class wxGLCanvasSubClass: public wxGLCanvas {
public:
wxGLCanvasSubClass(wxFrame* parent);
void Paintit(wxPaintEvent& event);
void Render();
protected:
DECLARE_EVENT_TABLE()
//GlBox myBox;
};
wxGLCanvasSubClass *thing;
static void timerRender(int count)
{
thing->Render();
cout << "render timeout was called \n";
glutTimerFunc(25, timerRender, 0);
}
static void myIdleFunc()
{
cout << "idleing \n";
cout.flush();
}
static void setRender(wxGLCanvasSubClass *thing2)
{
thing = thing2;
cout << "global thing was set \n";
glutTimerFunc(1100, timerRender, 0);
}
BEGIN_EVENT_TABLE(wxGLCanvasSubClass, wxGLCanvas)
EVT_PAINT (wxGLCanvasSubClass::Paintit)
END_EVENT_TABLE()
wxGLCanvasSubClass::wxGLCanvasSubClass(wxFrame *parent)
:wxGLCanvas(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0, wxT("GLCanvas")){
int argc = 1;
char* argv[1] = { wxString((wxTheApp->argv)[0]).char_str() };
/*
NOTE: this example uses GLUT in order to have a free teapot model
to display, to show 3D capabilities. GLUT, however, seems to cause problems
on some systems. If you meet problems, first try commenting out glutInit(),
then try comeenting out all glut code
*/
glutInit(&argc, argv);
glutIdleFunc(myIdleFunc);
}
void wxGLCanvasSubClass::Paintit(wxPaintEvent& WXUNUSED(event)){
Render();
}
void wxGLCanvasSubClass::Render()
{
SetCurrent();
wxPaintDC(this);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, (GLint)GetSize().x, (GLint)GetSize().y);
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glColor3f(0.4, 0.5, 0.4);
glVertex2f(0.0, -0.8);
glEnd();
// using a little of glut
glColor4f(0,0,1,1);
glutWireTeapot(0.4);
glLoadIdentity();
glColor4f(2,0,1,1);
glutWireTeapot(0.6);
//myBox.getIt();
SwapBuffers();
glFlush();
//
}
class MyApp: public wxApp
{
virtual bool OnInit();
void onIdle(wxIdleEvent& evt);
time_t lastEvent;
bool slept ;
wxGLCanvas * MyGLCanvas;
wxGLCanvasSubClass * myCanvas;
};
IMPLEMENT_APP(MyApp)
bool MyApp::OnInit()
{
slept = false;
lastEvent = time( &lastEvent);
wxFrame *frame = new wxFrame((wxFrame *)NULL, -1, wxT("Hello GL World"), wxPoint(50,50), wxSize(200,200));
myCanvas = new wxGLCanvasSubClass(frame);
frame->Show(TRUE);
Connect( wxID_ANY, wxEVT_IDLE, wxIdleEventHandler(MyApp::onIdle) );
return TRUE;
}
void MyApp::onIdle(wxIdleEvent& evt)
{
if(slept)
{
slept = false;
cout << "event hit " << lastEvent;
myCanvas->Render();
evt.RequestMore();
} else {
cout << "idleing \n " << lastEvent;
usleep(100000);
slept = true;
evt.RequestMore();
}
}
GLUT is not a mandatory thing to use when it comes to OpenGL. GLUT is a third party framework, wxWidgets is a third party framework. And both implement a event loop.
You should not mix these. Just use wxWidgets only, which provides a very fine OpenGL widget.
Related
how do I get my C++ glut project to have smooth input?
the only way I could get input was with a slight delay, which is annoying. I am new to C++ glut so pls help me.
#include <windows.h>
#include <GL/glut.h>
#include <GL/glu.h>
#include <stdio.h>
#include <iostream>
GLboolean upPressed = false;
using namespace std;
void display();
void reshape(int,int);
void timer(int);
void keyboard_callback(int, int, int);
void SpecialKeysUp(int, int, int);
void update();
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowPosition(200, 100);
glutInitWindowSize(500, 500);
glutCreateWindow("window");
glutSpecialFunc(keyboard_callback);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(0,timer,0);
glutSpecialUpFunc(SpecialKeysUp);
glutIdleFunc(update);
init();
glutMainLoop();
}
// sy is the first squares y position. no x value needed since the x value is static.
// also note that the "s" in sy stands for square, squarey_position
float x_position = 0.0;
float sy_position = -4.0;
int state = 1;
char input;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glBegin(GL_QUADS);
glColor3f(1.0,0.0,0.0);
glVertex2f(x_position,1.0);
glVertex2f(x_position,-1.0);
glVertex2f(x_position+2.0,-1.0);
glVertex2f(x_position+2.0,1.0);
glColor3f(1.0,0.0,0.0);
glVertex2f(-8.0,sy_position);
glVertex2f(-9.0,sy_position);
glVertex2f(-9.0,sy_position+8.0);
glVertex2f(-8.0,sy_position+8.0);
glEnd();
glutSwapBuffers();
}
void reshape(int w,int h)
{
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-10, 10, -10, 10);
glMatrixMode(GL_MODELVIEW);
}
void timer(int)
{
glutPostRedisplay();
glutTimerFunc(1000/60,timer,0);
switch(state)
{
case 1:
if(x_position<8)
x_position+=0.15;
else
state = -1;
break;
case -1:
if(x_position>-10)
x_position-=0.15;
else
state=1;
break;
}
}
void keyboard_callback(int key, int, int)
{
if(GLUT_KEY_UP == key)
upPressed = true;
}
void SpecialKeysUp(int key, int, int)
{
if(GLUT_KEY_UP == key)
upPressed = false;
}
void update()
{
// glutPostRedisplay();
// glutTimerFunc(1000/60,update,0);
if(upPressed)
sy_position+=0.15;
}
It is a ping pong game and currently the ball is working fine, however I am trying to get smooth input for the rectangle but the only way I could was with input delay.
I am new to glut C++ so pls help me
Actually I figured out how to do it. The glutTimerFunc updates and resets every 1000/60 milliseconds so valuing a bool at "true" whilst the up key is pressed(with GLUT_KEY_UP) and then valuing a bool at "false" when the key is not pressed(with the SpecialUpFunc) makes for smoother input since the update function resets must faster than just holding the key down.
Let's say I want to render an animated triangle using two threads. One thread manipulates the vertices and the other one manages the QT window and renders the GL context. I have a class cTriangle, with members Eigen::Vector3d m_A, m_B, m_C representing the triangle vertices and a method void updateCoordinates() that operates on the vertices.
cTriangle.h:
#ifndef CTRIANGLE_H
#define CTRIANGLE_H
#include <Eigen/Core>
#include <QObject>
#include <vector>
class cTriangle : public QObject
{
Q_OBJECT
public:
cTriangle();
public:
Eigen::Vector3d m_A;
Eigen::Vector3d m_B;
Eigen::Vector3d m_C;
public slots:
void updateCoordinates();
};
#endif // CTRIANGLE_H
cTriangle.cpp:
#include "cTriangle.h"
#include <iostream>
cTriangle::cTriangle()
{
m_A = Eigen::Vector3d(0.0, 0.0, 0.0);
m_B = Eigen::Vector3d(0.2, 0.0, 0.0);
m_C = Eigen::Vector3d(0.0, 0.2, 0.0);
}
void cTriangle::updateCoordinates()
{
int ctr = 0;
double t = 0.0;
while (true)
{
std::cout << "cTriangle::updateCoordinates " << ctr << std::endl;
double offset = 0.3*sin(t);
m_A = Eigen::Vector3d(offset, 0.0, 0.0);
t += 0.00001;
ctr++;
}
}
The driver creates a new cTriangle and initiates a QThread, which continuously loops over updateCoordinates(). I also initialize the MainWindow containing a GLWidget.
main.cpp:
int main(int argc, char *argv[])
{
cTriangle* tri = new cTriangle();
QThread* thread = new QThread;
tri->moveToThread(thread);
QObject::connect(thread, SIGNAL(started()), tri, SLOT(updateCoordinates()) );
thread->start();
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
glwidget.h:
#ifndef GLWIDGET_H
#define GLWIDGET_H
#include <QGLWidget>
#include "cTriangle.h"
class GLWidget : public QGLWidget
{
Q_OBJECT
public:
explicit GLWidget(QWidget *parent = 0);
void initializeGL();
void resizeGL(int w, int h);
private:
signals:
void repaint();
public slots:
void paintGL();
};
#endif // GLWIDGET_H
glwidget.cpp:
GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent)
{
}
void GLWidget::initializeGL()
{
glClearColor(0.2, 0.2, 0.2, 1);
}
void GLWidget::paintGL()
{
std::cout << "paintGL " << std::endl;
// ********************************
// How do I access Triangle data here??
// ********************************
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glBegin(GL_TRIANGLES);
glColor3f (1,0,0);
glVertex3f (0, -0.5, 0);
glColor3f (0,1,0);
glVertex3f (0.5, -0.5, 0);
glColor3f (0,0,1);
glVertex3f (0.0, 0.5, 0);
glEnd();
}
void GLWidget::resizeGL(int w, int h)
{}
Finally, my question: I need to access the vertex data in the object tri from within GLWidget::painGL. What is the best method for doing so? Setting up signal-slot functionality? Passing a pointer of tri in a thread-safe manner (if so, how?). This is just a toy example, generally the vertex data could be very large. Thanks!!
=============================
One step further:
My plan now is set set up a signal->signal->slot mechanics. GLWidget::glPaint() emits a SIGNAL GLWidget::requestVertices() to another SIGNAL cTriangle::sendVertices(whateverType vertexData), which is then received by SLOT GLWidget::receiveVertices(whateverType vertexData). In main.cpp, how do I CONNECT these signals? How do I get the reference to the GLWidget object? QObject::connect( ???, SIGNAL( requestVertices() ), tri, SIGNAL( sendVertices(double arg) ) );
Start by using the signal and slot mechanism. If you emit a signal with a QList of points, the QList uses implicit sharing, so will only copy-on-write.
This is the simplest method for using data across threads and doesn't require any handling of locks by you. If at some point you find this to be too slow, then you can look into optimising the code to improve the speed.
One possible method of optimisation would be to use multiple QList of points, so you write to one, then move on to writing to the next, while emitting the first, to ensure you're not writing to the list being shared and thus minimising the chance of a copy-on-write occurring.
However, there is no need to optimise, unless you're seeing a problem in the first place.
I have problem understanding some opengl stuff using GLFW and GLEW.
i have 3 files shown below:
main.cpp:
#include "gamewindow.h"
int main() {
GameWindow *gameWindow = new GameWindow(1024, 768, "FirstOpenGLGame");
/* Loop until the user closes the window */
while (gameWindow->getRunning()) {
/* Render here */
gameWindow->render();
gameWindow->update();
gameWindow->setRunning();
}
delete gameWindow;
glfwTerminate();
return 0;
}
This is where the problem is, gamewindow.cpp:
#include "gamewindow.h"
GameWindow::GameWindow(int width, int height, const char* title) : _running(true), _height(1024), _width(1024 * (16/9))
{
/* Initialize the library */
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(width, height, title, NULL, NULL);
if(!window) {
glfwTerminate();
exit(0);
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
if(!glewInit()){ // <-- problem is this
glfwTerminate();
exit(EXIT_FAILURE);
}
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
coordSettings();
}
void GameWindow::setRunning() {
_running = !glfwWindowShouldClose(window);
}
bool GameWindow::getRunning() {
return _running;
}
void GameWindow::render() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
glVertex2d(0.0f, 0.0f);
glVertex2d(100.0f, 0.0f);
glVertex2d(100.0f, 800.0f);
glVertex2d(0.0f, 800.0f);
glEnd();
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
void GameWindow::update() {
}
void GameWindow::coordSettings() {
glViewport( 0, 0, _width, _height );
glMatrixMode(GL_PROJECTION);
glOrtho(0.0, _width, 0.0, _height, 0.0, -1.0);
glMatrixMode(GL_MODELVIEW);
}
and last the header file gamewindow.h:
#ifndef GAMEWINDOW_H
#define GAMEWINDOW_H
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
class GameWindow
{
private:
GLFWwindow* window;
bool _running;
GLfloat _width;
GLfloat _height;
void coordSettings();
public:
GameWindow(int width, int height, const char* title);
void setRunning();
bool getRunning();
void render();
void update();
};
#endif // GAMEWINDOW_H
everything works fine, but then i try to call glewInit() (without really understanding is i need to, or when i need to) but then nothing works. the program starts, but there is no window with a quad in it, like before. why is this? how is GLEW even used, and do i need it?
I am using SDL2.00 with OpenGL to create a program. The program isn't supposed to do anything at the moment it's just a test bed. However I ran into a problem. When I create a window using SDL_CreateWindow, the window goes into a busy state and stops responding. The program flow isn't really affected by this however the window itself just won't work. All it does is show a white blank window and accept no input, can't resize can't move and can't quit. I will attach the code but I doubt it is code related since I already made a few programs using SDL and they seem to work just fine.
Using VS2013 SDL2.00 OpenGL
=========main========
#include "stdafx.h"
void init()
{
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, 640.0 / 480.0, 1.0, 500.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex3f(0.0, 2.0, -5.0);
glVertex3f(-2.0, -2.0, -5.0);
glVertex3f(2.0, -2.0, -5.0);
glEnd();
}
int main(int argc, char* argv[]){
SDL_Init(SDL_INIT_VIDEO);
SDL_Window * window;
window = SDL_CreateWindow("OpenGLTest", 300, 300, 640, 480, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
init();
while (true){
display();
SDL_GL_SwapWindow(window);
}
return 0;
}
=========stdafx======
#pragma once
#include <iostream>
#include <SDL.h>
#include <Windows.h>
#include <gl/GL.h>
#include <gl/GLU.h>
#define PI 3.14159265
using namespace std;
You forgot to process all the events, so the SDL window is just waiting and waiting and waiting, thereby "not responding" - Simply adding that should fix the problem!
while (true) {
SDL_PumpEvents();
display();
SDL_GL_SwapWindow(window);
}
You could also pull all the events manually with a loop calling SDL_PollEvent(SDL_Event *event)
SDL_Event event;
while (SDL_PollEvent(&event)) {
// Process the event...
}
Wiki
SDL_PumpEvents() - http://wiki.libsdl.org/SDL_PumpEvents
SDL_PollEvent() - http://wiki.libsdl.org/SDL_PollEvent
I am looking for a way to display two png images (in fact, it will be numbers, my aim is to create an overlay for live streaming to display players score and some further additional content) on a window with transparent background with no border. So that we just see the score over the window placed behind.
Is there any way to do something like that ? I have already tried many things with SDL, textures, but nothing led me to any satisfying result. The best I found was some dirty code almost working but completely unusable.
If possible, the solution may let possible to add the functionality that when you click left or right on one of the 2 scores, it is incremented or decremented.
Edit: Here is my current code. I started new without any bit of the dirty code I had before, because I would like to understand what I am doing. I have my 2 numbers displayed as textures, now I would like to remove borders and title bar of my window, and make my background transparent.
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <windows.h>
#include <windowsx.h>
#include <math.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include "sdlglutils.h"
#include <assert.h>
#include <tchar.h>
using namespace std;
int overlay;
int TimerEnabled;
GLfloat posX, posY, posZ;
GLuint texture_0, texture_1, texture_2, texture_3;
void Initialize()
{
glEnable(GL_ALPHA_TEST);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0, 0, 0, 0);
glEnable(GL_TEXTURE_2D);
}
void Reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)w/(GLfloat)h,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
texture_0 = loadTexture("0.png");
texture_1 = loadTexture("1.png");
texture_2 = loadTexture("2.png");
texture_3 = loadTexture("3.png");
return;
}
void Draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glTranslatef(posX,posY,posZ);
glScalef(1.0f,1.0f,1.0f);
glPushMatrix();
glTranslated(-6, 8, 0);
glBindTexture(GL_TEXTURE_2D, texture_1);
glBegin(GL_QUADS);
glTexCoord2d(0,0); glVertex2d(0,0);
glTexCoord2d(1,0); glVertex2d(1,0);
glTexCoord2d(1,1); glVertex2d(1,1);
glTexCoord2d(0,1); glVertex2d(0,1);
glEnd();
glPopMatrix();
glPushMatrix();
glTranslated(6, 8, 0);
glBindTexture(GL_TEXTURE_2D, texture_2);
glBegin(GL_QUADS);
glTexCoord2d(0,0); glVertex2d(0,0);
glTexCoord2d(1,0); glVertex2d(1,0);
glTexCoord2d(1,1); glVertex2d(1,1);
glTexCoord2d(0,1); glVertex2d(0,1);
glEnd();
glPopMatrix();
glFlush();
}
void Display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
Draw();
glutSwapBuffers();
}
void KeyboardSpecialEvent( int key, int x, int y)
{
switch(key)
{
case(GLUT_KEY_UP) :
{
posY += 0.3;
}break;
case(GLUT_KEY_DOWN) :
{
posY -= 0.3;
}break;
case(GLUT_KEY_LEFT) :
{
posX -= 0.3;
}break;
case(GLUT_KEY_RIGHT) :
{
posX += 0.3;
}break;
}
}
void MouseEvent( int button, int state, int x, int y){ };
void MotionMouseEvent( int x, int y ){ };
void IdleEvent(){ };
void TimerEvent(int time)
{
glutPostRedisplay();
if(TimerEnabled)
glutTimerFunc(10, TimerEvent, time);
}
void KeyboardEvent( unsigned char key, int x, int y)
{
switch(key)
{
case ' ' :
{
TimerEnabled = !TimerEnabled;
if (TimerEnabled)
glutTimerFunc(40, TimerEvent, 0);
}
break;
case 'q' :
{
exit(0);
}
break;
}
}
int main(int argc, char** argv)
{
posX = 0;
posY = 0;
posZ = -25;
TimerEnabled = 1;
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(1600,900);
glutInitWindowPosition(0,0);
overlay = glutCreateWindow("ScoreOverlay");
//glutFullScreen();
Initialize();
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);
glutKeyboardFunc(KeyboardEvent);
glutSpecialFunc(KeyboardSpecialEvent);
glutMouseFunc(MouseEvent);
glutMotionFunc(MotionMouseEvent);
glutIdleFunc(IdleEvent);
glutTimerFunc(40, TimerEvent, 0);
glutMainLoop();
return 0;
}
Here is a working base sample (using c++ with clr support) that could help you.
You need to adapt it to your need (for example use DrawImage instead of FillEllipse)
using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
ref class MyForm : public Form
{
public:
MyForm()
{
this->m_brush = gcnew SolidBrush(Color::Blue);
this->m_canvas = gcnew System::Windows::Forms::Panel();
this->m_canvas->BackColor = Color::Pink;
this->m_canvas->Dock = System::Windows::Forms::DockStyle::Fill;
this->m_canvas->Location = System::Drawing::Point(0, 0);
this->m_canvas->Margin = System::Windows::Forms::Padding(0);
this->m_canvas->Name = "Canvas";
this->m_canvas->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &MyForm::canvas_Paint);
this->m_canvas->TabIndex = 0;
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->BackColor = Color::Pink;
this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::None;
this->ClientSize = System::Drawing::Size(200, 200);
this->Controls->Add(this->m_canvas);
this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &MyForm::form_KeyDown);
this->TransparencyKey = Color::Pink;
this->Name = "MyForm";
this->Text = "MyForm";
}
private:
void canvas_Paint(Object^ sender, PaintEventArgs^ e) {
e->Graphics->FillEllipse(this->m_brush, Rectangle(50, 50, 100, 100));
}
void form_KeyDown(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e) {
// Test key ...
this->m_canvas->Invalidate();
// ...
}
Brush^ m_brush;
Panel^ m_canvas;
};
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
Application::Run(gcnew MyForm());
return 0;
}
It looks like you need this on Windows. Have you looked at Layered Windows?
Unless you need OpenGL for the graphics for some reason I think you'll be better off loading your images as regular bitmaps and blitting them to the layered window directly. Using OpenGL you would need to draw to a texture map, then take that texture and blit it to the layered window.