🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

why i see triangle not ball -no errors

Started by
3 comments, last by RSC_x 17 years, 2 months ago
[cool][headshake] can anyone say... how can i see my ball i see just triangle
i was modified this from sample for msvc
no erros ... but i hawe to see a ball...? i draw them but something is missing...

/**************************
* Includes
*
**************************/

using namespace std;
#define PI 3.1415265359
struct SVertex{	GLfloat x,y,z; 	GLfloat r,g,b;};
SVertex * Vertices;
int NumVertices;  //size of the vertex array
vector <GLuint> IndexVect;  //we first put the indices into this vector, then copy them to the array below
GLuint * Indices;
int NumIndices;   //size of the index array
GLfloat ChangeY = 0.025;//Indicates how much the y values of the highest and deepest vertex
float yRotated = 0.0;
bool PointsOnly = false;

void CreateSphere(int PointRows, int PointsPerRow)
{
NumVertices = (PointRows-2)*PointsPerRow + 2;
Vertices = new SVertex[NumVertices];
IndexVect.clear();  //to be sure it is empty
float x,y,z;
int i,j;
double r;
for (i = 1; i < (PointRows-1); i++){
for (j = 0; j < PointsPerRow; j++)
{
y = 1.0 - float(i) / float(PointRows-1)*2.0;
r = sin (acos(y));  //radius of the row
x = r * sin(float(j) / float(PointsPerRow)*PI*2.0);

z = r * cos(float(j) / float(PointsPerRow)*PI*2.0);
Vertices[(i-1)*PointsPerRow+j].x = x;
Vertices[(i-1)*PointsPerRow+j].y = y;
Vertices[(i-1)*PointsPerRow+j].z = z;
Vertices[(i-1)*PointsPerRow+j].r = (float)(i) / float(PointRows);
Vertices[(i-1)*PointsPerRow+j].g = 0.7;
Vertices[(i-1)*PointsPerRow+j].b = (float)(j) / float(PointsPerRow);
}
}

Vertices[(PointRows-2)*PointsPerRow].x = 0.0;
Vertices[(PointRows-2)*PointsPerRow].y = 1.0;
Vertices[(PointRows-2)*PointsPerRow].z = 0.0;
Vertices[(PointRows-2)*PointsPerRow].r = 1.0;
Vertices[(PointRows-2)*PointsPerRow].g = 0.7;
Vertices[(PointRows-2)*PointsPerRow].b = 1.0;
Vertices[(PointRows-2)*PointsPerRow+1].x = 0.0;
Vertices[(PointRows-2)*PointsPerRow+1].y = -1.0;
Vertices[(PointRows-2)*PointsPerRow+1].z = 0.0;
Vertices[(PointRows-2)*PointsPerRow+1].r = 1.0;
Vertices[(PointRows-2)*PointsPerRow+1].g = 0.7;
Vertices[(PointRows-2)*PointsPerRow+1].b = 1.0;

for (i = 1; i < (PointRows-2); i++)
{
for (j = 0; j < (PointsPerRow-1); j++)
{
IndexVect.push_back((i-1)*PointsPerRow+j);
IndexVect.push_back((i-1)*PointsPerRow+j+1);
IndexVect.push_back((i)*PointsPerRow+j);
IndexVect.push_back((i-1)*PointsPerRow+j+1);
IndexVect.push_back((i)*PointsPerRow+j+1);
IndexVect.push_back((i)*PointsPerRow+j);
}
IndexVect.push_back((i-1)*PointsPerRow+PointsPerRow-1);
IndexVect.push_back((i-1)*PointsPerRow);
IndexVect.push_back((i)*PointsPerRow+j);
IndexVect.push_back((i)*PointsPerRow);
IndexVect.push_back((i-1)*PointsPerRow);
IndexVect.push_back((i)*PointsPerRow+j);}		

	//The triangles to the highest and deepest vertices:
	for (j = 0; j< (PointsPerRow-1); j++)
	{
		IndexVect.push_back(j);
		IndexVect.push_back(j+1);
		IndexVect.push_back((PointRows-2)*PointsPerRow);
	}
	IndexVect.push_back(j);
	IndexVect.push_back(0);
	IndexVect.push_back((PointRows-2)*PointsPerRow);

	for (j = 0; j< (PointsPerRow-1); j++)
	{
		IndexVect.push_back((PointRows-3)*PointsPerRow+j);
		IndexVect.push_back((PointRows-3)*PointsPerRow+j+1);
		IndexVect.push_back((PointRows-2)*PointsPerRow+1);
	}
	IndexVect.push_back((PointRows-3)*PointsPerRow+j);
	IndexVect.push_back((PointRows-3)*PointsPerRow);
	IndexVect.push_back((PointRows-2)*PointsPerRow+1);
	Indices = new GLuint[IndexVect.size()];  //allocate the required memory
	for (i = 0; i < IndexVect.size(); i++)
	{
		Indices = IndexVect;
	}
	NumIndices = IndexVect.size();
	IndexVect.clear();  //no longer needed, takes only memory
}


void DrawSphere(void)
{
	if (!PointsOnly)
		glDrawElements(	GL_TRIANGLES, //mode
						NumIndices,  //count, ie. how many indices
						GL_UNSIGNED_INT, //type of the index array
						Indices);
	else 
		glDrawArrays(GL_POINTS,0,NumVertices);

}

void Display(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glLoadIdentity();
	glTranslatef(0.0,0.0,-4.0);
	glRotatef(yRotated, 0.0, 1.0, 0.0);
	DrawSphere();
	glFlush();			//Finish rendering
	//glutSwapBuffers();
}

void Reshape(int x, int y)
{
	if (y == 0 || x == 0) return;  //Nothing is visible then, so return
	//Set a new projection matrix
	glMatrixMode(GL_PROJECTION);  
	glLoadIdentity();
	//Angle of view:40 degrees
	//Near clipping plane distance: 0.5
	//Far clipping plane distance: 20.0
//	gluPerspective(40.0,(GLdouble)x/(GLdouble)y,0.5,20.0);
	glViewport(0,0,x,y);  //Use the whole window for rendering
	glMatrixMode(GL_MODELVIEW);
	
}

void Idle(void)
{
	Vertices[NumVertices-2].y += ChangeY;
	Vertices[NumVertices-1].y -= ChangeY;

	if (Vertices[NumVertices-2].y> 1.5 || Vertices[NumVertices-2].y< 0.5)
		 ChangeY *= -1;
	yRotated += 0.3;
	Display();
}
void Keyboard(unsigned char key, int x, int y)
{
	switch(key)
	{
	case 'p': 
		PointsOnly = !PointsOnly;
		break;
	}
}


/**************************
 * Function Declarations
 *
 **************************/

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);


/**************************
 * WinMain
 *
 **************************/

int WINAPI WinMain (HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpCmdLine,
                    int iCmdShow)
{
    WNDCLASS wc;
    HWND hWnd;
    HDC hDC;
    HGLRC hRC;        
    MSG msg;
    BOOL bQuit = FALSE;
    float theta = 0.0f;

    /* register window class */
    wc.style = CS_OWNDC;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor (NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "GLSample";
    RegisterClass (&wc);

    /* create main window */
    hWnd = CreateWindow (
      "GLSample", "OpenGL Sample", 
      WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
      0, 0, 256, 256,
      NULL, NULL, hInstance, NULL);

    /* enable OpenGL for the window */
    EnableOpenGL (hWnd, &hDC, &hRC);
CreateSphere(160,160);
Reshape(100, 100);
DrawSphere();
glDrawElements(	GL_TRIANGLES, 	NumIndices, GL_UNSIGNED_INT, Indices);
glDrawArrays(GL_POINTS,0,NumVertices);


/* program main loop */
    while (!bQuit)
    {
        /* check for messages */
        if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
        {
            /* handle or dispatch messages */
            if (msg.message == WM_QUIT)
            {
                bQuit = TRUE;
            }
            else
            {
                TranslateMessage (&msg);
                DispatchMessage (&msg);
            }
        }
        else
        {
            /* OpenGL animation code goes here */
            glClearColor (1.0f, 1.0f, 1.0f, 0.0f);
            glClear (GL_COLOR_BUFFER_BIT);
            glPushMatrix ();
            glRotatef (theta, 0.0f, 0.0f, 1.0f);
            glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
            glEnableClientState(GL_VERTEX_ARRAY);/*321*/
            glEnableClientState(GL_COLOR_ARRAY);
            glVertexPointer(3, GL_FLOAT,sizeof(SVertex),Vertices);
            glColorPointer(3,GL_FLOAT,sizeof(SVertex),&Vertices[0].r);  //Pointer to the first color
            glPointSize(2.0);
            glClearColor(0.0,0.0,0.0,0.0);
            glPushMatrix ();
            glPopMatrix ();
            glBegin (GL_TRIANGLES);
            glColor3f (1.0f, 0.0f, 0.0f);   glVertex2f (0.0f, 1.0f);
            glColor3f (0.0f, 1.0f, 0.0f);   glVertex2f (0.87f, -0.5f);
            glColor3f (0.0f, 0.0f, 1.0f);   glVertex2f (-0.87f, -0.5f);
            glEnd ();
            glPopMatrix ();
            SwapBuffers (hDC);
            theta += 1.0f;
            Sleep (1);
        }
    }

    /* shutdown OpenGL */
    DisableOpenGL (hWnd, hDC, hRC);

    /* destroy the window explicitly */
    DestroyWindow (hWnd);

    return msg.wParam;
}


/********************
 * Window Procedure
 *
 ********************/

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam)
{switch (message)
{
case WM_CREATE:return 0;
case WM_CLOSE:PostQuitMessage (0);return 0;
case WM_DESTROY:return 0;
case WM_KEYDOWN:
switch (wParam){case VK_ESCAPE:PostQuitMessage(0);return 0;}return 0;

default:return DefWindowProc (hWnd, message, wParam, lParam);}
}


/*******************
 * Enable OpenGL
 *
 *******************/

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

    /* 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 = 24;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;
    iFormat = ChoosePixelFormat (*hDC, &pfd);
    SetPixelFormat (*hDC, iFormat, &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);
}


© Loading... !!!
Please Wait...!
Advertisement
IF you have working example (you said something about modifying the sample), try replacing sections of code one by one by the sample's to isolate the faulty function/s and correct it.
Im not much of a help right? :)
thanks


but i rememeber something...
my complier can use msvc projects...
just with another button...
sorry ...




[Edited by - RSC_x on May 3, 2007 2:53:37 AM]
© Loading... !!!
Please Wait...!

You should replace the Triangle draw code in the else branche of "if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))" by a call to to DrawSphere(). You call it before the main loop, so it get's only called once. Putting it inside the main loop will result it in being called every frame.
Thanks Liquidchrome

it looks correct...
i hawe got something about opengl process style if it correct...[cool]
thanks...
© Loading... !!!
Please Wait...!

This topic is closed to new replies.

Advertisement