Skip to main content
GameDev.net gamedev.net
🔒 Locked

Sphere.

Started by zenon Feb 22, 2003 at 5:12 AM 1 replies 300+ views
Original Post
zenon
zenon
Okay.. instead of using glaux.lib for the use of auxSolidSphere, what lib should I use?
include
krazywest
krazywest
glu!

using gluSphere. Its very easy, but you have to make a quadratic object first. (not hard either, but cant remember how to do it from memory)

http://nehe.gamedev.net/

Mathematix
Mathematix
To declare an object to hold the sphere

GLUquadricObj *texturedSphere = NULL;

(Set it to NULL as it's a pointer object and easier to test for successful allocation).

texturedSphere = gluNewQuadric();

To allocate space in the free store.

And to use...

gluSphere(texturedSphere, 0.05f, 30, 20);

Radius of sphere object = 0.05f
Number of slices for sphere object = 30. Imagine lines of longitude, the greater this number, the great the lines of longitude and the smoother your sphere would look.
Number of stacks for sphere object = 20. Imagine line of latitude, the greater this number, the great the lines of latitude and the smoother your sphere would look.

Unless you have a good system and graphics engine, the greater the number of stacks and slices the slower your graphics will be.

I have included some early experimental code of mine for you to study.


  
// Program demonstrating the birth and destruction of a ball.


#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
//#include "loadbmp.h"


void Initialise();
void RenderSphere();
void CleanUp();
GLvoid BindTexturesAndSetMipmaps();


float angle = 0.0f;
float bounce = 0.0f;
bool boolVal = true;
GLUquadricObj *ASphere = NULL;
float factorR = 0.5f; // For birth/destruction colour manipulation.

float factorG = 0.5f;
float factorB = 0.5f;
float scaling = 0.5f; // For sphere resizing.

//BITMAPINFOHEADER textureInfo;

//unsigned char *sphereTexture;

//unsigned int textureGeneration;



// lighting variables for sphere

float ambientLight[] = {0.5f, 0.5f, 0.5f, 1.0f}; // ambient light

float diffuseLight[] = {0.5f, 0.5f, 0.5f, 1.0f}; // diffuse light


float spotlightPosition[] = {6.0f, 0.5f, 0.0f, 1.0f}; // spotlight position

float spotlightDirection[] = {-1.0f, 0.0f, -1.0f}; // spotlight direction


// material variables

float matAmbient[] = {1.0f, 1.0f, 1.0f, 1.0f}; // ambient material

float matDiff[] = {1.0f, 1.0f, 1.0f, 1.0f}; // diffuse material

float matSpecular[] = {1.0f, 1.0f, 1.0f, 1.0f}; // specular material


HDC g_HDC;

void SetupPixelFormat(HDC hDC)
{
int nPixelFormat;

static PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
16,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0};

nPixelFormat = ChoosePixelFormat(hDC, &pfd);

SetPixelFormat(hDC, nPixelFormat, &pfd);
}


LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HGLRC hRC;
static HDC hDC;
int width, height;

switch(message)
{
case WM_CREATE:
hDC = GetDC(hwnd);
g_HDC = hDC;
SetupPixelFormat(hDC);

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

return 0;
break;

case WM_CLOSE:

wglMakeCurrent(hDC, NULL);
wglDeleteContext(hRC);

CleanUp();

PostQuitMessage(0);

return 0;
break;

case WM_SIZE:
height = HIWORD(lParam);
width = LOWORD(lParam);

if(height == 0) // don't want to divide by zero!

height = 1;


glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION); // set projection matrix

glLoadIdentity(); // reset projection matrix


// calculate aspect ratio of window

gluPerspective(45.0f, (GLfloat) width/(GLfloat) height, 1.0f, 1000.0f);

glMatrixMode(GL_MODELVIEW); // set modelview matrix

glLoadIdentity(); // reset modelview matrix


return 0;
break;

default:
break;
}

return (DefWindowProc(hwnd, message, wParam, lParam));
}

// the main windows entry point

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEX windowClass;
HWND hwnd;
MSG msg;
bool done;

windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = WndProc;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.hInstance = hInstance;
windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
windowClass.hbrBackground = NULL; // don't need background

windowClass.lpszMenuName = NULL;
windowClass.lpszClassName = "KwakeClass";
windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);

if (!RegisterClassEx(&windowClass))
return 0;

hwnd = CreateWindowEx(NULL,
"KwakeClass",
"Sphere birth and destruction.",
WS_OVERLAPPEDWINDOW | WS_VISIBLE |
WS_SYSMENU | WS_CLIPCHILDREN |
WS_CLIPSIBLINGS,
100, 100, // x,y coordinate

400, 400, // width, height

NULL, // handle to parent

NULL, // handle to menu

hInstance, // application instance

NULL); // no extra params


if (!hwnd)
return 0;

ShowWindow(hwnd, SW_MAXIMIZE);
UpdateWindow(hwnd);

done = false;

// glEnable(GL_TEXTURE_2D);


// sphereTexture = LoadBitmapFile("standard.bmp", &textureInfo);

// BindTexturesAndSetMipmaps();


// main message loop

while (!done)
{
PeekMessage(&msg, hwnd, NULL, NULL, PM_REMOVE);

if (msg.message == WM_QUIT)
{
done = true;
}
else
{
// do rendering here

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

Initialise();


angle += 1.5f; // Translation speed.

if(angle >= 360.0f)
angle = 0.0f;

//glTranslatef(-bounce, bounce, -5.0f-bounce);

glTranslatef(0.0f, 0.0f, -3.0f);

RenderSphere();

if(bounce > 3.3f)
boolVal = false;
if(bounce < -3.0f)
boolVal = true;

if(!boolVal)
{
bounce -= 0.01f;
factorB += 0.01;
factorR -= 0.01;
scaling -= 0.002;
}
if(boolVal)
{
bounce += 0.01f;
factorR += 0.01;
factorB -= 0.01;
scaling += 0.002;
}

TranslateMessage(&msg);
DispatchMessage(&msg);

}
}

glDisable(GL_TEXTURE_2D);

return msg.wParam;

}

// Initialise openGL settings

void Initialise()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Clear background to black


glShadeModel(GL_SMOOTH); // smooth shading

glEnable(GL_DEPTH_TEST); // hidden surface removal

glEnable(GL_CULL_FACE); // inside of polys not calculated

glFrontFace(GL_CCW); // counterclockwise polygons are out


glEnable(GL_LIGHTING); // enable lighting


// setting up LIGHT0

// ambient element

glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);

// diffuse element

glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);

// place light in world

glLightfv(GL_LIGHT0, GL_POSITION, spotlightPosition);

// spotlight properties

glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 40.0f);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 30.0f);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spotlightDirection);

// Enable the light

glEnable(GL_LIGHT0);

// track the colors set by glColor() for ambient and diffuse light

glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

// materials have minimal shine

glMaterialfv(GL_FRONT, GL_SPECULAR, matSpecular);
glMaterialf(GL_FRONT, GL_SHININESS, 50.0f);
}

void RenderSphere()
{
ASphere = gluNewQuadric();
gluQuadricTexture(ASphere, GL_TRUE);

glPushMatrix();
glRotatef(angle, 0.0f, 1.0f, 0.0f);
glColor3f(0.5f+factorR, 0.5f+factorG, 0.5f+factorG);
gluSphere(ASphere, scaling, 30, 30);
glPopMatrix();

glFlush();
SwapBuffers(g_HDC);
}

void CleanUp()
{
gluDeleteQuadric(ASphere);
}

/*
GLvoid BindTexturesAndSetMipmaps()
{
glGenTextures(1, &textureGeneration);
glBindTexture(GL_TEXTURE_2D, textureGeneration);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, textureInfo.biWidth, textureInfo.biHeight,
GL_RGB, GL_UNSIGNED_BYTE, sphereTexture);
}
*/



[edited by - mathematix on February 22, 2003 9:52:26 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.