Results 1 to 3 of 3

Thread: Setting normals for a 3D Cube

  1. #1
    Newbie Newbie
    Join Date
    Jun 2015
    Posts
    2

    Setting normals for a 3D Cube

    Hello!
    So im a fresh beginner of opengl programming and im confronted with a simple problem.
    I have a cube with a texture. Now i want it to react to lighting. For that i have to give the cube some normals.

    Here are my cube functions:

    Code :
    GLuint VertexArrayIDSolidCube = 0;
     
    static void createCube()
    {
    	GLuint vertexbuffer;
    	GLuint normalbuffer;
     
    	glGenVertexArrays(1, &VertexArrayIDSolidCube);
    	glBindVertexArray(VertexArrayIDSolidCube);
     
    	// Our vertices. Tree consecutive floats give a 3D vertex; Three consecutive vertices give a triangle.
    	// A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices
    	static const GLfloat g_vertex_buffer_data[] = {
    		-1.0f,-1.0f,-1.0f, -1.0f,-1.0f, 1.0f, -1.0f, 1.0f, 1.0f,  // Left Side
     
    		-1.0f,-1.0f,-1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f,-1.0f,  // Left Side
    		 1.0f, 1.0f,-1.0f, -1.0f,-1.0f,-1.0f, -1.0f, 1.0f,-1.0f,  // Back Side
    		 1.0f,-1.0f, 1.0f, -1.0f,-1.0f,-1.0f,  1.0f,-1.0f,-1.0f,  // Bottom Side
    		 1.0f, 1.0f,-1.0f,  1.0f,-1.0f,-1.0f, -1.0f,-1.0f,-1.0f,  // Back Side
    		 1.0f,-1.0f, 1.0f, -1.0f,-1.0f, 1.0f, -1.0f,-1.0f,-1.0f,  // Bottom Side
    		-1.0f, 1.0f, 1.0f, -1.0f,-1.0f, 1.0f,  1.0f,-1.0f, 1.0f,  // Front Side
    		 1.0f, 1.0f, 1.0f,  1.0f,-1.0f,-1.0f,  1.0f, 1.0f,-1.0f,  // Right Side
    		 1.0f,-1.0f,-1.0f,  1.0f, 1.0f, 1.0f,  1.0f,-1.0f, 1.0f,  // Right Side
    		 1.0f, 1.0f, 1.0f,  1.0f, 1.0f,-1.0f, -1.0f, 1.0f,-1.0f,  // Top Side
    		 1.0f, 1.0f, 1.0f, -1.0f, 1.0f,-1.0f, -1.0f, 1.0f, 1.0f,  // Top Side
    		 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f,  1.0f,-1.0f, 1.0f   // Front Side
    	}; 
     
     
     
    	glGenBuffers(1, &vertexbuffer);
    	glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    	glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
     
     
     
    	static const GLfloat normals[] = { 
    		-1.0f, 0.0f, 0.0f, // Left Side
    		 0.0f, 0.0f, -1.0f, // Back Side
    		 0.0f,-1.0f, 0.0f, // Bottom Side
    		 0.0f, 0.0f, -1.0f, // Back Side
    		-1.0f, 0.0f, 0.0f, // Left Side
    		 0.0f, -1.0f, 0.0f, // Bottom Side
    		 0.0f, 0.0f, 1.0f, // front Side
    		 1.0f, 0.0f, 0.0f, // right Side
    		 1.0f, 0.0f, 0.0f, // right Side
    		 0.0f, 1.0f, 0.0f, // top Side
    		 0.0f, 1.0f, 0.0f, // top Side
    		 0.0f, 0.0f, 1.0f, // front Side
     
    	};
     
    	glGenBuffers(1, &normalbuffer);
    	glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
    	glBufferData(GL_ARRAY_BUFFER, sizeof(normals), normals, GL_STATIC_DRAW);
     
     
    	glEnableVertexAttribArray(0); // 
    	glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    	glVertexAttribPointer(
    			0,                  // attribute. No particular reason for 0, but must match the layout in the shader.
    			3,                  // size
    			GL_FLOAT,           // type
    			GL_FALSE,           // normalized?
    			0,                  // stride
    			(void*)0            // array buffer offset
    	);
     
    	// 2nd attribute buffer : normals
    	glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
    	glEnableVertexAttribArray(1); // 
    	glVertexAttribPointer(
    			1,                                // attribute. No particular reason for 1, but must match the layout in the shader.
    			3,                                // size
    			GL_FLOAT,                         // type
    			GL_FALSE,                         // normalized?
    			0,                                // stride
    			(void*)0                          // array buffer offset
    	);
     
    	glBindVertexArray(0);
    }
     
    void drawCube()
    {
    	if (!VertexArrayIDSolidCube)
    	{
    		createCube();
    	}
     
    	// Draw the triangles !
    	glBindVertexArray(VertexArrayIDSolidCube);
    	glDrawArrays(GL_TRIANGLES, 0, 12*3); // 12*3 indices starting at 0 -> 12 triangles
    }

    The result is, that it somewhat messes up my texture and it still doesnt react to lighting.
    Now when i try setting the attribute number of the normals from 1 to 2, my texture disappears, and only some triangles show light and definetly not how its supposed to be.

    Im assuming i have to set up some indices?
    But i have no clue how that works.

    I'd really appreciate any help.

    Greetings.

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,104
    For a flat-shaded cube, you need 24 distinct vertices (4 per face), each of which needs a position, a normal, and texture coordinates, i.e. each attribute array needs to contain 24 vectors (3-element vectors for position and normal, 2-element vectors for texture coordinates).

    If you're using triangles, you either need to duplicate the shared vertices, resulting in 36 vertices (3 per triangle, 6 per face), or use glDrawElements() with 36 indices (so you only need to duplicate the indices of shared vertices, rather than duplicating the attribute data).

    (It is actually possible to reduce the number of vertices, but that adds complexity, and you need to get the simple version working first).

    Also, I'd suggest that you don't hard-code attribute indices. Use glGetAttribLocation() even if the shader uses layout(location=) qualifiers on the attributes. This avoids having to chase down errors when you change the shader but not the program or vice-versa. If you need to force specific attribute locations (e.g. to ensure that multiple shaders use the same locations for the same attributes), define named constants and use glBindAttribLocation().

  3. #3
    Newbie Newbie
    Join Date
    Jun 2015
    Posts
    2
    Ah yeah. Honestly i figured it out like a second ago. For me it was logical, that you give a face a normal vector. After some trying i realized that you have to apply the normals to the verticles.

    I will also think about your other hints.

    Thanks for your help =)

    greetings

Similar Threads

  1. Mesh generation for Cube, Sphere etc with Vertices, UV's Normals, Tangent & Bitangent
    By imported_paul_g_griffiths in forum OpenGL: Basic Coding
    Replies: 9
    Last Post: 01-21-2017, 08:22 AM
  2. Setting normals on faces
    By Cucus in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 05-17-2011, 04:04 PM
  3. Problems with simple cube and normals
    By imported_icepic1984 in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 01-05-2011, 10:36 AM
  4. Setting to make lighting dismiss normals?
    By halo in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 07-24-2006, 02:32 AM
  5. Normals per-vertex for a cube
    By roguespear in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 11-23-2004, 08:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean