Picpenguin

OpenGL C loading and rendering models (SDL2, glew, OpenGL 3.3)

Recommended Posts

Picpenguin    0

Hi

I'm new to learning OpenGL and still learning C. I'm using SDL2, glew, OpenGL 3.3, linmath and stb_image.
I started following through learnopengl.com and got through it until I had to load models. The problem is, it uses Assimp for loading models. Assimp is C++ and uses things I don't want in my program (boost for example) and C support doesn't seem that good.
Things like glVertexAttribPointer and shaders are still confusing to me, but I have to start somewhere right?

I can't seem to find any good loading/rendering tutorials or source code that is simple to use and easy to understand.
I have tried this for over a week by myself, searching for solutions but so far no luck. With tinyobjloader-c and project that uses it, FantasyGolfSimulator, I was able to actually load the model with plain color (always the same color no matter what I do) on screen and move it around, but cannot figure out how to use textures or use its multiple textures with it.
I don't ask much: I just want to load models with textures in them, maybe have lights affect them (directional spotlight etc). Also, some models have multiple parts and multiple textures in them, how can I handle those?

Are there solutions anywhere?

Thank you for your time. Sorry if this is a bit confusing, English isn't my native language :)

Share this post


Link to post
Share on other sites
Shaarigan    1038

Talisson's solution is a usual approach in custom engines and frameworks because it makes loading faster for removing file format specific parsing to favor loading times, memory usage and so on.

An other approach is to use something simple like .obj file format. There exists various loaders out there that do their thing and could be a starting point for your first model/geometry rendering

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Similar Content

    • By pseudomarvin
      I assumed that if a shader is computationally expensive then the execution is just slower. But running the following GLSL FS instead just crashes
      void main() { float x = 0; float y = 0; int sum = 0; for (float x = 0; x < 10; x += 0.00005) { for (float y = 0; y < 10; y += 0.00005) { sum++; } } fragColor = vec4(1, 1, 1 , 1.0); } with unhandled exception in nvoglv32.dll. Are there any hard limits on the number of steps/time that a shader can take before it is shut down? I was thinking about implementing some time intensive computation in shaders where it would take on the order of seconds to compute a frame, is that possible? Thanks.
    • By DKdowneR
      Hi! I made a tile map reading from a file. Almost everything works good, but when a player go out of map, program runs into an error and says that "vector subscript out of range". My question is how to make check for it
      Drawing map : 
      void GameplayScreen::DrawMap(SDL_Renderer *renderer) { for (int y = map.size() - 1; y >= 0; --y) { for (int x = getStartBlockX(), xEnd = getEndBlockX(); x < xEnd && x < map[y].size(); ++x) { if (map[y][x] != "0,0") { int tempX = atoi(map[y][x].substr(0, map[y][x].find(',')).c_str()); int tempY = atoi(map[y][x].substr(map[y][x].find(',') + 1).c_str()); srcRect.x = tempX * 32; srcRect.y = tempY * 32; srcRect.w = 32; srcRect.h = 32; destRect.x = x * 32 + posX; destRect.y = (y * 32 + posY); destRect.w = 32; destRect.h = 32; vBlock[Earth]->Draw(renderer, srcRect, destRect); } } } } getStartBlockX returns first map block, getEndBlockX returns last, so it's like render on screen only a piece of map, not all blocks.
      tempX returns x coordinate of tile image,  tempY y coordinate. So, for example, if map is like :

      0,0 0,0 0,0 0,0 0,0
      0,0 0,0 0,0 0,0 0,0
      1,0 2,0 0,3 1,0 1,0
      0,0 is first block image, 1,0 is one next to the first, 0,3 is 2 under the first block etc.
         
    • By JackOfCandles
      I created a small test application where I handle the SDL_CONTROLLERDEVICEADDED and SDL_CONTROLLERDEVICEREMOVED events, and output information about the controller being added/removed, based on the information in this blog.
      I have two controllers connected to my PC, an Xbox 360 controller and a Logitech controller. What I found is that if they are both active and awake it produces the expected output, but if either one or both of them are asleep, it outputs the wrong name under certain conditions. I don't want to be overly verbose and go over every configuration I tested, but one example is if I start the application with both controllers in sleep mode, it fires the SDL_CONTROLLERDEVICEADDED for the Logitech controller but not the Xbox controller at application startup (The event for the Xbox control only happens when it is awakened), and then when I unplug/plug in the Logitech controller's USB receiver, it shows the device name as "X360" every time, until I wake them both up, at which point it outputs the correct name again.
      Perhaps I'm worrying too much about this, as most people probably don't mix and match controllers and likely only use one, but I just found it curious. I'm wondering if it might be a bug in SDL2, or if maybe there is some insight someone might have related to xbox controllers and how they behave in sleep mode.
       
      Here is the relevant code if anyone's interested:
      struct SdlGamepad { SDL_GameController* controller; SDL_Joystick* joystick; SDL_JoystickID joystickId; std::string deviceName; int deviceId; }; std::map<SDL_JoystickID, SdlGamepad> joystickIdToGamepadMap; SDL_Event event; bool hasQuit = false; while (hasQuit == false) { while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_CONTROLLERDEVICEADDED: { std::cout << "Controller device added." << std::endl; if (SDL_IsGameController(event.cdevice.which)) { SdlGamepad gamepad; gamepad.controller = SDL_GameControllerOpen(event.cdevice.which); gamepad.joystick = SDL_GameControllerGetJoystick(gamepad.controller); gamepad.joystickId = SDL_JoystickInstanceID(gamepad.joystick); std::string deviceName = SDL_GameControllerName(gamepad.controller); gamepad.deviceName = deviceName; gamepad.deviceId = event.cdevice.which; joystickIdToGamepadMap[gamepad.joystickId] = gamepad; std::cout << "Controller device name = " << gamepad.deviceName << std::endl << "Controller device ID = " << gamepad.deviceId << std::endl << "Joystick device ID = " << gamepad.joystickId << std::endl << std::endl; } break; } case SDL_CONTROLLERDEVICEREMOVED: { std::cout << "Controller device removed." << std::endl; SdlGamepad gamepad = joystickIdToGamepadMap[event.cdevice.which]; std::cout << "Controller device name = " << gamepad.deviceName << std::endl << "Controller device ID = " << gamepad.deviceId << std::endl << "Joystick device ID = " << gamepad.joystickId << std::endl << std::endl; SDL_GameControllerClose(gamepad.controller); joystickIdToGamepadMap.erase(event.cdevice.which); break; } case SDL_QUIT: { hasQuit = true; break; } } } }  
    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
       
      Thanks
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
  • Popular Now