Jump to content
  • Advertisement

OpenGL OpenGL Transformation Is Not Working As I Expected!!

Recommended Posts

Advertisement
4 hours ago, SomeoneRichards said:

In your sprite::addBrain function can you std::cout the sprite tag and the tag of the sprite held by the brain?

In your sprite::render function can you std::cout the sprite tag and the model matrix?

As you can see everything works as i expected. The model matrix of the player is changing every time, and also i double checked the model matrices of the other two sprites (they remain Identity Matrices). Also the add brain is adding the brain to the correct sprite (Player).

Output.PNG

 

First Call Of Player's Renderer.

First Change.PNG

 

Second Call Of Player's Renderer.

Second Change.PNG

 

Third Call Of Player's Renderer.

Third Cjange.PNG

Edited by babaliaris

Share this post


Link to post
Share on other sites

I'm not entirely sure if this is clear, but when you create your sprites, you are creating a new shader for each one. However, when you render them individually, you are not binding the corresponding shaders each time. The only time you bind a shader is in your render method. So I suspect (but am not sure, as it is difficult going through multiple files, and I'm also fairly new to this) that the last sprite you build is the one whose shader is being used, and therefore is getting the model view matrix mapped to it.

I'm not sure the best way to test or eradicate this issue You could try creating a single shader and sharing it, or you could try adding a bind call in each of your sprite's render functions.

You should really remove the useShader line from the sprite creator. To save from mishaps.

And call it just before rendering.

Does that make sense?

To make it clearer, it looks like this is what you are doing:

Create player sprite, bind player shader.

Create other sprite, bind other shader.

Create other2 sprite, bind other2 shader.

Then, when you render, everything is using the other2 shader (ie the last one you bound), so only this shader (and this corresponding sprite) is getting the model matrix.

Edited by SomeoneRichards

Share this post


Link to post
Share on other sites
43 minutes ago, SomeoneRichards said:

I'm not entirely sure if this is clear, but when you create your sprites, you are creating a new shader for each one. However, when you render them individually, you are not binding the corresponding shaders each time. The only time you bind a shader is in your render method. So I suspect (but am not sure, as it is difficult going through multiple files, and I'm also fairly new to this) that the last sprite you build is the one whose shader is being used, and therefore is getting the model view matrix mapped to it.

I'm not sure the best way to test or eradicate this issue You could try creating a single shader and sharing it, or you could try adding a bind call in each of your sprite's render functions.

You should really remove the useShader line from the sprite creator. To save from mishaps.

And call it just before rendering.

Does that make sense?

To make it clearer, it looks like this is what you are doing:

Create player sprite, bind player shader.

Create other sprite, bind other shader.

Create other2 sprite, bind other2 shader.

Then, when you render, everything is using the other2 shader (ie the last one you bound), so only this shader (and this corresponding sprite) is getting the model matrix.

OMG MAN I LOVE YOU!!!!!!!!!!!!!

I wasn't binding the shaders inside the Sprite::Render() Method So i was actually setting the mvp only on the shader that was binded previously inside the renderer!!!

Problem solved!!

I literally can't thank you enough! This will prevent a lot of frustration in the future.

This forum never led me down :) 

Thank you again!

This is what i had to change in my Sprite::Render Method:

//Render.
void Sprite::Render(Window * window)
{

	//I HAD TO DO THAT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	m_Impl->program->Bind();



	//Create the projection Matrix based on the current window width and height.
	glm::mat4 proj = glm::ortho(0.0f, (float)window->GetWidth(), 0.0f, (float)window->GetHeight(), -1.0f, 1.0f);


	//SO I CAN DO THAT ON THE RIGHT SHADER!!!!!!!!!!!!!!!!!!!
	//Set the MVP Uniform.
	m_Impl->program->setUniformMat4f("u_MVP", proj * m_Impl->model);


	std::cout << "Inside " << m_Impl->tag.c_str() << " Render() has model pointer: " << &m_Impl->model << std::endl;

	
	//Run All The Brains (Scripts) of this game object (sprite).
	for (unsigned int i = 0; i < m_Impl->brains.size(); i++)
	{

		//Get Current Brain.
		Brain *brain = m_Impl->brains[i];

		//Call the start function only once!
		if (brain->GetStart())
		{
			brain->SetStart(false);
			brain->Start();
		}

		//Call the update function every frame.
		brain->Update();
	}


	//Render.
	window->GetRenderer()->Draw(m_Impl->vao, m_Impl->ibo, m_Impl->texture, m_Impl->program);
}

 

Share this post


Link to post
Share on other sites

No worries :) I'm glad I could help.

I really don't think you want that many shaders or shader bindings though. Unless your shaders are actually doing different things.

Just put a shader in your renderer called something like SpriteRenderer, bind it at the start of the render process, and then update and draw each sprite in turn. From my understanding, shader bind calls are one of the most expensive.

Edited by SomeoneRichards

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

  • Advertisement
  • Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By DevAndroid
      Hello everyone,
      I'm trying to display a 2D texture to screen but the rendering isn't working correctly.
      First of all I did follow this tutorial to be able to render a Text to screen (I adapted it to render with OpenGL ES 2.0) : https://learnopengl.com/code_viewer.php?code=in-practice/text_rendering
      So here is the shader I'm using :
      const char gVertexShader[] = "#version 320 es\n" "layout (location = 0) in vec4 vertex;\n" "out vec2 TexCoords;\n" "uniform mat4 projection;\n" "void main() {\n" " gl_Position = projection * vec4(vertex.xy, 0.0, 1.0);\n" " TexCoords = vertex.zw;\n" "}\n"; const char gFragmentShader[] = "#version 320 es\n" "precision mediump float;\n" "in vec2 TexCoords;\n" "out vec4 color;\n" "uniform sampler2D text;\n" "uniform vec3 textColor;\n" "void main() {\n" " vec4 sampled = vec4(1.0, 1.0, 1.0, texture(text, TexCoords).r);\n" " color = vec4(textColor, 1.0) * sampled;\n" "}\n"; The render text works very well so I would like to keep those Shaders program to render a texture loaded from PNG.
      For that I'm using libPNG to load the PNG to a texture, here is my code :
      GLuint Cluster::loadPngFromPath(const char *file_name, int *width, int *height) { png_byte header[8]; FILE *fp = fopen(file_name, "rb"); if (fp == 0) { return 0; } fread(header, 1, 8, fp); if (png_sig_cmp(header, 0, 8)) { fclose(fp); return 0; } png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) { fclose(fp); return 0; } png_infop info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL); fclose(fp); return 0; } png_infop end_info = png_create_info_struct(png_ptr); if (!end_info) { png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); fclose(fp); return 0; } if (setjmp(png_jmpbuf(png_ptr))) { png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); fclose(fp); return 0; } png_init_io(png_ptr, fp); png_set_sig_bytes(png_ptr, 8); png_read_info(png_ptr, info_ptr); int bit_depth, color_type; png_uint_32 temp_width, temp_height; png_get_IHDR(png_ptr, info_ptr, &temp_width, &temp_height, &bit_depth, &color_type, NULL, NULL, NULL); if (width) { *width = temp_width; } if (height) { *height = temp_height; } png_read_update_info(png_ptr, info_ptr); int rowbytes = png_get_rowbytes(png_ptr, info_ptr); rowbytes += 3 - ((rowbytes-1) % 4); png_byte * image_data; image_data = (png_byte *) malloc(rowbytes * temp_height * sizeof(png_byte)+15); if (image_data == NULL) { png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); fclose(fp); return 0; } png_bytep * row_pointers = (png_bytep *) malloc(temp_height * sizeof(png_bytep)); if (row_pointers == NULL) { png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); free(image_data); fclose(fp); return 0; } int i; for (i = 0; i < temp_height; i++) { row_pointers[temp_height - 1 - i] = image_data + i * rowbytes; } png_read_image(png_ptr, row_pointers); GLuint texture; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexImage2D(GL_TEXTURE_2D, GL_ZERO, GL_RGB, temp_width, temp_height, GL_ZERO, GL_RGB, GL_UNSIGNED_BYTE, image_data); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); free(image_data); free(row_pointers); fclose(fp); return texture; } This code just generates the texture and I store the id on memory
      And then I want to display my texture on any position (X, Y) of my screen so I did the following (That's works, at least the positioning).
      //MY TEXTURE IS 32x32 pixels ! void Cluster::printTexture(GLuint idTexture, GLfloat x, GLfloat y) { glActiveTexture(GL_TEXTURE0); glBindVertexArray(VAO); GLfloat vertices[6][4] = { { x, y + 32, 0.0, 0.0 }, { x, y, 0.0, 1.0 }, { x + 32, y, 1.0, 1.0 }, { x, y + 32, 0.0, 0.0 }, { x + 32, y, 1.0, 1.0 }, { x + 32, y + 32, 1.0, 0.0 } }; glBindTexture(GL_TEXTURE_2D, idTexture); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferSubData(GL_ARRAY_BUFFER, GL_ZERO, sizeof(vertices), vertices); glBindBuffer(GL_ARRAY_BUFFER, GL_ZERO); glUniform1i(this->mTextShaderHandle, GL_ZERO); glDrawArrays(GL_TRIANGLE_STRIP, GL_ZERO, 6); } My .png is a blue square.
      The result is that my texture is not loaded correctly. It is not complete and there are many small black spots. I don't know what's going on ? It could be the vertices or the load ? Or maybe I need to add something on the shader. I don't know, I really need help.
      Thanks !
    • By Gnollrunner
      Hi again,  After some looking around I have decided to base my game directly on Direct X rather than using an existing game engine.  Because of the nature of the stuff I'm doing it just didn't seem to fit very well and I kept running into road blocks.  At this point I have a big blob of code for doing fractal world generation and some collision code,  and I'm trying to put it into some form that resembles a game engine.  Since I've never used one before It's a bit alien to me ..... so can someone direct me to a book, website, article, whatever... that covers this?  I'm mainly looking for stuff that covers C++ library design. I'm not adverse to using 3rd party tools for stuff I can used them for.
    • By LuigiLuigi
      I've been working on my own Metroidvania via GameMaker Studio for the past few years. You play as a bat named Ralph as he goes on an adventure to obtain 7 Crystal Medallions hidden in dungeons with the help of a cult known as the Crimson Fog. Along the way, there will be quests unlocked in Cedrus Village as you progress through the game. I've managed to complete a demo of the game up to the first dungeon and boss fight.
      I have only a PC build available, and the only gamepads I managed to install were Logitech Precision and Xbox PC gamepads. I had some trouble on gamepad detection though, so they may have connection issues. The desktop controls are similar to Terarria's control scheme if it's too much trouble. I don't have any music at this point, I'll need to get someone else to compose it later on. The music I make isn't bad, but it doesn't fit the aesthetic that well.
      I'm really hoping I can get feedback regarding the general content.
      Crimson Fog.0.2.zip
    • By mmmax3d
      Hi everyone,
      I would need some assistance from anyone who has a similar experience
      or a nice idea!
      I have created a skybox (as cube) and now I need to add a floor/ground.
      The skybox is created from cubemap and initially it was infinite.
      Now it is finite with a specific size. The floor is a quad in the middle
      of the skybox, like a horizon.
      I have two problems:
      When moving the skybox upwards or downwards, I need to
      sample from points even above the horizon while sampling
      from the botton at the same time.  I am trying to create a seamless blending of the texture
      at the points of the horizon, when the quad is connected
      to the skybox. However, I get skew effects. Does anybody has done sth similar?
      Is there any good practice?
      Thanks everyone!
    • By Alexander Winter
      Jumpaï is a game about creating platformer levels and playing them online with everyone. Will you become the most popular level maker or will you be a speedrunner holding world records on everyone's levels? More into casual play? No problem! You can happily play through the giant level database or chill at people's hub. Meet new people, make new friends, learn to master the game by asking pros or ask for people's favorite tricks on level making. Download here: https://jumpai.itch.io/jumpai Discord: https://discord.gg/dwRTNCG   Trailer:      (The following screenshots are older but still a bit representative)  





      Unlike other games of its genre, Jumpaï is about playing levels with everyone in real time. You have the fun to see how other people are playing and get to realize you are not the only one failing that jump!

      The game is currently into development and still have lots to do. I am looking for people willing to help how they can. Developer? Graphist? Play tester? Sound designer? Game designer? I'm welcoming any talent. The project is so big I have a lot of work to do in all areas. Server backend, UI/UX, Game networking, Gameplay and even the website some day. As you can see from the default buttons, the game has been made with LibGDX. This project is a perfect opportunity for you to get better in various fields as well as showing off your skills.

      If you plan to take an important role into the development of the game, we will discuss how you will get paid once the game generates money. Note that I'm not working on the game full-time. I'm studying full-time and working on it is a hobby. The project has started in november 2016 and experiences heavy progress.

      So, are you interested? If so join me on my discord https://discord.gg/dwRTNCG and I'll answer all your questions.

      Additionnal screenshots:
       



       
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!