Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (499)
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  
  Vertex Array - beginner problem  (Read 1388 times)
0 Members and 1 Guest are viewing this topic.
Offline akzyl

Senior Newbie





« Posted 2012-02-01 09:19:23 »

Hello.
I wont to create cube using Vertex Array but unfortunately I have problem. The problem is that the only one wall appear with second (left wall) something bad happening.
I also want to add that I haven't any problem with creating cube using immediate mode.

OK, this is 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  
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  
package com.applet;

import static org.lwjgl.opengl.GL11.GL_COLOR_ARRAY;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST;
import static org.lwjgl.opengl.GL11.GL_LEQUAL;
import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
import static org.lwjgl.opengl.GL11.GL_PERSPECTIVE_CORRECTION_HINT;
import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.GL_VERTEX_ARRAY;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glClearDepth;
import static org.lwjgl.opengl.GL11.glColorPointer;
import static org.lwjgl.opengl.GL11.glDepthFunc;
import static org.lwjgl.opengl.GL11.glDisableClientState;
import static org.lwjgl.opengl.GL11.glDrawArrays;
import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL11.glEnableClientState;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glShadeModel;
import static org.lwjgl.opengl.GL11.glVertexPointer;
import static org.lwjgl.opengl.GL11.glViewport;
import static org.lwjgl.util.glu.GLU.gluPerspective;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.nio.FloatBuffer;

import javax.swing.JApplet;

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

import com.Conf;

public class CubeVA  extends JApplet {
   
   /**
    *
    */

   private static final long serialVersionUID = -730031275017948496L;
   private Canvas canvas;
   private Thread gameThread;
   boolean running = false;
   
   public void startLWJGL(){
      gameThread = new Thread (){
         
         public void run(){
            running = true;
            try {
               Display.setParent(canvas);
               Display.create();
               Display.setVSyncEnabled(true);
               initGL();
            } catch (LWJGLException e) {
               e.printStackTrace();
            }
            gameLoop();
         }

      };
      gameThread.start();
   }
   
   public void stopLWJGL(){
      running = false;
      try {
         gameThread.join();
      } catch (InterruptedException e) {
         e.printStackTrace();
      }
   }

   public void destroy(){
      remove(canvas);
      super.destroy();
   }
   
   public void init(){
      setLayout(new BorderLayout());
      canvas = new Canvas(){
         /**
          *
          */

         private static final long serialVersionUID = 5972822387678455353L;

         public final void addNotify(){
            super.addNotify();
            startLWJGL();
         }
         
         public final void removeNotify(){
            stopLWJGL();
            super.removeNotify();
         }
      };
      canvas.setSize(Conf.W, Conf.H);
      setSize(Conf.W, Conf.H);
      canvas.setBackground(Color.BLACK);
      canvas.setFocusable(true);
      canvas.requestFocus();
      canvas.setIgnoreRepaint(false);
      add(canvas);
   }
   
   private void initGL() {
      glViewport(0, 0,  Conf.W,Conf.H); // Reset The Current Viewport
     glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      gluPerspective(45.0f, ((float)Conf.W / (float)Conf.H) , 0.1f, 100f);
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
     
     
      glShadeModel(GL11.GL_SMOOTH); // Enables Smooth Shading
     GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
     glClearDepth(1.0f); // Depth Buffer Setup
     glEnable(GL_DEPTH_TEST); // Enables Depth Testing
     glDepthFunc(GL_LEQUAL); // The Type Of Depth Test To Do
     GL11.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST); // Really Nice Perspective Calculations
     
   }
   float rquad; // Angle For The Quad
  public void gameLoop(){
      final int amountOfVertices = 8;
        final int vertexSize = 3;
        final int colorSize = 3;
     
        FloatBuffer vertexData = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize);
        vertexData.put(new float[]{-0.5f, 0.5f, 0.0f, 0.5f, 0.5f, 0.0f, 0.5f, -0.5f, 0.0f, -0.5f, -0.5f, 0.0f, 0.5f, 0.5f, 0.0f, 0.5f, -0.5f, 0.0f, 0.5f, -0.5f, -1.0f, 0.5f, 0.5f, -1.0f});
        vertexData.flip();

        FloatBuffer colorData = BufferUtils.createFloatBuffer(amountOfVertices * colorSize);
        colorData.put(new float[]{1, 0.2f, 0.2f, 0.2f, 1, 0.2f, 0.2f, 0.2f, 1});
        colorData.flip();
       
      while(running) {
         update();
         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
         glLoadIdentity();//Matrix reseting.
        GL11.glTranslatef(0.0f, 0.0f, -6.0f); // Move Right And Into The Screen
        GL11.glRotatef(rquad, 0.0f, 1.0f, 0.5f); // Rotate The Cube On X, Y & Z
       
         glEnableClientState(GL_VERTEX_ARRAY);
            glEnableClientState(GL_COLOR_ARRAY);
         
            glVertexPointer(vertexSize, 0, vertexData);
            glColorPointer(colorSize, 0, colorData);
           
            glDrawArrays(GL_QUADS, 0, amountOfVertices);
           
            glDisableClientState(GL_COLOR_ARRAY);
            glDisableClientState(GL_VERTEX_ARRAY);
         
         Display.sync(60);
         Display.update();
      }
     
      Display.destroy();
   }
   
   private void update(){
      rquad -= 1.55f; // Decrease The Rotation Variable For The Quad
  }

}


Offline Waterwolf

Junior Member


Medals: 3



« Reply #1 - Posted 2012-02-01 09:30:38 »

You probably missed the vertex array..
Offline akzyl

Senior Newbie





« Reply #2 - Posted 2012-02-01 09:34:58 »

You probably missed the vertex array..
Ehh sorry I pasted code from wrong class.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #3 - Posted 2012-02-01 10:00:10 »

Why do you only have 3 RGB colors in your color buffer?

Myomyomyo.
Offline akzyl

Senior Newbie





« Reply #4 - Posted 2012-02-01 10:10:20 »

Why do you only have 3 RGB colors in your color buffer?
Ehhh, I should set white background  Wink
Thanks.
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 (30 views)
2014-04-15 18:08:23

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

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

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

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

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

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

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

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

CJLetsGame (198 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!