• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By tj8146
      I am using immediate mode for OpenGL and I am creating a 2D top down car game. I am trying to configure my game loop in order to get my car-like physics working on a square shape. I have working code but it is not doing as I want it to. I am not sure as to whether it is my game loop that is incorrect or my code for the square is incorrect, or maybe both! Could someone help because I have been trying to work this out for over a day now
      I have attached my .cpp file if you wish to run it for yourself.. 
      WinMain code:
      /******************* WIN32 FUNCTIONS ***************************/ int WINAPI WinMain( HINSTANCE hInstance, // Instance HINSTANCE hPrevInstance, // Previous Instance LPSTR lpCmdLine, // Command Line Parameters int nCmdShow) // Window Show State { MSG msg; // Windows Message Structure bool done=false; // Bool Variable To Exit Loop Car car; car.x = 220; car.y = 140; car.dx = 0; car.dy = 0; car.ang = 0; AllocConsole(); FILE *stream; freopen_s(&stream, "CONOUT$", "w", stdout); // Create Our OpenGL Window if (!CreateGLWindow("OpenGL Win32 Example",screenWidth,screenHeight)) { return 0; // Quit If Window Was Not Created } while(!done) // Loop That Runs While done=FALSE { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting? { if (msg.message==WM_QUIT) // Have We Received A Quit Message? { done=true; // If So done=TRUE break; } else // If Not, Deal With Window Messages { TranslateMessage(&msg); // Translate The Message DispatchMessage(&msg); // Dispatch The Message } } else // If There Are No Messages { if(keys[VK_ESCAPE]) done = true; void processKeys(Car& car); //process keyboard while (game_is_running) { loops = 0; while (GetTickCount() > next_game_tick && loops < MAX_FRAMESKIP) { update(car); // update variables next_game_tick += SKIP_TICKS; loops++; } display(car); // Draw The Scene SwapBuffers(hDC); // Swap Buffers (Double Buffering) } } } // Shutdown KillGLWindow(); // Kill The Window return (int)(msg.wParam); // Exit The Program } //WIN32 Processes function - useful for responding to user inputs or other events. LRESULT CALLBACK WndProc( HWND hWnd, // Handle For This Window UINT uMsg, // Message For This Window WPARAM wParam, // Additional Message Information LPARAM lParam) // Additional Message Information { switch (uMsg) // Check For Windows Messages { case WM_CLOSE: // Did We Receive A Close Message? { PostQuitMessage(0); // Send A Quit Message return 0; // Jump Back } break; case WM_SIZE: // Resize The OpenGL Window { reshape(LOWORD(lParam),HIWORD(lParam)); // LoWord=Width, HiWord=Height return 0; // Jump Back } break; case WM_LBUTTONDOWN: { mouse_x = LOWORD(lParam); mouse_y = screenHeight - HIWORD(lParam); LeftPressed = true; } break; case WM_LBUTTONUP: { LeftPressed = false; } break; case WM_MOUSEMOVE: { mouse_x = LOWORD(lParam); mouse_y = screenHeight - HIWORD(lParam); } break; case WM_KEYDOWN: // Is A Key Being Held Down? { keys[wParam] = true; // If So, Mark It As TRUE return 0; // Jump Back } break; case WM_KEYUP: // Has A Key Been Released? { keys[wParam] = false; // If So, Mark It As FALSE return 0; // Jump Back } break; } // Pass All Unhandled Messages To DefWindowProc return DefWindowProc(hWnd,uMsg,wParam,lParam); }  
      C++ and OpenGL code:
      int mouse_x=0, mouse_y=0; bool LeftPressed = false; int screenWidth=1080, screenHeight=960; bool keys[256]; float radiansFromDegrees(float deg) { return deg * (M_PI / 180.0f); } float degreesFromRadians(float rad) { return rad / (M_PI / 180.0f); } bool game_is_running = true; const int TICKS_PER_SECOND = 50; const int SKIP_TICKS = 1000 / TICKS_PER_SECOND; const int MAX_FRAMESKIP = 10; DWORD next_game_tick = GetTickCount(); int loops; typedef struct { float x, y; float dx, dy; float ang; }Car; //OPENGL FUNCTION PROTOTYPES void display(const Car& car); //called in winmain to draw everything to the screen void reshape(int width, int height); //called when the window is resized void init(); //called in winmain when the program starts. void processKeys(Car& car); //called in winmain to process keyboard input void update(Car& car); //called in winmain to update variables /************* START OF OPENGL FUNCTIONS ****************/ void display(const Car& car) { const float w = 50.0f; const float h = 50.0f; glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); glTranslatef(100, 100, 0); glBegin(GL_POLYGON); glVertex2f(car.x, car.y); glVertex2f(car.x + w, car.y); glVertex2f(car.x + w, car.y + h); glVertex2f(car.x, car.y + h); glEnd(); glFlush(); } void reshape(int width, int height) // Resize the OpenGL window { screenWidth = width; screenHeight = height; // to ensure the mouse coordinates match // we will use these values to set the coordinate system glViewport(0, 0, width, height); // Reset the current viewport glMatrixMode(GL_PROJECTION); // select the projection matrix stack glLoadIdentity(); // reset the top of the projection matrix to an identity matrix gluOrtho2D(0, screenWidth, 0, screenHeight); // set the coordinate system for the window glMatrixMode(GL_MODELVIEW); // Select the modelview matrix stack glLoadIdentity(); // Reset the top of the modelview matrix to an identity matrix } void init() { glClearColor(1.0, 1.0, 0.0, 0.0); //sets the clear colour to yellow //glClear(GL_COLOR_BUFFER_BIT) in the display function //will clear the buffer to this colour. } void processKeys(Car& car) { if (keys[VK_UP]) { float cdx = sinf(radiansFromDegrees(car.ang)); float cdy = -cosf(radiansFromDegrees(car.ang)); car.dx += cdx; car.dy += cdy; } if (keys[VK_DOWN]) { float cdx = sinf(radiansFromDegrees(car.ang)); float cdy = -cosf(radiansFromDegrees(car.ang)); car.dx += -cdx; car.dy += -cdy; } if (keys[VK_LEFT]) { car.ang -= 2; } if (keys[VK_RIGHT]) { car.ang += 2; } } void update(Car& car) { car.x += car.dx*next_game_tick; }  
      game.cpp
    • By tj8146
      I am using immediate mode for OpenGL and I am creating a 2D top down car game. I am trying to configure my game loop in order to get my car-like physics working on a square shape. I have working code but it is not doing as I want it to. I am not sure as to whether it is my game loop that is incorrect or my code for the square is incorrect, or maybe both! Could someone help because I have been trying to work this out for over a day now
      I have attached my .cpp file if you wish to run it for yourself.. 
       
      This is my C++ and OpenGL code:
      int mouse_x=0, mouse_y=0; bool LeftPressed = false; int screenWidth=1080, screenHeight=960; bool keys[256]; float radiansFromDegrees(float deg) { return deg * (M_PI / 180.0f); } float degreesFromRadians(float rad) { return rad / (M_PI / 180.0f); } bool game_is_running = true; const int TICKS_PER_SECOND = 50; const int SKIP_TICKS = 1000 / TICKS_PER_SECOND; const int MAX_FRAMESKIP = 10; DWORD next_game_tick = GetTickCount(); int loops; typedef struct { float x, y; float dx, dy; float ang; }Car; //OPENGL FUNCTION PROTOTYPES void display(const Car& car); //called in winmain to draw everything to the screen void reshape(int width, int height); //called when the window is resized void init(); //called in winmain when the program starts. void processKeys(Car& car); //called in winmain to process keyboard input void update(Car& car); //called in winmain to update variables /************* START OF OPENGL FUNCTIONS ****************/ void display(const Car& car) { const float w = 50.0f; const float h = 50.0f; glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); glTranslatef(100, 100, 0); glBegin(GL_POLYGON); glVertex2f(car.x, car.y); glVertex2f(car.x + w, car.y); glVertex2f(car.x + w, car.y + h); glVertex2f(car.x, car.y + h); glEnd(); glFlush(); } void reshape(int width, int height) // Resize the OpenGL window { screenWidth = width; screenHeight = height; // to ensure the mouse coordinates match // we will use these values to set the coordinate system glViewport(0, 0, width, height); // Reset the current viewport glMatrixMode(GL_PROJECTION); // select the projection matrix stack glLoadIdentity(); // reset the top of the projection matrix to an identity matrix gluOrtho2D(0, screenWidth, 0, screenHeight); // set the coordinate system for the window glMatrixMode(GL_MODELVIEW); // Select the modelview matrix stack glLoadIdentity(); // Reset the top of the modelview matrix to an identity matrix } void init() { glClearColor(1.0, 1.0, 0.0, 0.0); //sets the clear colour to yellow //glClear(GL_COLOR_BUFFER_BIT) in the display function //will clear the buffer to this colour. } void processKeys(Car& car) { if (keys[VK_UP]) { float cdx = sinf(radiansFromDegrees(car.ang)); float cdy = -cosf(radiansFromDegrees(car.ang)); car.dx += cdx; car.dy += cdy; } if (keys[VK_DOWN]) { float cdx = sinf(radiansFromDegrees(car.ang)); float cdy = -cosf(radiansFromDegrees(car.ang)); car.dx += -cdx; car.dy += -cdy; } if (keys[VK_LEFT]) { car.ang -= 2; } if (keys[VK_RIGHT]) { car.ang += 2; } } void update(Car& car) { car.x += car.dx*next_game_tick; } My WinMain code:
      /******************* WIN32 FUNCTIONS ***************************/ int WINAPI WinMain( HINSTANCE hInstance, // Instance HINSTANCE hPrevInstance, // Previous Instance LPSTR lpCmdLine, // Command Line Parameters int nCmdShow) // Window Show State { MSG msg; // Windows Message Structure bool done=false; // Bool Variable To Exit Loop Car car; car.x = 220; car.y = 140; car.dx = 0; car.dy = 0; car.ang = 0; AllocConsole(); FILE *stream; freopen_s(&stream, "CONOUT$", "w", stdout); // Create Our OpenGL Window if (!CreateGLWindow("OpenGL Win32 Example",screenWidth,screenHeight)) { return 0; // Quit If Window Was Not Created } while(!done) // Loop That Runs While done=FALSE { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting? { if (msg.message==WM_QUIT) // Have We Received A Quit Message? { done=true; // If So done=TRUE break; } else // If Not, Deal With Window Messages { TranslateMessage(&msg); // Translate The Message DispatchMessage(&msg); // Dispatch The Message } } else // If There Are No Messages { if(keys[VK_ESCAPE]) done = true; void processKeys(Car& car); //process keyboard while (game_is_running) { loops = 0; while (GetTickCount() > next_game_tick && loops < MAX_FRAMESKIP) { update(car); // update variables next_game_tick += SKIP_TICKS; loops++; } display(car); // Draw The Scene SwapBuffers(hDC); // Swap Buffers (Double Buffering) } } } // Shutdown KillGLWindow(); // Kill The Window return (int)(msg.wParam); // Exit The Program } //WIN32 Processes function - useful for responding to user inputs or other events. LRESULT CALLBACK WndProc( HWND hWnd, // Handle For This Window UINT uMsg, // Message For This Window WPARAM wParam, // Additional Message Information LPARAM lParam) // Additional Message Information { switch (uMsg) // Check For Windows Messages { case WM_CLOSE: // Did We Receive A Close Message? { PostQuitMessage(0); // Send A Quit Message return 0; // Jump Back } break; case WM_SIZE: // Resize The OpenGL Window { reshape(LOWORD(lParam),HIWORD(lParam)); // LoWord=Width, HiWord=Height return 0; // Jump Back } break; case WM_LBUTTONDOWN: { mouse_x = LOWORD(lParam); mouse_y = screenHeight - HIWORD(lParam); LeftPressed = true; } break; case WM_LBUTTONUP: { LeftPressed = false; } break; case WM_MOUSEMOVE: { mouse_x = LOWORD(lParam); mouse_y = screenHeight - HIWORD(lParam); } break; case WM_KEYDOWN: // Is A Key Being Held Down? { keys[wParam] = true; // If So, Mark It As TRUE return 0; // Jump Back } break; case WM_KEYUP: // Has A Key Been Released? { keys[wParam] = false; // If So, Mark It As FALSE return 0; // Jump Back } break; } // Pass All Unhandled Messages To DefWindowProc return DefWindowProc(hWnd,uMsg,wParam,lParam); }  
      game.cpp
    • By lxjk
      Hi guys,
      There are many ways to do light culling in tile-based shading. I've been playing with this idea for a while, and just want to throw it out there.
      Because tile frustums are general small compared to light radius, I tried using cone test to reduce false positives introduced by commonly used sphere-frustum test.
      On top of that, I use distance to camera rather than depth for near/far test (aka. sliced by spheres).
      This method can be naturally extended to clustered light culling as well.
      The following image shows the general ideas

       
      Performance-wise I get around 15% improvement over sphere-frustum test. You can also see how a single light performs as the following: from left to right (1) standard rendering of a point light; then tiles passed the test of (2) sphere-frustum test; (3) cone test; (4) spherical-sliced cone test
       

       
      I put the details in my blog post (https://lxjk.github.io/2018/03/25/Improve-Tile-based-Light-Culling-with-Spherical-sliced-Cone.html), GLSL source code included!
       
      Eric
    • By Fadey Duh
      Good evening everyone!

      I was wondering if there is something equivalent of  GL_NV_blend_equation_advanced for AMD?
      Basically I'm trying to find more compatible version of it.

      Thank you!
    • By Jens Eckervogt
      Hello guys, 
       
      Please tell me! 
      How do I know? Why does wavefront not show for me?
      I already checked I have non errors yet.
      using OpenTK; using System.Collections.Generic; using System.IO; using System.Text; namespace Tutorial_08.net.sourceskyboxer { public class WaveFrontLoader { private static List<Vector3> inPositions; private static List<Vector2> inTexcoords; private static List<Vector3> inNormals; private static List<float> positions; private static List<float> texcoords; private static List<int> indices; public static RawModel LoadObjModel(string filename, Loader loader) { inPositions = new List<Vector3>(); inTexcoords = new List<Vector2>(); inNormals = new List<Vector3>(); positions = new List<float>(); texcoords = new List<float>(); indices = new List<int>(); int nextIdx = 0; using (var reader = new StreamReader(File.Open("Contents/" + filename + ".obj", FileMode.Open), Encoding.UTF8)) { string line = reader.ReadLine(); int i = reader.Read(); while (true) { string[] currentLine = line.Split(); if (currentLine[0] == "v") { Vector3 pos = new Vector3(float.Parse(currentLine[1]), float.Parse(currentLine[2]), float.Parse(currentLine[3])); inPositions.Add(pos); if (currentLine[1] == "t") { Vector2 tex = new Vector2(float.Parse(currentLine[1]), float.Parse(currentLine[2])); inTexcoords.Add(tex); } if (currentLine[1] == "n") { Vector3 nom = new Vector3(float.Parse(currentLine[1]), float.Parse(currentLine[2]), float.Parse(currentLine[3])); inNormals.Add(nom); } } if (currentLine[0] == "f") { Vector3 pos = inPositions[0]; positions.Add(pos.X); positions.Add(pos.Y); positions.Add(pos.Z); Vector2 tc = inTexcoords[0]; texcoords.Add(tc.X); texcoords.Add(tc.Y); indices.Add(nextIdx); ++nextIdx; } reader.Close(); return loader.loadToVAO(positions.ToArray(), texcoords.ToArray(), indices.ToArray()); } } } } } And It have tried other method but it can't show for me.  I am mad now. Because any OpenTK developers won't help me.
      Please help me how do I fix.

      And my download (mega.nz) should it is original but I tried no success...
      - Add blend source and png file here I have tried tried,.....  
       
      PS: Why is our community not active? I wait very longer. Stop to lie me!
      Thanks !
  • Advertisement
  • Advertisement
Sign in to follow this  

OpenGL Inspired By The Demoscene: Beginner Starting His Journey In Graphics Programming.. Seeking Advice

This topic is 619 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone!

 

So ever since I watched a few youtube videos of demos created by people from the demoscene, I have been fascinated by Graphics Programming. The way it allows us to express our creativity, artistic vision, and at the same time, showcase our programming prowess is something that have made a big impression on me.

 

 However, knowing how ardous this road is, I was hoping if anyone who have taken the plunge into Graphics Programming could share some advice, or point me in the right direction ?

 

I have some programming experience (namely Python) though I wouldn't call myself a very 'experienced' programmer. In addition, I have done majority of the college level math classes in the past: calculus, linear algebra, discrete mathematics- though I'll have to admit, that's largely forgotten by now ^_^

 

From my understanding, I would need to:

 

1. Learn C++:

I will be picking up a book or two on the subject soon, but if anyone have any good suggestions of a book on C++ that they think would be perfect for a beginner please let me know!

 

I know that C++ is one of the more complex languages when it comes to programming and that one never stops learning when it comes to C++ because of the plethora of libraries it has, but for Graphics Programming, what are some of the key programming concepts or area of focus I should pay more attention to while learning C++? And just how much fluency I would need to acquire before I should even attempt to learn about Graphics Programming? (once I have the fundamentals down? once I have reached an intermediate level?)

 

2. Brush up on Linear Algebra and more specifically, have a solid understanding of the use and applications of Vectors and Matrices?    (these seems to be the most important mathematical concepts when it comes to Graphics Programming right? as they are related to transformation and animation.

Are there anything else beside these two? Quaternions? Trigonometry?

   

3. Choose between DirectX or OpenGL

Personally I would probably pick OpenGL as it's crossplatform, so what comes next is understanding the entire openGL pipeline, its libraries, etc? glsl, shaders?

 

 

With so many things yet to learned, and how much depth Graphics Programming actually covers, it truly feels a little daunting.

If you were to advice a novice on how he should get started, what would be the step by step approach you would suggest? What would you recommend him to learn first, second, third.. etc. What learning materials would you recommend?

Share this post


Link to post
Share on other sites
Advertisement

Welcome to the wonderful world of graphics programming! I'll try to answer some of those questions for you.

 

1. Learn C++:

Any language which has access to a well-documented graphics library will do, so you don't necessarily have to go for C++. Honestly, if this is your first experience with programming I'd actually recommend going for something like C#. There's plenty of awesome libraries or wrappers available to do graphics related programming and it'll allow you to get up and running more quickly. It's going to be a while before you can get any real benefit out of working in C++ anyway, so it might be a smaller hurdle to get over by sticking with a more "approachable" language. I know a lot of people have different opinions on this though, so I'll let others add their opinions to this as well.

 

2. Brush up on Linear Algebra and more specifically, have a solid understanding of the use and applications of Vectors and Matrices?

Absolutely! Understanding linear algebra and trigonometry is going to help you out a lot if you want to make any progress. Once you advance a bit further it can become useful to have a good grasp of some elementary concepts in calculus, like differentials and integrals. Once you're at the point where you need those you should have an understanding of the following mathematical concepts you're going to need to advance. It's best not to worry about very complex things for now, just basic linear algebra can already get you far.

 

3. Choose between DirectX or OpenGL Personally I would probably pick OpenGL as it's crossplatform, so what comes next is understanding the entire openGL pipeline, its libraries, etc? glsl, shaders?

The fact that the OpenGL standard is supported on multiple platforms does not mean that it will work out of the box across multiple platforms or even across multiple graphics hardware vendors. OpenGL is notorious for being very inconsistent in these cases. If you're starting out I wouldn't focus on getting things to work on multiple platforms just yet. Start off with one and see how far you get. Worry about the mess that is cross-platform graphics development later.

 

If you want to focus on actually implementing graphical techniques (i.e. you actually want to write code and shaders to get thing to show up on screen asap) I'd actually recommend going with something a little higher level than plain DirectX or OpenGL. I'm a bit out of the loop on publicly available graphics engines, but there's plenty of them out there. I know libraries like Ogre used to be very popular, but I haven't had a look at any of those libraries in years. Even working in Unreal and Unity for experimenting with shaders and such can be a great introduction without too much stuff getting in your way.

I'd recommend looking at OpenGL and DirectX once you understand the needs and requirements of graphical applications a bit better, or if you're just really passionate about having a look at more architectural stuff.

 

The most important thing is to experiment a lot and to not worry about not getting things right immediately. Don't be intimidated by the complex looking math and such. Find sample implementations for things, play around with them, try to understand them and try to connect them mentally to their mathematical description.

 

Good luck!

Share this post


Link to post
Share on other sites

If you are focused on graphics programming, then I would suggest you try a different approach first.

 

It will take you a long time to learn enough to have a graphics framework you can experiment in if you write everything yourself.

 

I would suggest you start by looking at online sites that let you play without having to re-invent the wheel.

 

For example spend some time looking at shadertoy. Look at some of the simpler shaders until you think you understand them. Then change them and see if you are right. You can change the code in the browser and instantly see the effect your changes have had.

 

Once you are comfortable doing that, then you can start looking at what to do next.

Share this post


Link to post
Share on other sites
The most beautyful part of demoscene is, in my opinion, the old demos for ZX Spectrum , C64, and MS-DOS. That old taste is the most genuine for the Spirit of what demoscene really means. With that perspective, it is not a bad idea to use old versions of Allegro libraries (DOS) or SDL, though those are for C. For C++ we have SFML in any version. Of course may be possible to be even more rewarding to do all yourself or with little help from high level libraries, but I reccomend to be fluent in 2D before try 3D. I read very good critics about Pygame, the all famous lib for Python

Share this post


Link to post
Share on other sites

Are you trying to get into the demo scene or real-time video game graphics or offline rendering?

 

Trying to get into the demo scene, what they do is real time rendering via procedural generated graphics i believe.

Share this post


Link to post
Share on other sites

shadertoy looks really awesome. I will definetly play around with that first !

 

In regards to learning a new language, I was planning on learning a new language anyway so I really don't mind spending the extra time.

 

In your opinion, would it be better to start out with C or C++ ? I know C++ and C# are heavily used for game development, but for Demoscene, and Graphics Programming where one actually makes all his stuff, and where efficiency, size and speed are also more of a concern, then C or a Cish C++ is often used right?

 

When looking around online, some people have said that it's better to start out with C as C is a lot smaller in comparison to C++ and is stricter, which makes C a little easier to learn. But, on the other hand, there's also the opinion that starting with C sometimes makes people fall into bad habbits when they do try to write code in C++ as they often fall back to writing in a more C style code. is this true?

 

Lastly, does anyone know of any other Graphics Programming communities or forums? (other than this one of course.)

Edited by glober

Share this post


Link to post
Share on other sites

When looking around online, some people have said that it's better to start out with C as C is a lot smaller in comparison to C++ and is stricter, which makes C a little easier to learn. But, on the other hand, there's also the opinion that starting with C sometimes makes people fall into bad habbits when they do try to write code in C++ as they often fall back to writing in a more C style code. is this true?

 

Being C++ an extension of C, at the core there's really not much difference between both of them... C++ just adds new stuff to the language, abstracts some others and fixes a few little things here and there, to the point that you could almost compile C code on a C++ compiler without problems... so, by learning C you're already learning the basics of C++...

 

Also C makes you think more at the metal level, you could easily figure out the assembly produced by your code in C... and that's good... if you then move to C++ and see how it abstracted some things, you can figure out what's going on on those abstractions (nothing is pure magic), and you can decide if it's good for what you're doing or not...

 

But don't give it too much thought, as long as you understand what's going on with the code you write no matter what you learn first or after... the important thing is to take the next step in your career ;)

Share this post


Link to post
Share on other sites

C - as one of the most popular language in the universe - will be good for you at any production level, but you must find your own favorit programming language to work. 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement