Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 07 Oct 2011
Offline Last Active Oct 23 2016 02:42 PM

#5310391 OpenGL, oh my god

Posted by on 11 September 2016 - 09:09 PM

You can get a simple base going on with some basic setup code. Download GLEW and get that working if you haven't already.

I will post some code without error checking, and hopefully this step by step will help you understand better.


This is not a full example, and there might even be a typo in here, but if you start from the main function and read what's happening line by line, you will come to an understanding of how it all comes together.


Keep in mind the shader must have input attributes 0: vec4 position, 1: vec4 color and 2: vec2 uv.


This is intended to be a reference if you're stuck, not a complete example.


SDL_GLContext gl_context = nullptr;


std::string read_file(std::string path) {
    std::ifstream file(path, std::ios::binary);
    if (!file.is_open()) { return ""; }
    std::stringstream result;
    result << file.rdbuf();
    return result.str();

void init_gl(SDL_Window* window) {

    gl_context = SDL_GL_CreateContext(window);







int load_shader(int type, std::string path) {

    int id = glCreateShader(type);

    const char* source = read_file(path).c_str();

    glShaderSource(id, 1, &source, 0);


    return id; // look at glGetShaderInfoLog later on



int load_shader_program(std::string path_vert, std::string path_frag) {

    int shader_program_id = glCreateProgram();

    int vert_id = load_shader(GL_VERTEX_SHADER, path_vert);

    int frag_id = load_shader(GL_FRAGMENT_SHADER, path_frag);

    glAttachShader(shader_program_id, vert_id);

    glAttachShader(shader_program_id, frag_id);


    // look at glGetProgramInfoLog, glValidateProgram and glGetProgramiv later on

    return shader_program_id;



struct vector4f { float x = 0.0f, y = 0.0f, z = 0.0f, w = 0.0f; };

struct vector3f { float x = 0.0f, y = 0.0f, z = 0.0f; };

struct vector2f { float x = 0.0f, y = 0.0f; };


struct gl_vertex {

    vector4f position;

    float r = 1.0f, g = 1.0f, b = 1.0f, a = 1.0f;

    vector2f uv;



struct gl_shape {

    int vao = 0;

    int index_buffer = 0, vertex_buffer = 0;

    std::vector<unsigned int> indices;

    std::vector<gl_vertex> vertices;



void use_gl_shape(gl_shape* shape) {


    glBindBuffer(GL_ARRAY_BUFFER, shape->index_buffer);

    glBindBuffer(GL_ARRAY_BUFFER, shape->vertex_buffer);



gl_shape create_quad() {

    gl_shape quad;

    quad.indices = { 0, 1, 2, 3, 2, 0 };

    quad.vertices.insert(quad.vertices.begin(), 4, gl_vertex());

    quad.vertices[0].position = { 0.0f, 0.0f, 0.0f, 1.0f };

    quad.vertices[1].position = { 1.0f, 0.0f, 0.0f, 1.0f };

    quad.vertices[2].position = { 1.0f, 1.0f, 0.0f, 1.0f };

    quad.vertices[3].position = { 0.0f, 1.0f, 0.0f, 1.0f };

    quad.vertices[0].uv = { 0.0f, 0.0f };

    quad.vertices[1].uv = { 1.0f, 0.0f };

    quad.vertices[2].uv = { 1.0f, 1.0f };

    quad.vertices[3].uv = { 0.0f, 1.0f };

    glGenVertexArrays(1, &quad.vao);

    glGenBuffers(1, &quad.index_buffer);

    glGenBuffers(1, &quad.vertex_buffer);


    glBufferData(GL_ARRAY_BUFFER, sizeof(unsigned int) * quad.indices.size(), &quad.indices[0], GL_STATIC_DRAW);
    glBufferData(GL_ARRAY_BUFFER, sizeof(gl_vertex) * quad.vertices.size(), &quad.vertices[0], GL_STATIC_DRAW);
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(gl_vertex), nullptr);
    glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(gl_vertex), (void*) (4 * sizeof(float)));
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(gl_vertex), (void*) (8 * sizeof(float)));

    return quad;



int main() {

    //   ... setup ...


    int shader_program = load_shader_program("shader.vert", "shader.frag");

    gl_shape quad = create_quad();

    glClearColor(0.2f, 0.2f, 0.2f, 1.0f);

    while (true) {



        // <- This is where you set your projection and view matrix uniforms ->

        // ...

        // <- This is where you set your model matrix uniform ->


        glDrawElements(GL_TRIANGLES, shape.indices.size(), GL_UNSIGNED_INT, &shape.indices[0]);



    // ... cleanup ...

    return 0;




 If you need help figuring out projection and view matrices, feel free to ask.

#5287323 Multiple bullets?

Posted by on 17 April 2016 - 11:09 AM


Anyway, look into "std::vector".

Yes, you would be correct if the OP was using C++. But the tags on his topic states “Java,” so he should use java.util.ArrayList instead.


Oh my, I didn't catch that.

#5287194 Multiple bullets?

Posted by on 16 April 2016 - 10:09 AM

then it becomes null




Anyway, look into "std::vector".



You could just make a variable like this:

std::vector<Bullet> myBullets;

then use it like this:

myBullets.push_back(Bullet(x, y)); // or however your constructor goes, if any. You can probably do myBullets.push_back({ x, y }); as well

If you have update and draw functions in your bullet class, then you can do this to call those:

for (auto& i : myBullets) { // Loop through all the elements in "myBullet" and let "i" be a reference to the current element. 
    i.update();  // or i.draw();

Hope that helps in any way.

#5259110 C++ Passing an unknown class as an argument to a function

Posted by on 26 October 2015 - 05:45 AM

Lets say i want a function to take a class as a parameter but i do not know the class in advance or i want it to be able to take different classes, how can i do it?

Class object;
AnotherClass antherObject;

function(Class &object)

How can i make "function" take "AnotherClass" as a parameter as well? Overloading is a solution but i would still have to copy the code again. Is there another way?


The question is kind of vague to get a good answer.

Sure, you can use templates or polymorphism - but is that really what you need?


If you have a specific scenario in mind, I'd advise you to post that and see what response you get then.

Sometimes you might only need to pass a few common variables instead of the class itself.



Oops, I realised now that Lactose wrote the same thing :)

#5258248 Programming a simply 2D game

Posted by on 21 October 2015 - 12:40 AM

I think GameMaker is your best option. You barely need to know how to code.


#5229241 Best libraries to start a rendering engine project

Posted by on 15 May 2015 - 06:48 PM

I always thought that using window management APIs like SDL/glut is a rookie move. I got that feeling because I remember years ago when I first got into this, I used SDL and the window took forever to load.

Glut loads faster but the SDL window loading incident made me think this way.

Professional devs build their own window management library (and I mean input management as well), or use pro commercial apis like Qt, no?


Lol, what are you talking about? Took forever to load? If it was 20 seconds or so, a subsystem probably failed to load. Usually solved by restarting your computer. This has only happened to me once in 4 years of programming though. Game controller subsystem failed for some reason.


You probably did something wrong though. My games start in less than a second.


In my opinion, you only want to build your own window management library if you want to do fancy stuff. For regular window features, SDL is fine. 

#5226864 How To Make An RPG

Posted by on 02 May 2015 - 01:07 PM





Here are a few resources you can check out.

#5226103 sfml health bar

Posted by on 28 April 2015 - 11:11 AM

both maxHealth and m_playerHealth is an int


oh that actually worked though, mind explaining the static_cast<float> ?


It doesn't seem like you understood marcjulian, so I'll try to rephrase it in a different manner:

The function "setScale" takes floats. If you try to do this:  int health = 50 / 100;

You're not going to get 0.5, because an integer can only be whole numbers. So that is: 0, 1, 2, 3, 4, 5, 6...


Also, you could make a "healthRatio" function, like this:

float healthRatio() {
    return (float) m_playerHealth / (float) maxHealth;
healthBarSprite.setScale(healthRatio(), 1.0f);

I recommend reading marcjulian's post a few more times until you get it.

#5188304 input in realtime.

Posted by on 21 October 2014 - 08:09 AM


Every time I see a "realtime" thread, one moronic wannabe just shows up trying to make the OP feel as stupid as possible.
It is incredibly obvious that the OP is a beginner, and doesn't need actual realtime input.

Correcting misconceptions of a beginner is a very important step, especially because such misconceptions can make researching your own answer extremely difficult because the keywords you use are not the keywords which will be helpful. I did not see what tonemgub wrote as something to make someone feel stupid.



I just feel there are other ways to explain the difference.


I won't deny the fact that I'm overreacting, but it's just that I think beginners do not need to know things like this at the stage they're in.

They will probably not understand what you are talking about with the knowledge they have, so they will assume it's hard to check for input or use a timer.


You have to realise that people do not react to information the same way you do. Just because you understand what a post means, does not mean the relevant OP will understand. 

#5188285 input in realtime.

Posted by on 21 October 2014 - 05:00 AM

You will never be able to get real-time input from any Windows operating system, as these are not real-time operating systems (RTOS). Windows lets you get keyboard presses and depresses as events (and you can also get the internal timer value for when this happened) as window messages sent to a window, or to get the state of the keys (pressed or not) at any time. It's up to you to use all of these in such a way that the user thinks everything is happening in real-time.


Also, console programs are a bit different, as they put the key events into an input buffer that you can read from, and in this case, I don't think you can get the timer value for when the event happened...


Since you haven't specified what sort of program you are writing (console or GUI), the question still remains too broad for us to give you a straight answer.


Every time I see a "realtime" thread, one moronic wannabe just shows up trying to make the OP feel as stupid as possible.

It is incredibly obvious that the OP is a beginner, and doesn't need actual realtime input.



Lol at the downvotes. I'm speaking the damn truth. It's okay to mention that realtime isn't the correct term.

However: "omg but realtime is not possible on Windows" etc... that is the kind of attitude that I am talking about here.

Why not just casually mention it and move on to actually answering the question?

Don't just treat the topic as if it actually is something else than what the OP intended.

#5166109 Terrain - map editor

Posted by on 10 July 2014 - 08:28 PM

I would go height map for a terrain editor. Bullet's height map object allows for the map array to be dynamically modified. Creating a new btBvhTriangleMeshShape every time the terrain is modified may or may not be too slow, but using the height map wouldn't require quite as much Bullet-related code.


On the other hand, do you really need physics *while* the terrain is being altered/extruded/etc? You could stop physics, allow the user to extrude (terminology?) a hill, and then start simulating again after building a new btBvhTriangleMeshShape. If your editor includes placing other objects (like debris), you might need to use an invisible sphere to push away other objects before creating a new btBvhTriangleMeshShape. Or even try to detect which non-static objects would need to be moved and just warp them above the new highest point in the area and let them fall back down (the user will have to re-place them, but hey, they rose a hill there).


Disclaimer: I've never written such an editor; I use Blender (poorly).


True, that is an advantage to using a heightmap. But it really limits how you can edit the vertices.


As for the triangle mesh method:

I suppose I can disable physics while editing, which would make it necessary to make a new class which has the vertices. The problem I can see with that, is I would need to reimplement certain features Bullet does for me, such as raycasting. 


Anyone else have any thoughts?

#5166102 Terrain - map editor

Posted by on 10 July 2014 - 06:45 PM

I've been getting into 3D for a while now, and I'm using Bullet Physics for physics, and OpenGL 3+ for graphics.

Up until now, I've used Blender to create a map, and then load it to a btBvhTriangleMeshShape for use in Bullet. 


However, I want to create a map editor, and I first thought the easiest way would be to use a heightmap. This requires the entire terrain to be in a grid however, and that makes it hard to make good terrain without making a small grid full of unnecessary vertices. Therefore it seems like I can't use that.


I've tried Googling for resources on how I should go about this, but it's always topics which do not help me.

What I've been thinking about is to make the map in the editor a btBvhTriangleMeshShape, but I don't see any way to quickly modify vertices.


I hope anyone can give me some advice on what I should do.



#5152803 opengl question

Posted by on 10 May 2014 - 09:11 PM

well then am I just stupid?

Way to miss the point.



To clarify. Yes. I now see you as a stupid person.

#5152801 opengl question

Posted by on 10 May 2014 - 08:47 PM

I'm sorry, but have you been trolling for a whole decade now?

Downvote me all you want, but I can not stop laughing.


Phil, have you really been programming for 10 years? I looked at your first posts, and I'm amazed at how you're still trying to grasp the basics.

If you haven't managed to understand the basics by now, you will probably never do so.


I have a feeling I'm not the first one to tell you this.


I'm honestly feeling really, really weird writing this post. I just can't comprehend the fact that you've been trying to learn programming for 10 years. It feels like I'm daydreaming and that my post doesn't make sense. It is just that weird.



Also, you say: "Am I just stupid?"

Sure, that might be the case. However, it's maybe the fact that you do not have a talent related to programming.

People like to say that as long as you work hard enough, you will accomplish things. The reality of it is that you have to work hard and be talented.

Let's face it; you have no talent within the realm of programming. Try something different.

#5152017 Circular Dependency with Pimpls

Posted by on 07 May 2014 - 05:32 AM

Well, it seems like this would become a huge mess.

I'll stick with void pointers for now, and see what happens.


Thanks for your posts however.



Lol, just because I said I will see how it goes with void pointers, I get downvoted?

I mean, I get that they're hated, but seriously?