Hello, I am a student studying OpenGL alone. It's nothing but OpenGL right now I'm creating an object using an array of vertices, but I can't get those two sides colored like in the picture.
GLfloat vertecies[] = { -0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, -0.1f, -0.1f, 0.1f, -0.1f,
0.1f, -0.1f, 0.1f, 0.1f, 0.1f, 0.1f, -0.1f, 0.1f, 0.1f, -0.1f, -0.1f, 0.1f,
0.1f, 0.1f, -0.1f, 0.1f, 0.1f, 0.1f, 0.1f, -0.1f, 0.1f, 0.1f, -0.1f, -0.1f,
-0.1f, -0.1f, 0.1f,-0.1f, 0.1f, 0.1f, -0.1f, 0.1f, -0.1f, -0.1f, -0.1f, -0.1f,
0.1f, -0.1f, 0.1f, -0.1f, -0.1f, 0.1f, -0.1f, -0.1f, -0.1f, 0.1f, -0.1f, -0.1f,
0.1f, 0.1f, -0.1f, 0.1f, -0.1f, -0.1f, -0.1f, -0.1f, -0.1f, 0.1f, 0.1f, 0.1f }; // Block Information
GLfloat color[] = { 1,0,0,
1,1,0,
1,0,0,
0,1,0,
0,0,1,
1,0,1,
0,1,1,
0.5, 0.5, 0.5 }; //Color information
GLubyte index[] = { 0,1,2,3,
4,5,6,7,
8,9,10,11,
12,13,14,15,
16,17,18,19,
20,21,22,23}; // order of vertices
void Block()
{
glDrawElements(GL_QUADS,24, GL_UNSIGNED_BYTE, index);
}
void Draw()
{
glLoadIdentity();
glPushMatrix();
/*** Object conversion scheme ***/
//Assume that the object is already drawn at the origin of the global coordinate system and convert to the global coordinate system
glTranslatef(nx, 0.0, 0.0);
glTranslatef(0.0, ny, 0.0);
glTranslatef(0.0, 0.0, 0.0);
glRotatef(xAngle, 1.0f, 0.0f, 0.0f);
glRotatef(yAngle, 0.0f, 1.0f, 0.0f);
glRotatef(zAngle, 0.0f, 0.0f, 1.0f);
/********************/
Block(); // hexahedron
//Cylinder();
glPopMatrix();
}
void Display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the background with the color you specified
glPolygonMode(GL_FRONT_AND_BACK, GL_QUADS);
glVertexPointer(3, GL_FLOAT, 0, vertecies);
glColorPointer(3, GL_FLOAT, 0, color);
glShadeModel(GL_FLAT);
Draw();
glFlush();
// Run commands at once (immediately) by emptying the buffer
}
voidInit() // a function that initializes only once before entering the message loop on the main
{
glClearColor (1.0, 1.0, 1.0, 0.0); // Specify Background Color - Default Black
glEnable(GL_DEPTH_TEST);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
/*
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
// // glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light_direction);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_amibient);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
*/
}
void main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // Double Buffer | Default Color Mode Used by glut | Depth Buffer
glutInitWindowPosition (0, 0); // where the window opens when running
glutInitWindowSize (500, 500); // Window Workshop Size Area Settings, Window Size X
glutCreateWindow("Graphics Lego Project");
glutKeyboardFunc(Keyboard);
glutSpecialFunc(SpecialKey);
// // glutMotionFunc();
// // glutPassiveMotionFunc();
glutDisplayFunc(Display);
Init();
glutMainLoop();
}
As you can see from the sauce, I didn't put black in the color arrangement. And yet, two black noodles are being printed out, but I don't know what the problem is. I think the only problem that can be expected is the Block function, but I don't think there is a problem with my skills. What's wrong?
c c++ opengl
To use index It should be for the purpose of reusing vertex.
First of all, looking at the vertex composition, it's not reuse (four points that make up each side of the hexahedron) * I think you made up 24 of 6.
Once you've done that, if you're not going to change the vertex-index configuration, you have to make 24 colors as many as the vertex.
© 2024 OneMinuteCode. All rights reserved.