PDA

View Full Version : Gradientt Background



Digga
07-21-2009, 01:26 PM
Hello,
Im new to OpenGl and am having trouble setting up a gradient background:
Here is my code:


#include "stdafx.h"
#include "OpenGL.h"

COpenGL::COpenGL(void)
{
m_fPosX = 0.0f; // X position of model in camera view
m_fPosY = 0.0f; // Y position of model in camera view
m_fZoom = 10.0f; // Zoom on model in camera view
m_fRotX = 0.0f; // Rotation on model in camera view
m_fRotY = 0.0f; // Rotation on model in camera view
m_bIsMaximized = false;
}

COpenGL::~COpenGL(void)
{

}

BEGIN_MESSAGE_MAP(COpenGL, CWnd)
ON_WM_PAINT()
ON_WM_SIZE()
ON_WM_CREATE()
ON_WM_TIMER()
ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()

void COpenGL::OnPaint()
{
//CPaintDC dc(this); // device context for painting

ValidateRect(NULL);
}

void COpenGL::OnSize(UINT nType, int cx, int cy)
{
CWnd::OnSize(nType, cx, cy);

if (0 >= cx || 0 >= cy || nType == SIZE_MINIMIZED) return;

// Map the OpenGL coordinates.
glViewport(0, 0, cx, cy);

// Projection view
glMatrixMode(GL_PROJECTION);

glLoadIdentity();

// Set our current view perspective
gluPerspective(35.0f, (float)cx / (float)cy, 0.01f, 2000.0f);

// Model view
glMatrixMode(GL_MODELVIEW);

switch (nType)
{
// If window resize token is "maximize"
case SIZE_MAXIMIZED:
{
// Get the current window rect
GetWindowRect(m_rect);

// Move the window accordingly
MoveWindow(6, 6, cx - 14, cy - 14);

// Get the new window rect
GetWindowRect(m_rect);

// Store our old window as the new rect
m_oldWindow = m_rect;

break;
}

// If window resize token is "restore"
case SIZE_RESTORED:
{
// If the window is currently maximized
if (m_bIsMaximized)
{
// Get the current window rect
GetWindowRect(m_rect);

// Move the window accordingly (to our stored old window)
MoveWindow(m_oldWindow.left, m_oldWindow.top - 18, m_originalRect.Width() - 4, m_originalRect.Height() - 4);

// Get the new window rect
GetWindowRect(m_rect);

// Store our old window as the new rect
m_oldWindow = m_rect;
}

break;
}
}
}

int COpenGL::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1) return -1;

oglInitialize();

return 0;
}

void COpenGL::OnDraw(CDC *pDC)
{

// If the current view is perspective...
glLoadIdentity();

glTranslatef(0.0f, 0.0f, -m_fZoom);
glTranslatef(m_fPosX, m_fPosY, 0.0f);
glRotatef(m_fRotX, 1.0f, 0.0f, 0.0f);
glRotatef(m_fRotY, 0.0f, 1.0f, 0.0f);
}
void COpenGL::OnTimer(UINT nIDEvent)
{

switch (nIDEvent)
{
case 1:
{
// Clear color and depth buffer bits
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Draw OpenGL scene
oglDrawScene();

// Swap buffers
SwapBuffers(hdc);

break;
}

default:
break;
}

CWnd::OnTimer(nIDEvent);
}

void COpenGL::OnMouseMove(UINT nFlags, CPoint point)
{
int diffX = (int)(point.x - m_fLastX);
int diffY = (int)(point.y - m_fLastY);
m_fLastX = (float)point.x;
m_fLastY = (float)point.y;

// Left mouse button
if (nFlags & MK_LBUTTON)
{
m_fRotX += (float)0.5f * diffY;

if ((m_fRotX > 360.0f) || (m_fRotX < -360.0f))
{
m_fRotX = 0.0f;
}

m_fRotY += (float)0.5f * diffX;

if ((m_fRotY > 360.0f) || (m_fRotY < -360.0f))
{
m_fRotY = 0.0f;
}
}

// Right mouse button
else if (nFlags & MK_RBUTTON)
{
m_fZoom -= (float)0.1f * diffY;
}

// Middle mouse button
else if (nFlags & MK_MBUTTON)
{
m_fPosX += (float)0.05f * diffX;
m_fPosY -= (float)0.05f * diffY;
}

OnDraw(NULL);

CWnd::OnMouseMove(nFlags, point);
}

void COpenGL::oglCreate(CRect rect, CWnd *parent)
{
CString OpenGLWindow = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW | CS_OWNDC,
NULL, (HBRUSH)GetStockObject(LTGRAY_BRUSH), NULL);

CreateEx(0, OpenGLWindow, "OpenGL", WS_CHILD | WS_VISIBLE,
CRect(0,142,800,600), parent, 0);

// Set initial variables' values
m_oldWindow = rect;
m_originalRect = rect;


hWnd = parent;
}

void COpenGL::oglInitialize(void)
{
// Initial Setup:
//
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32, // bit depth
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16, // z-buffer depth
0, 0, 0, 0, 0, 0, 0,
};

// Get device context only once.
hdc = GetDC()->m_hDC;

// Pixel format.
m_nPixelFormat = ChoosePixelFormat(hdc, &amp;pfd);
SetPixelFormat(hdc, m_nPixelFormat, &amp;pfd);

// Create the OpenGL Rendering Context.
hrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, hrc);

// Basic Setup:
//
// Set color to use when clearing the background.
//glClearColor(0.0f, 0.0f, 0.0f, 1.0f);// black background

////////// gradient backgroud
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

glBegin(GL_QUADS);
//red color
glColor4f(0.9f,0.9f,1.0f,1.0f);
glVertex3f(-2.0,-2.0,-1.0);
glVertex3f(2.0,-2.0,-1.0);
//blue color
glColor4f(0.2f,0.2f,0.6f,1.0f);
glVertex3f(2.0, 2.0,-1.0);
glVertex3f(-2.0, 2.0,-1.0);
glEnd();
///////// end gradient

glClearDepth(1.0f);
glMatrixMode (GL_MODELVIEW);

// Turn on backface culling
glFrontFace(GL_CCW);
glCullFace(GL_BACK);

// Turn on depth testing
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

// Send draw request
OnDraw(NULL);
}

void COpenGL::oglDrawScene(void)
{

// draw carthesian axes
glBegin(GL_LINES);
// red x axis
glColor3f(1.f,0.f,0.f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(1.0f,0.0f,0.0f);
glVertex3f(1.0f,0.0f,0.0f);
glVertex3f(0.9f,0.1f,0.0f);
glVertex3f(1.0f,0.0f,0.0f);
glVertex3f(0.9f,-0.1f,0.0f);
// green y axis
glColor3f(0.f,1.f,0.f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
glVertex3f(0.1f,0.9f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
glVertex3f(-0.1f,0.9f,0.0f);
// blue z axis
glColor3f(0.f,0.f,1.f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f,0.1f,0.9f);
glVertex3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f,-0.1f,0.9f);
glEnd();
// Wireframe Mode
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

glBegin(GL_QUADS);
// Front Side
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);

// Back Side
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);

// Top Side
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);

// Bottom Side
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);

// Right Side
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);

// Left Side
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();
}

If I move the background drawing code to the begining of oglDrawSceme I get a box that rotates with the other initialized drawing.
Please help!!!

Thanks in Advanced
Derrek

dorbie
07-22-2009, 05:09 AM
Well this is normal.

If you want a gradient background on the window that doesn't move you should ensure the modelview is identity (or whatever xform you want to position the background relative to the eye) and that the projection matrix is appropriate perhaps even ortho to keep it simple although a gradient quad is easy to set up for any case you care to design.

Basically you need to draw the gradient while you're in eye space at a minimum. This just means putting that draw call in the right place and ensuring you position it correctly, don't expect to be ale to draw it any time unless you meet/restore these matrix requirements.

It is appropriate to modify ondraw to initialize these matrices with identity each frame, draw the gradient and then set the matrices to position the eye in the scene for 3D rendering purposes.

Digga
07-22-2009, 06:58 AM
I have added the gradient code to OnDraw but its still not drawing a background to the window,
code:

void COpenGL::OnDraw(CDC *pDC)
{
////////// gradient backgroud
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

glBegin(GL_QUADS);
//red color
glColor4f(0.9f,0.9f,1.0f,1.0f);
glVertex3f(-2.0,-2.0,-1.0);
glVertex3f(2.0,-2.0,-1.0);
//blue color
glColor4f(0.2f,0.2f,0.6f,1.0f);
glVertex3f(2.0, 2.0,-1.0);
glVertex3f(-2.0, 2.0,-1.0);
glEnd();
///////// end gradient

// If the current view is perspective...
glLoadIdentity();

glTranslatef(0.0f, 0.0f, -m_fZoom);
glTranslatef(m_fPosX, m_fPosY, 0.0f);
glRotatef(m_fRotX, 1.0f, 0.0f, 0.0f);
glRotatef(m_fRotY, 0.0f, 1.0f, 0.0f);
}

sammie381
07-22-2009, 06:26 PM
Setting identity for GL_MODELVIEW is ok, but it's not enough.

I don't think you're setting up the GL_PROJECTION mode properly.
Call glOrtho() in GL_PROJECTION mode.