Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Hythloday

Pages: [1]
1
Do you really want to color mask the image? Why not use real transparent colors instead?

If you render the texture with a SFML sprite, does it display correctly?

Ah, I fixed it, the reason is that to render this model we need to enable the repeat mode in opengl for this texture, however this is on default false in sfml.

2
The color mask is irrelevant here, I tried use sf::Texture::loadFromFile directly, and the problem is the same.

I tried make the texture into an sprite and render with sfml as you said, it is normal and fine(as shown in attached png)

In fact the left part of the body when using sf::Texture is fine in my main post, and the right half are using the exact same texture, the 3d model just flipped those texcoords in its vertices data file.

3
It should n't be like this since sfml use stb_image internally?

Anyway the different result are shown as the picture file attached.

If I use sfml texture class with opengl, the left half of the model is not correctly rendered, it seems as if the texture coords numbers are wrong or something, however if I change the way to read texture from sfml to directly use stb_image, this problems are gone.

The relavant codes are like this:

to read image files in sfml
else if ((ext == DATA_JPG) || (ext == DATA_PNG))
            {
                Image image;
                if (!image.loadFromFile(path))
                {
                    throw Exception(EX_BROKEN_DATA_FILE, name);
                }
                //image.createMaskFromColor(Color::White);

                if (!insertAsset<Texture>(name)->loadFromImage(image))
                {
                    throw Exception(EX_FAILED_TO_LOAD_TEXTURE, name);
                }
            }

to set the texture uniforms in opengl
                mat->GetTexture(type, i, &str);
                sf::Texture* current_tex = getAsset2<sf::Texture>(str.C_Str());
                if (current_tex)
                {
                    TextureInfo current_texture_info;
                    current_tex->generateMipmap();
                    current_texture_info.glID = current_tex->getNativeHandle();
                    current_texture_info.type = typeName;
                    current_texture_info.filename = str.C_Str();
                    textures.push_back(current_texture_info);
                }

To bind the texture in opengl
                // now set the sampler to the correct texture unit
                //shader.setInt((name + number), i);
                glUniform1i(glGetUniformLocation(shader.getID(), (name + number).c_str()), i);
                // and finally bind the texture
                glBindTexture(GL_TEXTURE_2D, textures[i].glID);

4
ah.I found where's the problem, it's a silly mistake

these lines:

        my_shader->setInt("card_front", 0);
        my_shader->setInt("card_back", 1);

should be:

        my_shader->setInt("frontTexture", 0);
        my_shader->setInt("backTexture", 1);

5
I am learning through the tutorials on learnopengl.com, and I met a unsolvable problem when I try to use a second Texture.

The problem is the same to this guy's post on official forum:
https://www.opengl.org/discussion_boards/showthread.php/200373-OpenGL-second-Texture-is-not-showing-up-%28OpenGL-Texture-Unit%29

However that guy didn't get an answer eventually, so I posted it here hope someone could help me.


The main code is like this:

#include <Alef\alef_application.h>
#include <Alef\gl_specific\alef_gl_shader.h>

#include <GL\glew.h>

#include <glm\glm.hpp>
#include <glm\gtc\matrix_transform.hpp>
#include <glm\gtc\type_ptr.hpp>

#include <lua\lua.h>


using namespace alef;

unsigned int VBO, VAO, EBO;

Application* app = nullptr;

IntPoint screen_size;
alef::gl_specific::Shader* my_shader = NULL;

glm::mat4 model, view, projection;

void _onDrawing(ObjectBase* sender)
{
        my_shader = getAsset2<alef::gl_specific::Shader>("shader1");
        my_shader->use();

        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, getAsset<Texture>("card_front.png").getNativeHandle());
        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_2D, getAsset<Texture>("card_back.png").getNativeHandle());


        my_shader->setInt("card_front", 0);
        my_shader->setInt("card_back", 1);

        model = glm::rotate(glm::mat4(1.0f), (app->getElapsedTime().asSeconds() * 0.75f) * glm::radians(50.0f), glm::vec3(0.0f, 1.0f, 0.0f));

        view = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -3.0f));

        projection = glm::perspective(glm::radians(45.0f), (float)screen_size.x / screen_size.y, 0.1f, 100.0f);

        my_shader->setMat4("model", model);
        my_shader->setMat4("view", view);
        my_shader->setMat4("projection", projection);
       
        glBindVertexArray(VAO);
               
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
        //glDrawArrays(GL_TRIANGLES, 0, 3);
       
        glBindVertexArray(0);
}

void _onClosing(ObjectBase* sender)
{
        alef::DialogResult result = app->showMsgBox(L"&#36864;&#20986;&#28216;&#25103;&#65311;", alef::BUTTON_OKCANCEL);
        if (result == RESULT_OK)
        {
                app->closing = true;
        }
}

FloatPoint getGLSize(IntPoint size)
{
        FloatPoint gl_size;
        IntPoint screen_size = app->getScreenSize();

        gl_size.x = (size.x / (float)screen_size.x) * 2;
        gl_size.y = (size.y / (float)screen_size.y) * 2;

        return gl_size;

}

int main()
{

#ifdef _DEBUG
    _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
   
       
    try
    {
                GLenum ERR = glewInit();
               
                app = Application::getInstance();
        app->init("test");
               
                app->onDrawing.Connect(_onDrawing);
                app->onClosing.Connect(_onClosing);
               
                IntPoint card_size;
                card_size.x = 200;
                card_size.y = 285;

                screen_size = app->getScreenSize();

                FloatPoint card_size_f = getGLSize(card_size);
               
                float vertices[] = {
                        //     ---- &#20301;&#32622; ----       ---- &#39068;&#33394; ----     - &#32441;&#29702;&#22352;&#26631; -
                        0.5f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f,   // &#21491;&#19978;
                        0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f,   // &#21491;&#19979;
                        -0.5f, -0.5f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f,   // &#24038;&#19979;
                        -0.5f,  0.5f, 0.0f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f    // &#24038;&#19978;
                };
                unsigned int indices[] = {
                        0, 1, 3, // first triangle
                        1, 2, 3  // second triangle
                };
                               
                glGenVertexArrays(1, &VAO);
                glGenBuffers(1, &VBO);
                glGenBuffers(1, &EBO);
                // 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);

                glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
                glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

                // position attribute
                glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
                glEnableVertexAttribArray(0);
                // color attribute
                glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
                glEnableVertexAttribArray(1);
                // coords attribute
                glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
                glEnableVertexAttribArray(2);
               
        app->run();

        app->dispose();
    }
    catch (Exception& e)
    {
        cout << e.description() << endl << e.info << endl;
        system("Pause");
    }
 
    return EXIT_SUCCESS;
}


the shaders:




#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;


uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;


out vec4 ourColor;
out vec2 TexCoord;


void main()
{
    gl_Position = projection * view * model * vec4(aPos, 1.0);
    ourColor = vec4(aColor, 1.0);
    TexCoord = aTexCoord;
}
 



#version 330 core
out vec4 FragColor;


in vec4 ourColor;
in vec2 TexCoord;


uniform sampler2D frontTexture;
uniform sampler2D backTexture;


void main() {

   if(gl_FrontFacing) {
      FragColor = texture(frontTexture, TexCoord);
   }
   else {
      FragColor = texture(backTexture, TexCoord);
   }
}
 


6
General / Re: how can I find out if a RenderTexture is created or not?
« on: September 15, 2012, 01:33:49 pm »
Actually, you guys missed my point。。。
Well you didn't explain it good enough then... ;)

Please make use of the code=cpp tags when presenting code. ;)

I don't fully know your code, etc. but from what you wrote you probably want to refractor your code, because to me it sounds like you'd leave behind some unitialized variables, which is a bad thing. Also you might want to handle the drag event better. For instance you check when the dragging is done and then call a function that (re)creates the texture and not every time the MouseMoveEvent is triggered. ;)

I'd also highly suggest to use smart pointers instead of raw pointers.

well... I didn't find the "code" style combobox in the post editor before you told me... sorry...

as for the raw pointer...dont't worry....I just trying to do it in a QT-like way...internally I did use shared_ptr to manage new and delete....

as for the dragging... it just a style.... in Windows, you can choose two different style when resizing, the first way is like your description... do not repaint the whole window, but show a plain box indicating the window size, and do the repaint when drag released... the second way is to constantly repaint the window while dragging... apparently the latter is more "comfortable to the eyes.."

anyway... thank you(and Laurent) for help.

7
General / Re: how can I find out if a RenderTexture is created or not?
« on: September 15, 2012, 11:27:07 am »
Actually, you guys missed my point。。。

I am currently working on a GUI.

and I try to avoid re-render the texture of a GUI in one game frame for multiple times.

For example, when I try to drag the border of a window to resize it, if the onDragTo() function re-create the Texture of the Window on each time of MouseMotionEvent. it would become extremely slow and unnecessary
, what I'm doing is to push a callback of these "slow" function to a list(and do a check to see if a similar callback is already pushed, that even if you call create multiple time, eventually there is only one onCreate() called in a frame), and do these callbacks(create, render, render contains etc.) on each frame of the main loop.

And this bring the problem that, during one frame, the acrually create callback is maybe just pending and have not actually done yet.

for example:

        Button* btn = Button::factory.newInstance2(gui);
        btn->create(80,25);
        btn->backColor.set(Color::Blue);
        btn->text.set(L"按钮");

the btn->create(80,25) line did not actually create the texture of the button, so the following code btn->backColor.set(Color::Blue); need not to trigger the render function to render the texture(it would be automatically done when onCreate() called).

8
General / how can I find out if a RenderTexture is created or not?
« on: September 15, 2012, 10:20:52 am »
Assume I receive a RenderTexture reference as Function parameter. or there is a RenderTexture member in my class, which is not created together with the instance of class.

if it's a sprite, I can do sth like this:

    if (sprite.getTexture())

but a RenderTexture's getTexture() returned a reference but not a pointer, so is there a simple way to know this ?

9
Graphics / Re: Coordinate system
« on: September 05, 2012, 11:06:50 am »
Thank you very much ..... that's exactly what I missed..... ah I'm so stupid....

10
Graphics / [solved]Coordinate system
« on: September 05, 2012, 10:42:24 am »
I am tring to build a GUI system of my own.
But weird thing happened when I try to render a RenderTexture on RenderWindow.

What I did was create a sprite from that RenderTexture:

        Sprite surface;       
        surface.setTexture(_finalSurface.getTexture());
        surface.setPosition(_screenBounds.left, _screenBounds.top);
        render_target.draw(surface);

and in main function:

        Window* wnd = Window::Factory.newInstance2(gui);       
        wnd->position.set(Point(50,50));
        wnd->size.set(Size(300,300));

but what I got is like this:

Instead of left-top corner, the form appeared in the left-bottom corner....

Isn't Y in SFML's coordinate system should be UP to DOWN???

[attachment deleted by admin]

Pages: [1]