Horde3D not rendering to GLFW window in OS X - c++

I'm an experienced programmer but totally new to C++. I'm using GLFW 3 and Horde3D in Xcode 5 on OS X 10.5.
I've followed the basic tutorials of GLFW and Horde3D. I'm able to create a window, make it the current context, and apparently a simple game loop is running fine, including h3dRender( cam ). But all I get is a black window. Any insight as to the step (or entire concept) I'm missing?
Thank you! (code below)
#include <iostream>
#include <GLFW/glfw3.h>
#include <Horde3D.h>
#include <Horde3DUtils.h>
GLFWwindow* window;
H3DNode model = 0, cam = 0;
int winWidth = 640, winHeight = 480;
float fps = 24;
static float t = 0;
bool running = false;
bool initWindow();
bool initGame();
void gameLoop();
void errorListener( int, const char* );
void windowCloseListener( GLFWwindow* );
int main(void)
{
glfwSetErrorCallback( errorListener );
if ( !initWindow() ) return -1;
if ( !initGame() ) return -1;
running = true;
while ( running )
{
gameLoop();
}
h3dRelease();
glfwDestroyWindow( window );
glfwTerminate();
exit( EXIT_SUCCESS );
}
bool initWindow()
{
if ( !glfwInit() ) return -1;
window = glfwCreateWindow( winWidth, winHeight, "Hello World", NULL, NULL );
if ( !window )
{
glfwTerminate();
exit( EXIT_FAILURE );
}
glfwSetWindowCloseCallback( window, windowCloseListener );
glfwMakeContextCurrent( window );
glfwSwapInterval( 0 );
return true;
}
bool initGame()
{
if ( !h3dInit() ) return false;
H3DRes pipeRes = h3dAddResource( H3DResTypes::Pipeline, "standard.pipeline", 0 );
H3DRes modelRes = h3dAddResource( H3DResTypes::SceneGraph, "character.scene.xml", 0 );
H3DRes animRes = h3dAddResource( H3DResTypes::Animation, "walk.anim.xml", 0 );
h3dutLoadResourcesFromDisk( "" );
model = h3dAddNodes( H3DRootNode, modelRes );
h3dSetupModelAnimStage( model, 0, animRes, 0, "", false );
H3DNode light = h3dAddLightNode( H3DRootNode, "Light 1", 0, "LIGHTING", "SHADOWMAP" );
h3dSetNodeTransform( light, 0, 20, 0, 0, 0, 0, 1, 1, 1 );
h3dSetNodeParamF( light, H3DLight::RadiusF, 0, 50.0f );
cam = h3dAddCameraNode( H3DRootNode, "Camera", pipeRes );
h3dSetNodeParamI( cam, H3DCamera::ViewportXI, 0 );
h3dSetNodeParamI( cam, H3DCamera::ViewportYI, 0 );
h3dSetNodeParamI( cam, H3DCamera::ViewportWidthI, winWidth );
h3dSetNodeParamI( cam, H3DCamera::ViewportHeightI, winHeight );
h3dSetupCameraView( cam, 45.0f, ( float ) winWidth / winHeight, 0.5f, 2048.0f );
h3dResizePipelineBuffers( pipeRes, winWidth, winHeight );
return true;
}
void gameLoop ()
{
t = t + 10.f * ( 1/ fps );
h3dSetModelAnimParams( model, 0, t, 1.0f );
h3dSetNodeTransform( model, t * 10, 0, 0, 0, 0, 0, 1, 1, 1 );
h3dRender( cam );
h3dFinalizeFrame();
glfwSwapBuffers( window );
glfwPollEvents();
}
void errorListener( int error, const char* description )
{
fputs( description, stderr );
}
void windowCloseListener( GLFWwindow* window )
{
running = false;
}

You are not able to view anything because the resources have not been loaded for your application. You need to specify the directory path which holds the resources(resources like character.scene.xml, walk.anim.xml etc.) as the an argument to the h3dutLoadResourcesFromDisk()

Related

Flickering background color

I created an OpenGL window using SDL2 but the background keeps switching between black and yellow.
#include <SDL2/SDL.h>
#include <GL/glew.h>
#define SCREEN_WIDTH 500
#define SCREEN_HEIGHT 500
int main( int argc, char** argv )
{
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 3 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
SDL_GL_SetAttribute( SDL_GL_ACCELERATED_VISUAL, 1 );
SDL_Window* gWindow = SDL_CreateWindow(
"Title",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_OPENGL);
SDL_GL_CreateContext( gWindow );
glewExperimental = GL_TRUE;
glewInit();
glPointSize(3);
glClearColor(1,1,0,0);
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
glClear(GL_COLOR_BUFFER_BIT);
SDL_GL_SetSwapInterval(1);
int quit=0;
SDL_Event event;
while( !quit )
{
while(SDL_PollEvent( &event ))
{
if( event.type == SDL_QUIT )
quit = 1;
}
SDL_GL_SwapWindow( gWindow );
}
SDL_DestroyWindow(gWindow);
return 0;
}
I expect the background to be yellow, as defined with glClearColor(1,1,0,0), without flickering while the program runs. Is there something wrong in the code?
The reason for flickering is that you're using double buffering but do not clear one of the buffers with the yellow color (i.e. note that you're calling glClear only once in your code).
I suggest you call glClear every frame. To fix your code, you can move the call into the loop, just before the SDL_GL_SwapWindow:
while( !quit )
{
while(SDL_PollEvent( &event ))
{
if( event.type == SDL_QUIT )
quit = 1;
}
glClear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapWindow( gWindow );
}

Auto redraw window possible in SDL-2?

Here is a minimal SDL-1.2 text output example (stripped from the lazyfoo tutorial for ttf):
#include <SDL.h>
#include <SDL_ttf.h>
#include <string>
SDL_Surface *message = NULL;
SDL_Surface *screen = NULL;
SDL_Event event;
TTF_Font *font = NULL;
SDL_Color color = { 255, 255, 255 };
int main( int argc, char* args[] )
{
SDL_Init( SDL_INIT_EVERYTHING );
screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );
TTF_Init();
SDL_WM_SetCaption( "Test message", NULL );
font = TTF_OpenFont( "anyttffont.ttf", 20 );
message = TTF_RenderText_Solid(
font, "My test message", color );
SDL_Rect offset;
offset.x = 0;
offset.y = 150;
SDL_BlitSurface( message, NULL, screen, &offset );
SDL_Flip( screen );
bool quit = false;
while( quit == false )
while( SDL_PollEvent( &event ) )
if( event.type == SDL_QUIT )
quit = true;
SDL_FreeSurface( message );
TTF_CloseFont( font );
TTF_Quit();
SDL_Quit();
return 0;
}
The resulting executable redraws the window automatically after re-exposing it (after minimizing or obscuring).
Is it possible to do something like this in SDL-2?
I tried the equivalent tutorial from lazyfoo for SDL-2, but this has code to constantly re-render the text in the event loop. It stops re-drawing when the render code is moved in front of the loop. I also tried rewriting it using the window surface directly and then using SDL_UpdateWindowSurface(gWindow);, but this behaves the same way.

SDL freeze (black screen)

Here is my full code.
When I click right for a few times(animation works normally), the screen freezes. I think the lines from if(SDL_GetTicks() - start_time < 1000) on are the problem (because of too many executions?)?
const int SCREEN_WIDTH = 600;
const int SCREEN_HEIGHT = 500;
const int SCREEN_BPP = 32;
//The surfaces
SDL_Surface *background = NULL;
SDL_Surface *upMessage = NULL;
SDL_Surface *downMessage = NULL;
SDL_Surface *leftMessage = NULL;
SDL_Surface *rightMessage = NULL;
SDL_Surface *message = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *finish = NULL;
SDL_Surface *startStop = NULL;
SDL_Surface *one = NULL;
SDL_Surface *two = NULL;
SDL_Surface *three = NULL;
SDL_Surface *four = NULL;
SDL_Surface *five = NULL;
//The event structure
SDL_Event event;
SDL_Surface *load_image( std::string filename ){
//The image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized surface that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
loadedImage = IMG_Load( filename.c_str() );
//If the image loaded
if( loadedImage != NULL )
{
//Create an optimized surface
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old surface
SDL_FreeSurface( loadedImage );
//If the surface was optimized
if( optimizedImage != NULL )
{
//Color key surface
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
}
}
//Return the optimized surface
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
//Holds offsets
SDL_Rect offset;
//Get offsets
offset.x = x;
offset.y = y;
//Blit
SDL_BlitSurface( source, clip, destination, &offset );
}
bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}
//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//If there was an error in setting up the screen
if( screen == NULL )
{
return false;
}
//Set the window caption
SDL_WM_SetCaption( "Press an Arrow Key", NULL );
//If everything initialized fine
return true;
}
bool load_files()
{
//Load the background image
background = load_image( "background .png" );
message = load_image( "guy.png" );
finish = load_image( "finish .png" );
//If there was a problem in loading the background
if( background == NULL )
{
return false;
}
//If everything loaded fine
return true;
}
void clean_up()
{
//Free the surfaces
SDL_FreeSurface( background );
SDL_FreeSurface( upMessage );
SDL_FreeSurface( downMessage );
SDL_FreeSurface( leftMessage );
SDL_FreeSurface( rightMessage );
SDL_FreeSurface( startStop );
SDL_FreeSurface( one );
SDL_FreeSurface( two );
SDL_FreeSurface( three );
SDL_FreeSurface( four );
SDL_FreeSurface( five );
//Quit SDL
SDL_Quit();
}
void anim(SDL_Surface *number,std::string name[],int n){
number = NULL;
number = load_image(name[n] +".png" );
apply_surface( 0, 0, number, screen );
}
int main( int argc, char* args[] ){
bool quit = false;
bool start = true;
bool cilj_test = true;
int x=35,y=10;
bool animation = false;
Uint32 start_time = 0;
//The timer start/stop flag
bool running = false;
//Initialize
if( init() == false )
{
return 1;
}
//Load the files
if( load_files() == false )
{
return 1;
}
//Apply the background
apply_surface( 0, 0, background, screen );
//While the user hasn't quit
while( quit == false ){
//If there's an event to handle
if( SDL_PollEvent( &event ) ){
//If a key was pressed
if( event.type == SDL_KEYDOWN ){
//Set the proper message surface
switch( event.key.keysym.sym ){
case SDLK_UP:
break;
case SDLK_DOWN:
start=false;
if(y!=410 && animation==false){
y+=100;
SDL_FillRect( screen, 0, 0 );
background = NULL;
message = NULL;
background = load_image( "background .png" );
apply_surface( 0, 0, background, screen );
finish = load_image( "finish.png" );
apply_surface(506, 420, finish, screen );
message = load_image( "guy.png" );
apply_surface( x, y, message, screen );
// if()
}
break;
case SDLK_LEFT:
break;
case SDLK_RIGHT:
start=false;
if(x!=535 && animation==false){
x+=100;
SDL_FillRect( screen, 0, 0 );
background = NULL;
message = NULL;
background = load_image( "background.png" );
apply_surface( 0, 0, background, screen );
finish = load_image( "finish.png" );
apply_surface(506, 420, finish, screen );
message = load_image( "guy.png" );
apply_surface( x, y, message, screen );
if(x==535 && y==410){
start_time=SDL_GetTicks();
animation=true;
x=35,y=10;
}
}
break;
}
}
//If the user has Xed out the window
else if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}
if(animation){
std::string name[5]={"one","two","three","four","five"};
if(SDL_GetTicks() - start_time < 1000){
anim(one,name,4);
} else if(SDL_GetTicks() - start_time > 1000 && SDL_GetTicks() - start_time < 2000){
anim(two,name,3);
} else if(SDL_GetTicks() - start_time > 2000 && SDL_GetTicks() - start_time < 3000){
anim(three,name,2);
} else if(SDL_GetTicks() - start_time > 3000 && SDL_GetTicks() - start_time < 4000){
anim(four,name,1);
} else if(SDL_GetTicks() - start_time > 4000 && SDL_GetTicks() - start_time < 5000){
anim(five,name,0);
} else if(SDL_GetTicks() - start_time > 5000){
SDL_FillRect( screen, 0, 0 );
background = NULL;
message = NULL;
background = load_image( "background.png" );
apply_surface( 0, 0, background, screen );
finish = load_image( "finish.png" );
apply_surface(506, 420, finish, screen );
message = load_image( "guy.png" );
apply_surface( x, y, message, screen );
animation=false;
start_time=0;
}
}
if( message != NULL ){
if(start){
apply_surface(35, 10, message, screen );
apply_surface(506, 420, finish, screen );
}
message = NULL;
}
//Update the screen
if( SDL_Flip( screen ) == -1 ){
return 1;
}
}
//Clean up
clean_up();
return 0;
}
There are two issues with your code.
You do not have a while loop, meaning that the application will draw once, get one input, and stop. You are experiencing a freeze because there are too many events on the event queue.
Not really the cause of your problem, but it would show up eventually. You are loading the image each time you want to draw it. Instead load all of the images before, and just apply the correct surface. If you won't loading the images and not freeing them will result in no running out of memory.
EDIT:
After seeing your whole code, it is the second issue that is the problem. Load all your images before the loop, and just blit them accordingly.

How to refresh the graphics to avoid black screen?

When I minimized my directx framework and open up the task manage then go back to the framework. It gives me the black screen. How should I modify my code to fix this flaw?
GraphicsClass.h
#ifndef _GRAPHICSCLASS_H_
#define _GRAPHICSCLASS_H_
//////////////
// INCLUDES //
//////////////
#include <d3dx9.h>
#include <assert.h>
#include <string>
using namespace std;
/////////////
// GLOBALS //
/////////////
const bool FULL_SCREEN = false;
const unsigned int SCREEN_WIDTH = 1280;
const unsigned int SCREEN_HEIGHT = 720;
////////////////////////////////////////////////////////////////////////////////
// Class name: GraphicsClass
////////////////////////////////////////////////////////////////////////////////
class GraphicsClass
{
public:
GraphicsClass( HWND hwnd );
~GraphicsClass();
void SetPath( const string& loadFolder );
void BeginFrame();
void EndFrame();
void Load( IDirect3DTexture9*& texture, string imgName, int imgWidth, int imgHeight );
void setFont(ID3DXFont*& font, char* fontName, int fontHeight);
void SourceRect( RECT& sourceRect, int left, int right, int top, int bottom );
void DrawText( ID3DXFont*& font, LPCSTR string, int charNum, RECT& SourceRect, DWORD format, D3DCOLOR color );
void Draw( IDirect3DTexture9* texture );
void Draw( IDirect3DTexture9* texture, float posX, float posY );
void Draw( IDirect3DTexture9* texture, RECT& sourceRect, float posX, float posY );
private:
string path;
HRESULT result;
ID3DXSprite* pSprite;
IDirect3D9* pDirect3D;
IDirect3DDevice9* pDevice;
};
#endif
GraphicsClass.cpp
#include "GraphicsClass.h"
GraphicsClass::GraphicsClass( HWND hwnd )
{
pDirect3D = Direct3DCreate9( D3D_SDK_VERSION );
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof( d3dpp ) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferWidth = SCREEN_WIDTH;
d3dpp.BackBufferHeight = SCREEN_HEIGHT;
d3dpp.hDeviceWindow = hwnd;
//d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
result = pDirect3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
assert( !FAILED(result) );
result = D3DXCreateSprite( pDevice, &pSprite );
assert( !FAILED(result) );
}
GraphicsClass::~GraphicsClass()
{
// Release and clean up sprite
pSprite->Release();
pSprite = NULL;
}
void GraphicsClass::SetPath( const string& path )
{
this->path = path;
}
void GraphicsClass::BeginFrame()
{
pDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 255, 255, 255 ), 1.0f, 0 );
pDevice->BeginScene();
// Specify alpha blend will ensure that the sprite will render the background with alpha.
pSprite->Begin( D3DXSPRITE_ALPHABLEND );
}
void GraphicsClass::EndFrame()
{
// End sprite drawing
pSprite->End();
pDevice->EndScene();
pDevice->Present( NULL, NULL, NULL, NULL );
}
void GraphicsClass::Load( IDirect3DTexture9*& texture, string imgName, int imgWidth, int imgHeight )
{
string fullPath;
fullPath = path + imgName;
result = D3DXCreateTextureFromFileEx( pDevice, fullPath.c_str(), imgWidth, imgHeight,
D3DX_DEFAULT, NULL, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED,
D3DX_DEFAULT, D3DX_DEFAULT, NULL,
NULL, NULL, &texture );
}
void GraphicsClass::setFont(ID3DXFont*& font, char* fontName, int fontHeight)
{
result = D3DXCreateFont(pDevice, fontHeight, 0, 0, 1, false,
DEFAULT_CHARSET, OUT_TT_ONLY_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH | FF_DONTCARE, fontName, &font);
}
void GraphicsClass::SourceRect( RECT& sourceRect, int left, int right, int top, int bottom )
{
sourceRect.left = left;
sourceRect.right = right;
sourceRect.top = top;
sourceRect.bottom = bottom;
}
void GraphicsClass::DrawText( ID3DXFont*& font, LPCSTR string, int charNum, RECT& SourceRect, DWORD format, D3DCOLOR color)
{
font->DrawText(pSprite, string, charNum, &SourceRect, format, color);
}
void GraphicsClass::Draw( IDirect3DTexture9* texture )
{
pSprite->Draw( texture, NULL, NULL, &D3DXVECTOR3( 0, 0, 0 ), D3DCOLOR_XRGB( 255, 255, 255 ) );
}
void GraphicsClass::Draw( IDirect3DTexture9* texture, float posX, float posY )
{
pSprite->Draw( texture, NULL, NULL, &D3DXVECTOR3( posX, posY, 0 ), D3DCOLOR_XRGB( 255, 255, 255 ) );
}
void GraphicsClass::Draw( IDirect3DTexture9* texture, RECT& sourceRect, float posX, float posY )
{
pSprite->Draw( texture, &sourceRect, NULL, &D3DXVECTOR3( posX, posY, 0 ), D3DCOLOR_XRGB( 255, 255, 255 ) );
}
SystemClass.cpp
void SystemClass::InitializeWindows()
{
WNDCLASSEX wc;
DEVMODE dmScreenSettings;
int screenWidth, screenHeight;
int posX, posY;
// Get an external pointer to this object
ApplicationHandle = this;
// Get the instance of this application
m_hinstance = GetModuleHandle( NULL );
// Give the application a name
m_applicationName = "Zero DirectX Framework";
// Setup the windows class with default settings
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = m_hinstance;
wc.hIcon = LoadIcon( NULL, IDI_WINLOGO );
wc.hIconSm = wc.hIcon;
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
wc.lpszMenuName = NULL;
wc.lpszClassName = m_applicationName;
wc.cbSize = sizeof( WNDCLASSEX );
// Register the window class
RegisterClassEx( &wc );
// Determine the resolution of the clients desktop screen
screenWidth = GetSystemMetrics( SM_CXSCREEN );
screenHeight = GetSystemMetrics( SM_CYSCREEN );
// Setup the screen settings depending on whether it is running in full screen or in windowed mode
if ( FULL_SCREEN )
{
// If full screen set the screen to maximum size of the users desktop and 32bit
memset( &dmScreenSettings, 0, sizeof(dmScreenSettings) );
dmScreenSettings.dmSize = sizeof(dmScreenSettings);
dmScreenSettings.dmPelsWidth = (unsigned long)screenWidth;
dmScreenSettings.dmPelsHeight = (unsigned long)screenHeight;
dmScreenSettings.dmBitsPerPel = 32;
dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
// Change the display settings to full screen
ChangeDisplaySettings( &dmScreenSettings, CDS_FULLSCREEN );
// Set the position of the window to the top left corner
posX = posY = 0;
// Create the window with the screen settings and get the handle to it
m_hwnd = CreateWindowEx( WS_EX_APPWINDOW, m_applicationName, m_applicationName,
WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_POPUP,
posX, posY, screenWidth, screenHeight, NULL, NULL, m_hinstance, NULL);
SetMenu( m_hwnd, NULL );
}
else
{
// If windowed then set it to 800x600 resolution
screenWidth = 1280;
screenHeight = 768;
// Place the window in the middle of the screen
posX = ( GetSystemMetrics( SM_CXSCREEN ) - screenWidth ) / 2;
posY = ( GetSystemMetrics( SM_CYSCREEN ) - screenHeight) / 2;
m_hwnd = CreateWindowEx( 0, m_applicationName, m_applicationName, WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU,
posX, posY, screenWidth, screenHeight,
NULL, NULL, m_hinstance, NULL );
}
// Bring the window up on the screen and set it as main focus
ShowWindow( m_hwnd, SW_SHOW );
SetForegroundWindow( m_hwnd );
SetFocus( m_hwnd );
// Hide the mouse cursor
ShowCursor(true);
}
You're not handling situation when device is lost, which is (possibly) what happens in this case.
Basically, BEFORE rendering anything you should test cooperative level, if it returns D3DERR_DEVICELOST, do not do not draw anything. If it returns D3DERR_DEVICENOTRESET, you should reset the device, if it returns 0 (D3DERR_OK or use SUCCEEDED macro), then proceed to draw the frame.
When device is reset, it loses all resources that are not in D3DPOOL_MANAGED, and you'll need to recreate them, or reload them. You can release all shaders and id3dx objects (like ID3DXFont) before calling IDirect3DDevice9::Reset, then recreate them after succesfull reset call.
Failure to handle "lost devices" will mean that you won't be able to alt-tab your game when running in fullscreen, and switching display mode might "break it". IN your situation black screen most likely happens for the same reason.

How to make action on keyboard input be instant

I am writing in C++ and using OpenGL in Windows.
I've created a cube and I want it to be rotated around the y axis ( using glRotate3f(), not gluLookat() ) by pressing '4' or '6' numpad keys.
The problem is that when I press any key, there is a slight rotation and then stops for a while and then it starts rotating continuously.
Is there any way to achieve this by using glutkeyboardfunc?
If not, how can it be achieved?
Maintain your own key state map and trigger redraws on a timer:
// g++ main.cpp -o main -lglut -lGL -lGLU && ./main
#include <GL/glut.h>
#include <map>
bool paused = false;
std::map< unsigned char, bool > state;
void keyboard_down( unsigned char key, int x, int y )
{
if( key == 'p' )
paused = !paused;
state[ key ] = true;
}
void keyboard_up( unsigned char key, int x, int y )
{
state[ key ] = false;
}
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
double ar = w / h;
glOrtho( -2 * ar, 2 * ar, -2, 2, -1, 1);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
static float angle = 0;
if( state[ '6' ] ) angle -= 3;
if( state[ '4' ] ) angle += 3;
if( !paused )
angle += 0.5;
glPushMatrix();
glRotatef( angle, 0, 0, 1 );
glColor3ub( 255, 0, 0 );
glBegin( GL_QUADS );
glVertex2i( -1, -1 );
glVertex2i( 1, -1 );
glVertex2i( 1, 1 );
glVertex2i( -1, 1 );
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void timer( int extra )
{
glutPostRedisplay();
glutTimerFunc( 16, timer, 0 );
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
glutInitWindowSize( 640, 480 );
glutCreateWindow( "Keyboard" );
glutDisplayFunc( display );
glutKeyboardFunc( keyboard_down );
glutKeyboardUpFunc( keyboard_up );
glutTimerFunc( 0, timer, 0 );
glutIgnoreKeyRepeat( GL_TRUE );
glutMainLoop();
return 0;
}