简体   繁体   中英

OpenGL and GLFW - Do I need shaders to draw a point?

I am working through a book on OpenGL , and I am stuck on the third chapter. The following code should open a window and draw a single dot in the center.

The window shows up, glClearColor and glClear seem to be working, but no dot. This answer leads me do believe I may need to compile and link simple vertex and fragment shaders.

The book has made no mention of them, but do I need shaders? Am I missing something else with how I am drawing the dot? Something with the window? The book uses some WIN32 thing for windowing, I am using Linux Mint 17.1

Code below:

#include <stdlib.h>
#include <stdio.h>

#include <GL/glew.h>
#include <GL/gl.h>
#include <GLFW/glfw3.h>
#include <GL/glut.h>

#define BUFFER_OFFSET(i) ( (char*)NULL + (i) )

static void error_callback(int error, const char* description) {
  fputs(description, stderr);
}

static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
  if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
    glfwSetWindowShouldClose(window, GL_TRUE);
}

int main(void) {
  GLFWwindow* window;
  glfwSetErrorCallback(error_callback);
  if (!glfwInit()) {
    exit(EXIT_FAILURE);
  }

  window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
  if (!window) {
    glfwTerminate();
    exit(EXIT_FAILURE);
  }
  glfwMakeContextCurrent(window);
  glfwSwapInterval(1);
  glfwSetKeyCallback(window, key_callback);

  if (glewInit()) {
    glfwTerminate();
    exit(EXIT_FAILURE);
  }

  glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

  float ratio;
  int width, height;
  glfwGetFramebufferSize(window, &width, &height);
  ratio = width / (float) height;
  glViewport(0, 0, width, height);

  GLfloat vertex [] = {0.0f, 0.0f, -2.0f};
  GLuint m_vertexBuffer;
  glGenBuffers(1, &m_vertexBuffer);
  glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
  glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3, &vertex[0], GL_STATIC_DRAW);

  while (!glfwWindowShouldClose(window)) {
    glClear(GL_COLOR_BUFFER_BIT);

    glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
    glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
    glPointSize(10.0f);
    glEnableClientState(GL_VERTEX_ARRAY);
    glDrawArrays(GL_POINTS, 0, 1);
    glDisableClientState(GL_VERTEX_ARRAY);

    glfwSwapBuffers(window);
    glfwPollEvents();
  }

  glfwDestroyWindow(window);
  glfwTerminate();
  exit(EXIT_SUCCESS);
}

Also, I am compiling with g++ main.cpp -lGL -lglfw -lGLU -lGLEW

I have OpenGL 4.5 installed, but am opening a fixed-pipeline context.

From your code:

GLfloat vertex [] = {0.0f, 0.0f, -2.0f};

Since you use the fixed function pipelione and never set any matrices, both the projection and the modelView matrix will be left at the default, and the point (0, 0, -2) is outside fo the viewing volume. Try (0, 0, 0) instead.

If the book uses excatly this code with exactly that vertex coordinates, you'd better search for another one. You should also consider learning "modern" GL instead. The fixed-funciton pipeline basically has been superseeded by shaders in GL2.0 a decade ago .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM