Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (799)
Games in Android Showcase (237)
games submitted by our members
Games in WIP (865)
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  
  [Solved] [Super Bible] Basic shader and point drawing isn't working?  (Read 4477 times)
0 Members and 1 Guest are viewing this topic.
Offline Ecumene

JGO Kernel


Medals: 200
Projects: 4
Exp: 8 years


I did not hit her! I did not!


« Posted 2014-08-02 18:41:17 »

I'm reading the 'OpenGL Super Bible, 6th Edition' and I tried to copy the exact code. But I can't figure out why my point isn't drawing.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
package ecumene;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import org.lwjgl.opengl.PixelFormat;

public class Test1 {
   private static String fragmentSource[] = {
         "#version 430 core                     \n"+
         "                                      \n"+
         "out vec4 color;                       \n"+
         "                                      \n"+
         "void main(void)                       \n"+
         "{                                     \n"+
         "    color = vec4(0.0, 0.8, 1.0, 1.0); \n"+
         "}                                     \n"
    };
   
   private static String vertexSource[] ={
         "#version 430 core                           \n"+
         "                                            \n"+
         "void main(void)                             \n"+
         "{                                           \n"+
         "    gl_Position = vec4(0.0, 0.0, 0.5, 1.0); \n"+
         "}                                           \n"
   };
   
   private static int shaderProgram;
   private static int fragmentShader;
   private static int vertexShader;
   
   private static int vertexArray;
   
   public static void main(String[] args) throws Exception {
      {
         Display.setDisplayMode(new DisplayMode(800, 600));
         Display.setTitle("Screen");
         Display.setResizable(false);
         Display.create(new PixelFormat());
      }
     
      fragmentShader = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);
      GL20.glShaderSource(fragmentShader, fragmentSource);
      GL20.glCompileShader(fragmentShader);
     
      vertexShader = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
      GL20.glShaderSource(vertexShader, vertexSource);
      GL20.glCompileShader(vertexShader);
     
      shaderProgram = GL20.glCreateProgram();
      GL20.glAttachShader(shaderProgram, vertexShader);
      GL20.glAttachShader(shaderProgram, fragmentShader);
      GL20.glLinkProgram(shaderProgram);
     
      vertexArray = GL30.glGenVertexArrays();
      GL30.glBindVertexArray(vertexArray);
     
      GL11.glPointSize(40.0f);
     
      FloatBuffer backgroundColor = BufferUtils.createFloatBuffer(4);
      backgroundColor.put(new float[]{1.0f, 0.0f, 0.0f, 1.0f});
      backgroundColor.flip();
     
      while(!Display.isCloseRequested()){
         GL30.glClearBuffer(GL11.GL_COLOR, 0, backgroundColor);
         
         GL20.glUseProgram(shaderProgram);
         GL11.glDrawArrays(GL11.GL_POINT, 0, 1);
         
         Display.update();
      }
     
      GL20.glUseProgram(0);
     
      GL30.glDeleteVertexArrays(vertexArray);
      GL20.glDeleteShader(fragmentShader);
      GL20.glDeleteShader(vertexShader);
      GL20.glDeleteProgram(shaderProgram);
      Display.destroy();
   }
}


Here's the result:

Offline basil_

« JGO Bitwise Duke »


Medals: 418
Exp: 13 years



« Reply #1 - Posted 2014-08-02 19:09:50 »

i think the point is not drawn cos' there is no VBO bound to source the drawArrays call.
Offline Ecumene

JGO Kernel


Medals: 200
Projects: 4
Exp: 8 years


I did not hit her! I did not!


« Reply #2 - Posted 2014-08-02 19:15:29 »

Yes, but this is what the book showed, but it was written in C++.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline trollwarrior1
« Reply #3 - Posted 2014-08-02 19:15:55 »

Try checking for gl errors. Also, try printing out shader errors.

I bet that you are not created your shader program properly, or something like that, so GL uses default shader.

EDIT--
GL11.glGetError()
GL20.glGetShaderLog() - or something along that
Offline ra4king

JGO Kernel


Medals: 508
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2014-08-02 20:53:53 »

Running your code locally, checking for GL Errors, glDrawArrays throws an error: Invalid Enum. GL_POINT isn't the right enum, it's GL_POINTS Wink

Here's a good method to have around:
1  
2  
3  
4  
5  
public static void checkGLError() {
    int error = glGetError();
    if(error != GL_NO_ERROR)
        throw new RuntimeException("GL Error: " + gluErrorString(error));
}

Offline Ecumene

JGO Kernel


Medals: 200
Projects: 4
Exp: 8 years


I did not hit her! I did not!


« Reply #5 - Posted 2014-08-02 20:54:58 »

Running your code locally, checking for GL Errors, glDrawArrays throws an error: Invalid Enum. GL_POINT isn't the right enum, it's GL_POINTS Wink

wooooooooooow.

Works now. Thank you so much!

Offline PandaMoniumHUN

JGO Coder


Medals: 32
Exp: 3 years


White-bearded OGL wizard


« Reply #6 - Posted 2014-08-02 20:55:25 »

As others mentioned before me use
glGetError()
and
glGetShaderInfoLog()
/
glGetProgramInfoLog()
.
Here's how to use
glGetError()
:
1  
2  
3  
4  
int error = GL11.glGetError();
if(error != GL11.GL_NO_ERROR){
   System.err.println(new OpenGLException(error).getMessage());
}

And the shader info logs:
1  
String infoLog = GL20.glGetShaderInfoLog(id, GL20.glGetShaderi(id, GL20.GL_INFO_LOG_LENGTH));

I wrote this of the top of my head so I hope I didn't make any mistakes.

By the way as someone who read this book I can tell you that these code snippets are meant to be really short demonstrations of the basic principles and you shouldn't use these in real world situations. Normally you would detach and delete shaders after linking the shaderprogram, use VBOs filled with data inside VAOs with vertex attribute pointers, create and pass your own matrices to the shaders, etc.

Don't worry because the book will teach you all of the above mentioned stuff, just keep it in mind that those examples in the beginning are all extremely simplified and exactly because of that they present some bad habits. Pointing

Edit: Daymn, I was too slow. Tongue I was thinking about POINTS vs POINT too, but I was quite unsure. It's weird how sometimes OGL uses plural and sometimes singular namings, like GL_TRIANGLES and GL_TRIANGLE_STRIP, or the fact that you have to use GL_LINES for rendering but GL_LINE for
glPolygonMode(...)
. Smiley

My Blog | Jumpbutton Studio - INOP Programmer
Can't stress enough: Don't start game development until you haven't got the basics of programming down! Pointing
Offline Ecumene

JGO Kernel


Medals: 200
Projects: 4
Exp: 8 years


I did not hit her! I did not!


« Reply #7 - Posted 2014-08-02 20:56:45 »

Yeah, I checked for errors before. Didn't print anything, ra4king was right, wrong enum. Thanks though Wink

Pages: [1]
  ignore  |  Print  
 
 

 
Riven (198 views)
2019-09-04 15:33:17

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

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

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

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

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

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

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

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

nelsongames (4305 views)
2018-04-24 18:15:36
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

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
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!