• Advertisement

Archived

This topic is now archived and is closed to further replies.

3D Rotating Cube

This topic is 6041 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello guys, I am just getting into OpenGL and am having some problems. I get access violation errors and my computer freezes when I try to draw a cube on the screen. At the moment I just want to display the cube without rotating it. The following is my Update() function that is being executed in the message loop of my program.
  

BOOL MAIN_Update( void )
{
	if ( keys[ VK_ESCAPE ] )
		return FALSE;

	glClear( GL_COLOR_BUFFER_BIT );
	glLoadIdentity();

	glBegin( GL_QUADS );
		// face 1 (front)

		glColor3f( 1, 0, 0 );
		glVertex3f( 1, 1, 1 );
		glVertex3f( 1, -1, 1 );
		glVertex3f( -1, -1, 1 );
		glVertex3f( -1, 1, 1 );

		// face 2 (left)

		glColor3f( 0, 1, 0 );
		glVertex3f( -1, 1, 1 );
		glVertex3f( -1, -1, 1 );
		glVertex3f( -1, -1, -1 );
		glVertex3f( -1, 1, -1 );

		// face 3 (back)

		glColor3f( 0, 0, 1 );
		glVertex3f( 1, 1, -1 );
		glVertex3f( 1, -1, -1 );
		glVertex3f( -1, -1, -1 );
		glVertex3f( -1, 1, -1 );

		// face 4 (right)

		glColor3f( 1, 0, 0 );
		glVertex3f( 1, 1, 1 );
		glVertex3f( 1, -1, 1 );
		glVertex3f( 1, -1, -1 );
		glVertex3f( 1, 1, -1 );

		// face 5 (top)

		glColor3f( 1, 1, 0 );
		glVertex3f( 1, 1, 1 );
		glVertex3f( -1, 1, 1 );
		glVertex3f( -1, 1, -1 );
		glVertex3f( 1, 1, -1 );

		// face 6 (bottom)

		glColor3f( 0, 1, 1 );
		glVertex3f( 1, -1, 1 );
		glVertex3f( -1, -1, 1 );
		glVertex3f( -1, -1, -1 );
		glVertex3f( 1, -1, -1 );
	glEnd();
			
	SwapBuffers( hDC );

	return TRUE;
}

  
If anyone knows what I did wrong please let me know. ------------------------------- Did someone say "banana"?
..-=gLaDiAtOr=-..

Share this post


Link to post
Share on other sites
Advertisement
Yes, other programs run fine. So you are saying that the below code is correct?

hmm... well, here''s the functions I use to enable/disable OGL.

  

///////////////////////////////////////

// opengl.h

#ifndef __OPENGL_H
#define __OPENGL_H

#include "window.h" // this is my windows init stuff
#include <gl/gl.h>


#define COLORBITS 24
#define DEPTHBITS 16

void enableOpenGL( HWND, HDC *, HGLRC * );
void disableOpenGL( HWND, HDC, HGLRC );

#endif

///////////////////////////////////////////////


#include "opengl.h"

// opengl.cpp - my own file (is there another file in lib named the same?... i also have opengl.h


// Enable OpenGL

void enableOpenGL( HWND hWnd, HDC *hDC, HGLRC *hRC )
{
PIXELFORMATDESCRIPTOR pfd;
int format;

// get the device context (DC)

*hDC = GetDC( hWnd );

// set the pixel format for the DC

ZeroMemory( &pfd, sizeof( pfd ) );
pfd.nSize = sizeof( pfd );
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = COLORBITS;
pfd.cDepthBits = DEPTHBITS;
pfd.iLayerType = PFD_MAIN_PLANE;
format = ChoosePixelFormat( *hDC, &pfd );
SetPixelFormat( *hDC, format, &pfd );

// create and enable the render context (RC)

*hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC );

}

// Disable OpenGL

void disableOpenGL( HWND hWnd, HDC hDC, HGLRC hRC )
{
wglMakeCurrent( NULL, NULL );
wglDeleteContext( hRC );
ReleaseDC( hWnd, hDC );
}

///////////////////////////////

// main.cpp

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

//---------------------------------------------------------------------

// INCLUDES

//---------------------------------------------------------------------

#include "window.h"
#include "logger.h"
#include "opengl.h"

//---------------------------------------------------------------------

// GLOBALS

//---------------------------------------------------------------------

HDC hDC;
HGLRC hRC;

//---------------------------------------------------------------------

// PROTOTYPES

//---------------------------------------------------------------------


BOOL MAIN_Init( void );
BOOL MAIN_Update( void );
void MAIN_Release( void );

//---------------------------------------------------------------------

// IMPLEMENTATION

//---------------------------------------------------------------------


//=====================================================================

// WinMain() - Main Program

//=====================================================================

int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow )
{
MSG msg;

if ( MAIN_Init() == FALSE )
{
MAIN_Release();
return FALSE;
}

while ( TRUE )
{
if ( PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) == TRUE )
{
if ( msg.message == WM_QUIT )
break;
TranslateMessage( &msg );
DispatchMessage( &msg );
}

if ( MAIN_Update() == FALSE )
break;
}

MAIN_Release();

return msg.wParam;
}

//=====================================================================

// MAIN_Init() - Does some initializations before the main part starts

//=====================================================================

BOOL MAIN_Init( void )
{
if ( LOGGER_Open( "log.txt" ) == FALSE )
return FALSE;

if ( WINDOW_Init( GetModuleHandle( NULL ), WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE, SW_SHOW ) == FALSE )
{
LOGGER_Write( "WINDOW_Init() error" );
return FALSE;
}

enableOpenGL( hMainWnd, &hDC, &hRC );

return TRUE;
}

//=====================================================================

// MAIN_Update() - The main part of the program

//=====================================================================

BOOL MAIN_Update( void )
{
if ( keys[ VK_ESCAPE ] )
return FALSE;

glClear( GL_COLOR_BUFFER_BIT ); // Clear The Screen And The Depth Buffer

glLoadIdentity();

glBegin( GL_QUADS );
// face 1 (front)

glColor3f( 1, 0, 0 );
glVertex3f( 1, 1, 1 );
glVertex3f( 1, -1, 1 );
glVertex3f( -1, -1, 1 );
glVertex3f( -1, 1, 1 );

// face 2 (left)

glColor3f( 0, 1, 0 );
glVertex3f( -1, 1, 1 );
glVertex3f( -1, -1, 1 );
glVertex3f( -1, -1, -1 );
glVertex3f( -1, 1, -1 );

// face 3 (back)

glColor3f( 0, 0, 1 );
glVertex3f( 1, 1, -1 );
glVertex3f( 1, -1, -1 );
glVertex3f( -1, -1, -1 );
glVertex3f( -1, 1, -1 );

// face 4 (right)

glColor3f( 1, 0, 0 );
glVertex3f( 1, 1, 1 );
glVertex3f( 1, -1, 1 );
glVertex3f( 1, -1, -1 );
glVertex3f( 1, 1, -1 );

// face 5 (top)

glColor3f( 1, 1, 0 );
glVertex3f( 1, 1, 1 );
glVertex3f( -1, 1, 1 );
glVertex3f( -1, 1, -1 );
glVertex3f( 1, 1, -1 );

// face 6 (bottom)

glColor3f( 0, 1, 1 );
glVertex3f( 1, -1, 1 );
glVertex3f( -1, -1, 1 );
glVertex3f( -1, -1, -1 );
glVertex3f( 1, -1, -1 );
glEnd();

SwapBuffers( hDC );

return TRUE;
}

//=====================================================================

// MAIN_Release() - Releases all the subsystems

//=====================================================================

void MAIN_Release( void )
{
disableOpenGL( hMainWnd, hDC, hRC );
LOGGER_Close();
}



anything look suspicious? can anyone help me now?

-------------------------------
Did someone say "banana"?

..-=gLaDiAtOr=-..

Share this post


Link to post
Share on other sites
hmm i didnt get a reply and i thought this was everyone''s first project? im sorry if im wrong... can anyone post the easiest way to accomplish a 3D rotating cube in similar manner to the way im trying to do it? post your code pls.. im sure ill find mistake after i see your code

Thanks,
Gladiator

-------------------------------
Did someone say "banana"?

..-=gLaDiAtOr=-..

Share this post


Link to post
Share on other sites
  
glRotatef(angle, x, y, z)


This is what you need to rotate the cube

so you have this variable angle defined as global

and in your update functions you put:

  

bool MAIN_UPDATE (void)
{

// after glLoadIdentity you place this functions to rotate the cube round the x, y and z axis


glRotatef(angle, 1.0f, 1.0f, 1.0f);

// Now you draw your cube

// After that you place the following line


angle++;
}



And that''s it !

Share this post


Link to post
Share on other sites
aight..tnx so far.... what if i just wanna display the cube (before i start coding the rotation part)?? i cant get that to work...

-------------------------------
Did someone say "banana"?

..-=gLaDiAtOr=-..

Share this post


Link to post
Share on other sites
I don''t have a direct answer for you, but if you seriously want to get through learning OpenGL with the minimum frustration level, head over to nehe.gamedev.net.

For the rotating cube problem, try this:
http://nehe.gamedev.net/tutorials/lesson05.asp

That''s exactly what you''re asking about, and it walks you through it one step at a time.

~Thek

Share this post


Link to post
Share on other sites

  • Advertisement