Dark Bit Factory & Gravity
PROGRAMMING => C / C++ /C# => Topic started by: Rbz on May 23, 2006
-
Here's my entrie at Tunnel effect challenge, that I post here just to make it more easy to find for C/C++ coders.
Compile with Dev-cpp 4.9.9.2, should work on VC 6.0 too.
Enjoy!
//
// OpenGL Vector Tunnel by Rbraz - May 2006
//
// Original BB code by Shockwave
//
//
/**************************
* Includes
*
**************************/
#include <windows.h>
#include <math.h>
#include <gl/gl.h>
#include <gl/glu.h>
/**************************
* Function Declarations
*
**************************/
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);
void glInit();
void glDraw();
void Init_Structure();
void Draw_Structure();
// Global screen variables
int width = 640;
int height = 480;
//===================================================================================
// Define Box Storage;
//===================================================================================
float off=0.0f;
const int maxsegs=30;
float genadd;
float genadd2;
float scale=200.0f;
float celx [4][maxsegs];
float cely [4][maxsegs];
float celz[4][maxsegs];
/**************************
* WinMain
*
**************************/
int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int iCmdShow)
{
WNDCLASS wc;
HWND hWnd;
HDC hDC;
HGLRC hRC;
MSG msg;
BOOL bQuit = FALSE;
/* register window class */
wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "OGL Vector Tunnel by Rbraz 2006";
RegisterClass (&wc);
/* create main window */
hWnd = CreateWindow (
wc.lpszClassName, wc.lpszClassName,
WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
0, 0, width, height,
NULL, NULL, hInstance, NULL);
/* enable OpenGL for the window */
EnableOpenGL (hWnd, &hDC, &hRC);
//Initialyze OGL
glInit();
/* program main loop */
while (!bQuit)
{
/* check for messages */
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
{
/* handle or dispatch messages */
if (msg.message == WM_QUIT)
{
bQuit = TRUE;
}
else
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
else
{
/* OpenGL animation code goes here */
glDraw();
SwapBuffers (hDC);
Sleep (1);
}
}
/* shutdown OpenGL */
DisableOpenGL (hWnd, hDC, hRC);
/* destroy the window explicitly */
DestroyWindow (hWnd);
return msg.wParam;
}
/********************
* Window Procedure
*
********************/
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
return 0;
case WM_CLOSE:
PostQuitMessage (0);
return 0;
case WM_DESTROY:
return 0;
case WM_KEYDOWN:
switch (wParam)
{
case VK_ESCAPE:
PostQuitMessage(0);
return 0;
}
return 0;
default:
return DefWindowProc (hWnd, message, wParam, lParam);
}
}
/*******************
* Enable OpenGL
*
*******************/
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;
/* get the device context (DC) */
*hDC = GetDC (hWnd);
/* set the pixel format for the DC */
ZeroMemory (&pfd, sizeof (pfd));
pfd.nSize = sizeof (pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
iFormat = ChoosePixelFormat (*hDC, &pfd);
SetPixelFormat (*hDC, iFormat, &pfd);
/* create and enable the render context (RC) */
*hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC );
}
/******************
* Disable OpenGL
*
******************/
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
wglMakeCurrent (NULL, NULL);
wglDeleteContext (hRC);
ReleaseDC (hWnd, hDC);
}
void glInit()
{
// Start Of User Initialization
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
gluPerspective(45.0f, (float)width/(float)height, 0.1f, 100.0f); // Set Our Perspective
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
glEnable(GL_TEXTURE_2D); // Enable 2D Texture Mapping
Init_Structure();
}
void glDraw() // Draw The Scene
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The View
glPushMatrix();
glTranslatef(0.0f,0.0f,-40.0f);
genadd=genadd+0.01f;
genadd2=genadd2+0.02f;
Draw_Structure();
glPopMatrix();
}
//===================================================================================
// Define Boxes Structure;
//===================================================================================
void Init_Structure()
{
float zp=-((maxsegs+1)*2);
int a;
for (a=1; a <= maxsegs; a++)
{
celx[1][a]=-scale;
celx[2][a]=scale;
celx[3][a]=scale;
celx[4][a]=-scale;
cely[1][a]=-scale;
cely[2][a]=-scale;
cely[3][a]=scale;
cely[4][a]=scale;
celz[1][a]=zp;
celz[2][a]=zp;
celz[3][a]=zp;
celz[4][a]=zp;
zp=zp+2;
}
}
//===================================================================================
// Draw Boxes Structure;
//===================================================================================
void Draw_Structure()
{
GLfloat tx1,tx2,tx3,tx4;
GLfloat ty1,ty2,ty3,ty4;
GLfloat tx5,tx6,tx7,tx8;
GLfloat ty5,ty6,ty7,ty8;
GLfloat otx1,otx2,otx3,otx4;
GLfloat oty1,oty2,oty3,oty4;
GLfloat otx5,otx6,otx7,otx8;
GLfloat oty5,oty6,oty7,oty8;
GLfloat xs1,xs2;
GLfloat ys1,ys2;
int a=1;
GLfloat r,g,b;
glBegin(GL_QUADS);
for (a=1;a < maxsegs; a++)
{
xs1=sinf(genadd+(((float)a/10.0f)))*10.0f;
xs2=sinf(genadd+(((float)a+1)/10.0f))*10;
ys1=sinf(genadd2+(((float)a/10.0f)))*10;
ys2=sinf(genadd2+(((float)a+1)/10.0f))*10;
tx1= ((celx[1][a]/(celz[1][a]+off))+xs1);
tx2= ((celx[2][a]/(celz[2][a]+off))+xs1);
tx3= ((celx[3][a]/(celz[3][a]+off))+xs1);
tx4= ((celx[4][a]/(celz[4][a]+off))+xs1);
tx5= ((celx[1][a+1]/(celz[1][a+1]+off))+xs2);
tx6= ((celx[2][a+1]/(celz[2][a+1]+off))+xs2);
tx7= ((celx[3][a+1]/(celz[3][a+1]+off))+xs2);
tx8= ((celx[4][a+1]/(celz[4][a+1]+off))+xs2);
ty1= ((cely[1][a]/(celz[1][a]+off))+ys1);
ty2= ((cely[2][a]/(celz[2][a]+off))+ys1);
ty3= ((cely[3][a]/(celz[3][a]+off))+ys1);
ty4= ((cely[4][a]/(celz[4][a]+off))+ys1);
ty5= ((cely[1][a+1]/(celz[1][a+1]+off))+ys2);
ty6= ((cely[2][a+1]/(celz[2][a+1]+off))+ys2);
ty7= ((cely[3][a+1]/(celz[3][a+1]+off))+ys2);
ty8= ((cely[4][a+1]/(celz[4][a+1]+off))+ys2);
r= (float)a * 5 / 255.0f;
g= (float)a * 6 / 255.0f;
b= (float)a * 8 / 255.0f;
glColor3f(r,g,b);
glTexCoord2f(0.0f, 0.0f); glVertex2f (tx1,ty1);
glTexCoord2f(1.0f, 0.0f); glVertex2f (tx2,ty2);
glTexCoord2f(1.0f, 1.0f); glVertex2f (otx2,oty2);
glTexCoord2f(0.0f, 1.0f); glVertex2f (otx1,oty1);
glTexCoord2f(0.0f, 0.0f); glVertex2f (tx3,ty3);
glTexCoord2f(1.0f, 0.0f); glVertex2f (tx4,ty4);
glTexCoord2f(1.0f, 1.0f); glVertex2f (otx4,oty4);
glTexCoord2f(0.0f, 1.0f); glVertex2f (otx3,oty3);
glTexCoord2f(0.0f, 0.0f); glVertex2f (tx1,ty1);
glTexCoord2f(1.0f, 0.0f); glVertex2f (tx4,ty4);
glTexCoord2f(1.0f, 1.0f); glVertex2f (otx4,oty4);
glTexCoord2f(0.0f, 1.0f); glVertex2f (otx1,oty1);
glTexCoord2f(0.0f, 0.0f); glVertex2f (tx2,ty2);
glTexCoord2f(1.0f, 0.0f); glVertex2f (tx3,ty3);
glTexCoord2f(1.0f, 1.0f); glVertex2f (otx3,oty3);
glTexCoord2f(0.0f, 1.0f); glVertex2f (otx2,oty2);
// grab the old values
otx1 = tx1; otx2 = tx2; otx3 = tx3; otx4 = tx4;
oty1 = ty1; oty2 = ty2; oty3 = ty3; oty4 = ty4;
otx5 = tx5; otx6 = tx6; otx7 = tx7; otx8 = tx7;
oty5 = ty5; oty6 = ty6; oty7 = ty7; oty8 = ty7;
}
glEnd();
off=off+0.15f;
if (off > 2.0f)
{
off=off-2.0f;
genadd=genadd-0.1f;
genadd2=genadd2-0.1f;
}
}
-
Cool snippet dude! Nice one.
Cheers and all the best,
Clyde.
-
You know I love this Rbraz. Thanks for converting it and making it look better! :)
-
I tried compiling under DevCPP 2.9.9.2 on XP SP2 but got this :( Any ideas?
Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\Makefile.win"
Executing make...
make.exe -f "C:\Dev-Cpp\Makefile.win" all
g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"
g++.exe main.o -o "Project5.exe" -L"C:/Dev-Cpp/lib" -mwindows
main.o(.text+0x328):main.cpp: undefined reference to `wglCreateContext@4'
main.o(.text+0x348):main.cpp: undefined reference to `wglMakeCurrent@8'
main.o(.text+0x368):main.cpp: undefined reference to `wglMakeCurrent@8'
main.o(.text+0x376):main.cpp: undefined reference to `wglDeleteContext@4'
main.o(.text+0x3a2):main.cpp: undefined reference to `glMatrixMode@4'
main.o(.text+0x3aa):main.cpp: undefined reference to `glLoadIdentity@0'
main.o(.text+0x3de):main.cpp: undefined reference to `gluPerspective@32'
main.o(.text+0x3ed):main.cpp: undefined reference to `glMatrixMode@4'
main.o(.text+0x3f5):main.cpp: undefined reference to `glLoadIdentity@0'
main.o(.text+0x401):main.cpp: undefined reference to `glShadeModel@4'
main.o(.text+0x42c):main.cpp: undefined reference to `glClearColor@16'
main.o(.text+0x439):main.cpp: undefined reference to `glClearDepth@8'
main.o(.text+0x448):main.cpp: undefined reference to `glEnable@4'
main.o(.text+0x457):main.cpp: undefined reference to `glDepthFunc@4'
main.o(.text+0x46e):main.cpp: undefined reference to `glHint@8'
main.o(.text+0x47d):main.cpp: undefined reference to `glEnable@4'
main.o(.text+0x49a):main.cpp: undefined reference to `glClear@4'
main.o(.text+0x4a2):main.cpp: undefined reference to `glLoadIdentity@0'
main.o(.text+0x4a7):main.cpp: undefined reference to `glPushMatrix@0'
main.o(.text+0x4c6):main.cpp: undefined reference to `glTranslatef@12'
main.o(.text+0x4fb):main.cpp: undefined reference to `glPopMatrix@0'
main.o(.text+0x617):main.cpp: undefined reference to `glBegin@4'
main.o(.text+0xa07):main.cpp: undefined reference to `glColor3f@12'
main.o(.text+0xa20):main.cpp: undefined reference to `glTexCoord2f@8'
main.o(.text+0xa35):main.cpp: undefined reference to `glVertex2f@8'
main.o(.text+0xa4e):main.cpp: undefined reference to `glTexCoord2f@8'
main.o(.text+0xa63):main.cpp: undefined reference to `glVertex2f@8'
main.o(.text+0xa7c):main.cpp: undefined reference to `glTexCoord2f@8'
main.o(.text+0xa91):main.cpp: undefined reference to `glVertex2f@8'
main.o(.text+0xaaa):main.cpp: undefined reference to `glTexCoord2f@8'
main.o(.text+0xabf):main.cpp: undefined reference to `glVertex2f@8'
main.o(.text+0xad8):main.cpp: undefined reference to `glTexCoord2f@8'
main.o(.text+0xaed):main.cpp: undefined reference to `glVertex2f@8'
main.o(.text+0xb06):main.cpp: undefined reference to `glTexCoord2f@8'
main.o(.text+0xb1b):main.cpp: undefined reference to `glVertex2f@8'
main.o(.text+0xb34):main.cpp: undefined reference to `glTexCoord2f@8'
main.o(.text+0xb49):main.cpp: undefined reference to `glVertex2f@8'
main.o(.text+0xb62):main.cpp: undefined reference to `glTexCoord2f@8'
main.o(.text+0xb77):main.cpp: undefined reference to `glVertex2f@8'
main.o(.text+0xb90):main.cpp: undefined reference to `glTexCoord2f@8'
main.o(.text+0xba5):main.cpp: undefined reference to `glVertex2f@8'
main.o(.text+0xbbe):main.cpp: undefined reference to `glTexCoord2f@8'
main.o(.text+0xbd3):main.cpp: undefined reference to `glVertex2f@8'
main.o(.text+0xbec):main.cpp: undefined reference to `glTexCoord2f@8'
main.o(.text+0xc01):main.cpp: undefined reference to `glVertex2f@8'
main.o(.text+0xc1a):main.cpp: undefined reference to `glTexCoord2f@8'
main.o(.text+0xc2f):main.cpp: undefined reference to `glVertex2f@8'
main.o(.text+0xc48):main.cpp: undefined reference to `glTexCoord2f@8'
main.o(.text+0xc5d):main.cpp: undefined reference to `glVertex2f@8'
main.o(.text+0xc76):main.cpp: undefined reference to `glTexCoord2f@8'
main.o(.text+0xc8b):main.cpp: undefined reference to `glVertex2f@8'
main.o(.text+0xca4):main.cpp: undefined reference to `glTexCoord2f@8'
main.o(.text+0xcb9):main.cpp: undefined reference to `glVertex2f@8'
main.o(.text+0xcd2):main.cpp: undefined reference to `glTexCoord2f@8'
main.o(.text+0xce7):main.cpp: undefined reference to `glVertex2f@8'
main.o(.text+0xd5c):main.cpp: undefined reference to `glEnd@0'
collect2: ld returned 1 exit status
make.exe: *** [Project5.exe] Error 1
Execution terminated
-
Make a project and add the libs(opengl32/glu32) in the parameters tab.
-
Make a project and add the libs(opengl32/glu32) in the parameters tab.
Yeah this will work, or try the attach below.
-
Damn wanna download but I dont have enough credits.... ???
-
Ah, you should be able to download the file taj.
I think you have a corrupt cookie. First log out of your account, then clear all your cookies and then log back in. You can then download the file.
I disabled downloads for guests to stop ppl remote linking and using this place as a filestore so if you try and download while you are not logged into your account it can still be fucked up when you log in. Sorry. But if you log out, clear cookies and then log back in you will be able to get attachments.
-
Sorry no. I tried what you said and no I still cant. Tried 4 times. :-[
-
Mmm it downloads here. Do you have any trouble downloading other attachments here?
-
Works fine now... I have no idea :-(
-
Nice tunnel Rbraz, it's a cool effect and a nice learning resource for the OGL beginners around here (me :)). I'm going through a book right now and I'm just about to dive into texturing. :)
-
<-- Mm. that's probably because the corrupt cookie has expired now. Just make sure that you are logged into your account before you try to download attachments and it won't happen again :)
-
Nice tunnel Rbraz, it's a cool effect and a nice learning resource for the OGL beginners around here (me :)). I'm going through a book right now and I'm just about to dive into texturing. :)
No problem dude, there's always someone here that can help you with OGL stuff, so, you're in the right place ;)
-
@Rbraz:
Nice conversion ... thx for sharing !
@all:
If you want to run this source with Visual C++ 2005 Express (which I really start to like), you may get
a Run-Time Check Failure #3 because of the use of uninitialized variables. This is a common check to
generally avoid possible buffer overflows in your code. You can simply turn that check off by changing
the Basic Runtime Checks-Flag in your project-properties. Change the /RTC1 to /RTCs and
it works ;)
-
Or fix the real bug ;)
JIm
-
I got "undeclared variable" and "identifier not found" errors when trying to combine this program to transition sourcecode...in the attachment I have commented out the "new" tunnel code so it can be seen more easy. I hope somebody can direct me to the right path on this (how to combine these properly), cos I really need this cool tunnel to be in this transition source code :) This transition sourcecode is little bit tweaked too, so don't wonder if it's not exactly the same as in example. It has mp3 playback for example and then scaling.
-Kypho the beginner
-
That's a bit too much of a mess to deal with, you'll need to understand more C and OpenGL before I can really help you. Sorry :(
Jim