opengl szescian tablice wierzchołków

0

witam, zrobiłem sześcian z wykorzystaniem tablic wierzchołków. sześcian się rysuje ale niektóre ściany są wklęsłe środkiem. proszę o przejrzenie kodu i o uwagi. pozdrawiam

/* 
 * File:   main.c
 * Author: marcin
 *
 * Created on 13 październik 2012, 16:29
 */

#include <stdio.h>
#include <stdlib.h>
#include <GL/freeglut.h>
#include <math.h>

/*
 * 
 */
const float delta_alfa = 0.05;
float alfa = 0.0;
float a = 0.725;
int widthWindow = 600;
int heightWindow = 800;
float r = 1.0;
float fovy = 45.0, aspect = 1.0, near_ = 0.1, far_ = 10.0;//parametry dla gluPerspective()

void ukladWspolrzednych(){
    glBegin(GL_LINES);
      //linia x rozowa
      glColor3f(1.0, 0.0, 1.0);
      glVertex3f(5.0, 0.0, 0.0);
      glVertex3f(-5.0, 0.0, 0.0);
      //linia y zielona
      glColor3f(0.0, 1.0, 1.0);
      glVertex3f(0.0, 5.0, 0.0);
      glVertex3f(0.0, -5.0, 0.0);
      //linia z zolta
      glColor3f(1.0, 1.0, 0.0);
      glVertex3f(0.0, 0.0, 5.0);
      glVertex3f(0.0, 0.0, -5.0);
    glEnd();
}

void szescian(){
    static float wierzcholki[] = {
        0.0,    0.0,    0.0,//0 0 0 0
       0.75,    0.0,    0.0,//1 a 0 0
       0.75,    0.0,   0.75,//2 a 0 a
        0.0,    0.0,   0.75,//3 0 0 a
        0.0,   0.75,    0.0,//4 0 a 0
       0.75,   0.75,    0.0,//5 a a 0
       0.75,   0.75,   0.75,//6 a a a
        0.0,   0.75,   0.75 //7 0 a a
    };
    float kolory[] = {
        1.0, 0.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 0.0, 1.0,
        1.0, 1.0, 0.0,
        1.0, 0.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 0.0, 1.0,
        1.0, 1.0, 0.0
    }; 
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, wierzcholki);
    glColorPointer(3, GL_FLOAT, 0, kolory);
    
    static unsigned short dolnaSciana[] = {0, 1, 2, 3};
    static unsigned short gornaSciana[] = {4, 5, 6, 7};
    static unsigned short tylnaSciana[] = {0, 1, 4, 5};
    static unsigned short przodSciana[] = {2, 3, 6, 7};
    static unsigned short  lewaSciana[] = {0, 3, 4, 7};
    static unsigned short prawaSciana[] = {1, 2, 5, 6};
    
    glDrawElements(GL_QUADS, 4, GL_UNSIGNED_SHORT, dolnaSciana);
    glDrawElements(GL_QUADS, 4, GL_UNSIGNED_SHORT, gornaSciana);
    glDrawElements(GL_QUADS, 4, GL_UNSIGNED_SHORT, tylnaSciana);
    glDrawElements(GL_QUADS, 4, GL_UNSIGNED_SHORT, przodSciana);
    glDrawElements(GL_QUADS, 4, GL_UNSIGNED_SHORT, lewaSciana);
    glDrawElements(GL_QUADS, 4, GL_UNSIGNED_SHORT, prawaSciana);
}

void anim(){
    alfa += delta_alfa;
    glutPostRedisplay();
}

void init(){
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glEnable(GL_DEPTH_TEST);
}

void odrysuj(int szerokosc, int wysokosc){
    float h = wysokosc;
    float w = szerokosc;
    glViewport(0, 0, szerokosc, wysokosc);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(fovy, w/h, near_, far_);
    glMatrixMode(GL_MODELVIEW);
}

void scena(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    gluLookAt(2.0, 2.0, 2.0,  0.0, 0.0, 0.0,  0.0, 0.1, 0.0);
    ukladWspolrzednych();
    glRotatef(alfa, 0.0, 1.0, 0.0);
    //glTranslatef(r, 0.0, 0.0);
    glTranslatef(-a / 2, 0.0, -a / 2);
      szescian();
    glFlush();
    glutSwapBuffers();
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);  
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
    glutInitWindowSize(heightWindow, widthWindow);
    glutInitWindowPosition(100,100);
    glutCreateWindow("Scena testowa");
    glutDisplayFunc(scena);
    glutReshapeFunc(odrysuj);
    glutIdleFunc(anim);
    init();
    glutMainLoop();
    return (EXIT_SUCCESS);
}

 
0

problemem była zła kolejność wierzchołków, wstawiam kod dla zainteresowanych, pozdrawiam.

/* 
 * File:   main.c
 * Author: marcin
 *
 * Created on 13 październik 2012, 16:29
 */

#include <stdio.h>
#include <stdlib.h>
#include <GL/freeglut.h>
#include <math.h>

/*
 * 
 */
const float delta_alfa = 0.05;
float alfa = 0.0;
float a = 0.725;
int widthWindow = 600;
int heightWindow = 800;
float r = 1.0;
float fovy = 45.0, aspect = 1.0, near_ = 0.1, far_ = 10.0;//parametry dla gluPerspective()

void ukladWspolrzednych(){
    glBegin(GL_LINES);
      //linia x rozowa
      glColor3f(1.0, 0.0, 1.0);
      glVertex3f(5.0, 0.0, 0.0);
      glVertex3f(-5.0, 0.0, 0.0);
      //linia y zielona
      glColor3f(0.0, 1.0, 1.0);
      glVertex3f(0.0, 5.0, 0.0);
      glVertex3f(0.0, -5.0, 0.0);
      //linia z zolta
      glColor3f(1.0, 1.0, 0.0);
      glVertex3f(0.0, 0.0, 5.0);
      glVertex3f(0.0, 0.0, -5.0);
    glEnd();
}

void szescian(){
    static float wierzcholki[] = {
        0.0,    0.0,    0.0,//0 0 0 0
       0.75,    0.0,    0.0,//1 a 0 0
       0.75,    0.0,   0.75,//2 a 0 a
        0.0,    0.0,   0.75,//3 0 0 a
        0.0,   0.75,    0.0,//4 0 a 0
       0.75,   0.75,    0.0,//5 a a 0
       0.75,   0.75,   0.75,//6 a a a
        0.0,   0.75,   0.75 //7 0 a a
    };
    float kolory[] = {
        1.0, 0.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 0.0, 1.0,
        1.0, 1.0, 0.0,
        1.0, 0.0, 0.0,
        0.0, 1.0, 0.0,
        0.0, 0.0, 1.0,
        1.0, 1.0, 0.0
    }; 
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, wierzcholki);
    glColorPointer(3, GL_FLOAT, 0, kolory);
    
    static unsigned short dolnaSciana[] = {0, 1, 2, 3};
    static unsigned short gornaSciana[] = {4, 5, 6, 7};
    static unsigned short tylnaSciana[] = {0, 4, 7, 3};
    static unsigned short przodSciana[] = {1, 2, 6, 5};
    static unsigned short  lewaSciana[] = {0, 1, 5, 4};
    static unsigned short prawaSciana[] = {2, 3, 7, 6};
    
    glDrawElements(GL_QUADS, 4, GL_UNSIGNED_SHORT, dolnaSciana);
    glDrawElements(GL_QUADS, 4, GL_UNSIGNED_SHORT, gornaSciana);
    glDrawElements(GL_QUADS, 4, GL_UNSIGNED_SHORT, tylnaSciana);
    glDrawElements(GL_QUADS, 4, GL_UNSIGNED_SHORT, przodSciana);
    glDrawElements(GL_QUADS, 4, GL_UNSIGNED_SHORT, lewaSciana);
    glDrawElements(GL_QUADS, 4, GL_UNSIGNED_SHORT, prawaSciana);
}

void anim(){
    alfa += delta_alfa;
    glutPostRedisplay();
}

void init(){
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glEnable(GL_DEPTH_TEST);
}

void odrysuj(int szerokosc, int wysokosc){
    float h = wysokosc;
    float w = szerokosc;
    glViewport(0, 0, szerokosc, wysokosc);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(fovy, w/h, near_, far_);
    glMatrixMode(GL_MODELVIEW);
}

void scena(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    gluLookAt(2.0, 2.0, 2.0,  0.0, 0.0, 0.0,  0.0, 0.1, 0.0);
    ukladWspolrzednych();
    glRotatef(alfa, 0.0, 1.0, 0.0);
    //glTranslatef(r, 0.0, 0.0);
    glTranslatef(-a / 2, 0.0, -a / 2);
      szescian();
    glFlush();
    glutSwapBuffers();
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);  
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
    glutInitWindowSize(heightWindow, widthWindow);
    glutInitWindowPosition(100,100);
    glutCreateWindow("Scena testowa");
    glutDisplayFunc(scena);
    glutReshapeFunc(odrysuj);
    glutIdleFunc(anim);
    init();
    glutMainLoop();
    return (EXIT_SUCCESS);
}

 
0
    glDrawElements(GL_QUADS, 4, GL_UNSIGNED_SHORT, dolnaSciana);
    glDrawElements(GL_QUADS, 4, GL_UNSIGNED_SHORT, gornaSciana);
    glDrawElements(GL_QUADS, 4, GL_UNSIGNED_SHORT, tylnaSciana);
    glDrawElements(GL_QUADS, 4, GL_UNSIGNED_SHORT, przodSciana);
    glDrawElements(GL_QUADS, 4, GL_UNSIGNED_SHORT, lewaSciana);
    glDrawElements(GL_QUADS, 4, GL_UNSIGNED_SHORT, prawaSciana);

Właściwie to nie musisz tego rozbijać na sześć wywołań…

0

możesz napisać coś więcej??

0

możesz napisać jak mogę to prościej zrobić??

0

Wszystkie ściany w jednej tablicy.

0

jak taki zapis powinien wyglądać??

0

Prawdopodobnie mniej więcej tak:

static unsigned short sciany[] = {0, 1, 2, 3, 4, 5, 6, 7, 0, 4, 7, 3, 1, 2, 6, 5, 0, 1, 5, 4, 2, 3, 7, 6};
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_SHORT, sciany);
0

dziękuje, przyjże się temu bliżej ;)

1 użytkowników online, w tym zalogowanych: 0, gości: 1