Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (426)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
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  
  Is this the correct way to do textures?  (Read 711 times)
0 Members and 1 Guest are viewing this topic.
Offline DavidW

Junior Member


Medals: 3
Exp: 7 years



« Posted 2012-04-27 01:42:18 »

Okay so let me start off by saying that this code does work.  It does exactly what I expected it to do, no problem (whew!) My question is, is this the proper way?  I know I shouldn't be using a static context here, but this is my first foray into textures so I wanted the entire program to be in one simple class.  I am more interested in things like, am I putting "glUseProgram()" in the proper place.  I know, I know, if it works who cares, but I will eventually want to divide a lot of this into objects and I don't want to put code places where it shouldn't go.

Here is the code:

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  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL12.*;
import static org.lwjgl.opengl.GL13.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;
import static org.lwjgl.opengl.GL33.*;

import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

public class TextureTestWindow {
   
   private static final float[] verts = {
      0.8f, 0.8f, 0f, 1f,
      -0.8f, 0.8f, 0f, 1f,
      -0.8f, -0.8f, 0f, 1f,  
      0.8f, -0.8f, 0f, 1f,
      };
   
   private static final float[] texCords = {
      1f, 0f,
      0f, 0f,
      0f, 1f,
      1f, 1f};
   
   private static final byte[] texData = {
      (byte) 255, (byte) 0, (byte) 0, (byte) 255,
      (byte) 0, (byte) 255, (byte) 0, (byte) 255,
      (byte) 255, (byte) 255, (byte) 0, (byte) 255,
      (byte) 0, (byte) 0, (byte) 255, (byte) 255};
   
   private static final String easyTexVert =
         "#version 330\n" +
         "layout (location = 0) in vec4 position;\n" +
         "layout (location = 1) in vec2 texCord;\n" +                        
         "varying vec2 outTexCord;\n" +                            
         "void main() {\n" +
         "   gl_Position = position;\n" +
         "   outTexCord = texCord;\n" +
         "}";
   
   private static final String easyTexFrag =
         "#version 330\n" +
         "varying vec2 outTexCord;\n" +
         "out vec4 outputColor;\n" +                    
         "uniform sampler2D tex;\n" +                
         "void main() {\n" +
         "    outputColor = texture(tex, outTexCord);\n" +
         "}";

   public static void main(String[] args) {
      try {
         Display.setDisplayMode(new DisplayMode(500, 500));
         Display.create();
      } catch (LWJGLException e) {
         e.printStackTrace();
         System.exit(1);
      }
     
      // Create the program      

      int vShader = glCreateShader(GL_VERTEX_SHADER);
      glShaderSource(vShader, easyTexVert);
      glCompileShader(vShader);
      int fShader = glCreateShader(GL_FRAGMENT_SHADER);
      glShaderSource(fShader, easyTexFrag);
      glCompileShader(fShader);
     
      int program = glCreateProgram();
      glAttachShader(program, vShader);
      glAttachShader(program, fShader);
      glLinkProgram(program);
      glValidateProgram(program);

      // Set up the buffers
     
      FloatBuffer vertBuffer = BufferUtils.createFloatBuffer(verts.length+texCords.length);
      vertBuffer.put(verts);
      vertBuffer.put(texCords);
      vertBuffer.flip();
     
      int vertBufferObj = glGenBuffers();
      glBindBuffer(GL_ARRAY_BUFFER, vertBufferObj);
       glBufferData(GL_ARRAY_BUFFER, vertBuffer, GL_STATIC_DRAW);
      glBindBuffer(GL_ARRAY_BUFFER, 0);
     
      ByteBuffer texBuffer = BufferUtils.createByteBuffer(texData.length);
      texBuffer.put(texData);
      texBuffer.flip();
     
      // Create the texture      

      int texture = glGenTextures();
      glBindTexture(GL_TEXTURE_2D, texture);
      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, texBuffer);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
       glBindTexture(GL_TEXTURE_2D, 0);
       
      // get the Uniform location

       int textureUnif = glGetUniformLocation(program, "tex");
       
      // set up the sampler

      int sampler = glGenSamplers();
      glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
      glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
      glSamplerParameteri(sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
      glSamplerParameteri(sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
     
      int i = 0;   // using the first texture
     
      // Put the texture into the uniform

      glUseProgram(program);
      glUniform1i(textureUnif, i);
      glUseProgram(0);
     
      // make the VAO

      int vao = glGenVertexArrays();
      glBindVertexArray(vao);
      glBindBuffer(GL_ARRAY_BUFFER, vertBufferObj);
      glEnableVertexAttribArray(0);
      glVertexAttribPointer(0, 4, GL_FLOAT, false, 0, 0);
      glEnableVertexAttribArray(1);
      glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, Float.SIZE/8 * verts.length);
      glBindVertexArray(0);

      // Finally reader to render!! :)
     
      while(!Display.isCloseRequested()) {
         
         glClear(GL_COLOR_BUFFER_BIT);
         
         // Set up the context
        glUseProgram(program);
         glActiveTexture(GL_TEXTURE0 + i);
         glBindTexture(GL_TEXTURE_2D, texture);
         glBindSampler(i, sampler);
         glBindVertexArray(vao);
         glDrawArrays(GL_QUADS, 0, 4);
         // Clean up
        glBindSampler(i, 0);
         glBindTexture(GL_TEXTURE_2D, 0);
         glBindVertexArray(0);
         glUseProgram(0);
         
         Display.update();
         Display.sync(30);
      }
     
   }

}


Any help is appreciated!  Grin (EDIT: I added some comments to the code)

Hello!
Offline sproingie

JGO Kernel


Medals: 200



« Reply #1 - Posted 2012-04-27 02:08:25 »

You are not checking the compile, link, or validate status at any time.  You need to do that after each of those steps using glGetShader/glGetProgram, and print the appropriate log using glGetShaderInfoLog/glGetProgramInfoLog in the case of an error.
Offline DavidW

Junior Member


Medals: 3
Exp: 7 years



« Reply #2 - Posted 2012-04-27 03:07:18 »

Thank you for the tip!  I have added the following into my code:

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  
      int vShader = glCreateShader(GL_VERTEX_SHADER);
      glShaderSource(vShader, easyTexVert);
      glCompileShader(vShader);
     
      int success = glGetShader(vShader, GL_COMPILE_STATUS);      
      if(success != 1) {
         String s = glGetShaderInfoLog(vShader, 1024);
         System.out.println("Error Compiling Shader: "+s);
         System.exit(1);
      }
     
      int fShader = glCreateShader(GL_FRAGMENT_SHADER);
      glShaderSource(fShader, easyTexFrag);
      glCompileShader(fShader);
     
      success = glGetShader(fShader, GL_COMPILE_STATUS);      
      if(success != 1) {
         String s = glGetShaderInfoLog(fShader, 1024);
         System.out.println("Error Compiling Shader: "+s);
         System.exit(1);
      }
     
      int program = glCreateProgram();
      glAttachShader(program, vShader);
      glAttachShader(program, fShader);
      glLinkProgram(program);
      glValidateProgram(program);
     
      success = glGetProgram(program, GL_LINK_STATUS);      
      if(success != 1) {
         String s = glGetProgramInfoLog(program, 1024);
         System.out.println("Error Linking Program: "+s);
         System.exit(1);
      }


Not sure what else to do if there is an error, aside from just exit the program.

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

xsi3rr4x (74 views)
2014-04-15 18:08:23

BurntPizza (68 views)
2014-04-15 03:46:01

UprightPath (80 views)
2014-04-14 17:39:50

UprightPath (65 views)
2014-04-14 17:35:47

Porlus (81 views)
2014-04-14 15:48:38

tom_mai78101 (105 views)
2014-04-10 04:04:31

BurntPizza (165 views)
2014-04-08 23:06:04

tom_mai78101 (261 views)
2014-04-05 13:34:39

trollwarrior1 (210 views)
2014-04-04 12:06:45

CJLetsGame (220 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!