Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

1

24.08.2014, 14:21

Sphere (Kugel) wird nicht richtig gerendert - C++/SDL2/OpenGL ES 1

Hallo... :)

Ersteinmal nochmals ein dickes dankeschön an allen die mir im vorigen Thead geholfen haben

Ich habe mittlerweile verstanden , wie OpenGL Es mit Indices (Punkten) und allen arbeitet.

Nunmöchte nun eine Kugel erstellen...

ich bin im Internet auf eine "fertige" Kugel gestoßen und habe es geschafft diese auch zu rendern ...

https://gist.github.com/stuartjmoore/1076642

aber irgendwie wird diese nicht richtig dargestellt (sie ist zwar ganz aber die textur wird komisch dargestellt)


(Link)


Vollbild:

http://www.pic-upload.de/view-24364694/S…-49-01.png.html

und das ist der code, von mir:

-> Code für Android (arbeite mit C4droid)

C-/C++-Quelltext

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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_opengles.h>

static SDL_Window *window;
static SDL_GLContext context;
int move_me;

float posX = 0, posY = 0;
float rotX = 0,  rotY = 0;


static void AspectAdjust(int w, int h)
{
    float aspectAdjust;

    aspectAdjust = (4.0f / 3.0f) / ((float)w / h);
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrthof(-2.0, 2.0, -2.0 * aspectAdjust, 2.0 * aspectAdjust, -20.0, 20.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    glShadeModel(GL_SMOOTH);
}

// sphere function - from google search (copy-paste)
void renderSphere(float cx, float cy, float cz, float r, int p)
{
    float theta1 = 0.0, theta2 = 0.0, theta3 = 0.0;
    float ex = 0.0f, ey = 0.0f, ez = 0.0f;
    float px = 0.0f, py = 0.0f, pz = 0.0f;
    GLfloat vertices[p*6+6], normals[p*6+6], texCoords[p*4+4];
    
    if( r < 0 )
        r = -r;
    
    if( p < 0 )
        p = -p;
    
    for(int i = 0; i < p/2; ++i)
    {
        theta1 = i * (M_PI*2) / p - M_PI_2;
        theta2 = (i + 1) * (M_PI*2) / p - M_PI_2;
        
        for(int j = 0; j <= p; ++j)
        {
            theta3 = j * (M_PI*2) / p;
            
            ex = cosf(theta2) * cosf(theta3);
            ey = sinf(theta2);
            ez = cosf(theta2) * sinf(theta3);
            px = cx + r * ex;
            py = cy + r * ey;
            pz = cz + r * ez;
            
            vertices[(6*j)+(0%6)] = px;
            vertices[(6*j)+(1%6)] = py;
            vertices[(6*j)+(2%6)] = pz;
            
            normals[(6*j)+(0%6)] = ex;
            normals[(6*j)+(1%6)] = ey;
            normals[(6*j)+(2%6)] = ez;
            
            texCoords[(4*j)+(0%4)] = -(j/(float)p);
            texCoords[(4*j)+(1%4)] = 2*(i+1)/(float)p;
            
            
            ex = cosf(theta1) * cosf(theta3);
            ey = sinf(theta1);
            ez = cosf(theta1) * sinf(theta3);
            px = cx + r * ex;
            py = cy + r * ey;
            pz = cz + r * ez;
            
            vertices[(6*j)+(3%6)] = px;
            vertices[(6*j)+(4%6)] = py;
            vertices[(6*j)+(5%6)] = pz;
            
            normals[(6*j)+(3%6)] = ex;
            normals[(6*j)+(4%6)] = ey;
            normals[(6*j)+(5%6)] = ez;
            
            texCoords[(4*j)+(2%4)] = -(j/(float)p);
            texCoords[(4*j)+(3%4)] = 2*i/(float)p;
        }
        glVertexPointer(3, GL_FLOAT, 0, vertices);
        glNormalPointer(GL_FLOAT, 0, normals);
        glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
        glDrawArrays(GL_TRIANGLE_STRIP, 0, (p+1)*2);
    }
}

static void Render()
{
    static GLubyte color[] = {
    120, 120, 120, 0,
    };

    /* Do our drawing, too. */
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);
    glEnableClientState(GL_COLOR_ARRAY);
    glEnableClientState(GL_VERTEX_ARRAY);
    
    // draw the sphere
    renderSphere(0.0f, 0.0f, 0.0f, 0.5f, 50);

    glMatrixMode(GL_MODELVIEW);
    glRotatef(rotX, -0.5 ,1.0 ,0.0);
}



int main(int argc, char *argv[])
{
    int done;
    SDL_DisplayMode mode;
    SDL_Event event;
    
    if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
    {                   
        SDL_Log("Unable to initialize SDL");
        return 1;
    }

    SDL_GetDesktopDisplayMode(0, &mode);

    SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);

    // Create our window centered
    window = SDL_CreateWindow("GLES example", SDL_WINDOWPOS_CENTERED,
                      SDL_WINDOWPOS_CENTERED, mode.w, mode.h,
                      SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN |
                      SDL_WINDOW_FULLSCREEN);

    // Create our opengl context and attach it to our window
    context = SDL_GL_CreateContext(window);

    SDL_GL_MakeCurrent(window, context);
    SDL_GL_SetSwapInterval(1);

    AspectAdjust(mode.w, mode.h);

     bool run = true;
                
         
    while (run)
    {
        /* Check for events */
        while (SDL_PollEvent(&event))
        {   
        /*
         if (event.type == SDL_MOUSEMOTION)
          {
           posX  = event.motion.x ;
          }
       */           
        switch (event.type)
          {
           case SDL_QUIT:
           run = false;
           break;
            
            // touch the screen , rotate the object
              case SDL_MOUSEBUTTONDOWN: 
            {
                rotX = 0.5;
              }
              break;
       
             case SDL_MOUSEBUTTONUP: 
             {
              rotX = 0;
             }  
            break;
           //--------------------------------------
           
            case SDL_KEYDOWN:  // exit
             if (event.key.keysym.scancode == SDL_SCANCODE_AC_BACK)
             {
              done = false;
             }
            break;


            case SDL_WINDOWEVENT:
                switch (event.window.event)
                {
                case SDL_WINDOWEVENT_RESIZED:
                    /* Change view port to the new window dimensions */
                    AspectAdjust(event.window.data1, event.window.data2);
                    /* Update window content */                                  
                    Render();
                    SDL_GL_SwapWindow(window);
                    break;
                }
            }
        }
        
        // Main loop    
            
        Render();

        SDL_RenderPresent(renderer);
        SDL_GL_SwapWindow(window);
    }


    SDL_GL_DeleteContext(context);
    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}


was mache ich falsch ?

und wie kann ich meinen touch screen sagen , dass er mal in die andere richtung drehen soll ?

C-/C++-Quelltext

1
2
3
4
5
6
/*
 if (event.type == SDL_MOUSEMOTION)
 {
  posX  = event.motion.x ;
  }
*/


so dreht sich die kugel mit einen Affen-Zahn um sich selbst :D .. und nur in eine richtung , trotz event.motion :(
desswegen laüft das noch mit MOUSEBUTTON_DOWN und UP

2

24.08.2014, 16:19

Du baust ja zwei Probleme in den Ihnen Thread ein..

Erstmal zur darstellung:
Ich habe mich in den leider sehr verwirrenden Quelltext der renderSphere Methode noch nicht ganz rein gefunden, um zu schauen, ob das überhaupt so gehen kann. Was mir spontan auffällt, ist, dass float variablen mit Double Werten multipliziert werden. Das kann zu Datenverlust, bzw. Unerwarteten Ergebnissen führen. (M_PI ist ein Double wert)

Interessant wäre mal die Textur, die du versuchst, dort drauf zu legen. Wie sieht die aus?
Ich finde auch nirgendwo einen Punkt, wo die geladen werden soll. Derzeit verwendest du einen color buffer (glColorPointer)
EnvisionGame(); EnableGame(); AchieveGame(); - Visionen kann man viele haben. Sie umzusetzen und auf das Ergebnis stolz zu sein ist die eigentliche Kunst.

Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von »iSmokiieZz« (24.08.2014, 16:27)


3

25.08.2014, 19:14

Ja - ich hab rausgefunden, dass es nicht an der Kugel liegt,
sondern nur am ColorArray denn, so lässt sich die Kugel
tadellos Rendern:


http://www.pic-upload.de/view-24380800/S…-08-21.png.html

-> ich hab jetzt mal einfach mal GL_LINE_LOOP genommen und

C-/C++-Quelltext

1
2
3
    //glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);
    //glEnableClientState(GL_COLOR_ARRAY);
    


"gekillt"...

-> die kugel funktioniert anscheind so: RenderSphere(x,y,z,durchmesser,segmente)

Werbeanzeige