Changeset 93baa0e in opengl-game


Ignore:
Timestamp:
Aug 5, 2017, 6:55:49 PM (7 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
49756cb
Parents:
d0b9596
Message:

Add face culling and a model matrix that can be changed to move the scene

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    rd0b9596 r93baa0e  
    99#include <iostream>
    1010#include <fstream>
     11#include <cmath>
    1112
    1213using namespace std;
     
    7475   printf("Renderer: %s\n", renderer);
    7576   printf("OpenGL version supported %s\n", version);
     77
    7678   glEnable(GL_DEPTH_TEST);
    7779   glDepthFunc(GL_LESS);
    7880
     81   glEnable(GL_CULL_FACE);
     82   // glCullFace(GL_BACK);
     83   // glFrontFace(GL_CW);
     84
    7985   GLfloat points[] = {
    8086      0.0f,  0.5f,  0.0f,
     87     -0.5f, -0.5f,  0.0f,
    8188      0.5f, -0.5f,  0.0f,
    82      -0.5f, -0.5f,  0.0f,
    8389   };
    8490
    8591   GLfloat colors[] = {
    8692     1.0, 0.0, 0.0,
     93     0.0, 0.0, 1.0,
    8794     0.0, 1.0, 0.0,
    88      0.0, 0.0, 1.0,
     95   };
     96
     97   GLfloat model[] = {
     98     1.0f, 0.0f, 0.0f, 0.0f, // column 1
     99     0.0f, 1.0f, 0.0f, 0.0f, // column 2
     100     0.0f, 0.0f, 1.0f, 0.0f, // column 3
     101     0.5f, 0.0f, 0.0f, 1.0f, // column 4
    89102   };
    90103
     
    119132   glLinkProgram(shader_program);
    120133
     134   GLint location = glGetUniformLocation(shader_program, "model");
     135
     136   float speed = 1.0f;
     137   float last_position = 0.0f;
     138
     139   double previous_seconds = glfwGetTime();
    121140   while (!glfwWindowShouldClose(window)) {
     141      double current_seconds = glfwGetTime();
     142      double elapsed_seconds = current_seconds - previous_seconds;
     143      previous_seconds = current_seconds;
     144
     145      if (fabs(last_position) > 1.0f) {
     146         speed = -speed;
     147      }
     148
     149      model[12] = last_position + speed*elapsed_seconds;
     150      last_position = model[12];
     151      glUseProgram(shader_program);
     152      glUniformMatrix4fv(location, 1, GL_FALSE, model);
     153
    122154      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    123       glUseProgram(shader_program);
    124155      glBindVertexArray(vao);
     156
    125157      glDrawArrays(GL_TRIANGLES, 0, 3);
    126158
  • test.vert

    rd0b9596 r93baa0e  
    11#version 410
     2
     3uniform mat4 model;
    24
    35layout(location = 0) in vec3 vertex_position;
     
    810void main() {
    911  color = vertex_color;
    10   gl_Position = vec4(vertex_position, 1.0);
     12  gl_Position = model * vec4(vertex_position, 1.0);
    1113}
Note: See TracChangeset for help on using the changeset viewer.