Replaced the boring 2D rendering with a colorful spinning 3D cube.
git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@20807 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -7,6 +7,9 @@
|
|||||||
#include <DirectWindow.h>
|
#include <DirectWindow.h>
|
||||||
#include <CheckBox.h>
|
#include <CheckBox.h>
|
||||||
#include <GLView.h>
|
#include <GLView.h>
|
||||||
|
#include <GL/glu.h>
|
||||||
|
|
||||||
|
#define REDRAW_MSG 'rdrw'
|
||||||
|
|
||||||
class SampleGLView : public BGLView
|
class SampleGLView : public BGLView
|
||||||
{
|
{
|
||||||
@@ -14,17 +17,18 @@ public:
|
|||||||
SampleGLView(BRect frame, uint32 type);
|
SampleGLView(BRect frame, uint32 type);
|
||||||
virtual void AttachedToWindow(void);
|
virtual void AttachedToWindow(void);
|
||||||
virtual void FrameResized(float newWidth, float newHeight);
|
virtual void FrameResized(float newWidth, float newHeight);
|
||||||
virtual void ErrorCallback(GLenum which);
|
virtual void MessageReceived(BMessage * msg);
|
||||||
|
|
||||||
void Render(void);
|
void Render(void);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void gInit(void);
|
void gInit(void);
|
||||||
void gDraw(void);
|
void gDraw(float rotation = 0);
|
||||||
void gReshape(int width, int height);
|
void gReshape(int width, int height);
|
||||||
|
|
||||||
float width;
|
float width;
|
||||||
float height;
|
float height;
|
||||||
|
float rotate;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -33,11 +37,14 @@ class SampleGLWindow : public BDirectWindow
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
SampleGLWindow(BRect frame, uint32 type);
|
SampleGLWindow(BRect frame, uint32 type);
|
||||||
|
~SampleGLWindow();
|
||||||
|
|
||||||
virtual bool QuitRequested();
|
virtual bool QuitRequested();
|
||||||
virtual void DirectConnected( direct_buffer_info *info );
|
virtual void DirectConnected( direct_buffer_info *info );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SampleGLView *theView;
|
SampleGLView *theView;
|
||||||
|
BMessageRunner *updateRunner;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -54,7 +61,7 @@ SampleGLApp::SampleGLApp()
|
|||||||
: BApplication("application/x-vnd.sample")
|
: BApplication("application/x-vnd.sample")
|
||||||
{
|
{
|
||||||
BRect windowRect;
|
BRect windowRect;
|
||||||
uint32 type = BGL_RGB|BGL_DOUBLE;
|
uint32 type = BGL_RGB|BGL_DOUBLE|BGL_DEPTH;
|
||||||
|
|
||||||
windowRect.Set(50, 50, 350, 350);
|
windowRect.Set(50, 50, 350, 350);
|
||||||
|
|
||||||
@@ -72,10 +79,20 @@ SampleGLWindow::SampleGLWindow(BRect frame, uint32 type)
|
|||||||
theView = new SampleGLView(r, type);
|
theView = new SampleGLView(r, type);
|
||||||
AddChild(theView);
|
AddChild(theView);
|
||||||
Show();
|
Show();
|
||||||
|
|
||||||
|
updateRunner = new BMessageRunner(BMessenger(theView),
|
||||||
|
new BMessage(REDRAW_MSG), 1000000/60 /* 60 fps */);
|
||||||
|
|
||||||
theView->Render();
|
theView->Render();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
SampleGLWindow::~SampleGLWindow()
|
||||||
|
{
|
||||||
|
delete updateRunner;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
bool SampleGLWindow::QuitRequested()
|
bool SampleGLWindow::QuitRequested()
|
||||||
{
|
{
|
||||||
theView->EnableDirectMode(false);
|
theView->EnableDirectMode(false);
|
||||||
@@ -90,10 +107,10 @@ void SampleGLWindow::DirectConnected(direct_buffer_info *info)
|
|||||||
theView->EnableDirectMode(true);
|
theView->EnableDirectMode(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ----
|
||||||
|
|
||||||
SampleGLView::SampleGLView(BRect frame, uint32 type)
|
SampleGLView::SampleGLView(BRect frame, uint32 type)
|
||||||
: BGLView(frame, "SampleGLView", B_FOLLOW_ALL_SIDES, 0, type)
|
: BGLView(frame, "SampleGLView", B_FOLLOW_ALL_SIDES, 0, type), rotate(0)
|
||||||
{
|
{
|
||||||
width = frame.right-frame.left;
|
width = frame.right-frame.left;
|
||||||
height = frame.bottom-frame.top;
|
height = frame.bottom-frame.top;
|
||||||
@@ -126,93 +143,65 @@ void SampleGLView::FrameResized(float newWidth, float newHeight)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void SampleGLView::ErrorCallback(GLenum whichError)
|
|
||||||
{
|
|
||||||
// fprintf(stderr, "Unexpected error occured (%d):\\n", whichError);
|
|
||||||
// fprintf(stderr, " %s\\n", gluErrorString(whichError));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// globals
|
|
||||||
GLenum use_stipple_mode; // GL_TRUE to use dashed lines
|
|
||||||
GLenum use_smooth_mode; // GL_TRUE to use anti-aliased lines
|
|
||||||
GLint linesize; // Line width
|
|
||||||
GLint pointsize; // Point diameter
|
|
||||||
|
|
||||||
float pntA[3] = {
|
|
||||||
-160.0, 0.0, 0.0
|
|
||||||
};
|
|
||||||
float pntB[3] = {
|
|
||||||
-130.0, 0.0, 0.0
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void SampleGLView::gInit(void)
|
void SampleGLView::gInit(void)
|
||||||
{
|
{
|
||||||
glClearColor(0.0, 0.0, 0.0, 0.0);
|
glClearColor(0.0, 0.0, 0.0, 0.0);
|
||||||
glLineStipple(1, 0xF0E0);
|
glEnable(GL_DEPTH_TEST);
|
||||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
|
glDepthMask(GL_TRUE);
|
||||||
use_stipple_mode = GL_FALSE;
|
|
||||||
use_smooth_mode = GL_TRUE;
|
|
||||||
linesize = 2;
|
|
||||||
pointsize = 6;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void SampleGLView::gDraw(void)
|
void SampleGLView::gDraw(float rotation)
|
||||||
{
|
{
|
||||||
GLint i;
|
/* Clear the buffer, clear the matrix */
|
||||||
|
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glLoadIdentity();
|
||||||
glLineWidth(linesize);
|
|
||||||
|
|
||||||
/*
|
|
||||||
|
|
||||||
if (use_stipple_mode) {
|
/* A step backward, then spin the cube */
|
||||||
glEnable(GL_LINE_STIPPLE);
|
glTranslatef(0, 0, -5);
|
||||||
} else {
|
glRotatef(rotation, 0, 0, 1);
|
||||||
glDisable(GL_LINE_STIPPLE);
|
glRotatef(rotation, 1, 0.6, 0);
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
glDisable(GL_POINT_SMOOTH);
|
/* We tell we want to draw quads */
|
||||||
|
glBegin (GL_QUADS);
|
||||||
|
|
||||||
|
/* Every four calls to glVertex, a quad is drawn */
|
||||||
|
glColor3f (0, 0, 0); glVertex3f (-1, -1, -1);
|
||||||
|
glColor3f (0, 0, 1); glVertex3f (-1, -1, 1);
|
||||||
|
glColor3f (0, 1, 1); glVertex3f (-1, 1, 1);
|
||||||
|
glColor3f (0, 1, 0); glVertex3f (-1, 1, -1);
|
||||||
|
|
||||||
glPushMatrix();
|
glColor3f (1, 0, 0); glVertex3f ( 1, -1, -1);
|
||||||
|
glColor3f (1, 0, 1); glVertex3f ( 1, -1, 1);
|
||||||
glPointSize(pointsize); // Set size for point
|
glColor3f (1, 1, 1); glVertex3f ( 1, 1, 1);
|
||||||
|
glColor3f (1, 1, 0); glVertex3f ( 1, 1, -1);
|
||||||
|
|
||||||
for (i = 0; i < 360; i += 5) {
|
glColor3f (0, 0, 0); glVertex3f (-1, -1, -1);
|
||||||
glRotatef(5.0, 0,0,1); // Rotate right 5 degrees
|
glColor3f (0, 0, 1); glVertex3f (-1, -1, 1);
|
||||||
|
glColor3f (1, 0, 1); glVertex3f ( 1, -1, 1);
|
||||||
|
glColor3f (1, 0, 0); glVertex3f ( 1, -1, -1);
|
||||||
|
|
||||||
if (use_smooth_mode) {
|
glColor3f (0, 1, 0); glVertex3f (-1, 1, -1);
|
||||||
glEnable(GL_LINE_SMOOTH);
|
glColor3f (0, 1, 1); glVertex3f (-1, 1, 1);
|
||||||
glEnable(GL_BLEND);
|
glColor3f (1, 1, 1); glVertex3f ( 1, 1, 1);
|
||||||
} else {
|
glColor3f (1, 1, 0); glVertex3f ( 1, 1, -1);
|
||||||
glDisable(GL_LINE_SMOOTH);
|
|
||||||
glDisable(GL_BLEND);
|
|
||||||
}
|
|
||||||
|
|
||||||
glColor3f(1.0, 1.0, 0.0); // Set color for line
|
glColor3f (0, 0, 0); glVertex3f (-1, -1, -1);
|
||||||
glBegin(GL_LINE_STRIP); // And create the line
|
glColor3f (0, 1, 0); glVertex3f (-1, 1, -1);
|
||||||
glVertex3fv(pntA);
|
glColor3f (1, 1, 0); glVertex3f ( 1, 1, -1);
|
||||||
glVertex3fv(pntB);
|
glColor3f (1, 0, 0); glVertex3f ( 1, -1, -1);
|
||||||
glEnd();
|
|
||||||
|
|
||||||
glDisable(GL_POINT_SMOOTH);
|
glColor3f (0, 0, 1); glVertex3f (-1, -1, 1);
|
||||||
glDisable(GL_BLEND);
|
glColor3f (0, 1, 1); glVertex3f (-1, 1, 1);
|
||||||
|
glColor3f (1, 1, 1); glVertex3f ( 1, 1, 1);
|
||||||
|
glColor3f (1, 0, 1); glVertex3f ( 1, -1, 1);
|
||||||
|
|
||||||
glColor3f(0.0, 1.0, 0.0); // Set color for point
|
/* No more quads */
|
||||||
glBegin(GL_POINTS);
|
glEnd ();
|
||||||
glVertex3fv(pntA); // Draw point at one end
|
|
||||||
glVertex3fv(pntB); // Draw point at other end
|
/* End */
|
||||||
glEnd();
|
glFlush ();
|
||||||
}
|
|
||||||
|
|
||||||
glPopMatrix(); // Done with matrix
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -221,7 +210,7 @@ void SampleGLView::gReshape(int width, int height)
|
|||||||
glViewport(0, 0, width, height);
|
glViewport(0, 0, width, height);
|
||||||
glMatrixMode(GL_PROJECTION);
|
glMatrixMode(GL_PROJECTION);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
glOrtho(-175, 175, -175, 175, -1, 1);
|
gluPerspective(45, width / (float) height, 1, 500);
|
||||||
glMatrixMode(GL_MODELVIEW);
|
glMatrixMode(GL_MODELVIEW);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,11 +218,25 @@ void SampleGLView::gReshape(int width, int height)
|
|||||||
void SampleGLView::Render(void)
|
void SampleGLView::Render(void)
|
||||||
{
|
{
|
||||||
LockGL();
|
LockGL();
|
||||||
gDraw();
|
gDraw(rotate);
|
||||||
SwapBuffers();
|
SwapBuffers();
|
||||||
UnlockGL();
|
UnlockGL();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SampleGLView::MessageReceived(BMessage * msg)
|
||||||
|
{
|
||||||
|
switch (msg->what) {
|
||||||
|
case REDRAW_MSG:
|
||||||
|
Render();
|
||||||
|
/* Rotate a bit more */
|
||||||
|
rotate++;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
BGLView::MessageReceived(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
|
|||||||
Reference in New Issue
Block a user