PDA

View Full Version : rendering a cube



joey
12-13-2001, 10:31 PM
I've used a console application that supports MFC to create a rotating cube...
The codes are as follows....


// 3D Cube.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "3D Cube.h"

#include<windows.h>
#include <stdio.h>
#include<gl/glut.h>
#include<gl/glu.h>
#include<gl/gl.h>


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;


void changeSize(int w, int h)
{

// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
h = 1;

float ratio = 1.0* w / h;

// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Set the viewport to be the entire window
glViewport(0, 0, w, h);

// Set the correct perspective.
gluPerspective(45,ratio,1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,5.0,
0.0,0.0,-1.0,
0.0f,1.0f,0.0f);

}


void DrawCube(void)
{
static GLfloat wAngleX = 0.0f;
static GLfloat wAngleY = 0.0f;
static GLfloat wAngleZ = 0.0f;

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();

glTranslatef(0.0f, 0.0f, 0.0f);
glRotatef(wAngleX, 1.0f, 0.0f, 0.0f);
glRotatef(wAngleY, 0.0f, 1.0f, 0.0f);
glRotatef(wAngleZ, 0.0f, 0.0f, 1.0f);

wAngleX += 0.05f;
wAngleY += 0.05f;
wAngleZ += 0.05f;

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glBegin(GL_QUADS);
glColor3f(1.0,0.0,0.0);
glVertex3f(-0.5,0.5,0.5);

glColor3f(1.0,0.0,0.0);
glVertex3f(0.5,0.5,0.5);

glColor3f(1.0,0.0,0.0);
glVertex3f(0.5,-0.5,0.5);

glColor3f(1.0,0.0,0.0);
glVertex3f(-0.5,-0.5,0.5);

glColor3f(1.0,0.0,0.0);
glVertex3f(0.5,0.5,0.5);

glColor3f(1.0,0.0,0.0);
glVertex3f(0.5,0.5,-0.5);

glColor3f(1.0,0.0,0.0);
glVertex3f(0.5,-0.5,-0.5);

glColor3f(1.0,0.0,0.0);
glVertex3f(0.5,-0.5,0.5);

glColor3f(1.0,0.0,0.0);
glVertex3f(-0.5,0.5,-0.5);

glColor3f(1.0,0.0,0.0);
glVertex3f(0.5,0.5,-0.5);

glColor3f(1.0,0.0,0.0);
glVertex3f(0.5,-0.5,-0.5);

glColor3f(1.0,0.0,0.0);
glVertex3f(-0.5,-0.5,-0.5);

glColor3f(1.0,0.0,0.0);
glVertex3f(-0.5,0.5,0.5);

glColor3f(1.0,0.0,0.0);
glVertex3f(-0.5,0.5,-0.5);

glColor3f(1.0,0.0,0.0);
glVertex3f(-0.5,-0.5,-0.5);

glColor3f(1.0,0.0,0.0);
glVertex3f(-0.5,-0.5,0.5);

glColor3f(1.0,0.0,0.0);
glVertex3f(-0.5f, 0.5f, 0.5f);

glColor3f(1.0,0.0,0.0);
glVertex3f(0.5f, 0.5f, 0.5f);

glColor3f(1.0,0.0,0.0);
glVertex3f(0.5f, 0.5f, -0.5f);

glColor3f(1.0,0.0,0.0);
glVertex3f(-0.5f, 0.5f, -0.5f);

glColor3f(1.0,0.0,0.0);
glVertex3f(-0.5f, -0.5f, 0.5f);

glColor3f(1.0,0.0,0.0);
glVertex3f(0.5f, -0.5f, 0.5f);

glColor3f(1.0,0.0,0.0);
glVertex3f(0.5f, -0.5f, -0.5f);

glColor3f(1.0,0.0,0.0);
glVertex3f(-0.5f, -0.5f, -0.5f);

glEnd();

glPopMatrix();

glFinish();

glutSwapBuffers();
}


int _tmain(int argc, char **argv)
{
int nRetCode = 0;

// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
printf("This is a rotating cube !!\n");
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(200,150);
glutInitWindowSize(320,320);
glutCreateWindow("Rotating Cube");
glutDisplayFunc(DrawCube);
glutIdleFunc(DrawCube);
glutReshapeFunc(changeSize);
glutMainLoop();
}

return nRetCode;
}


I need to render a .bmp file onto the surface of this cube...
But i do not know how to go about doing it....
Can anyone help me???

Thanks in advance....

Gavin
12-14-2001, 12:58 AM
Try nehe's tutorials on texture mapping.

bondi
12-14-2001, 01:24 AM
nehe.gamedev.net -> nehe.
To have a good understanding about texturing,
you can also read the Redbook, etc.