Oops.. :D
Yep, you're right.Damn.Sorry for this simple mistake.Thanks a lot :)
Edit: Now I have another problem.I succeded drawing a lens(one with cube shape), but I want to draw it as a circle.But with the geometry shader it doesn't work.
Before:
[attachment=35886:lens.png]
After:
[attachment=35887:clens.png]
Here is my final code(a little bit messy):
#include <iostream>
//GLEW
#define GLEW_STATIC
#include<glew.h>
//GLFW
#include <glfw3.h>
#include "Shader.h"
//#include "Camera.h"
#include <SOIL.h>
// GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
// Properties
GLuint screenWidth = 1800, screenHeight = 1000;
// Function prototypes
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void do_movement();
const GLchar* cvShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 position;\n"
"void main()\n"
"{\n"
"gl_Position = vec4(position.x, position.y, position.z, 1.0);\n"
"}\0";
const GLchar* cfShaderSource = "#version 330 core\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
"color = vec4(1.0f, 0.0f, 0.0f, 1.0f);\n"
"}\n";
const GLchar* gShaderSource = "#version 330 core\n"
"layout(points) in;\n"
"layout(line_strip, max_vertices = 11) out;\n"
"const float PI = 3.1415926;\n"
"void main() {\n"
"for (int i = 0; i <= 40; i++) {\n"
"float ang = PI * 2.0 / 10.0 * i;\n"
"vec4 offset = vec4(cos(ang) * 0.2, -sin(ang) * 0.4, 0.0, 0.0);\n"
"gl_Position = gl_in[0].gl_Position + offset;\n"
"EmitVertex();\n"
"}\n"
"EndPrimitive();\n"
"}\n";
GLfloat deltaTime = 0.0f;
GLfloat lastFrame = 0.0f;
GLfloat posX = 0.0f;
GLfloat posY = 0.0f;
GLfloat zoomZ = 0.1f;
bool keys[1024];
// The MAIN function, from here we start our application and run our Game loop
int main()
{
// Init GLFW
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
glfwWindowHint(GLFW_SAMPLES, 4);
GLFWwindow* window = glfwCreateWindow(screenWidth, screenHeight, "LearnOpenGL", nullptr, nullptr); // Windowed
glfwMakeContextCurrent(window);
// Set the required callback functions
glfwSetKeyCallback(window, key_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
// Options
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// Initialize GLEW to setup the OpenGL Function pointers
glewExperimental = GL_TRUE;
glewInit();
// Define the viewport dimensions
glViewport(0, 0, screenWidth, screenHeight);
// Setup some OpenGL options
glEnable(GL_DEPTH_TEST);
glEnable(GL_STENCIL_TEST);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
// Setup and compile our shaders
Shader imageShader("shader.vs", "imageShader.frag");
// Set up our vertex data (and buffer(s)) and attribute pointers
GLfloat imgVertices[] = {
0.5f, 0.5f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f
};
GLuint indices[] = { // Note that we start from 0!
0, 1, 3, // First Triangle
1, 2, 3 // Second Triangle
};
GLuint imgVBO, imgVAO, imgEBO;
glGenVertexArrays(1, &imgVAO);
glGenBuffers(1, &imgVBO);
glGenBuffers(1, &imgEBO);
// Bind our Vertex Array Object first, then bind and set our buffers and pointers.
glBindVertexArray(imgVAO);
glBindBuffer(GL_ARRAY_BUFFER, imgVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(imgVertices), imgVertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, imgEBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// Position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
// TexCoord attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
glBindVertexArray(0); // Unbind VAO
GLuint texture;
// --== TEXTURE 1 == --
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture); // All upcoming GL_TEXTURE_2D operations now have effect on our texture object
// Set our texture parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Set texture filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Load, create texture and generate mipmaps
int width, height;
unsigned char* image = SOIL_load_image("space.jpg", &width, &height, 0, SOIL_LOAD_RGB);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);
SOIL_free_image_data(image);
glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture when done, so we won't accidentily mess up our texture.
// --== TEXTURE 2 == --
//compilare vertex shader
GLuint cvShader;
cvShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(cvShader, 1, &cvShaderSource, NULL);
glCompileShader(cvShader);
// verificare compilare vertex shader
GLint success;
GLchar infoLog[512];
glGetShaderiv(cvShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(cvShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
//compilare fragment shader
GLuint cfShader;
cfShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(cfShader, 1, &cfShaderSource, NULL);
glCompileShader(cfShader);
glGetShaderiv(cfShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(cfShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// Geometry shader
GLuint gShader;
gShader = glCreateShader(GL_GEOMETRY_SHADER);
glShaderSource(gShader, 1, &gShaderSource, NULL);
glCompileShader(gShader);
glGetShaderiv(gShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(gShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::GEOMETRY::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// Link shaders
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, cvShader);
glAttachShader(shaderProgram, cfShader);
glAttachShader(shaderProgram, gShader);
glLinkProgram(shaderProgram);
// Check for linking errors
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
glDeleteShader(cvShader);
glDeleteShader(cfShader);
glDeleteShader(gShader);
//CIRCLE
GLfloat circleVertex[] = {
0.0f, 0.0f, 0.0f
};
GLuint crcVBO, crcVAO;
glGenVertexArrays(1, &crcVAO);
glGenBuffers(1, &crcVBO);
glBindVertexArray(crcVAO);
glBindBuffer(GL_ARRAY_BUFFER, crcVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(circleVertex), circleVertex, GL_STATIC_DRAW);
// Position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
glBindVertexArray(0); // Unbind VAO
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(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// Bind Textures using texture units
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(glGetUniformLocation(imageShader.Program, "ourTexture1"), 0);
glStencilFunc(GL_ALWAYS, 1, 0xFF); // All fragments should update the stencil buffer
glStencilMask(0xFF);
glDisable(GL_DEPTH_TEST);
glUseProgram(shaderProgram);
glm::mat4 model;
glm::mat4 view;
glm::mat4 projection;
model = glm::translate(model, glm::vec3(posX, posY, -1.0f));
view = glm::lookAt(glm::vec3(0.0f, 0.0f, 3.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
projection = glm::perspective(45.0f, (float)screenWidth / (float)screenHeight, 0.1f, 100.0f);
GLint modelLoc = glGetUniformLocation(shaderProgram, "model");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
GLint viewLoc = glGetUniformLocation(shaderProgram, "view");
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
GLint projLoc = glGetUniformLocation(shaderProgram, "projection");
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
glBindVertexArray(crcVAO);
glDrawArrays(GL_POINTS, 0, 1);
glBindVertexArray(0);
glStencilFunc(GL_EQUAL, 1, 0xFF); // All fragments should update the stencil buffer
glStencilMask(0xFF);
//glDisable(GL_DEPTH_TEST);
// Draw our first triangle
imageShader.Use();
model = glm::mat4();
model = glm::translate(model, glm::vec3(0.0f, 0.0f, zoomZ));
model = glm::scale(model, glm::vec3(4.0f, 3.0f, 0.0));
//GLint modelLoc = glGetUniformLocation(imageShader.Program, "model");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
glBindVertexArray(imgVAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
// Swap the buffers
glfwSwapBuffers(window);
}
// Properly de-allocate all resources once they've outlived their purpose
glDeleteVertexArrays(1, &imgVAO);
glDeleteBuffers(1, &imgVBO);
glDeleteVertexArrays(1, &crcVAO);
glDeleteBuffers(1, &crcVBO);
glfwTerminate();
return 0;
}
// Moves/alters the camera positions based on user input
void Do_Movement()
{
}
// Is called whenever a key is pressed/released via GLFW
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
if (key >= 0 && key < 1024)
{
if (action == GLFW_PRESS)
keys[key] = true;
else if (action == GLFW_RELEASE)
keys[key] = false;
}
}
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
}
void do_movement()
{
GLfloat speed = 5.0f * deltaTime;
if (keys[GLFW_KEY_LEFT])
{
posX -= 1.0f * speed;
}
if (keys[GLFW_KEY_RIGHT])
{
posX += 1.0f * speed;
}
if (keys[GLFW_KEY_UP])
{
posY += 1.0f * speed;
}
if (keys[GLFW_KEY_DOWN])
{
posY -= 1.0f * speed;
}
if (keys[GLFW_KEY_W])
{
zoomZ += 1.0f * speed;
}
if (keys[GLFW_KEY_S])
{
zoomZ -= 1.0f * speed;
}
}
Thank you!