Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (116)
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  
  Rendering 2d textures onto a 3d object Lwjgl  (Read 1526 times)
0 Members and 1 Guest are viewing this topic.
Offline lcass
« Posted 2014-01-04 13:59:05 »

Im attempting to draw a 2d texture onto a 3d cube. 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  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
public class test1 extends Game {
   // VBO Vertex Buffer ID
   int vboVertexID;
    // VBO Color Buffer ID
   int vboColorID;
   
    /**
     * Initialize
     */

    private texture te;
    public void init()
    {
        Display.setTitle("Tutorial 11: A Rotating Cube");
       
        // Initialize OpenGL
       glMatrixMode(GL_PROJECTION);
        gluPerspective(70f, Display.getWidth()/Display.getHeight(), 1, 1000);
        glViewport(0, 0, Display.getWidth(), Display.getHeight());
       
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
       
        // Enable Depth Testing
       glEnable(GL_DEPTH_TEST);
       
        // Enable client states
       glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_COLOR_ARRAY);
        glEnable(GL_TEXTURE_2D);
        // Create Cube vertices
       FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(3*24);
        vertexBuffer.put(new float[]
        {
            // Front face
           -0.5f, +0.5f, +0.5f,
            +0.5f, +0.5f, +0.5f,
            -0.5f, -0.5f, +0.5f,
            +0.5f, -0.5f, +0.5f,

            // Right face
           +0.5f, +0.5f, +0.5f,
            +0.5f, +0.5f, -0.5f,
            +0.5f, -0.5f, +0.5f,
            +0.5f, -0.5f, -0.5f,
           
            // Back face
           +0.5f, +0.5f, -0.5f,
            -0.5f, +0.5f, -0.5f,
            +0.5f, -0.5f, -0.5f,
            -0.5f, -0.5f, -0.5f,
           
            // Left face
           -0.5f, +0.5f, -0.5f,
            -0.5f, +0.5f, +0.5f,
            -0.5f, -0.5f, -0.5f,
            -0.5f, -0.5f, +0.5f,
           
            // Top face
           -0.5f, +0.5f, +0.5f,
            +0.5f, +0.5f, +0.5f,
            -0.5f, +0.5f, -0.5f,
            +0.5f, +0.5f, -0.5f,
           
            // Bottom face
           -0.5f, -0.5f, +0.5f,
            +0.5f, -0.5f, +0.5f,
            -0.5f, -0.5f, -0.5f,
            +0.5f, -0.5f, -0.5f,
        });
        vertexBuffer.rewind();
       
        // Create cube colors
       te = texture.loadtexture("graphics/test].png");
        FloatBuffer colorBuffer = BufferUtils.createFloatBuffer(2*24);
        colorBuffer.put(new float[]
        {
              0,0,
              1,0,
              1,1,
              0,1,
              0,0,
              1,0,
              1,1,
              0,1,
              0,0,
              1,0,
              1,1,
              0,1,
              0,0,
              1,0,
              1,1,
              0,1,
              0,0,
              1,0,
              1,1,
              0,1,
              0,0,
              1,0,
              1,1,
              0,1
             
             
           
        });
        colorBuffer.rewind();
       
        // Create vertex VBO
       vboVertexID = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, vboVertexID);
        glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_STATIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
       
        // Create color VBO
       vboColorID = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, vboColorID);
        glBufferData(GL_ARRAY_BUFFER, colorBuffer, GL_STATIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
    }
   
    /**
     * Update logic
     */

    public void update(long elapsedTime)
    {
        if (isKeyDown(KEY_ESCAPE))
            end();
    }
   
    /**
     * Render to screen
     */

    public void render()
    {
        // Clean both color and depth buffers
       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
       
        // Translate into the view
       glTranslatef(0, 0, -2);
       
        // Rotate on both x and y axes
       glRotatef(1, 1, 1, 1);
        glBindTexture(GL_TEXTURE_2D
, te.id);
        // Bind the vertex VBO
       glBindBuffer(GL_ARRAY_BUFFER, vboVertexID);
        glVertexPointer(3, GL_FLOAT, 0, 0);
       
        // Bind the color VBO
       glBindBuffer(GL_ARRAY_BUFFER, vboColorID);
        glTexCoordPointer(2, GL_FLOAT, 0, 0);
       
        // Draw the cube with triangle strip
       glDrawArrays(GL_TRIANGLE_STRIP, 0, 24);
       
        // Translate back
       glTranslatef(0, 0, 2);
    }
   
    /**
     * Display resized
     */

    public void resized()
    {
        glViewport(0, 0, Display.getWidth(), Display.getHeight());
    }
   
    /**
     * Dispose resources
     */

    public void dispose()
    {
        glDeleteBuffers(vboVertexID);
        glDeleteBuffers(vboColorID);
    }
   
    public static void main(String[] args)
    {
        new test1();
    }
}

what im trying to do is map the texture to each face within the floatbuffer , however I get an error when I try to run it Selected DisplayMode: 800 x 600 x 0 @0Hz
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000000005327c300, pid=6608, tid=6256
#
# JRE version: Java(TM) SE Runtime Environment (7.0_45-b18) (build 1.7.0_45-b18)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (24.45-b08 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [nvoglv64.DLL+0x98c300]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\Users\lucas\workspace\LWJGL test\hs_err_pid6608.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.sun.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

if there is a better way to do this it would be appreciated.
Offline Drenius
« Reply #1 - Posted 2014-01-04 14:36:52 »

So you are using your color buffer for the texture coord pointer?? Personally not used to VBO's, but I believe you will have to use three buffers, the color buffer with glColorPointer and an additional texCoord buffer with glTexCoordPointer...
btw:
Does the texture path "graphics/test].png" really contain ] ?
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #2 - Posted 2014-01-04 14:53:42 »

He doesn't need all three, just as long as he makes sure he uses them correctly. The texture path (good catch) wouldn't crash the runtime, but you should see if that's correct or not.

Maybe part of your issue is that you're enabling client states so early? Enable them right before you render, and then disable them immediately after.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Drenius
« Reply #3 - Posted 2014-01-04 15:09:08 »

Just noticed that the color buffer is used correctly, but is no color but the text coord buffer...
Maybe it would help readability to change this.
Offline lcass
« Reply #4 - Posted 2014-01-04 15:36:36 »

its ok I fixed it I just need to get used to placing the textures on the image.
Offline Drenius
« Reply #5 - Posted 2014-01-04 16:22:19 »

If you use an vertex and an texture coord array and nothing for color, why are you enabling GL_COLOR_ARRAY and not GL_TEXTURE_COORD_ARRAY?
You are really messing around with texCoord and color.
Offline lcass
« Reply #6 - Posted 2014-01-04 17:38:23 »

Very new to it and im playing around with it abit.
Offline Drenius
« Reply #7 - Posted 2014-01-04 17:43:48 »

Ok, but it won't work if you use Textures without enabling them.
It is also useless to enable something (color) you do not use.
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.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (30 views)
2014-09-21 02:42:18

BurntPizza (20 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (28 views)
2014-09-20 20:14:06

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

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

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

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

Tekkerue (50 views)
2014-09-09 02:24:56
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!