大家看过来:关于OPENGL的问题

liuyun1984 2003-12-12 08:56:57

为什么我画出来的正方形是红色的,而不是绿色的;
怎么样才能够让其起成为绿色。
程序在下面。

源程序
#include <vcl.h>
#include <math.h>

#pragma hdrstop

#include "draw.h"
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
TOpenGL_Form *OpenGL_Form;
//---------------------------------------------------------------------------
__fastcall TOpenGL_Form::TOpenGL_Form(TComponent* Owner)
: TForm(Owner)
{

}
//---------------------------------------------------------------------------
void __fastcall TOpenGL_Form::Timer1Timer(TObject *Sender)
{
Timer1->Enabled = false;
DrawScene();
Yield();
Timer1->Enabled = true;
}

BOOL bSetupPixelFormat(HDC hdc)
{
PIXELFORMATDESCRIPTOR pfd, *ppfd;
int pixelformat;

ppfd = &pfd;

ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
ppfd->nVersion = 1;
ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER;
ppfd->dwLayerMask = PFD_MAIN_PLANE;
ppfd->iPixelType = PFD_TYPE_COLORINDEX;
ppfd->cColorBits = 8;
ppfd->cDepthBits = 16;

ppfd->cAccumBits = 0;
ppfd->cStencilBits = 0;

if ( (pixelformat = ChoosePixelFormat(hdc, ppfd)) == 0 )
{
MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
return FALSE;
}

if (SetPixelFormat(hdc, pixelformat, ppfd) == FALSE)
{
MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
return FALSE;
}

return TRUE;

}

void TOpenGL_Form::DrawScene()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

glPushMatrix();


polarView( radius, 0, latitude, longitude );
createObjects();



glPopMatrix();


SwapBuffers(ghDC);
}

GLvoid TOpenGL_Form::resize( GLsizei width, GLsizei height )
{
GLfloat aspect;

glViewport( 0, 0, width, height );

aspect = (GLfloat) width / height;

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 90.0, aspect, 3.0, 17.0 );
glMatrixMode( GL_MODELVIEW );
}

GLvoid TOpenGL_Form::createObjects()
{

glBegin(GL_QUADS);
glShadeModel(GL_FLAT);
glColor3f(0.0,1.0,1.0);
glVertex3f(0.0,0.0,0.0);
glVertex3f(1.0,0.0,0.0);
glVertex3f(1.0,1.0,0.0);
glVertex3f(0.0,1.0,0.0);
glEnd();


}

GLvoid TOpenGL_Form::initializeGL(GLsizei width, GLsizei height)
{
GLfloat maxObjectSize, aspect;
GLdouble near_plane;

glClearIndex( (GLfloat)BLACK_INDEX);

glClearDepth( 1.0 );

glEnable(GL_DEPTH_TEST);

glMatrixMode( GL_PROJECTION );
aspect = (GLfloat) width / height;
gluPerspective( 45.0, aspect, 3.0,7.0 );
glMatrixMode( GL_MODELVIEW );


createObjects();
}

void TOpenGL_Form::polarView(GLdouble radius, GLdouble twist, GLdouble latitude,
GLdouble longitude)
{
glTranslated(0.0, 0.0, -4.5);

}


//---------------------------------------------------------------------------
void __fastcall TOpenGL_Form::FormCreate(TObject *Sender)
{
ghDC = GetDC(Handle);
if (!bSetupPixelFormat(ghDC))
Close();
ghRC = wglCreateContext(ghDC);
wglMakeCurrent(ghDC, ghRC);
initializeGL(ClientRect.Right, ClientRect.Bottom);
}
//---------------------------------------------------------------------------
void __fastcall TOpenGL_Form::FormResize(TObject *Sender)
{
resize(ClientRect.Right, ClientRect.Bottom);
}
//---------------------------------------------------------------------------
void __fastcall TOpenGL_Form::FormClose(TObject *Sender, TCloseAction &Action)
{
Timer1->Enabled = false;
if (ghRC)
wglDeleteContext(ghRC);
if (ghDC)
ReleaseDC(Handle, ghDC);
}
//---------------------------------------------------------------------------
void __fastcall TOpenGL_Form::FormKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
Close();
}
//---------------------------------------------------------------------------


头文件
//---------------------------------------------------------------------------
#ifndef drawH
#define drawH
//---------------------------------------------------------------------------
#include <vcl\Classes.hpp>
#include <vcl\Controls.hpp>
#include <vcl\StdCtrls.hpp>
#include <vcl\Forms.hpp>
#include <vcl\ExtCtrls.hpp>
#include <GL/gl.h>
#include <GL/glu.h>

//---------------------------------------------------------------------------

#define BLACK_INDEX 0
#define RED_INDEX 13
#define GREEN_INDEX 14
#define BLUE_INDEX 16

#define GLOBE 1
#define CYLINDER 2
#define CONE 3

//---------------------------------------------------------------------------
class TOpenGL_Form : public TForm
{
__published: // IDE-managed Components
TTimer *Timer1;
void __fastcall Timer1Timer(TObject *Sender);
void __fastcall FormCreate(TObject *Sender);
void __fastcall FormResize(TObject *Sender);
void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
void __fastcall FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift);
private: // User declarations
void DrawScene();
void polarView(GLdouble radius, GLdouble twist, GLdouble latitude,
GLdouble longitude);
GLvoid initializeGL(GLsizei width, GLsizei height);
GLvoid createObjects();
GLvoid resize( GLsizei width, GLsizei height );
GLfloat latitude, longitude, latinc, longinc;
GLdouble radius;
HGLRC ghRC;
HDC ghDC;

public: // User declarations
__fastcall TOpenGL_Form(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern TOpenGL_Form *OpenGL_Form;
//---------------------------------------------------------------------------
#endif
...全文
11 4 打赏 收藏 举报
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
liuyun1984 2003-12-13
我已经解决了,我使用了索引形式的颜色
  • 打赏
  • 举报
回复
liuyun1984 2003-12-13
这是bcb6.0下的原代码
  • 打赏
  • 举报
回复
liuyun1984 2003-12-13
大家帮忙啊!我急需解决这个问题
  • 打赏
  • 举报
回复
constantine 2003-12-12
帮你顶一下。
  • 打赏
  • 举报
回复
相关推荐
发帖
C++ Builder
加入

1.3w+

社区成员

C++ Builder相关内容讨论区
申请成为版主
帖子事件
创建了帖子
2003-12-12 08:56
社区公告
暂无公告