Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (784)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (858)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  OpenGL 4.1 Drawing a texture  (Read 4609 times)
0 Members and 1 Guest are viewing this topic.
Offline Joshua Waring
« Posted 2013-08-26 12:18:48 »

I realize the code is in C++, but the OpenGL is the part I'm questioning.
I've been trying to render a Texture to the screen... this is my third attempt... first time was in Intermediate mode, second with method removed in OpenGL 3.
I now have a shader (inside the initialize function) and the code to draw at the bottom. Currently nothing renders, I used to get a untextured quad until I fixed an error in the glUniform1i
This is the whole function.
Drawing starts at line 70
Can someone please help me, I mainly just need to see how it all fits together,
even a copy and paste from a working draw texture function or something that does the same would be sufficient from me to reverse engineer.

The world is big, so learn it in small bytes.
Offline davedes
« Reply #1 - Posted 2013-08-26 12:50:06 »

Lots of things to note here... I will be brutally honest. Tongue

  • Don't copy-paste. Instead, read up on theory; learn about things like matrix math, how GLSL and the shader pipeline works, and what VBOs really are. If you aren't ready do actually do the reading, then you are barking up the wrong tree trying to write your own OpenGL code.
  • It looks like you are creating and deleting your shaders and VBOs in the same function that you are drawing your textures. This is horrible practice. As a rule of thumb, try not to create or allocate things in your game loop rendering functions. Create things in the beginning of your application lifecycle; then render; then "clean up" your resources when the user is quitting.
  • gl_TexCoord, gl_MultiTexCood0, etc. are deprecated in modern pipeline. You need to define your own attributes and varyings.
  • Don't use ARB function calls in modern pipeline.
  • Don't glEnable texturing -- this is not necessary in modern pipeline.
  • Pass zero to glUniform1i for samplers, not GL_TEXTURE0
  • You didn't specify any texture coordinates -- how will GL know what to sample from?
  • Where is your perspective or orthographic projection matrix?

    Honestly, your code looks like a copy-pasted mish-mash of different techniques that span both the programmable and fixed-function pipeline. It's no surprise anything renders; pretty much every aspect of your code is not correct. Like I said; read some tutorials and stop copy-pasting, or use something other than raw OpenGL.



    Offline Cero
    « Reply #2 - Posted 2013-08-26 13:06:01 »

    Is there a good modern opengl book you would recommend?
    By modern I mean not as old. But I still wanna use OpenGL 2, not 3+ exclusively.

    Games published by our own members! Check 'em out!
    Legends of Yore - The Casual Retro Roguelike
    Offline Joshua Waring
    « Reply #3 - Posted 2013-08-26 13:09:42 »

    Butally honest, I like that

    When I came into openGL 4.1 I brought along a lot of my old OpenGL 1.1 so the main reason I'm using things that are long long gone is that I don't know what's in and what's out.
    I do destroy the VBO per function call, but the shader is created once and never again.

    I've taken note from the dot points and thank you :3
    Not too sure how I'd provide the UV coordinates though

    the reason the drawing part looks like Frankenstein is because I've been moving and trying all sorts of functions to try and get it to work by using the OpenGL specification which didn't go so well.

    The world is big, so learn it in small bytes.
    Offline Joshua Waring
    « Reply #4 - Posted 2013-08-30 05:22:31 »

    I've come back with a new version of the function and this time there's no errors and I understand what I'm doing. using Indexed draws. I changed the shader and the drawing code, but nothing appears still on the screen.

    Maybe to help this is the code that runs at the start of a frame and end

    glClearColor(0.25f, 0.25f, 0.25f, 1);
    glViewport(0, 0, windowSize.x, windowSize.y);

    The world is big, so learn it in small bytes.
    Offline Grunnt

    JGO Kernel

    Medals: 143
    Projects: 8
    Exp: 5 years

    Complex != complicated

    « Reply #5 - Posted 2013-08-30 08:02:32 »

    Here's some OpenGL tutorials I'm currently going through. Really, start by going through such tutorials step by step and you'll get there.

    - Good starting point: with the code samples converted to Java by "our" Ra4king:

    Offline davedes
    « Reply #6 - Posted 2013-08-30 13:43:55 »

    Where is your texcoord buffer? Why are you still using ARB extension calls? Don't use "varying" for new versions of GLSL, instead you need "in" and "out" syntax.

    Offline Joshua Waring
    « Reply #7 - Posted 2013-09-01 11:51:15 »

    So I've removed all ARB functions, added a texture coords and moved to the in and out keywords. aswell as have the shader just use the colour white to at least get a shape.
    and I have results!

    Still can't see it?

    zoom scan and enhance

    There we go, one pixel Smiley it's progress
    there's still something going wrong and it might be the way I give it the vertices?
    I'm also moving repeated code into functions for ease.

    The world is big, so learn it in small bytes.
    Offline davedes
    « Reply #8 - Posted 2013-09-01 15:15:26 »

    You need an orthographic projection matrix. Vertex shader typically looks like this:
    uniform mat4 projection;

    in vec2 in_pos;

    out vec4 out_pos; //gl_Position is deprecated

    void main() {
        out_pos = projection * vec4(in_pos, 0.0, 1.0);

    The matrix is fairly easy to compute, see here:

    Though typically in C++ you would use something like GLM which handles matrix math for you.

    You need to start reading a book and/or some tutorials. Copy-pasting will not get you very far.

    Pages: [1]
      ignore  |  Print  

    hadezbladez (1632 views)
    2018-11-16 13:46:03

    hadezbladez (647 views)
    2018-11-16 13:41:33

    hadezbladez (1631 views)
    2018-11-16 13:35:35

    hadezbladez (337 views)
    2018-11-16 13:32:03

    EgonOlsen (2682 views)
    2018-06-10 19:43:48

    EgonOlsen (2949 views)
    2018-06-10 19:43:44

    EgonOlsen (1646 views)
    2018-06-10 19:43:20

    DesertCoockie (2356 views)
    2018-05-13 18:23:11

    nelsongames (2265 views)
    2018-04-24 18:15:36

    nelsongames (2958 views)
    2018-04-24 18:14:32
    Deployment and Packaging
    by philfrei
    2019-02-17 20:25:53

    Deployment and Packaging
    by mudlee
    2018-08-22 18:09:50

    Java Gaming Resources
    by gouessej
    2018-08-22 08:19:41

    Deployment and Packaging
    by gouessej
    2018-08-22 08:04:08

    Deployment and Packaging
    by gouessej
    2018-08-22 08:03:45

    Deployment and Packaging
    by philfrei
    2018-08-20 02:33:38

    Deployment and Packaging
    by philfrei
    2018-08-20 02:29:55

    Deployment and Packaging
    by philfrei
    2018-08-19 23:56:20 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
    Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!