Sign in to follow this  
supercat1

OpenGL homemade engine issues

Recommended Posts

hi! this is my first post here, though i have been looking around and gleaming info for a while now (much thanks there!). :) so, the reason i have been floating around here trying to glean info is because i would like to build a 3d engine using opengl and have had some issues. well, my friend and i finally got to the point where we can move around using wasd and looking around with a mouse. however, we have run into two issues that concern us. 1. when moving with the keyboard alone everything looks find. when looking around with the mouse alone everything looks fine. however, when we move (left and right are the most pronounced, though surely forward and backward do it too but aren't as visible) and rotate the view with the mouse, normal mouse look like in an fps is the goal, we have an issue where the objects appear to be oscilating from one spot to another. i'm not sure how to describe this, but it is like the computer is rapidly flipping a childs flipbook and the pictures drawn aren't lining up so it seems to vibrate. originally we thought this was due to having a PostRedisplay call for the keyboard call back and the passive mouse motion call back. so to remedy that we looked around online and saw that different places were using the timer call back and doing the redisplay in there. so we are now doing that too, however we are still experiencing the same issue. also, online we noticed that most people suggest moving the world around the camera in the opposite direction that the input states (so to move left move the world right, etc etc). however, we started and still are doing it so that the actual eye position is the one moving and not the rest of the world. my questions on this are -- has anyone ever experienced this before, and if so how did you solve the issue? also, is it vital to move the world around the eye as opposed to the eye through the world? 2. my second major issue with the engine (that i know of :) ) is that when i press down on the keyboard to move there is a stutter before the movement starts. so, for example, if the eye is stationary (no translation or rotation occuring) and then i push 'w' to move forward, there is a slight move forward and a pause and then continuous movement until the key is released. this occurrs for all four directions (forward, backward, right, and left) but not for mouse look. my question is pretty much the same as before -- has anyone ever experienced anything like this before and if so, how did you solve the issue? i know i haven't posted any code here, but that is because the faq said not to be a "fix this for me" kind of person. :) i have done lots of searching online and tried to find a tutorial or paper or anything on how to effectively create a mouse look system, but haven't been very successful, only tutorials on how to get the mouse location. about the most useful mouse info i have found is how to warp the pointer around the screen (which is allowing us to have continuous movement as opposed to being locked at the edges). however, having said that, i would be glad to post any code that you might think helpful in diagnosing the issues. bonus question -- does anyone know where i can find out what is actually going on in glutMainLoop()? i have checked the man pages and what not, but i really would like to see the code in there or a list of what is happening and in what order. thanks! and, i would like to say thank you for all the help ya'll have provided so far (unwittingly ;)) and any that is to come.

Share this post


Link to post
Share on other sites
I'm not sure about the movement problems you're having; it sounds like that could be a problem in your code or perhaps related to using glut. As for moving the world or moving the camera, that's purely a conceptual distinction; it's all the same to OpenGL.

Share this post


Link to post
Share on other sites
Number one is probably an issue with applying the transformations and rotations, possibly in the wrong order. I haven't touched 3d code in some months so am not too sure about it. Your description is difficult to comprehend :) but from what you say it might be something else. Any chance of a screenshot?

Number two is most likely an issue with your keyboard polling / processing code. Think of how you press a key in a text editor. First one letter appears, then a pause, then others follow if the key is still down. Therefore, if you only move once for each keydown, you will get this initial stutter (I think). So you want to start moving on the first keydown and ignore subsequent keydowns of the same key until a keyup for that key is detected. (I might be digging my grave here tho :D)

Edit: answer to your bonus question

Edit2: You might wanna ditch GLUT :) I suppose it only brings some startup code and main loop code to the table, and that stuff is a write-and-forget non-issue. Write it once, your way, and you can keep using it. Portability is usually not an issue unless you are aiming at the small devices market or the console market or really want to impress linux users.

Share this post


Link to post
Share on other sites
Quote:
Your description is difficult to comprehend :) but from what you say it might be something else. Any chance of a screenshot?


yeah, i know, like i said, it is kinda hard to explain. i tried to grab a screen shot that demonstrated the issue i am seeing and i can't seem to do it. apparently the issue is then that the view is being drawn incorrectly like every other frame. this then raises a question of when the input is taken in. when using glut (which i really would like to get away from myself however my friend is dead set on it, i suggested we use the gl commands ourself write wrappers for what we need and let allegro do keyboard and mouse input if that is possible ... is it?) i don't know if keyboard and mouse are taken in at alternating times. my friend explains it as whenever the windows interupt is registered glut then deals with the respective call back. i'm not sure if that is how it is, but it doesn't explain why they can't be registered in the same loop and then have the transforms applied and then have the PostRedisplay called. that is what would make sense to me -- mess with stuff and then display it. if any of that made sense, can some one please comment on it?

Quote:
Number two is most likely an issue with your keyboard polling / processing code. Think of how you press a key in a text editor. First one letter appears, then a pause, then others follow if the key is still down. Therefore, if you only move once for each keydown, you will get this initial stutter (I think). So you want to start moving on the first keydown and ignore subsequent keydowns of the same key until a keyup for that key is detected. (I might be digging my grave here tho :D)


on this topic i did some research for what glut does with keyboard polling. [url=http://www.lighthouse3d.com/opengl/glut/index.php?7]this is what i came up with.[/url] at first i was really excited because it seemed to be what i wanted. however, it turns out that glutSetKeyRepeat() actually doesn't work in win32 and to do something similar you have to do glutIgnoreKeyRepeat(). in that case when you set it to ignore key repeats and use the glutMainLoop (man, this thing keeps popping up and i dislike it more and more ever time ...) you have to repress the key for it to reregister. now i freely admit that i really didn't know what to do after setting glutIgnoreKeyRepeat() to ignore. my guess is that there is a way to make it so that there is no need to keep repressing whatever key, but i don't know it. so any help there would be good.

Quote:
Edit: answer to your bonus question


thanks!

Quote:
Edit2: You might wanna ditch GLUT :) I suppose it only brings some startup code and main loop code to the table, and that stuff is a write-and-forget non-issue. Write it once, your way, and you can keep using it. Portability is usually not an issue unless you are aiming at the small devices market or the console market or really want to impress linux users.


yeah, that is what i'm thinking more and more. like i mentioned earlier in this post, i was contemplating dropping glut, writing my own main and wrappers that i know are working and how, and letting allegro get the keyboard and mouse data (because i don't know how opengl does it, but i am familiar with allegro). any suggestions there? think that will work?

again, thanks for the help so far! and remember, if you want to see any particular code i'm more than happy to post it if it means generating insight that i can use. :)

Share this post


Link to post
Share on other sites
Quote:
Original post by supercat1
my guess is that there is a way to make it so that there is no need to keep repressing whatever key, but i don't know it. so any help there would be good.

What your probably could do is to store the key states in a table. You can use the callbacks described at the bottom of that page to reset the key's state in your table. Then every frame you check if the key is down according to your table, and if it is, commence movement. This should give smooth results.

Quote:
Original post by supercat1
yeah, that is what i'm thinking more and more. like i mentioned earlier in this post, i was contemplating dropping glut, writing my own main and wrappers that i know are working and how, and letting allegro get the keyboard and mouse data (because i don't know how opengl does it, but i am familiar with allegro). any suggestions there? think that will work?

OpenGL is a graphics library. Unlike DirectX, it does not provide for any input methods. Therefore, you really have to use another library (if moving away from GLUT). Apart from Allegro, there's also SDL input, and DirectX input. Someone in an old thread also suggested GL Framework (GLFW), although it seems to do a lot more than just input.

[Edited by - lightbringer on June 7, 2006 6:16:21 PM]

Share this post


Link to post
Share on other sites
Hi,

Are you developing on XP/2000? If so, try looking up WM_INPUT on the msdn site. I use that and it works fine, I have a big if/else statement for the keyboard processing.

As for your objects being skewed, Sounds like you may have either
a) a problem with your FOV setup
or most likely
b) a problem with your camera code.

Are you using a vector system for you camera? If not, I would recomend using that.

Hope this helps somewhat.
( i need a new keyboard, so if there are any r's missing, that's my excuse )

Share this post


Link to post
Share on other sites
I was reading an article just now and something came up that sounded kind of like what you described. clicky. see the part about "shake" rendering problems with star systems. I'm assuming that your scene is not that big, but maybe you inadvertently used large numbers?

Share this post


Link to post
Share on other sites
The first issue is almost definitely a problem with the order that transformation are being applied. I had the same kind of problem until I fixed some bugs with matrix orders and stuff, then it worked - that was a good day.

The second issue is due to the keyboard repeat delay. Even at its fastest it is not quick enough. A better way is to poll the keyboard status in your main loop. If you're using Windows for example, call GetKeyboardState to find out which keys are currently depressed.

Share this post


Link to post
Share on other sites
For smooth keyboard input you can try:

#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

Instead of checking the array or whatever you are storing key states in just use one of these in its place. You won't have any of the waiting when you use the default Windows keyboard stuff.

Also, its not that hard to create a real WinMain() and WndProc().

Share this post


Link to post
Share on other sites
thanks for all the suggestions so far.

however, i'm still unable to find anything that is wrong. or more accurately, i'm not seeing it, cause there is definitely something wrong. furthermore, this is the third day in which i have been essentially alone in trying to figure this out. my friend who did much of the original coding has been anything but helpful in determining the problem and i'm getting very frustrated.

to that end, i'm contemplating just redoing the whole thing myself. however, if anyone would be willing to look at our code and could help me figure out the image thing (i can live with the key delay for now, that is definitely an easier fix) i would be extremely thankful, even if said help was just "start over, it's easier that way".

anyways, thanks for the help so far!

Share this post


Link to post
Share on other sites
sorry for the double post, but i wanted to make sure that those who were helping me before saw this as a new post. to you all, i am very thankful.

now, i have essentially gone through what was the engine before now (as my friend had mostly written it) and forced it to fornicate with nehe's lesson 10 code. what it spawned was this:


// 6/7/06
// Andrew Monshizadeh
// OpenGL 3d engine using glut

#include <GL/glut.h>
#include <cmath>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;

// window global variables
int WINDOW_WIDTH = 800;
int WINDOW_HEIGHT = 600;
int windowids[1]; // can be used to store multiple window ids in case there are multiple windows
char title[13] = "test engine";

// call back functions to be used
void initializecallbacks();
void displaycallback();
void keyboardcallback(unsigned char key, int x, int y);
void passivemotioncallback(int x, int y);
void reshapecallback(int w, int h);
void timercallback(int time);

// setup visual requirments
void initializevisuals();

// variables used for movement
const float piover180 = 0.0174532925f; // pi / 180, for degree to radian transform
float heading; // direction character is facing
float xpos; // x-axis position of the player
float zpos; // z-axis position of the player
float ypos = 0.5; // y-axis position of the player
GLfloat yrot; // y-axis rotation
GLfloat lookupdown = 0.0f; // value to look up or down
GLfloat z=0.0f; // Depth Into The Screen
int mousebasex = WINDOW_WIDTH/2;
int mousebasey = WINDOW_HEIGHT/2;

int main(int argc, char* argv[])
{
glutInit(&argc, argv);
initializevisuals();
initializecallbacks();

glutMainLoop();

return 0;
}

//----------------------------------------------------------------------------//
// call back section //
//----------------------------------------------------------------------------//
void initializecallbacks()
{
// setting up the call back functions
glutDisplayFunc(displaycallback); // display call back
glutKeyboardFunc(keyboardcallback); // keyboard call back
glutPassiveMotionFunc(passivemotioncallback); // passive mouse motion call back
glutReshapeFunc(reshapecallback); // screen reshape call back
glutTimerFunc(15, timercallback, 0); // timer call back
}

void keyboardcallback(unsigned char key, int x, int y)
{
switch (key)
{
// miscellaneous input
case 27: // 'esc'
exit(0); // end the program
break;

// movement input
case 'a':
xpos -= (float)sin(heading*piover180 + 90.0f*piover180) * 0.05f;
zpos -= (float)cos(heading*piover180 + 90.0f*piover180) * 0.05f;
break;
case 'd':
xpos += (float)sin(heading*piover180 + 90.0f*piover180) * 0.05f;
zpos += (float)cos(heading*piover180 + 90.0f*piover180) * 0.05f;
break;
case 'w':
xpos -= (float)sin(heading*piover180) * 0.05f;
zpos -= (float)cos(heading*piover180) * 0.05f;
break;
case 's':
xpos += (float)sin(heading*piover180) * 0.05f;
zpos += (float)cos(heading*piover180) * 0.05f;
}

int dims[4];
glGetIntegerv(GL_VIEWPORT, dims);

int dx = x - mousebasex;
int dy = y - mousebasey;
mousebasex = x;
mousebasey = y;

// if (x < 20)
// {
// glutWarpPointer(dims[2]/2, y);
// mousebasex = dims[2]/2;
// }
// if (x > (dims[2]/2 - 20))
// {
// glutWarpPointer(dims[2]/2, y);
// mousebasex = dims[2]/2;
// }

lookupdown += dy * 0.5f;
heading -= dx * 0.5f;
if(heading < 0.0f)
heading = heading + 360.0f;
else if(heading > 360.0f)
heading = heading - 360.0f;
yrot = heading;
}

void passivemotioncallback(int x, int y)
{
int dims[4];
glGetIntegerv(GL_VIEWPORT, dims);

int dx = x - mousebasex;
int dy = y - mousebasey;
mousebasex = x;
mousebasey = y;

if (x < 20)
{
glutWarpPointer(dims[2]/2, y);
mousebasex = dims[2]/2;
}
if (x > (dims[2] - 20))
{
glutWarpPointer(dims[2]/2, y);
mousebasex = dims[2]/2;
}

lookupdown += dy * 0.5f;
heading -= dx * 0.5f;
if(heading < 0.0f)
heading = heading + 360.0f;
else if (heading > 360.0f)
heading = heading - 360.0f;
yrot = heading;
}

void reshapecallback(int w, int h)
{
glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix

// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}

void timercallback(int time)
{
glutPostRedisplay();
glutTimerFunc(10, timercallback, 0);
}

void displaycallback()
{
int dims[4];
glGetIntegerv(GL_VIEWPORT, dims);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); // Reset The View

GLfloat xtrans = -xpos;
GLfloat ztrans = -zpos;
GLfloat ytrans = 0; // -walkbias-0.25f;
GLfloat sceneroty = 360.0f - yrot;

glRotatef(lookupdown,1.0f,0,0);
glRotatef(sceneroty,0,1.0f,0);
glTranslatef(xtrans, ytrans, ztrans);

glBegin(GL_QUADS);
glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green
glVertex3f( 0.5f, 0.5f,-0.5f); // Top Right Of The Quad (Top)
glVertex3f(-0.5f, 0.5f,-0.5f); // Top Left Of The Quad (Top)
glVertex3f(-0.5f, 0.5f, 0.5f); // Bottom Left Of The Quad (Top)
glVertex3f( 0.5f, 0.5f, 0.5f); // Bottom Right Of The Quad (Top)

glColor3f(1.0f,0.5f,0.0f); // Set The Color To Orange
glVertex3f( 0.5f,-0.5f, 0.5f); // Top Right Of The Quad (Bottom)
glVertex3f(-0.5f,-0.5f, 0.5f); // Top Left Of The Quad (Bottom)
glVertex3f(-0.5f,-0.5f,-0.5f); // Bottom Left Of The Quad (Bottom)
glVertex3f( 0.5f,-0.5f,-0.5f); // Bottom Right Of The Quad (Bottom)

glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red
glVertex3f( 0.5f, 0.5f, 0.5f); // Top Right Of The Quad (Front)
glVertex3f(-0.5f, 0.5f, 0.5f); // Top Left Of The Quad (Front)
glVertex3f(-0.5f,-0.5f, 0.5f); // Bottom Left Of The Quad (Front)
glVertex3f( 0.5f,-0.5f, 0.5f); // Bottom Right Of The Quad (Front)

glColor3f(1.0f,1.0f,0.0f); // Set The Color To Yellow
glVertex3f( 0.5f,-0.5f,-0.5f); // Bottom Left Of The Quad (Back)
glVertex3f(-0.5f,-0.5f,-0.5f); // Bottom Right Of The Quad (Back)
glVertex3f(-0.5f, 0.5f,-0.5f); // Top Right Of The Quad (Back)
glVertex3f( 0.5f, 0.5f,-0.5f); // Top Left Of The Quad (Back)

glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue
glVertex3f(-0.5f, 0.5f, 0.5f); // Top Right Of The Quad (Left)
glVertex3f(-0.5f, 0.5f,-0.5f); // Top Left Of The Quad (Left)
glVertex3f(-0.5f,-0.5f,-0.5f); // Bottom Left Of The Quad (Left)
glVertex3f(-0.5f,-0.5f, 0.5f); // Bottom Right Of The Quad (Left)

glColor3f(1.0f,0.0f,1.0f); // Set The Color To Violet
glVertex3f( 0.5f, 0.5f,-0.5f); // Top Right Of The Quad (Right)
glVertex3f( 0.5f, 0.5f, 0.5f); // Top Left Of The Quad (Right)
glVertex3f( 0.5f,-0.5f, 0.5f); // Bottom Left Of The Quad (Right)
glVertex3f( 0.5f,-0.5f,-0.5f); // Bottom Right Of The Quad (Right)
glEnd(); // Done Drawing The Quad

glFlush();
glutSwapBuffers();
}


void initializevisuals()
{
// creating the window and drawing buffers
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); // create double buffer and z-depth buffer
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); // create window using predetermined window sizes
glutInitWindowPosition(0,0);
windowids[0]=glutCreateWindow(title);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // set background color to white
glClearDepth(1.0); // enables clearing of the depth buffer
glEnable(GL_DEPTH_TEST); // enable depth testing
glDepthFunc(GL_LESS); // the type of depth test to do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // really nice perspective calculations, got from NeHe tutorial 10

int dims[4];
glGetIntegerv(GL_VIEWPORT, dims);

glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix

// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)dims[2]/(GLfloat)dims[3],0.1f,100.0f);

glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}



any comments y'all have would be much appreciated. the jittery effect i was talking about before is still kinda present here, but not as bad. the key delay is here, but we have definitely discussed that into the ground. :)

any suggestions on how to further eliminate the jitteryness would be greatly appreciated.

[Edited by - supercat1 on June 8, 2006 12:43:23 AM]

Share this post


Link to post
Share on other sites
quadruple post ftw!

but this is a good triple post -- i got the issue fixed! turns out that what was causing the jumpy visuals was the fact that glut was poling the keyboard for key presses every time through glutMainLoop(). so after putting in a version of delay-less input (similar to what y'all suggested, but having to go through glut's messy call backs since i'm not building it only for win32). but anyways, i got the delay-less input coded, compiled, ran, and started to freak out. :) i was pretty happy to finally not have nausea inducing vibrating objects on the screen.

i can't say thank you enough to the people on this board that helped with the correction of this/these issues! my hat's off to y'all!

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

Sign in to follow this  

  • Announcements

  • Forum Statistics

    • Total Topics
      628327
    • Total Posts
      2982090
  • Similar Content

    • By mellinoe
      Hi all,
      First time poster here, although I've been reading posts here for quite a while. This place has been invaluable for learning graphics programming -- thanks for a great resource!
      Right now, I'm working on a graphics abstraction layer for .NET which supports D3D11, Vulkan, and OpenGL at the moment. I have implemented most of my planned features already, and things are working well. Some remaining features that I am planning are Compute Shaders, and some flavor of read-write shader resources. At the moment, my shaders can just get simple read-only access to a uniform (or constant) buffer, a texture, or a sampler. Unfortunately, I'm having a tough time grasping the distinctions between all of the different kinds of read-write resources that are available. In D3D alone, there seem to be 5 or 6 different kinds of resources with similar but different characteristics. On top of that, I get the impression that some of them are more or less "obsoleted" by the newer kinds, and don't have much of a place in modern code. There seem to be a few pivots:
      The data source/destination (buffer or texture) Read-write or read-only Structured or unstructured (?) Ordered vs unordered (?) These are just my observations based on a lot of MSDN and OpenGL doc reading. For my library, I'm not interested in exposing every possibility to the user -- just trying to find a good "middle-ground" that can be represented cleanly across API's which is good enough for common scenarios.
      Can anyone give a sort of "overview" of the different options, and perhaps compare/contrast the concepts between Direct3D, OpenGL, and Vulkan? I'd also be very interested in hearing how other folks have abstracted these concepts in their libraries.
    • By aejt
      I recently started getting into graphics programming (2nd try, first try was many years ago) and I'm working on a 3d rendering engine which I hope to be able to make a 3D game with sooner or later. I have plenty of C++ experience, but not a lot when it comes to graphics, and while it's definitely going much better this time, I'm having trouble figuring out how assets are usually handled by engines.
      I'm not having trouble with handling the GPU resources, but more so with how the resources should be defined and used in the system (materials, models, etc).
      This is my plan now, I've implemented most of it except for the XML parts and factories and those are the ones I'm not sure of at all:
      I have these classes:
      For GPU resources:
      Geometry: holds and manages everything needed to render a geometry: VAO, VBO, EBO. Texture: holds and manages a texture which is loaded into the GPU. Shader: holds and manages a shader which is loaded into the GPU. For assets relying on GPU resources:
      Material: holds a shader resource, multiple texture resources, as well as uniform settings. Mesh: holds a geometry and a material. Model: holds multiple meshes, possibly in a tree structure to more easily support skinning later on? For handling GPU resources:
      ResourceCache<T>: T can be any resource loaded into the GPU. It owns these resources and only hands out handles to them on request (currently string identifiers are used when requesting handles, but all resources are stored in a vector and each handle only contains resource's index in that vector) Resource<T>: The handles given out from ResourceCache. The handles are reference counted and to get the underlying resource you simply deference like with pointers (*handle).  
      And my plan is to define everything into these XML documents to abstract away files:
      Resources.xml for ref-counted GPU resources (geometry, shaders, textures) Resources are assigned names/ids and resource files, and possibly some attributes (what vertex attributes does this geometry have? what vertex attributes does this shader expect? what uniforms does this shader use? and so on) Are reference counted using ResourceCache<T> Assets.xml for assets using the GPU resources (materials, meshes, models) Assets are not reference counted, but they hold handles to ref-counted resources. References the resources defined in Resources.xml by names/ids. The XMLs are loaded into some structure in memory which is then used for loading the resources/assets using factory classes:
      Factory classes for resources:
      For example, a texture factory could contain the texture definitions from the XML containing data about textures in the game, as well as a cache containing all loaded textures. This means it has mappings from each name/id to a file and when asked to load a texture with a name/id, it can look up its path and use a "BinaryLoader" to either load the file and create the resource directly, or asynchronously load the file's data into a queue which then can be read from later to create the resources synchronously in the GL context. These factories only return handles.
      Factory classes for assets:
      Much like for resources, these classes contain the definitions for the assets they can load. For example, with the definition the MaterialFactory will know which shader, textures and possibly uniform a certain material has, and with the help of TextureFactory and ShaderFactory, it can retrieve handles to the resources it needs (Shader + Textures), setup itself from XML data (uniform values), and return a created instance of requested material. These factories return actual instances, not handles (but the instances contain handles).
       
       
      Is this a good or commonly used approach? Is this going to bite me in the ass later on? Are there other more preferable approaches? Is this outside of the scope of a 3d renderer and should be on the engine side? I'd love to receive and kind of advice or suggestions!
      Thanks!
    • By nedondev
      I 'm learning how to create game by using opengl with c/c++ coding, so here is my fist game. In video description also have game contain in Dropbox. May be I will make it better in future.
      Thanks.
    • By Abecederia
      So I've recently started learning some GLSL and now I'm toying with a POM shader. I'm trying to optimize it and notice that it starts having issues at high texture sizes, especially with self-shadowing.
      Now I know POM is expensive either way, but would pulling the heightmap out of the normalmap alpha channel and in it's own 8bit texture make doing all those dozens of texture fetches more cheap? Or is everything in the cache aligned to 32bit anyway? I haven't implemented texture compression yet, I think that would help? But regardless, should there be a performance boost from decoupling the heightmap? I could also keep it in a lower resolution than the normalmap if that would improve performance.
      Any help is much appreciated, please keep in mind I'm somewhat of a newbie. Thanks!
    • By test opty
      Hi,
      I'm trying to learn OpenGL through a website and have proceeded until this page of it. The output is a simple triangle. The problem is the complexity.
      I have read that page several times and tried to analyse the code but I haven't understood the code properly and completely yet. This is the code:
       
      #include <glad/glad.h> #include <GLFW/glfw3.h> #include <C:\Users\Abbasi\Desktop\std_lib_facilities_4.h> using namespace std; //****************************************************************************** void framebuffer_size_callback(GLFWwindow* window, int width, int height); void processInput(GLFWwindow *window); // settings const unsigned int SCR_WIDTH = 800; const unsigned int SCR_HEIGHT = 600; const char *vertexShaderSource = "#version 330 core\n" "layout (location = 0) in vec3 aPos;\n" "void main()\n" "{\n" " gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n" "}\0"; const char *fragmentShaderSource = "#version 330 core\n" "out vec4 FragColor;\n" "void main()\n" "{\n" " FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n" "}\n\0"; //******************************* int main() { // glfw: initialize and configure // ------------------------------ glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // glfw window creation GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "My First Triangle", nullptr, nullptr); if (window == nullptr) { cout << "Failed to create GLFW window" << endl; glfwTerminate(); return -1; } glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); // glad: load all OpenGL function pointers if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { cout << "Failed to initialize GLAD" << endl; return -1; } // build and compile our shader program // vertex shader int vertexShader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr); glCompileShader(vertexShader); // check for shader compile errors int success; char infoLog[512]; glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); if (!success) { glGetShaderInfoLog(vertexShader, 512, nullptr, infoLog); cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << endl; } // fragment shader int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr); glCompileShader(fragmentShader); // check for shader compile errors glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); if (!success) { glGetShaderInfoLog(fragmentShader, 512, nullptr, infoLog); cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << endl; } // link shaders int shaderProgram = glCreateProgram(); glAttachShader(shaderProgram, vertexShader); glAttachShader(shaderProgram, fragmentShader); glLinkProgram(shaderProgram); // check for linking errors glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success); if (!success) { glGetProgramInfoLog(shaderProgram, 512, nullptr, infoLog); cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << endl; } glDeleteShader(vertexShader); glDeleteShader(fragmentShader); // set up vertex data (and buffer(s)) and configure vertex attributes float vertices[] = { -0.5f, -0.5f, 0.0f, // left 0.5f, -0.5f, 0.0f, // right 0.0f, 0.5f, 0.0f // top }; unsigned int VBO, VAO; glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); // bind the Vertex Array Object first, then bind and set vertex buffer(s), //and then configure vertex attributes(s). glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); // note that this is allowed, the call to glVertexAttribPointer registered VBO // as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind glBindBuffer(GL_ARRAY_BUFFER, 0); // You can unbind the VAO afterwards so other VAO calls won't accidentally // modify this VAO, but this rarely happens. Modifying other // VAOs requires a call to glBindVertexArray anyways so we generally don't unbind // VAOs (nor VBOs) when it's not directly necessary. glBindVertexArray(0); // uncomment this call to draw in wireframe polygons. //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // render loop while (!glfwWindowShouldClose(window)) { // input // ----- processInput(window); // render // ------ glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); // draw our first triangle glUseProgram(shaderProgram); glBindVertexArray(VAO); // seeing as we only have a single VAO there's no need to // bind it every time, but we'll do so to keep things a bit more organized glDrawArrays(GL_TRIANGLES, 0, 3); // glBindVertexArray(0); // no need to unbind it every time // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.) glfwSwapBuffers(window); glfwPollEvents(); } // optional: de-allocate all resources once they've outlived their purpose: glDeleteVertexArrays(1, &VAO); glDeleteBuffers(1, &VBO); // glfw: terminate, clearing all previously allocated GLFW resources. glfwTerminate(); return 0; } //************************************************** // process all input: query GLFW whether relevant keys are pressed/released // this frame and react accordingly void processInput(GLFWwindow *window) { if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); } //******************************************************************** // glfw: whenever the window size changed (by OS or user resize) this callback function executes void framebuffer_size_callback(GLFWwindow* window, int width, int height) { // make sure the viewport matches the new window dimensions; note that width and // height will be significantly larger than specified on retina displays. glViewport(0, 0, width, height); } As you see, about 200 lines of complicated code only for a simple triangle. 
      I don't know what parts are necessary for that output. And also, what the correct order of instructions for such an output or programs is, generally. That start point is too complex for a beginner of OpenGL like me and I don't know how to make the issue solved. What are your ideas please? What is the way to figure both the code and the whole program out correctly please?
      I wish I'd read a reference that would teach me OpenGL through a step-by-step method. 
  • Popular Now