PDA

View Full Version : sos~~~my program goes wrong!!!



beginnertom
02-16-2010, 06:39 AM
hi,i am a beginner on opengl .
i set up a class,here is
class my{
public: void drawsquad(GLfloat,GLfloat,GLfloat,GLfloat);
GLfloat ra;
};
the method is

void my::drawsquad(GLfloat x1=0.0f,GLfloat y1=0.0f,GLfloat z1=0.0f,GLfloat ra=1.0f)
{
if(ra>=1&amp;&amp;ra<=360)
{
glLoadIdentity(); // &amp;#37325;&amp;#24314;&amp;#21407;&amp;#28857;

glTranslatef(x1,y1,z1); // &amp;#31227;&amp;#21160;&amp;#21407;&amp;#28857;
//glRotatef(ra,1.0f,0.0f,0.0f); //&amp;#19978;&amp;#19979;&amp;#27491;&amp;#21518;
//glRotatef(90,0.0f,1.0f,0.0f); //&amp;#24038;&amp;#21491;&amp;#27491;&amp;#21518;
//glRotatef(90,1.0f,0.0f,0.0f);
//zheng mian
glColor3f(0.5f,0.4f,0.7f); //&amp;#30333;&amp;#33394;
glBegin(GL_QUADS); // Start Drawing A Quad
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad
glEnd();

//shang mian
glColor3f(0.0f,0.7f,0.4f);
glBegin(GL_QUADS); // Start Drawing A Quad
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad
glVertex3f( 1.0f,1.0f, -1.0f); // Bottom Right Of The Quad
glVertex3f(-1.0f,1.0f, -1.0f); // Bottom Left Of The Quad
glEnd();

//xia mian
glColor3f(0.9f,0.0f,0.4f);
glBegin(GL_QUADS); // Start Drawing A Quad
glVertex3f(-1.0f, -1.0f, 1.0f); // Top Left Of The Quad
glVertex3f( 1.0f, -1.0f, 1.0f); // Top Right Of The Quad
glVertex3f( 1.0f,-1.0f, -1.0f); // Bottom Right Of The Quad
glVertex3f(-1.0f,-1.0f, -1.0f); // Bottom Left Of The Quad
glEnd();

//hou mian
glColor3f(0.5f,0.3f,0.4f);
glBegin(GL_QUADS); // Start Drawing A Quad
glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Quad
glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Quad
glVertex3f( 1.0f,-1.0f, -1.0f); // Bottom Right Of The Quad
glVertex3f(-1.0f,-1.0f, -1.0f); // Bottom Left Of The Quad
glEnd();

//zuo mian
glColor3f(1.0f,0.0f,0.0f);
glBegin(GL_QUADS); // Start Drawing A Quad
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad
glVertex3f( -1.0f, -1.0f, 1.0f); // Top Right Of The Quad
glVertex3f( -1.0f,-1.0f, -1.0f); // Bottom Right Of The Quad
glVertex3f(-1.0f,1.0f, -1.0f); // Bottom Left Of The Quad
glEnd();

// you mian
glColor3f(1.0f,1.0f,0.0f);
glBegin(GL_QUADS); // Start Drawing A Quad
glVertex3f(1.0f, 1.0f, 1.0f); // Top Left Of The Quad
glVertex3f( 1.0f, -1.0f, 1.0f); // Top Right Of The Quad
glVertex3f( 1.0f,-1.0f, -1.0f); // Bottom Right Of The Quad
glVertex3f(1.0f,1.0f, -1.0f); // Bottom Left Of The Quad
glEnd();
ra++;
glRotatef(ra,1.0f,1.0f,0.0f); //&amp;#19978;&amp;#19979;&amp;#27491;&amp;#21518;
}
else
{
ra=1.0;
drawsquad(x1,y1,z1,ra);
}

in my main,cpp
i set up gloab Variable my* my1
winmain
my1->ra=1.0;

When I do opengl drawing, I was back in a function of drawsquad lost my1 method


my1->drawsquad(1.0,0.0,-16.0,my1->ra);
but my programs seems goes wrong,


help me ~~~

beginnertom
02-16-2010, 08:16 AM
ok here is my file;
my.h
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>
#include <stdio.h>
class my{
public: void drawsquad(GLfloat,GLfloat,GLfloat);

my(void);
};

beginnertom
02-16-2010, 08:19 AM
my.cpp


#include "my.h"

my::my(void)
{

}

void my::drawsquad(GLfloat x1=0.0f,GLfloat y1=0.0f,GLfloat z1=0.0f)
{

glLoadIdentity();

glTranslatef(x1,y1,z1);


glColor3f(0.5f,0.4f,0.7f); //&amp;#30333;&amp;#33394;
glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glEnd();


glColor3f(0.0f,0.7f,0.4f);
glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f,1.0f, -1.0f);
glVertex3f(-1.0f,1.0f, -1.0f);
glEnd();


glColor3f(0.9f,0.0f,0.4f);
glBegin(GL_QUADS);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f, -1.0f);
glVertex3f(-1.0f,-1.0f, -1.0f);
glEnd();


glColor3f(0.5f,0.3f,0.4f);
glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f( 1.0f, 1.0f, -1.0f);
glVertex3f( 1.0f,-1.0f, -1.0f);
glVertex3f(-1.0f,-1.0f, -1.0f);
glEnd();

//zuo mian
glColor3f(1.0f,0.0f,0.0f);
glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad
glVertex3f( -1.0f, -1.0f, 1.0f);
glVertex3f( -1.0f,-1.0f, -1.0f);
glVertex3f(-1.0f,1.0f, -1.0f);
glEnd();


glColor3f(1.0f,1.0f,0.0f);
glBegin(GL_QUADS);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f, -1.0f);
glVertex3f(1.0f,1.0f, -1.0f);
glEnd();




}

beginnertom
02-16-2010, 08:37 AM
main.cpp
#include "my.h"
#include <windows.h>
#include "resource.h"
HDC hDC=NULL;
HGLRC hRC=NULL;
HWND hWnd=NULL;
HINSTANCE hInstance;
my* my2;
GLuint texture[1];
bool keys[256];
bool active=TRUE;
bool fullscreen=TRUE;

AUX_RGBImageRec* LoadBMP(char *Filename)
{
FILE *File=NULL;
if (!Filename)
{
return NULL;
}
File=fopen(Filename,"r");
if (File)
{
fclose(File);
return auxDIBImageLoad(Filename);
}
return NULL;
}

int LoadGLTextures()
{
int Status=FALSE;
AUX_RGBImageRec *TextureImage[1];
memset(TextureImage,0,sizeof(void *)*1);

if (TextureImage[0]=LoadBMP("guonian.bmp"))
{
Status=TRUE;
glGenTextures(1, &amp;texture[0]);


glBindTexture(GL_TEXTURE_2D, texture[0]);

glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
}
if (TextureImage[0])
{
if (TextureImage[0]->data)
{
free(TextureImage[0]->data);
}

free(TextureImage[0]);
}
return Status;

}










LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

GLvoid ReSizeGLScene(GLsizei width, GLsizei height)
{
if (height==0)
{
height=1;
}

glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();


gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

int InitGL(GLvoid)
{
if (LoadGLTextures())
{
return FALSE;
}
glEnable(GL_TEXTURE_2D);


glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
return TRUE;
}

int DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

my2->drawsquad(1.0,0.0,-16.0);

return TRUE;
}

GLvoid KillGLWindow(GLvoid)
{
if (fullscreen)
{
ChangeDisplaySettings(NULL,0);
ShowCursor(TRUE);
}

if (hRC)
{
if (!wglMakeCurrent(NULL,NULL))
MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}

if (!wglDeleteContext(hRC))
{
MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}
hRC=NULL;
}

if (hDC &amp;&amp; !ReleaseDC(hWnd,hDC))
{
MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hDC=NULL;
}

if (hWnd &amp;&amp; !DestroyWindow(hWnd))
{
MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hWnd=NULL;
}

if (!UnregisterClass("OpenGL",hInstance))
{
MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hInstance=NULL;
}
}



BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
{
GLuint PixelFormat;
WNDCLASS wc;
DWORD dwExStyle;
DWORD dwStyle;
RECT WindowRect;
WindowRect.left=(long)0;
WindowRect.right=(long)width;
WindowRect.top=(long)0;
WindowRect.bottom=(long)height;

fullscreen=fullscreenflag;

hInstance = GetModuleHandle(NULL);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MYICON));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = "OpenGL";

if (!RegisterClass(&amp;wc))
{
MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}

if (fullscreen)
{
DEVMODE dmScreenSettings;
memset(&amp;dmScreenSettings,0,sizeof(dmScreenSettings ));
dmScreenSettings.dmSize=sizeof(dmScreenSettings);
dmScreenSettings.dmPelsWidth = width;
dmScreenSettings.dmPelsHeight = height;
dmScreenSettings.dmBitsPerPel = bits;
dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWID TH|DM_PELSHEIGHT;


if (ChangeDisplaySettings(&amp;dmScreenSettings,CDS_FULLS CREEN)!=DISP_CHANGE_SUCCESSFUL)
{

if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
{
fullscreen=FALSE;
}
else
{

MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
return FALSE;
}
}
}

if (fullscreen)
{
dwExStyle=WS_EX_APPWINDOW;
dwStyle=WS_POPUP;
ShowCursor(FALSE);
}
else
{
dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
dwStyle=WS_OVERLAPPEDWINDOW;
}

AdjustWindowRectEx(&amp;WindowRect, dwStyle, FALSE, dwExStyle);

// Create The Window
if (!(hWnd=CreateWindowEx( dwExStyle,
"OpenGL",
title,
dwStyle |
WS_CLIPSIBLINGS |
WS_CLIPCHILDREN,
0, 0,
WindowRect.right-WindowRect.left,
WindowRect.bottom-WindowRect.top,
NULL,
NULL,
hInstance,
NULL)))
{
KillGLWindow();
MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}

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

if (!(hDC=GetDC(hWnd)))
{
KillGLWindow();
MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}

if (!(PixelFormat=ChoosePixelFormat(hDC,&amp;pfd)))
{
KillGLWindow();
MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}

if(!SetPixelFormat(hDC,PixelFormat,&amp;pfd))
{
KillGLWindow();
MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}

if (!(hRC=wglCreateContext(hDC)))
{
KillGLWindow();
MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}

if(!wglMakeCurrent(hDC,hRC))
{
KillGLWindow();
MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}

ShowWindow(hWnd,SW_SHOW);
SetForegroundWindow(hWnd);
SetFocus(hWnd);
ReSizeGLScene(width, height);

if (!InitGL())
{
KillGLWindow();
MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}

return TRUE;
}

LRESULT CALLBACK WndProc( HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
switch (uMsg)
{
case WM_ACTIVATE:
{
if (!HIWORD(wParam))
{
active=TRUE;
}
else
{
active=FALSE;
}

return 0;
}

case WM_SYSCOMMAND:
{
switch (wParam)
{
case SC_SCREENSAVE:
case SC_MONITORPOWER:
return 0;
}
break;
}

case WM_CLOSE:
{
PostQuitMessage(0);
return 0;
}

case WM_KEYDOWN:
{
keys[wParam] = TRUE;
return 0;
}

case WM_KEYUP:
{
keys[wParam] = FALSE;
return 0;
}

case WM_SIZE:
{
ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));
return 0;
}
}


return DefWindowProc(hWnd,uMsg,wParam,lParam);
}

int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
BOOL done=FALSE;

my* my2=new my();

if (MessageBox(NULL,"Would You Like To Run In Fullscreen Mode?", "Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO)
{
fullscreen=FALSE;
}


if (!CreateGLWindow("xinnianhao",640,480,16,fullscreen))
{
return 0;
}

while(!done)
{
if (PeekMessage(&amp;msg,NULL,0,0,PM_REMOVE))
{
if (msg.message==WM_QUIT)
{
done=TRUE;
}
else
{
TranslateMessage(&amp;msg);
DispatchMessage(&amp;msg);
}
}
else
{

if (active)
{
if (keys[VK_ESCAPE])
{
done=TRUE;
}
else
{
DrawGLScene();
SwapBuffers(hDC);
}
}

if (keys[VK_F1])
{
keys[VK_F1]=FALSE;
KillGLWindow();
fullscreen=!fullscreen;

if (!CreateGLWindow("xinnianhao",640,480,16,fullscreen))
{
return 0;
}
}
}
}

// Shutdown
KillGLWindow();
return (msg.wParam);
}

beginnertom
02-16-2010, 08:39 AM
i just want to rotation my box
how can i do it ?

beginnertom
02-17-2010, 01:12 AM
here is my new update



my::clear(void)
{
this->~my();
}
modify in my.cpp

beginnertom
02-17-2010, 10:44 AM
int m,k1;
glTranslatef(-23,-25,-60);
glBegin(GL_LINES);
glVertex2fv(f[0]);
glVertex2fv(f[1]);
glEnd();
glBegin(GL_LINES);
glVertex2fv(f[1]);
glVertex2fv(f[2]);
glEnd();
glBegin(GL_LINES);
glVertex2fv(f[2]);
glVertex2fv(f[0]);
glEnd();


glBegin(GL_POINTS);
for(k1=0;k1<5000;k1++)
{
m=rand()%3;
k[0]=(k[0]+f[m][0])/2;
k[1]=(k[1]+f[m][1])/2;
glVertex2fv(k);
}
glEnd();
glFlush();

beginnertom
02-17-2010, 12:52 PM
[B]GLusignbyte a
//xiao xiao l j
glEnable (GL_TEXTURE_2D);

glBindTexture (GL_TEXTURE_2D, 1);//set id
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);// read a byte
// set param
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_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);


glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); //set enviroment
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, imageWidth, imageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, a); //copy to id

//use

glBindTexture (GL_TEXTURE_2D, 1);// must before a begin
glBegin (...);
glVertex (...);
glTexCoord (...);// textture coord
glEnd (...);
glDisable (GL_TEXTURE_2D); //free it

beginnertom
02-17-2010, 08:42 PM
here is a code come from a japanese
it should be nothing.
but it slove many problem about my simple code


cb.keyboard_function = key;
cb.motion_function = motion;
camera.configure_buttons(2);
object.configure_buttons(2);
spotlight.configure_buttons(2);

object.dolly.dolly[2] = -3; // push plane forward
camera.trackball.centroid = vec3f(0, 0, -3); // rotate camera trackball about the plane...
camera.trackball.r =
rotationf(vec3f(0,1,0), to_radians(-45.f)) *
rotationf(vec3f(1.f, 0.f, 0.f), to_radians(-30.f));
spotlight.pan.pan[0] = -.25;
spotlight.pan.pan[1] = 1.5;
spotlight.dolly.dolly[2] = -2.; // push spotlight forward
spotlight.trackball.r = rotationf(vec3f(1.f, 0.f, 0.f), to_radians(-60.f));
spotlight.trackball.invert_increment = true;

beginnertom
02-18-2010, 05:41 AM
thanks finally i slove my problem.i just wanted to say.
how difficult it is ,no one reply.o ~`[censored]
here is my slove code

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();
gluOrtho2D(-25,25,-25,25);
glTranslatef(0,0,-4);
glClear(GL_COLOR_BUFFER_BIT);
if (animate)
glRotatef(30.0f, 1.0f, 0.0f, 0.0f);
glBegin(GL_TRIANGLES);
glIndexi(1);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2i(0, 10);
glIndexi(2);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2i(-10, -10);
glIndexi(3);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex2i(10, -10);
glEnd();
glFlush();