Do not render anything (think its bcs of shaders) - opengl

My opengl program renders nothing. I think the problem is coming from my shaders. Can someone tell me what's wrong, or if there is nothing wrong, tell me from where the problem could come from?
Fragment shader:
#version 330 core
in vec2 TexCoords;
out vec4 color;
uniform sampler2D image;
uniform vec3 spriteColor;
void main()
{
color = vec4(spriteColor, 1.0) * texture(image, TexCoords);
}
Vertex Shader
#version 330 core
layout (location = 0) in vec3 vertex;
layout (location = 2) in vec2 text;
out vec2 TexCoords;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
TexCoords = text;
gl_Position = projection * view * model * vec4(vertex, 1.0);
}
The function I use to sent data to shaders :
void SpriteRenderer::initRenderData()
{
GLfloat taille = 1.0f;
taille /= 2;
int m_tailleCoordTextureBytes = 72 * sizeof(float);
int m_tailleVerticesBytes = 108 * sizeof(float);
GLuint VBO;
GLfloat verticesTmp[] = {-taille, -taille, -taille, taille, -taille, -taille, taille, taille, -taille, // Face 1
-taille, -taille, -taille, -taille, taille, -taille, taille, taille, -taille, // Face 1
taille, -taille, taille, taille, -taille, -taille, taille, taille, -taille, // Face 2
taille, -taille, taille, taille, taille, taille, taille, taille, -taille, // Face 2
-taille, -taille, taille, taille, -taille, taille, taille, -taille, -taille, // Face 3
-taille, -taille, taille, -taille, -taille, -taille, taille, -taille, -taille, // Face 3
-taille, -taille, taille, taille, -taille, taille, taille, taille, taille, // Face 4
-taille, -taille, taille, -taille, taille, taille, taille, taille, taille, // Face 4
-taille, -taille, -taille, -taille, -taille, taille, -taille, taille, taille, // Face 5
-taille, -taille, -taille, -taille, taille, -taille, -taille, taille, taille, // Face 5
-taille, taille, taille, taille, taille, taille, taille, taille, -taille, // Face 6
-taille, taille, taille, -taille, taille, -taille, taille, taille, -taille}; // Face 6
GLfloat coordtextures[] = {0, 0, 1, 0, 1, 1, // Face 1
0, 0, 0, 1, 1, 1, // Face 1
0, 0, 1, 0, 1, 1, // Face 2
0, 0, 0, 1, 1, 1, // Face 2
0, 0, 1, 0, 1, 1, // Face 3
0, 0, 0, 1, 1, 1, // Face 3
0, 0, 1, 0, 1, 1, // Face 4
0, 0, 0, 1, 1, 1, // Face 4
0, 0, 1, 0, 1, 1, // Face 5
0, 0, 0, 1, 1, 1, // Face 5
0, 0, 1, 0, 1, 1, // Face 6
0, 0, 0, 1, 1, 1}; // Face 6
for (int i = 0; i < 72; i++)
{
m_coord_texture[i] = coordtextures[i];
}
for (int j = 0; j < 108; j++)
{
m_vertices[j] = verticesTmp[j];
}
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(m_vertices)+sizeof(m_coord_texture), 0, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, m_tailleVerticesBytes, m_vertices);
glBufferSubData(GL_ARRAY_BUFFER, m_tailleVerticesBytes, m_tailleCoordTextureBytes, m_coord_texture);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenVertexArrays(1, &this->quadVAO);
glBindVertexArray(this->quadVAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
// Accès aux vertices
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), BUFFER_OFFSET(0));
// Accès aux coord des textures
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), BUFFER_OFFSET(m_tailleVerticesBytes));
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);

From the data you have, it looks as if the second vertex pointer is wrong:
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat),
BUFFER_OFFSET(m_tailleVerticesBytes));
The offset between uv-coordinates is set to 3 floats, but since you only provide two of them, it should be 2 * sizeof(GLfloat).

Related

Drawing a plane with vertex buffer object

I'm trying to draw a square plane of 10 x 10 using the VBO on OpenGL. So I have a function getPlaneCoords that calculate the vertices and the indexes however the program just ends up crashing when I run it.
fragment shader:
#version 330 core
out vec4 color;
void main()
{
color = vec4(0.5, 0.2, 0.3, 1.0);
}
vertex shader:
#version 330 core
layout (location = 0) in vec3 position;
out vec3 FragPos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
gl_Position = projection * view * model * vec4(position, 1.0f);
FragPos = vec3(model * vec4(position, 1.0f));
}
Main.cpp:
constexpr GLuint dimensions{ 10 };
int numberOfPoints{ 0 };
int numberOfIndexes{ 0 };
float vertices[dimensions * dimensions * 3];
unsigned int indices[(dimensions - 1) * (dimensions - 1) * 6];
void getPlaneCoords()
{
int half = dimensions / 2;
for (int i{ 0 }; i < dimensions; ++i)
{
for (int j{ 0 }; j < dimensions; ++j)
{
float x = j - half;
float y = 0;
float z = i - half;
vertices[numberOfPoints++] = x;
vertices[numberOfPoints++] = y;
vertices[numberOfPoints++] = z;
}
}
for (int row{ 0 }; row < dimensions - 1; ++row)
{
for (int col{ 0 }; col < dimensions - 1; ++col)
{
indices[numberOfIndexes++] = dimensions * row + col;
indices[numberOfIndexes++] = dimensions * row + col + dimensions;
indices[numberOfIndexes++] = dimensions * row + col + dimensions + 1;
indices[numberOfIndexes++] = dimensions * row + col;
indices[numberOfIndexes++] = dimensions * row + col + dimensions + 1;
indices[numberOfIndexes++] = dimensions * row + col + 1;
}
}
}
int main()
{
glViewport(0, 0, WIDTH, HEIGHT);
// OpenGL options
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
getPlaneCoords();
// Build and compile our shader program
Shader lightingShader("lighting.vs", "lighting.frag");
GLuint squareVAO, VBO, EBO;
glGenVertexArrays(1, &squareVAO);
glBindVertexArray(squareVAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, numberOfPoints * sizeof(vertices), vertices, GL_STATIC_DRAW);
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, numberOfIndexes * sizeof(GLuint), indices, GL_STATIC_DRAW);
GLint PosAttrib{ glGetAttribLocation(lightingShader.Program, "position") };
glVertexAttribPointer(PosAttrib, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0); // Note that we skip over the normal vectors
glEnableVertexAttribArray(PosAttrib);
glBindVertexArray(0);
glm::mat4 projection = glm::perspective(camera.GetZoom(), (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 1000.0f);
while(window.running)
{
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
lightingShader.Use();
glm::mat4 view;
view = camera.GetViewMatrix();
// Get the uniform locations
GLint modelLoc = glGetUniformLocation(lightingShader.Program, "model");
GLint viewLoc = glGetUniformLocation(lightingShader.Program, "view");
GLint projLoc = glGetUniformLocation(lightingShader.Program, "projection");
// Pass the matrices to the shader
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
glBindVertexArray(squareVAO);
glm::mat4 model = glm::mat4();
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glDrawElements(GL_TRIANGLES, numberOfIndexes, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
window.display();
}
// Missing important display code but that is related to SFML. The code shared is primarily the OpenGL methods
}
It's meant to be a 10 x 10 plane with the color rgba ratio (0.5, 0.2, 0.3, 1.0);
I'm not sure as to where I went wrong?

OpenGL unwanted triangle is appearing with texturing a square composed with 2 triangles

I use SFML/OpenGl
When I'm texturing my square, (which is composed by 2 triangles), with an image, 1 unwanted triangle is appearing:
The error is the little brown triangle on the middle of the screen.
This is my code:
Drawing function:
glUseProgram(shader->getProgramID());
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vertices);
glEnableVertexAttribArray(0);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, coordTexture);
glEnableVertexAttribArray(2);
glUniformMatrix4fv(glGetUniformLocation(shader->getProgramID(), "projection"), 1, GL_FALSE, value_ptr(projection));
glUniformMatrix4fv(glGetUniformLocation(shader->getProgramID(), "modelview"), 1, GL_FALSE, value_ptr(modelview));
sf::Texture::bind(&textTest, sf::Texture::Normalized);
glDrawArrays(GL_TRIANGLES, 0, 9);
glDisableVertexAttribArray(2);
glDisableVertexAttribArray(0);
sf::Texture::bind(NULL);
glUseProgram(0);
Parameters:
shader = new Shader(vertexSource, fragmentSource);
float t = taille/2;
float points[] = { -t,-2.0,-t, -t,-2.0,t, t,-2.0,-t,
t,-2.0,-t, t,-2.0,t, -t,-2.0,t};
float text[] = {
0.0,0.0, 0.0,1.0, 1.0,0.0,
1.0,0.0, 1.0,1.0, 0.0,1.0,
};
for (int i = 0; i < 6*3; i++)
{
vertices[i] = points[i];
this->couleurs[i] = 1.0;
}
for (int i = 0; i < 6 * 2; i++)
{
this->coordTexture[i] = text[i];
}
shader->charger();
textTest.loadFromFile("lama.jpg");
And it's strange, because when I change:
float text[] = {
0.0,0.0, 0.0,1.0, 1.0,0.0,
1.0,0.0, 1.0,1.0, 0.0,1.0,
};
to (switch coordinates) :
float text[] = {
1.0,0.0, 1.0,1.0, 0.0,1.0,
0.0,0.0, 0.0,1.0, 1.0,0.0,
};
The unwanted triangle disappear, but of course the result for the texturing is not the good.

Texture not showing up in OpenGL ES 2.0

Currently working in OpenGL ES 2.0 in native on iOS. For some reason, my texture quad won't render, it just won't appear. I try to load in a .bmp file for the texture, and I do know that it goes through, because the log function I put in PolygonRenderer.addTexture prints out "Image size: 256, 256", which is the size of my texture.
Edit: My .bmp texture is saved in R8, G8, B8 form.
VertexShader:
std::string textureVertex =
"attribute vec3 vertexloc; \n"
"attribute vec3 vertexcol; \n"
"attribute vec2 vertexuv; \n"
"varying vec2 TexCoords; \n"
"varying vec3 textColor; \n"
"uniform mat4 projection; \n"
"uniform mat4 view; \n"
"uniform mat4 offset; \n"
"void main() \n"
"{ \n"
" gl_Position = projection * view * offset * vec4(vertexloc, 1.0); \n"
" TexCoords = vertexuv; \n"
" textColor = vertexcol; \n"
"}";
Fragment Shader:
std::string textureFragment =
"precision mediump float; \n"
"varying vec2 TexCoords; \n"
"varying vec3 textColor; \n"
"uniform sampler2D text; \n"
"void main() \n"
"{ \n"
" vec4 sampled = vec4(1.0, 1.0, 1.0, texture2D(text, TexCoords).r); \n"
" gl_FragColor = vec4(textColor, 1.0) * sampled; \n"
"}";
Vertices:
x = sWindowWidth / 2 - 1000 / 2;
y = - sWindowHeight / 2 - 172 / 2;
w = 1000;
h = 172;
temp = {
x, y + h, 0.3,
textureColor.x, textureColor.y, textureColor.z,
0, 1,
x, y, 0.3,
textureColor.x, textureColor.y, textureColor.z,
0, 0,
x + w, y, 0.3,
textureColor.x, textureColor.y, textureColor.z,
1, 0,
x, y + h, 0.3,
textureColor.x, textureColor.y, textureColor.z,
0, 1,
x + w, y, 0.3,
textureColor.x, textureColor.y, textureColor.z,
1, 0,
x + w, y + h, 0.3,
textureColor.x, textureColor.y, textureColor.z,
1, 1
};
polygonRenderer.addLoadingPolygon(temp);
PolygonRenderer excerpts:
AddLoadingPolygon:
void PolygonRenderer::addLoadingPolygon(std::vector<GLfloat> vertices) {
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat),
vertices.data(), GL_STATIC_DRAW);
if(vertexBuffer == 0){
log("gl vertexBuffer not generated");
}
loadingPolygons.push_back(PolygonRenderObject{
vertexBuffer,
(long)vertices.size() / 8,
0
});
}
AddTexture:
void PolygonRenderer::addTexture(const char* imagePath) {
// Data read from the header of the BMP file
unsigned char header[54]; // Each BMP file begins by a 54-bytes header
unsigned int dataPos; // Position in the file where the actual data begins
unsigned int width, height;
unsigned int imageSize; // = width*height*3
// Actual RGB data
unsigned char * data;
// Open the file
FILE * file = fopen(imagePath,"rb");
if (!file) {
log("Image could not be opened\n");
return;
}
if (fread(header, 1, 54, file) != 54) {
log("Not a correct BMP file");
return;
}
if (header[0] != 'B' || header[1] != 'M') {
log("Not a correct BMP file");
return;
}
// Read ints from the byte array
dataPos = *(int*)&(header[0x0A]);
imageSize = *(int*)&(header[0x22]);
width = *(int*)&(header[0x12]);
height = *(int*)&(header[0x16]);
if (imageSize == 0) {
imageSize = width * height;
}
log("Image size: %d, %d", width, height);
if (dataPos == 0) {
dataPos = 54;
}
data = new unsigned char[imageSize];
fread(data, 1, imageSize, file);
fclose(file);
glActiveTexture(GL_TEXTURE0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
GLuint textureId;
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
GL_UNSIGNED_BYTE, data);
checkForGLError("Add texture:");
log("Texture created: %d", textureId);
textures.push_back(textureId);
}
Render for this part:
glUseProgram(shader.get(1));
glBindAttribLocation(shader.get(1), 2, "vertexloc");
glBindAttribLocation(shader.get(1), 3, "vertexcol");
glBindAttribLocation(shader.get(1), 4, "vertexuv");
glBindTexture(GL_TEXTURE_2D, textures[0]);
glEnableVertexAttribArray(2);
glEnableVertexAttribArray(3);
glEnableVertexAttribArray(4);
glUniformMatrix4fv(viewLocation, 1, GL_FALSE,
&frame.combinedMatrix[16 * objects[1].ViewGroup]);
glBindBuffer(GL_ARRAY_BUFFER, objects[1].Buffer);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat),
0);
glBindBuffer(GL_ARRAY_BUFFER, objects[1].Buffer);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat),
(GLvoid*)(3 * sizeof(GLfloat)));
glBindBuffer(GL_ARRAY_BUFFER, objects[1].Buffer);
glVertexAttribPointer(4, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat),
(GLvoid*)(6 * sizeof(GLfloat)));
checkForGLError("In Renderer");
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(2);
glDisableVertexAttribArray(3);
glDisableVertexAttribArray(4);
Unfortunately, this was simply me not assigning a uniform to a value, which caused all vertices to go to (0,0) (or something like that).

How to create a rectangular grid (of triangles) in openGL?

I'm using OpenGL, GLFW and GLEW to create a simulation of water waves for a school project. I'm using a textured grid and will manipulate said grid in the vertex shader.
I tried to create the grid by adapting the GeometryGenerator class in Frank Luna's book "Introduction to 3D Game Programming". I created the function but it gives me an odd scalene triangle instead of the grid.
Can anyone suggest a fix or a different function which would work better?
Here are the relevant snippets of code:
struct vertex
{
GLfloat x, y, z;
GLfloat u, v;
};
struct index
{
GLuint a, b, c;
};
struct MeshData
{
std::vector<vertex> Vertices;
std::vector<index> Indices;
//std::vector<GLuint> Indices;
};
void CreateGrid(float width, float depth, int m, int n, MeshData& meshData)
{
int vertexCount = m*n;
int faceCount = (m-1)*(n-1)*2;
// Create the vertices.
float halfWidth = 0.5f*width;
float halfDepth = 0.5f*depth;
float dx = width / (n-1);
float dz = depth / (m-1);
float du = 1.0f / (n-1);
float dv = 1.0f / (m-1);
meshData.Vertices.resize(vertexCount);
for(GLfloat i = 0.0f; i < m; ++i)
{
GLfloat z = halfDepth - i*dz;
for(GLfloat j = 0.0f; j < n; ++j)
{
//code
GLfloat x = -halfWidth + j*dx;
meshData.Vertices[i*n+j].x = x;
meshData.Vertices[i*n+j].y = 0.0f;
meshData.Vertices[i*n+j].z = z;
meshData.Vertices[i*n+j].u = j*du;
meshData.Vertices[i*n+j].v = i*dv;
//vertex temp = {x, 0.0f, z, j*du, i*dv};
//meshData.Vertices.push_back(temp);
}
}
//indices
meshData.Indices.resize(faceCount);
GLuint offset = 0;
for(GLuint i = 0; i < m-1; ++i)
{
for(GLuint j = 0; j < n-1; ++j)
{
//offset = x * m;
index temp =
{
i*n+j,
i*n+j+1,
(i+1)*n+j
};
index temp_2 =
{
(i+1)*n+j,
i*n+j+1,
(i+1)*n+j+1
};
meshData.Indices.push_back(temp);
meshData.Indices.push_back(temp_2);
}
}
}
Also passing the data to the VAO.
#pragma region "object_initialization"
// Set the object data (buffers, vertex attributes)
MeshData grid;
CreateGrid(30, 20, 2.0f, 3.0f, grid);
// Setup plane VAO
GLuint planeVAO, planeVBO, planeEBO;
glGenVertexArrays(1, &planeVAO);
glGenBuffers(1, &planeVBO);
glGenBuffers(1, &planeEBO);
glBindVertexArray(planeVAO);
glBindBuffer(GL_ARRAY_BUFFER, planeVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(grid.Vertices) * grid.Vertices.size(), &grid.Vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, planeEBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(grid.Indices) * grid.Indices.size(), &grid.Indices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glBindVertexArray(0);
// Load textures
GLuint floorTexture = loadTexture("C://path to.../brushwater.png");
#pragma endregion
Here is the rendering code as requested:
while(!glfwWindowShouldClose(window))
{
// Set frame time
GLfloat currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// Check and call events
glfwPollEvents();
Do_Movement();
// Clear the colorbuffer
glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Draw objects
shader.Use();
glm::mat4 model;
glm::mat4 view = camera.GetViewMatrix();
glm::mat4 projection = glm::perspective(camera.Zoom, (float)screenWidth/(float)screenHeight, 0.1f, 100.0f);
glUniformMatrix4fv(glGetUniformLocation(shader.Program, "view"), 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(shader.Program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
// Floor
glBindVertexArray(planeVAO);
glBindTexture(GL_TEXTURE_2D, floorTexture);
model = glm::mat4();
glUniformMatrix4fv(glGetUniformLocation(shader.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
// Swap the buffers
glfwSwapBuffers(window);
}
And the shaders:
Vertex Shader
#version 430 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec2 texCoords;
out vec2 TexCoords;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
gl_Position = projection * view * model * vec4(position, 1.0f);
TexCoords = texCoords;
}
Fragment Shader
#version 430 core
in vec2 TexCoords;
out vec4 color;
uniform sampler2D texture1;
void main()
{
color = texture(texture1, TexCoords);
}
My outputs are either textured scalene triangles or just a blank background.
Thanks in advanced for reading it though, and thanks for your help!

(progam) undeclared error

I have debugged this code to create a torus to the best of my ability and I still cannot get it to run. The errors I am getting are in my (void init()) specifically. It tells me that I have not declared vPosition and vNormal but I copy/pasted it directly from working code and I do not understand why it is not working. The code should produce a torus that is shaded and can be rotated using a mouse.
#include "mat.h"
#include "vec.h"
#include "Angel.h"
typedef Angel::vec4 color4;
typedef Angel::vec4 point4;
GLuint ModelView, Projection;
GLuint torus;
int click_button;
GLfloat click_rotation_x;
GLfloat click_rotation_y;
GLfloat click_position_z;
GLfloat click_x;
GLfloat click_y;
GLfloat rotation_x = 0.0;
GLfloat rotation_y = 0.0;
GLfloat position_z = -5;
GLuint buffers[1];
GLuint loc;
GLint matrix_loc, projection_loc, color_loc;
void mouse(int button, int state, int x, int y)
{
click_x = x;
click_y = y;
click_rotation_x = rotation_x;
click_rotation_y = rotation_y;
click_position_z = position_z;
}
void motion(int x, int y)
{
GLfloat dx = GLfloat(x - click_x) / 512;
GLfloat dy = GLfloat(y - click_y) / 512;
if (click_button == GLUT_MIDDLE_BUTTON)
{
rotation_x = click_rotation_x + 90.0 * dy;
rotation_y = click_rotation_y + 180.0 * dx;
if (rotation_x > 90.0) rotation_x = 90.0;
if (rotation_x < -90.0) rotation_x = -90.0;
if (rotation_y > 180.0) rotation_y -= 360.0;
if (rotation_y < -180.0) rotation_y += 360.0;
}
else
{
position_z = click_position_z + 5.0 * dy;
}
glutPostRedisplay();
}
struct vertex
{
vec3 v;
vec3 n;
};
const int n = 64;
const int m = 64;
struct vertex triangles[3*2*n*m];
struct vertex torus_vertex(float a, float b)
{
const float R = 2.0f;
const float r = 0.5f;
struct vertex vertex;
vertex.v.x = (R + r*cos(2.0*M_PI*a))* cos(2.0*M_PI*b);
vertex.v.y = (R + r*cos(2.0*M_PI*a))* sin(2.0*M_PI*b);
vertex.v.z = r*sin(2.0*M_PI*a);
vertex.n.x = cos(2.0*M_PI*a)* cos(2.0*M_PI*b);
vertex.n.y = cos(2.0*M_PI*a)* sin(2.0*M_PI*b);
vertex.n.z = sin(2.0*M_PI*a);
return vertex;
}
GLuint torus_create(GLuint vPosition, GLuint vNormal);
int i,j,k =0;
for (int i=0, i<n, i++)
for (int j=0, j<m, j++)
{
triangles[k++] = torus_vertex(float (i ) / n, float (j ) / n);
triangles[k++] = torus_vertex(float (i + 1) / n, float (j ) / n);
triangles[k++] = torus_vertex(float (i + 1) / n, float (j + 1) / n);
triangles[k++] = torus_vertex(float (i ) / n, float (j ) / n);
triangles[k++] = torus_vertex(float (i + 1) / n, float (j + 1) / n);
triangles[k++] = torus_vertex(float (i ) / n, float (j + 1) / n);
}
void
init()
{
GLuint (vbo);
glGenBuffers(1, &vbo);
glBindBuffer (GL_ARRAY_BUFFER, vbo);
glBufferData (GL_ARRAY_BUFFER, sizeof (struct vertex) * k, triangles, GL_STATIC_DRAW);
GLuint program = InitShader("vshader53.glsl", "fshader53.glsl");
glUseProgram( program );
glEnableVertexAttribArray(vPosition);
glEnableVertexAttribArray(vNormal);
glVertexAttribPointer(vPosition, 3, GL_FLOAT, GL_FALSE, sizeof (struct vertex), (GLvoid *) 0);
glVertexAttribPointer(vNormal, 3, GL_FLOAT, GL_FALSE, sizeof (struct vertex), (GLvoid *) 12);
GLuint vPosition = glGetAttribLocation( program, "vPosition" );
glEnableVertexAttribArray( vPosition );
glVertexAttribPointer( vPosition, 4, GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET(0) );
GLuint vNormal = glGetAttribLocation( program, "vNormal" );
glEnableVertexAttribArray( vNormal );
glVertexAttribPointer( vNormal, 3, GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET(sizeof(vertex)) );
point4 light_position( 0.0, 0.0, -1.0, 0.0);
color4 light_ambient( 0.2, 0.2, 0.2, 1.0);
color4 light_diffuse( 1.0, 1.0, 1.0, 1.0);
color4 light_specular( 1.0, 1.0, 1.0, 1.0);
color4 material_ambient( 1.0, 0.0, 1.0, 1.0);
color4 material_diffuse( 1.0, 0.8, 0.0, 1.0);
color4 material_specular( 1.0, 0.8, 0.0, 1.0);
float material_shininess = 100.0;
color4 ambient_product = light_ambient * material_ambient;
color4 diffuse_product = light_diffuse * material_diffuse;
color4 specular_product = light_specular * material_specular;
glUniform4fv( glGetUniformLocation(program, "AmbientProduct" ), 1, ambient_product);
glUniform4fv( glGetUniformLocation(program, "DiffuseProduct" ), 1, diffuse_product);
glUniform4fv( glGetUniformLocation(program, "SpecularProduct"), 1, specular_product);
glUniform4fv( glGetUniformLocation(program, "LightPosition" ), 1, light_position);
glUniform1f( glGetUniformLocation(program, "Shininess"), material_shininess);
ModelView = glGetUniformLocation(program, "ModelView");
Projection = glGetUniformLocation(program, "Projection");
}
int
main()
{
printf("%d %d %d\n", n, m, k);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(512, 512);
glutCreateWindow("Torus");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 0;
}
It looks like you are using the variables before you are defining them. Move the following from the middle of the init() function to the top of the init() function:
GLuint vPosition = glGetAttribLocation( program, "vPosition" );
glEnableVertexAttribArray( vPosition );
glVertexAttribPointer( vPosition, 4, GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET(0) );
GLuint vNormal = glGetAttribLocation( program, "vNormal" );
glEnableVertexAttribArray( vNormal );
glVertexAttribPointer( vNormal, 3, GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET(sizeof(vertex)) );