EDN Admin
Well-known member
Hi everyone. Ive been struggling sometime now to get my OpenGL windows screensaver to display a preview in the preview window. Just to make sure Ive cleared that hurdle before moving on to actually making a miniature version draw itself in the preview
window Im trying to get a white texture to show up in the preview window. It isnt working. When I select my screensaver the preview window remains black. However, I have noticed that when I go to select a different screensaver for a split second from when
I select the new screensaver to when it actually changes over the preview window does turn white. I suspect something like a redisplay command is needed but InvalidateRect isnt doing the trick. Ill post my code here. WARNING! Its long.
#include <windows.h><br/>
#include <scrnsave.h><br/>
#include <GL/glu.h><br/>
#include <GL/gl.h><br/>
#include <stdlib.h><br/>
#include <math.h><br/>
#include <time.h><br/>
#include "resource.h"<br/>
<br/>
#pragma comment(lib, "OpenGL32.lib")<br/>
#pragma comment(lib, "GLu32.lib")<br/>
<br/>
<br/>
#ifdef UNICODE<br/>
<br/>
#pragma comment(lib, "ScrnSavw.lib")<br/>
<br/>
#else<br/>
<br/>
#pragma comment(lib, "ScrnSave.lib")<br/>
<br/>
#endif<br/>
<br/>
<br/>
<br/>
#pragma comment(lib, "comctl32.lib")<br/>
<br/>
//get rid of these warnings:<br/>
//truncation from const double to float<br/>
//conversion from double to float<br/>
#pragma warning(disable: 4305 4244) <br/>
<br/>
//Define a Windows timer<br/>
<br/>
#define TIMER 1<br/>
<br/>
struct PIXELA<br/>
{<br/>
GLubyte r, g, b, a;<br/>
};<br/>
<br/>
bool previewMode = false;<br/>
<br/>
int sphereDiameter;<br/>
float mag = 1.0f;<br/>
<br/>
float **offsetX1, **offsetY1, **offsetX2, **offsetY2;<br/>
float **offsetX3, **offsetY3, **offsetX4, **offsetY4;<br/>
<br/>
PIXELA *thetex;<br/>
<br/>
GLubyte *pixelBuffer;<br/>
<br/>
GLuint textureId, tex;<br/>
<br/>
int Width, Height;<br/>
<br/>
int xpos, ypos, step = 6;<br/>
bool goRight = true, goUp = true;<br/>
<br/>
bool inChild = false;<br/>
<br/>
void InitGL(HWND hWnd, HDC & hDC, HGLRC & hRC);<br/>
void CloseGL(HWND hWnd, HDC hDC, HGLRC hRC);<br/>
void SetupAnimation(int Width, int Height);<br/>
void OnTimer(HDC hDC);<br/>
<br/>
// Screen Saver Procedure<br/>
LRESULT WINAPI ScreenSaverProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)<br/>
{<br/>
static HDC hDC;<br/>
static HGLRC hRC;<br/>
static RECT rect;<br/>
LPWSTR lpCmdLine;<br/>
char cmdLine[40] = {0};<br/>
HWND parentWnd;<br/>
int i;<br/>
<br/>
switch(message) <br/>
{<br/>
<br/>
case WM_CREATE: <br/>
lpCmdLine = GetCommandLine();<br/>
<br/>
wcstombs(cmdLine, lpCmdLine, 40);<br/>
<br/>
for(i = 0;i < 30;i++)<br/>
{<br/>
if(cmdLine == / && cmdLine[i + 1] == p)<br/>
{<br/>
previewMode = true;<br/>
break;<br/>
} <br/>
}<br/>
<br/>
if(previewMode && !inChild)<br/>
{<br/>
parentWnd = (HWND)atoi(&cmdLine[i + 3]);<br/>
<br/>
GetClientRect(parentWnd, &rect);<br/>
<br/>
Width = rect.right - rect.left; <br/>
Height = rect.bottom - rect.top;<br/>
<br/>
LPCTSTR lpszClassName = TEXT("LensEffectScreenSaver");<br/>
<br/>
WNDCLASSEX wc;<br/>
<span style="white-spacere <br/>
ZeroMemory(&wc, sizeof(wc));<br/>
wc.cbSize<span style="white-spacere = sizeof(wc);<br/>
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;<br/>
wc.lpfnWndProc = (WNDPROC) ScreenSaverProc;<br/>
wc.cbClsExtra<span style="white-spacere = 0;<br/>
wc.cbWndExtra = 0;<br/>
wc.hInstance = (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE);<br/>
wc.hIcon = NULL;<br/>
wc.hCursor = LoadCursor(NULL, IDC_ARROW);<br/>
wc.hbrBackground = NULL;<br/>
wc.lpszMenuName = NULL;<br/>
wc.lpszClassName = lpszClassName;<br/>
<br/>
RegisterClassEx(&wc);<br/>
<br/>
inChild = true;<br/>
<br/>
hWnd = CreateWindowEx(NULL, lpszClassName, TEXT(""), WS_VISIBLE | WS_CHILD | WS_CLIPCHILDREN, 0, 0, Width, Height, parentWnd, NULL, (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), NULL); <br/>
<br/>
SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);<br/>
ShowWindow(hWnd, SW_SHOW);<br/>
UpdateWindow(hWnd); <br/>
SetFocus(hWnd);<br/>
<br/>
sphereDiameter = (int)(0.29296875 * Width);<br/>
<br/>
if(sphereDiameter % 2 != 0)<br/>
sphereDiameter++;<br/>
<br/>
xpos = -1 * sphereDiameter / 2;<br/>
ypos = -1 * sphereDiameter / 2;<br/>
<br/>
<br/>
// setup OpenGL, then animation<br/>
InitGL(hWnd, hDC, hRC); <br/>
<br/>
SetupAnimation(Width, Height);<br/>
<br/>
//set timer to tick every 10 ms<br/>
SetTimer(hWnd, TIMER, 10, NULL); <br/>
} <br/>
else if(!previewMode)<br/>
{ <br/>
GetClientRect(hWnd, &rect);<br/>
<br/>
Width = rect.right; <br/>
Height = rect.bottom;<br/>
<br/>
sphereDiameter = (int)(0.29296875 * Width);<br/>
<br/>
if(sphereDiameter % 2 != 0)<br/>
sphereDiameter++;<br/>
<br/>
xpos = -1 * sphereDiameter / 2;<br/>
ypos = -1 * sphereDiameter / 2;<br/>
<br/>
<br/>
// setup OpenGL, then animation<br/>
InitGL(hWnd, hDC, hRC); <br/>
<br/>
SetupAnimation(Width, Height);<br/>
<br/>
//set timer to tick every 10 ms<br/>
SetTimer(hWnd, TIMER, 10, NULL); <br/>
} <br/>
<br/>
return 0;<br/>
<br/>
case WM_DESTROY:<br/>
<br/>
KillTimer(hWnd, TIMER);<br/>
CloseGL(hWnd, hDC, hRC);<br/>
<br/>
return 0;<br/>
<br/>
case WM_TIMER:<br/>
<br/>
OnTimer(hDC); //animate! <br/>
<br/>
return 0; <br/>
<br/>
}<br/>
<br/>
return DefScreenSaverProc(hWnd, message, wParam, lParam);<br/>
<br/>
}<br/>
<br/>
BOOL WINAPI ScreenSaverConfigureDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)<br/>
{<br/>
<br/>
// Dialog message handling<br/>
switch(message)<br/>
{<br/>
case WM_COMMAND:<br/>
switch(LOWORD(wParam))<br/>
{<br/>
case IDOK:<br/>
EndDialog(hDlg, LOWORD(wParam) == IDOK);<br/>
return true;<br/>
<br/>
case IDCANCEL:<br/>
EndDialog(hDlg, LOWORD(wParam) == IDOK);<br/>
return true;<br/>
}<br/>
}<br/>
<br/>
return false;<br/>
}<br/>
<br/>
<br/>
<br/>
// needed for SCRNSAVE.LIB<br/>
BOOL WINAPI RegisterDialogClasses(HANDLE hInst)<br/>
{<br/>
return TRUE;<br/>
}<br/>
<br/>
void transform(float i, float j, float *ix, float *iy)<br/>
{<br/>
float x, y, xnew, ynew;<br/>
float r, theta, rnew, thetanew;<br/>
<br/>
x = i / ((float)sphereDiameter / 2.0f) - 1.0f;<br/>
y = j / ((float)sphereDiameter / 2.0f) - 1.0f;<br/>
<br/>
r = sqrtf(x * x + y * y);<br/>
<br/>
theta = atan2f(y, x);<br/>
<br/>
rnew = asinf(r) / 1.5707963f;<br/>
<br/>
thetanew = theta;<br/>
xnew = rnew * cos(thetanew) * mag;<br/>
ynew = rnew * sin(thetanew) * mag;<br/>
<br/>
if(rnew <= r)<br/>
{<br/>
*ix = (xnew + 1.0f) / 2.0f;<br/>
*iy = (ynew + 1.0f) / 2.0f;<br/>
}<br/>
else<br/>
{<br/>
*ix = (x + 1.0f) / 2.0f;<br/>
*iy = (y + 1.0f) / 2.0f; <br/>
} <br/>
}<br/>
<br/>
// Initialize OpenGL<br/>
static void InitGL(HWND hWnd, HDC & hDC, HGLRC & hRC)<br/>
{<br/>
static PIXELFORMATDESCRIPTOR pfd = <br/>
{<br/>
sizeof(PIXELFORMATDESCRIPTOR),<br/>
1, // nVersion should be set to 1<br/>
PFD_DRAW_TO_WINDOW | // buffer can draw to window<br/>
PFD_SUPPORT_OPENGL | // buffer supports OpenGL drawing<br/>
PFD_DOUBLEBUFFER, // buffer is double buffered<br/>
PFD_TYPE_RGBA, // rgba pixels<br/>
32, // 32-bit color depth<br/>
0, 0, 0, 0, 0, 0, // look up rest at MSDN, color bits ignored<br/>
0, // no alpha buffer<br/>
0, // shift bit ignored<br/>
0, // no accumulation buffer<br/>
0, 0, 0, 0, // accumulation bits<br/>
16, // 16 bit z buffer<br/>
0, // no stencil buffer<br/>
0, // no auxiliary buffer<br/>
PFD_MAIN_PLANE, // main drawing layer<br/>
0, // reserved<br/>
0, 0, 0 // layer mask ignored<br/>
};<br/>
<br/>
if(!previewMode)<br/>
{<br/>
hDC = GetDC(hWnd);<br/>
<br/>
int pf = ChoosePixelFormat(hDC, &pfd); <br/>
SetPixelFormat(hDC, pf, &pfd);<br/>
<br/>
hRC = wglCreateContext(hDC);<br/>
wglMakeCurrent(hDC, hRC);<br/>
<br/>
pixelBuffer = new GLubyte[sizeof(GLubyte) * Width * Height * 4];<br/>
<br/>
glReadBuffer(GL_FRONT);<br/>
<span style="white-spacere <br/>
GLint ReadBuffer;<br/>
glGetIntegerv(GL_READ_BUFFER, &ReadBuffer);<br/>
glPixelStorei(GL_READ_BUFFER, GL_RGBA);<br/>
<span style="white-spacere <br/>
GLint PackAlignment;<br/>
glGetIntegerv(GL_PACK_ALIGNMENT, &PackAlignment); <br/>
glPixelStorei(GL_PACK_ALIGNMENT, 1);<br/>
<span style="white-spacere <br/>
glReadPixels(0, 0, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, pixelBuffer);<br/>
<br/>
thetex = (PIXELA *)malloc(sphereDiameter * sphereDiameter * sizeof(PIXELA));<br/>
}<br/>
else<br/>
{ <br/>
hDC = GetDC(hWnd);<br/>
<br/>
if(!hDC)<br/>
MessageBox(NULL, TEXT("Bad device context"), TEXT("Bad hDC"), MB_OK);<br/>
<br/>
int pf = ChoosePixelFormat(hDC, &pfd); <br/>
SetPixelFormat(hDC, pf, &pfd);<br/>
<br/>
hRC = wglCreateContext(hDC);<br/>
<br/>
if(!hRC)<br/>
MessageBox(NULL, TEXT("Bad rendering context"), TEXT("Bad hRC"), MB_OK);<br/>
<br/>
wglMakeCurrent(hDC, hRC);<br/>
<br/>
pixelBuffer = new GLubyte[sizeof(GLubyte) * Width * Height * 4];<br/>
<br/>
for(int i = 0;i < Width * Height * 4;i++)<br/>
pixelBuffer = (GLubyte)255; <br/>
}<br/>
<br/>
offsetX1 = new float*[sphereDiameter];<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
offsetX1 = new float[sphereDiameter];<br/>
<br/>
offsetX2 = new float*[sphereDiameter];<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
offsetX2 = new float[sphereDiameter];<br/>
<br/>
offsetX3 = new float*[sphereDiameter];<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
offsetX3 = new float[sphereDiameter];<br/>
<br/>
offsetX4 = new float*[sphereDiameter];<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
offsetX4 = new float[sphereDiameter];<br/>
<br/>
offsetY1 = new float*[sphereDiameter];<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
offsetY1 = new float[sphereDiameter];<br/>
<br/>
offsetY2 = new float*[sphereDiameter];<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
offsetY2 = new float[sphereDiameter];<br/>
<br/>
offsetY3 = new float*[sphereDiameter];<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
offsetY3 = new float[sphereDiameter];<br/>
<br/>
offsetY4 = new float*[sphereDiameter];<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
offsetY4 = new float[sphereDiameter]; <br/>
}<br/>
<br/>
// Shut down OpenGL<br/>
static void CloseGL(HWND hWnd, HDC hDC, HGLRC hRC)<br/>
{<br/>
wglMakeCurrent(NULL, NULL);<br/>
wglDeleteContext(hRC);<br/>
<br/>
ReleaseDC(hWnd, hDC);<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
delete offsetX1;<br/>
<br/>
delete offsetX1;<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
delete offsetX2;<br/>
<br/>
delete offsetX2;<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
delete offsetX3;<br/>
<br/>
delete offsetX3;<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
delete offsetX4;<br/>
<br/>
delete offsetX4;<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
delete offsetY1;<br/>
<br/>
delete offsetY1;<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
delete offsetY2;<br/>
<br/>
delete offsetY2;<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
delete offsetY3;<br/>
<br/>
delete offsetY3;<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
delete offsetY4;<br/>
<br/>
delete offsetY4;<br/>
<br/>
if(!previewMode)<br/>
free(thetex);<br/>
<br/>
delete pixelBuffer;<br/>
}<br/>
<br/>
void SetupAnimation(int Width, int Height)<br/>
{ <br/>
//window resizing stuff<br/>
glViewport(0, 0, (GLsizei)Width, (GLsizei)Height);<br/>
<br/>
glMatrixMode(GL_PROJECTION);<br/>
glLoadIdentity();<br/>
<br/>
gluPerspective(45.0, (double)Width / (double)Height, 1.0, 200.0);<br/>
<br/>
glMatrixMode(GL_MODELVIEW);<br/>
<br/>
glLoadIdentity();<br/>
<br/>
//background<br/>
//glClearColor(0.0, 0.0, 0.0, 0.0); //0.0s is black<br/>
<br/>
glShadeModel(GL_SMOOTH); <br/>
<br/>
float i, j;<br/>
int p, q;<br/>
<br/>
for(i = 0.0f, p = 0;i < (float)sphereDiameter;i += 1.0f, p++)<br/>
{<br/>
for(j = 0.0f, q = 0;j < (float)sphereDiameter;j += 1.0f, q++)<br/>
{<br/>
transform(i, j, &offsetX1[p][q], &offsetY1[p][q]);<br/>
transform(i, j + 1.0f, &offsetX2[p][q], &offsetY2[p][q]);<br/>
transform(i + 1.0f, j, &offsetX3[p][q], &offsetY3[p][q]);<br/>
transform(i + 1.0f, j + 1.0f, &offsetX4[p][q], &offsetY4[p][q]);<br/>
} <br/>
}<br/>
}<br/>
<br/>
void OnTimer(HDC hDC) <br/>
{<br/>
float x, y, i = 0.0f, j = 0.0f;<br/>
int a, b, Y;<br/>
<br/>
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);<br/>
<br/>
glLoadIdentity();<br/>
<br/>
glDepthMask(GL_FALSE);<br/>
<br/>
glGenTextures(1, &textureId);<br/>
glBindTexture(GL_TEXTURE_2D, textureId);<br/>
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);<br/>
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);<br/>
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); <br/>
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);<br/>
<br/>
glTexImage2D(GL_TEXTURE_2D, 0, 4, Width, Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixelBuffer);<br/>
<br/>
glMatrixMode(GL_PROJECTION);<br/>
glLoadIdentity();<br/>
gluPerspective(180.0, (float)Width / (float)Height, 0.1, 10000.0);<br/>
glMatrixMode(GL_MODELVIEW);<br/>
glLoadIdentity();<br/>
<br/>
gluLookAt(70, 0, 0, 0, 0, 0, 0, 1, 0);<br/>
<br/>
glEnable(GL_TEXTURE_2D); <br/>
<br/>
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);<br/>
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);<br/>
<br/>
glMatrixMode(GL_PROJECTION);<br/>
glLoadIdentity();<br/>
glOrtho(-1 * Width / 2, Width / 2, -1 * Height / 2, Height / 2, 1.0, 10000.0);<br/>
glMatrixMode(GL_MODELVIEW);<br/>
glLoadIdentity();<br/>
gluLookAt(0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);<br/>
glNormal3f(0.0, 0.0, 1.0);<br/>
<br/>
glBegin(GL_TRIANGLE_STRIP);<br/>
<br/>
glTexCoord2f(0.0f, 0.0f);<br/>
glVertex3f(-1.0f * (float)Width / 2.0f, -1.0f * (float)Height / 2.0f, 0.0f);<br/>
glTexCoord2f(0.0f, 1.0f);<br/>
glVertex3f(-1.0f * (float)Width / 2.0f, (float)Height / 2.0f, 0.0f);<br/>
glTexCoord2f(1.0f, 0.0f);<br/>
glVertex3f((float)Width / 2.0f, -1.0f * (float)Height / 2.0f, 0.0f);<br/>
glTexCoord2f(1.0f, 1.0f);<br/>
glVertex3f((float)Width / 2.0f, (float)Height / 2.0f, 0.0f);<br/>
<br/>
glEnd();<br/>
<br/>
glDisable(GL_TEXTURE_2D);<br/>
<br/>
glDeleteTextures(1, &textureId);<br/>
<br/>
if(!previewMode)<br/>
{ <br/>
Y = ypos;<br/>
<br/>
b = (((Height / 2 + Y) * Width) * 4) + (((Width / 2) + xpos) * 4);<br/>
<br/>
for(a = 0;a < sphereDiameter * sphereDiameter;a++, b += 4)<br/>
{<br/>
if(a % 300 == 0 && a != 0)<br/>
{<br/>
Y++;<br/>
b = (((Height / 2 + Y) * Width) * 4) + (((Width / 2) + xpos) * 4);<br/>
} <br/>
<br/>
thetex[a].r = pixelBuffer;<br/>
thetex[a].g = pixelBuffer[b + 1];<br/>
thetex[a].b = pixelBuffer[b + 2];<br/>
thetex[a].a = pixelBuffer[b + 3]; <br/>
} <br/>
<br/>
glGenTextures(1, &tex);<br/>
glBindTexture(GL_TEXTURE_2D, tex);<br/>
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);<br/>
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);<br/>
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); <br/>
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);<br/>
glTexImage2D(GL_TEXTURE_2D, 0, 4, sphereDiameter, sphereDiameter, 0, GL_RGBA, GL_UNSIGNED_BYTE, thetex);<br/>
<br/>
glEnable(GL_TEXTURE_2D);<br/>
glBindTexture(GL_TEXTURE_2D, tex);<br/>
<br/>
glBegin(GL_TRIANGLE_STRIP);<br/>
<br/>
glTexCoord2f(offsetX1[(int)i][(int)j], offsetY1[(int)i][(int)j]);<br/>
glVertex3f((float)xpos, (float)ypos, 0.0f);<br/>
glTexCoord2f(offsetX2[(int)i][(int)j], offsetY2[(int)i][(int)j]);<br/>
glVertex3f(i + (float)xpos, (j + 1.0f) + (float)ypos, 0.0f);<br/>
glTexCoord2f(offsetX3[(int)i][(int)j], offsetY3[(int)i][(int)j]);<br/>
glVertex3f((i + 1.0f) + (float)xpos, j + (float)ypos, 0.0f);<br/>
glTexCoord2f(offsetX4[(int)i][(int)j], offsetY4[(int)i][(int)j]);<br/>
glVertex3f((i + 1.0f) + (float)xpos, (j + 1.0f) + (float)ypos, 0.0f);<br/>
<br/>
while(true)<br/>
{ <br/>
i += 1.0f;<br/>
<br/>
if(i == (float)sphereDiameter)<br/>
{ <br/>
glEnd();<br/>
<br/>
j += 1.0f;<br/>
<br/>
if(i == (float)sphereDiameter && j == (float)sphereDiameter)<br/>
break;<br/>
<br/>
i = 0.0f;<br/>
<br/>
glBegin(GL_TRIANGLE_STRIP);<br/>
<br/>
glTexCoord2f(offsetX1[(int)i][(int)j], offsetY1[(int)i][(int)j]);<br/>
glVertex3f((float)xpos, (float)ypos, 0.0f);<br/>
glTexCoord2f(offsetX2[(int)i][(int)j], offsetY2[(int)i][(int)j]);<br/>
glVertex3f(i + (float)xpos, (j + 1.0f) + (float)ypos, 0.0f);<br/>
glTexCoord2f(offsetX3[(int)i][(int)j], offsetY3[(int)i][(int)j]);<br/>
glVertex3f((i + 1.0f) + (float)xpos, j + (float)ypos, 0.0f);<br/>
glTexCoord2f(offsetX4[(int)i][(int)j], offsetY4[(int)i][(int)j]);<br/>
glVertex3f((i + 1.0f) + (float)xpos, (j + 1.0f) + (float)ypos, 0.0f);<br/>
<br/>
//i += 1.0f;<br/>
}<br/>
<br/>
glTexCoord2f(offsetX3[(int)i][(int)j], offsetY3[(int)i][(int)j]);<br/>
glVertex3f((i + 1.0f) + (float)xpos, j + (float)ypos, 0.0f);<br/>
glTexCoord2f(offsetX4[(int)i][(int)j], offsetY4[(int)i][(int)j]);<br/>
glVertex3f((i + 1.0f) + (float)xpos, (j + 1.0f) + (float)ypos, 0.0f); <br/>
}<br/>
<br/>
<br/>
glBegin(GL_QUADS);<br/>
transform(0.0f, 0.0f, &x, &y);<br/>
glTexCoord2f(x, y);<br/>
glVertex3f((float)xpos, (float)ypos, 0.0f);<br/>
transform(1.0f, 0.0f + 4.0f, &x, &y);<br/>
glTexCoord2f(x, y);<br/>
glVertex3f(1.0f + (float)xpos, (float)ypos, 0.0f);<br/>
transform(1.0f, (float)sphereDiameter, &x, &y);<br/>
glTexCoord2f(x, y);<br/>
glVertex3f(1.0f + (float)xpos, (float)sphereDiameter + (float)ypos, 0.0f);<br/>
transform(0.0f, (float)sphereDiameter, &x, &y);<br/>
glTexCoord2f(x, y);<br/>
glVertex3f((float)xpos, (float)sphereDiameter + (float)ypos, 0.0f);<br/>
glEnd();<br/>
<br/>
glDisable(GL_TEXTURE_2D); <br/>
}<br/>
<br/>
glFlush();<br/>
<br/>
hDC = wglGetCurrentDC();<br/>
<br/>
SwapBuffers(hDC);<br/>
<br/>
if(!previewMode) <br/>
glDeleteTextures(1, &tex);<br/>
<br/>
if(xpos >= Width / 2 - (sphereDiameter + step))<br/>
goRight = false;<br/>
else if(xpos <= -1 * Width / 2 + step)<br/>
goRight = true;<br/>
<br/>
if(ypos >= Height / 2 - (sphereDiameter + step))<br/>
goUp = false;<br/>
else if(ypos <= -1 * Height / 2 + step)<br/>
goUp = true;<br/>
<br/>
if(goRight)<br/>
xpos += step;<br/>
else<br/>
xpos -= step;<br/>
<br/>
if(goUp)<br/>
ypos += step;<br/>
else<br/>
ypos -= step;<br/>
}
Any ideas where I might be going wrong?
View the full article
window Im trying to get a white texture to show up in the preview window. It isnt working. When I select my screensaver the preview window remains black. However, I have noticed that when I go to select a different screensaver for a split second from when
I select the new screensaver to when it actually changes over the preview window does turn white. I suspect something like a redisplay command is needed but InvalidateRect isnt doing the trick. Ill post my code here. WARNING! Its long.
#include <windows.h><br/>
#include <scrnsave.h><br/>
#include <GL/glu.h><br/>
#include <GL/gl.h><br/>
#include <stdlib.h><br/>
#include <math.h><br/>
#include <time.h><br/>
#include "resource.h"<br/>
<br/>
#pragma comment(lib, "OpenGL32.lib")<br/>
#pragma comment(lib, "GLu32.lib")<br/>
<br/>
<br/>
#ifdef UNICODE<br/>
<br/>
#pragma comment(lib, "ScrnSavw.lib")<br/>
<br/>
#else<br/>
<br/>
#pragma comment(lib, "ScrnSave.lib")<br/>
<br/>
#endif<br/>
<br/>
<br/>
<br/>
#pragma comment(lib, "comctl32.lib")<br/>
<br/>
//get rid of these warnings:<br/>
//truncation from const double to float<br/>
//conversion from double to float<br/>
#pragma warning(disable: 4305 4244) <br/>
<br/>
//Define a Windows timer<br/>
<br/>
#define TIMER 1<br/>
<br/>
struct PIXELA<br/>
{<br/>
GLubyte r, g, b, a;<br/>
};<br/>
<br/>
bool previewMode = false;<br/>
<br/>
int sphereDiameter;<br/>
float mag = 1.0f;<br/>
<br/>
float **offsetX1, **offsetY1, **offsetX2, **offsetY2;<br/>
float **offsetX3, **offsetY3, **offsetX4, **offsetY4;<br/>
<br/>
PIXELA *thetex;<br/>
<br/>
GLubyte *pixelBuffer;<br/>
<br/>
GLuint textureId, tex;<br/>
<br/>
int Width, Height;<br/>
<br/>
int xpos, ypos, step = 6;<br/>
bool goRight = true, goUp = true;<br/>
<br/>
bool inChild = false;<br/>
<br/>
void InitGL(HWND hWnd, HDC & hDC, HGLRC & hRC);<br/>
void CloseGL(HWND hWnd, HDC hDC, HGLRC hRC);<br/>
void SetupAnimation(int Width, int Height);<br/>
void OnTimer(HDC hDC);<br/>
<br/>
// Screen Saver Procedure<br/>
LRESULT WINAPI ScreenSaverProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)<br/>
{<br/>
static HDC hDC;<br/>
static HGLRC hRC;<br/>
static RECT rect;<br/>
LPWSTR lpCmdLine;<br/>
char cmdLine[40] = {0};<br/>
HWND parentWnd;<br/>
int i;<br/>
<br/>
switch(message) <br/>
{<br/>
<br/>
case WM_CREATE: <br/>
lpCmdLine = GetCommandLine();<br/>
<br/>
wcstombs(cmdLine, lpCmdLine, 40);<br/>
<br/>
for(i = 0;i < 30;i++)<br/>
{<br/>
if(cmdLine == / && cmdLine[i + 1] == p)<br/>
{<br/>
previewMode = true;<br/>
break;<br/>
} <br/>
}<br/>
<br/>
if(previewMode && !inChild)<br/>
{<br/>
parentWnd = (HWND)atoi(&cmdLine[i + 3]);<br/>
<br/>
GetClientRect(parentWnd, &rect);<br/>
<br/>
Width = rect.right - rect.left; <br/>
Height = rect.bottom - rect.top;<br/>
<br/>
LPCTSTR lpszClassName = TEXT("LensEffectScreenSaver");<br/>
<br/>
WNDCLASSEX wc;<br/>
<span style="white-spacere <br/>
ZeroMemory(&wc, sizeof(wc));<br/>
wc.cbSize<span style="white-spacere = sizeof(wc);<br/>
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;<br/>
wc.lpfnWndProc = (WNDPROC) ScreenSaverProc;<br/>
wc.cbClsExtra<span style="white-spacere = 0;<br/>
wc.cbWndExtra = 0;<br/>
wc.hInstance = (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE);<br/>
wc.hIcon = NULL;<br/>
wc.hCursor = LoadCursor(NULL, IDC_ARROW);<br/>
wc.hbrBackground = NULL;<br/>
wc.lpszMenuName = NULL;<br/>
wc.lpszClassName = lpszClassName;<br/>
<br/>
RegisterClassEx(&wc);<br/>
<br/>
inChild = true;<br/>
<br/>
hWnd = CreateWindowEx(NULL, lpszClassName, TEXT(""), WS_VISIBLE | WS_CHILD | WS_CLIPCHILDREN, 0, 0, Width, Height, parentWnd, NULL, (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), NULL); <br/>
<br/>
SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);<br/>
ShowWindow(hWnd, SW_SHOW);<br/>
UpdateWindow(hWnd); <br/>
SetFocus(hWnd);<br/>
<br/>
sphereDiameter = (int)(0.29296875 * Width);<br/>
<br/>
if(sphereDiameter % 2 != 0)<br/>
sphereDiameter++;<br/>
<br/>
xpos = -1 * sphereDiameter / 2;<br/>
ypos = -1 * sphereDiameter / 2;<br/>
<br/>
<br/>
// setup OpenGL, then animation<br/>
InitGL(hWnd, hDC, hRC); <br/>
<br/>
SetupAnimation(Width, Height);<br/>
<br/>
//set timer to tick every 10 ms<br/>
SetTimer(hWnd, TIMER, 10, NULL); <br/>
} <br/>
else if(!previewMode)<br/>
{ <br/>
GetClientRect(hWnd, &rect);<br/>
<br/>
Width = rect.right; <br/>
Height = rect.bottom;<br/>
<br/>
sphereDiameter = (int)(0.29296875 * Width);<br/>
<br/>
if(sphereDiameter % 2 != 0)<br/>
sphereDiameter++;<br/>
<br/>
xpos = -1 * sphereDiameter / 2;<br/>
ypos = -1 * sphereDiameter / 2;<br/>
<br/>
<br/>
// setup OpenGL, then animation<br/>
InitGL(hWnd, hDC, hRC); <br/>
<br/>
SetupAnimation(Width, Height);<br/>
<br/>
//set timer to tick every 10 ms<br/>
SetTimer(hWnd, TIMER, 10, NULL); <br/>
} <br/>
<br/>
return 0;<br/>
<br/>
case WM_DESTROY:<br/>
<br/>
KillTimer(hWnd, TIMER);<br/>
CloseGL(hWnd, hDC, hRC);<br/>
<br/>
return 0;<br/>
<br/>
case WM_TIMER:<br/>
<br/>
OnTimer(hDC); //animate! <br/>
<br/>
return 0; <br/>
<br/>
}<br/>
<br/>
return DefScreenSaverProc(hWnd, message, wParam, lParam);<br/>
<br/>
}<br/>
<br/>
BOOL WINAPI ScreenSaverConfigureDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)<br/>
{<br/>
<br/>
// Dialog message handling<br/>
switch(message)<br/>
{<br/>
case WM_COMMAND:<br/>
switch(LOWORD(wParam))<br/>
{<br/>
case IDOK:<br/>
EndDialog(hDlg, LOWORD(wParam) == IDOK);<br/>
return true;<br/>
<br/>
case IDCANCEL:<br/>
EndDialog(hDlg, LOWORD(wParam) == IDOK);<br/>
return true;<br/>
}<br/>
}<br/>
<br/>
return false;<br/>
}<br/>
<br/>
<br/>
<br/>
// needed for SCRNSAVE.LIB<br/>
BOOL WINAPI RegisterDialogClasses(HANDLE hInst)<br/>
{<br/>
return TRUE;<br/>
}<br/>
<br/>
void transform(float i, float j, float *ix, float *iy)<br/>
{<br/>
float x, y, xnew, ynew;<br/>
float r, theta, rnew, thetanew;<br/>
<br/>
x = i / ((float)sphereDiameter / 2.0f) - 1.0f;<br/>
y = j / ((float)sphereDiameter / 2.0f) - 1.0f;<br/>
<br/>
r = sqrtf(x * x + y * y);<br/>
<br/>
theta = atan2f(y, x);<br/>
<br/>
rnew = asinf(r) / 1.5707963f;<br/>
<br/>
thetanew = theta;<br/>
xnew = rnew * cos(thetanew) * mag;<br/>
ynew = rnew * sin(thetanew) * mag;<br/>
<br/>
if(rnew <= r)<br/>
{<br/>
*ix = (xnew + 1.0f) / 2.0f;<br/>
*iy = (ynew + 1.0f) / 2.0f;<br/>
}<br/>
else<br/>
{<br/>
*ix = (x + 1.0f) / 2.0f;<br/>
*iy = (y + 1.0f) / 2.0f; <br/>
} <br/>
}<br/>
<br/>
// Initialize OpenGL<br/>
static void InitGL(HWND hWnd, HDC & hDC, HGLRC & hRC)<br/>
{<br/>
static PIXELFORMATDESCRIPTOR pfd = <br/>
{<br/>
sizeof(PIXELFORMATDESCRIPTOR),<br/>
1, // nVersion should be set to 1<br/>
PFD_DRAW_TO_WINDOW | // buffer can draw to window<br/>
PFD_SUPPORT_OPENGL | // buffer supports OpenGL drawing<br/>
PFD_DOUBLEBUFFER, // buffer is double buffered<br/>
PFD_TYPE_RGBA, // rgba pixels<br/>
32, // 32-bit color depth<br/>
0, 0, 0, 0, 0, 0, // look up rest at MSDN, color bits ignored<br/>
0, // no alpha buffer<br/>
0, // shift bit ignored<br/>
0, // no accumulation buffer<br/>
0, 0, 0, 0, // accumulation bits<br/>
16, // 16 bit z buffer<br/>
0, // no stencil buffer<br/>
0, // no auxiliary buffer<br/>
PFD_MAIN_PLANE, // main drawing layer<br/>
0, // reserved<br/>
0, 0, 0 // layer mask ignored<br/>
};<br/>
<br/>
if(!previewMode)<br/>
{<br/>
hDC = GetDC(hWnd);<br/>
<br/>
int pf = ChoosePixelFormat(hDC, &pfd); <br/>
SetPixelFormat(hDC, pf, &pfd);<br/>
<br/>
hRC = wglCreateContext(hDC);<br/>
wglMakeCurrent(hDC, hRC);<br/>
<br/>
pixelBuffer = new GLubyte[sizeof(GLubyte) * Width * Height * 4];<br/>
<br/>
glReadBuffer(GL_FRONT);<br/>
<span style="white-spacere <br/>
GLint ReadBuffer;<br/>
glGetIntegerv(GL_READ_BUFFER, &ReadBuffer);<br/>
glPixelStorei(GL_READ_BUFFER, GL_RGBA);<br/>
<span style="white-spacere <br/>
GLint PackAlignment;<br/>
glGetIntegerv(GL_PACK_ALIGNMENT, &PackAlignment); <br/>
glPixelStorei(GL_PACK_ALIGNMENT, 1);<br/>
<span style="white-spacere <br/>
glReadPixels(0, 0, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, pixelBuffer);<br/>
<br/>
thetex = (PIXELA *)malloc(sphereDiameter * sphereDiameter * sizeof(PIXELA));<br/>
}<br/>
else<br/>
{ <br/>
hDC = GetDC(hWnd);<br/>
<br/>
if(!hDC)<br/>
MessageBox(NULL, TEXT("Bad device context"), TEXT("Bad hDC"), MB_OK);<br/>
<br/>
int pf = ChoosePixelFormat(hDC, &pfd); <br/>
SetPixelFormat(hDC, pf, &pfd);<br/>
<br/>
hRC = wglCreateContext(hDC);<br/>
<br/>
if(!hRC)<br/>
MessageBox(NULL, TEXT("Bad rendering context"), TEXT("Bad hRC"), MB_OK);<br/>
<br/>
wglMakeCurrent(hDC, hRC);<br/>
<br/>
pixelBuffer = new GLubyte[sizeof(GLubyte) * Width * Height * 4];<br/>
<br/>
for(int i = 0;i < Width * Height * 4;i++)<br/>
pixelBuffer = (GLubyte)255; <br/>
}<br/>
<br/>
offsetX1 = new float*[sphereDiameter];<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
offsetX1 = new float[sphereDiameter];<br/>
<br/>
offsetX2 = new float*[sphereDiameter];<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
offsetX2 = new float[sphereDiameter];<br/>
<br/>
offsetX3 = new float*[sphereDiameter];<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
offsetX3 = new float[sphereDiameter];<br/>
<br/>
offsetX4 = new float*[sphereDiameter];<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
offsetX4 = new float[sphereDiameter];<br/>
<br/>
offsetY1 = new float*[sphereDiameter];<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
offsetY1 = new float[sphereDiameter];<br/>
<br/>
offsetY2 = new float*[sphereDiameter];<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
offsetY2 = new float[sphereDiameter];<br/>
<br/>
offsetY3 = new float*[sphereDiameter];<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
offsetY3 = new float[sphereDiameter];<br/>
<br/>
offsetY4 = new float*[sphereDiameter];<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
offsetY4 = new float[sphereDiameter]; <br/>
}<br/>
<br/>
// Shut down OpenGL<br/>
static void CloseGL(HWND hWnd, HDC hDC, HGLRC hRC)<br/>
{<br/>
wglMakeCurrent(NULL, NULL);<br/>
wglDeleteContext(hRC);<br/>
<br/>
ReleaseDC(hWnd, hDC);<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
delete offsetX1;<br/>
<br/>
delete offsetX1;<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
delete offsetX2;<br/>
<br/>
delete offsetX2;<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
delete offsetX3;<br/>
<br/>
delete offsetX3;<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
delete offsetX4;<br/>
<br/>
delete offsetX4;<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
delete offsetY1;<br/>
<br/>
delete offsetY1;<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
delete offsetY2;<br/>
<br/>
delete offsetY2;<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
delete offsetY3;<br/>
<br/>
delete offsetY3;<br/>
<br/>
for(int i = 0; i < sphereDiameter; i++)<br/>
delete offsetY4;<br/>
<br/>
delete offsetY4;<br/>
<br/>
if(!previewMode)<br/>
free(thetex);<br/>
<br/>
delete pixelBuffer;<br/>
}<br/>
<br/>
void SetupAnimation(int Width, int Height)<br/>
{ <br/>
//window resizing stuff<br/>
glViewport(0, 0, (GLsizei)Width, (GLsizei)Height);<br/>
<br/>
glMatrixMode(GL_PROJECTION);<br/>
glLoadIdentity();<br/>
<br/>
gluPerspective(45.0, (double)Width / (double)Height, 1.0, 200.0);<br/>
<br/>
glMatrixMode(GL_MODELVIEW);<br/>
<br/>
glLoadIdentity();<br/>
<br/>
//background<br/>
//glClearColor(0.0, 0.0, 0.0, 0.0); //0.0s is black<br/>
<br/>
glShadeModel(GL_SMOOTH); <br/>
<br/>
float i, j;<br/>
int p, q;<br/>
<br/>
for(i = 0.0f, p = 0;i < (float)sphereDiameter;i += 1.0f, p++)<br/>
{<br/>
for(j = 0.0f, q = 0;j < (float)sphereDiameter;j += 1.0f, q++)<br/>
{<br/>
transform(i, j, &offsetX1[p][q], &offsetY1[p][q]);<br/>
transform(i, j + 1.0f, &offsetX2[p][q], &offsetY2[p][q]);<br/>
transform(i + 1.0f, j, &offsetX3[p][q], &offsetY3[p][q]);<br/>
transform(i + 1.0f, j + 1.0f, &offsetX4[p][q], &offsetY4[p][q]);<br/>
} <br/>
}<br/>
}<br/>
<br/>
void OnTimer(HDC hDC) <br/>
{<br/>
float x, y, i = 0.0f, j = 0.0f;<br/>
int a, b, Y;<br/>
<br/>
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);<br/>
<br/>
glLoadIdentity();<br/>
<br/>
glDepthMask(GL_FALSE);<br/>
<br/>
glGenTextures(1, &textureId);<br/>
glBindTexture(GL_TEXTURE_2D, textureId);<br/>
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);<br/>
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);<br/>
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); <br/>
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);<br/>
<br/>
glTexImage2D(GL_TEXTURE_2D, 0, 4, Width, Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixelBuffer);<br/>
<br/>
glMatrixMode(GL_PROJECTION);<br/>
glLoadIdentity();<br/>
gluPerspective(180.0, (float)Width / (float)Height, 0.1, 10000.0);<br/>
glMatrixMode(GL_MODELVIEW);<br/>
glLoadIdentity();<br/>
<br/>
gluLookAt(70, 0, 0, 0, 0, 0, 0, 1, 0);<br/>
<br/>
glEnable(GL_TEXTURE_2D); <br/>
<br/>
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);<br/>
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);<br/>
<br/>
glMatrixMode(GL_PROJECTION);<br/>
glLoadIdentity();<br/>
glOrtho(-1 * Width / 2, Width / 2, -1 * Height / 2, Height / 2, 1.0, 10000.0);<br/>
glMatrixMode(GL_MODELVIEW);<br/>
glLoadIdentity();<br/>
gluLookAt(0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);<br/>
glNormal3f(0.0, 0.0, 1.0);<br/>
<br/>
glBegin(GL_TRIANGLE_STRIP);<br/>
<br/>
glTexCoord2f(0.0f, 0.0f);<br/>
glVertex3f(-1.0f * (float)Width / 2.0f, -1.0f * (float)Height / 2.0f, 0.0f);<br/>
glTexCoord2f(0.0f, 1.0f);<br/>
glVertex3f(-1.0f * (float)Width / 2.0f, (float)Height / 2.0f, 0.0f);<br/>
glTexCoord2f(1.0f, 0.0f);<br/>
glVertex3f((float)Width / 2.0f, -1.0f * (float)Height / 2.0f, 0.0f);<br/>
glTexCoord2f(1.0f, 1.0f);<br/>
glVertex3f((float)Width / 2.0f, (float)Height / 2.0f, 0.0f);<br/>
<br/>
glEnd();<br/>
<br/>
glDisable(GL_TEXTURE_2D);<br/>
<br/>
glDeleteTextures(1, &textureId);<br/>
<br/>
if(!previewMode)<br/>
{ <br/>
Y = ypos;<br/>
<br/>
b = (((Height / 2 + Y) * Width) * 4) + (((Width / 2) + xpos) * 4);<br/>
<br/>
for(a = 0;a < sphereDiameter * sphereDiameter;a++, b += 4)<br/>
{<br/>
if(a % 300 == 0 && a != 0)<br/>
{<br/>
Y++;<br/>
b = (((Height / 2 + Y) * Width) * 4) + (((Width / 2) + xpos) * 4);<br/>
} <br/>
<br/>
thetex[a].r = pixelBuffer;<br/>
thetex[a].g = pixelBuffer[b + 1];<br/>
thetex[a].b = pixelBuffer[b + 2];<br/>
thetex[a].a = pixelBuffer[b + 3]; <br/>
} <br/>
<br/>
glGenTextures(1, &tex);<br/>
glBindTexture(GL_TEXTURE_2D, tex);<br/>
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);<br/>
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);<br/>
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); <br/>
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);<br/>
glTexImage2D(GL_TEXTURE_2D, 0, 4, sphereDiameter, sphereDiameter, 0, GL_RGBA, GL_UNSIGNED_BYTE, thetex);<br/>
<br/>
glEnable(GL_TEXTURE_2D);<br/>
glBindTexture(GL_TEXTURE_2D, tex);<br/>
<br/>
glBegin(GL_TRIANGLE_STRIP);<br/>
<br/>
glTexCoord2f(offsetX1[(int)i][(int)j], offsetY1[(int)i][(int)j]);<br/>
glVertex3f((float)xpos, (float)ypos, 0.0f);<br/>
glTexCoord2f(offsetX2[(int)i][(int)j], offsetY2[(int)i][(int)j]);<br/>
glVertex3f(i + (float)xpos, (j + 1.0f) + (float)ypos, 0.0f);<br/>
glTexCoord2f(offsetX3[(int)i][(int)j], offsetY3[(int)i][(int)j]);<br/>
glVertex3f((i + 1.0f) + (float)xpos, j + (float)ypos, 0.0f);<br/>
glTexCoord2f(offsetX4[(int)i][(int)j], offsetY4[(int)i][(int)j]);<br/>
glVertex3f((i + 1.0f) + (float)xpos, (j + 1.0f) + (float)ypos, 0.0f);<br/>
<br/>
while(true)<br/>
{ <br/>
i += 1.0f;<br/>
<br/>
if(i == (float)sphereDiameter)<br/>
{ <br/>
glEnd();<br/>
<br/>
j += 1.0f;<br/>
<br/>
if(i == (float)sphereDiameter && j == (float)sphereDiameter)<br/>
break;<br/>
<br/>
i = 0.0f;<br/>
<br/>
glBegin(GL_TRIANGLE_STRIP);<br/>
<br/>
glTexCoord2f(offsetX1[(int)i][(int)j], offsetY1[(int)i][(int)j]);<br/>
glVertex3f((float)xpos, (float)ypos, 0.0f);<br/>
glTexCoord2f(offsetX2[(int)i][(int)j], offsetY2[(int)i][(int)j]);<br/>
glVertex3f(i + (float)xpos, (j + 1.0f) + (float)ypos, 0.0f);<br/>
glTexCoord2f(offsetX3[(int)i][(int)j], offsetY3[(int)i][(int)j]);<br/>
glVertex3f((i + 1.0f) + (float)xpos, j + (float)ypos, 0.0f);<br/>
glTexCoord2f(offsetX4[(int)i][(int)j], offsetY4[(int)i][(int)j]);<br/>
glVertex3f((i + 1.0f) + (float)xpos, (j + 1.0f) + (float)ypos, 0.0f);<br/>
<br/>
//i += 1.0f;<br/>
}<br/>
<br/>
glTexCoord2f(offsetX3[(int)i][(int)j], offsetY3[(int)i][(int)j]);<br/>
glVertex3f((i + 1.0f) + (float)xpos, j + (float)ypos, 0.0f);<br/>
glTexCoord2f(offsetX4[(int)i][(int)j], offsetY4[(int)i][(int)j]);<br/>
glVertex3f((i + 1.0f) + (float)xpos, (j + 1.0f) + (float)ypos, 0.0f); <br/>
}<br/>
<br/>
<br/>
glBegin(GL_QUADS);<br/>
transform(0.0f, 0.0f, &x, &y);<br/>
glTexCoord2f(x, y);<br/>
glVertex3f((float)xpos, (float)ypos, 0.0f);<br/>
transform(1.0f, 0.0f + 4.0f, &x, &y);<br/>
glTexCoord2f(x, y);<br/>
glVertex3f(1.0f + (float)xpos, (float)ypos, 0.0f);<br/>
transform(1.0f, (float)sphereDiameter, &x, &y);<br/>
glTexCoord2f(x, y);<br/>
glVertex3f(1.0f + (float)xpos, (float)sphereDiameter + (float)ypos, 0.0f);<br/>
transform(0.0f, (float)sphereDiameter, &x, &y);<br/>
glTexCoord2f(x, y);<br/>
glVertex3f((float)xpos, (float)sphereDiameter + (float)ypos, 0.0f);<br/>
glEnd();<br/>
<br/>
glDisable(GL_TEXTURE_2D); <br/>
}<br/>
<br/>
glFlush();<br/>
<br/>
hDC = wglGetCurrentDC();<br/>
<br/>
SwapBuffers(hDC);<br/>
<br/>
if(!previewMode) <br/>
glDeleteTextures(1, &tex);<br/>
<br/>
if(xpos >= Width / 2 - (sphereDiameter + step))<br/>
goRight = false;<br/>
else if(xpos <= -1 * Width / 2 + step)<br/>
goRight = true;<br/>
<br/>
if(ypos >= Height / 2 - (sphereDiameter + step))<br/>
goUp = false;<br/>
else if(ypos <= -1 * Height / 2 + step)<br/>
goUp = true;<br/>
<br/>
if(goRight)<br/>
xpos += step;<br/>
else<br/>
xpos -= step;<br/>
<br/>
if(goUp)<br/>
ypos += step;<br/>
else<br/>
ypos -= step;<br/>
}
Any ideas where I might be going wrong?
View the full article