Jump to content

  • Log In with Google      Sign In   
  • Create Account

3D Rotating Cube


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 Gladiator   Members   -  Reputation: 127

Like
Likes
Like

Posted 28 September 2001 - 01:15 PM

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=-..

Sponsor:

#2 Null and Void   Moderators   -  Reputation: 1087

Like
Likes
Like

Posted 28 September 2001 - 02:07 PM

The problem is very likely elsewhere in your code, or with your drivers. Do other OpenGL programs run fine?

[Resist Windows XP''s Invasive Production Activation Technology!]

#3 Gladiator   Members   -  Reputation: 127

Like
Likes
Like

Posted 29 September 2001 - 09:27 AM

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=-..

#4 Gladiator   Members   -  Reputation: 127

Like
Likes
Like

Posted 01 October 2001 - 01:30 AM

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=-..

#5 xstreme2000   Members   -  Reputation: 152

Like
Likes
Like

Posted 01 October 2001 - 02:37 AM

http://nehe.gamedev.net/

#6 Acer   Members   -  Reputation: 122

Like
Likes
Like

Posted 01 October 2001 - 12:46 PM

I am not sure, but I dont think there is an anyways, I think it is not sure. I could be wrong.

#7 Icarus3   Members   -  Reputation: 122

Like
Likes
Like

Posted 04 October 2001 - 09:00 AM

  
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 !



#8 Gladiator   Members   -  Reputation: 127

Like
Likes
Like

Posted 06 October 2001 - 07:07 PM

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=-..

#9 Thek   Members   -  Reputation: 122

Like
Likes
Like

Posted 06 October 2001 - 07:15 PM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS