Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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 696 times)
0 Members and 1 Guest are viewing this topic.
Offline Joshua Waring

Senior Member


Medals: 4
Projects: 2



« 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

http://www.pastebucket.com/19567
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.

    C/C++:
    http://www.gamefromscratch.com/post/2013/02/27/Modern-OpenGL-resources-round-up.aspx

    Java:
    https://github.com/mattdesl/lwjgl-basics/wiki


    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

    Senior Member


    Medals: 4
    Projects: 2



    « 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

    Senior Member


    Medals: 4
    Projects: 2



    « 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. http://www.pastebucket.com/19672 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);
    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
    glViewport(0, 0, windowSize.x, windowSize.y);
       
    end:
    glFlush();
    SwapBuffers(hdc);



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

    JGO Wizard


    Medals: 68
    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: http://www.arcsynthesis.org/gltut/ with the code samples converted to Java by "our" Ra4king: https://bitbucket.org/ra4king/lwjgl-shader-tutorials
    - http://www.opengl-tutorial.org/
    - http://ogldev.atspace.co.uk/index.html

    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

    Senior Member


    Medals: 4
    Projects: 2



    « 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.
    http://www.pastebucket.com/19690

    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:
    1  
    2  
    3  
    4  
    5  
    6  
    7  
    8  
    9  
    10  
    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:
    http://www.scratchapixel.com/lessons/3d-advanced-lessons/perspective-and-orthographic-projection-matrix/orthographic-projection/

    Though typically in C++ you would use something like GLM which handles matrix math for you.
    http://open.gl/transformations

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

    Pages: [1]
      ignore  |  Print  
     
     
    You cannot reply to this message, because it is very, very old.

     

    Add your game by posting it in the WIP section,
    or publish it in Showcase.

    The first screenshot will be displayed as a thumbnail.

    BurntPizza (22 views)
    2014-09-19 03:14:18

    Dwinin (35 views)
    2014-09-12 09:08:26

    Norakomi (62 views)
    2014-09-10 13:57:51

    TehJavaDev (90 views)
    2014-09-10 06:39:09

    Tekkerue (44 views)
    2014-09-09 02:24:56

    mitcheeb (65 views)
    2014-09-08 06:06:29

    BurntPizza (48 views)
    2014-09-07 01:13:42

    Longarmx (35 views)
    2014-09-07 01:12:14

    Longarmx (40 views)
    2014-09-07 01:11:22

    Longarmx (37 views)
    2014-09-07 01:10:19
    List of Learning Resources
    by Longor1996
    2014-08-16 10:40:00

    List of Learning Resources
    by SilverTiger
    2014-08-05 19:33:27

    Resources for WIP games
    by CogWheelz
    2014-08-01 16:20:17

    Resources for WIP games
    by CogWheelz
    2014-08-01 16:19:50

    List of Learning Resources
    by SilverTiger
    2014-07-31 16:29:50

    List of Learning Resources
    by SilverTiger
    2014-07-31 16:26:06

    List of Learning Resources
    by SilverTiger
    2014-07-31 11:54:12

    HotSpot Options
    by dleskov
    2014-07-08 01:59:08
    java-gaming.org 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‑gaming.org
    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!